Created
July 14, 2012 16:17
-
-
Save jhthorsen/3111962 to your computer and use it in GitHub Desktop.
cloudinary lite app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojolicious::Lite; | |
# the params can be found at https://cloudinary.com/console | |
plugin cloudinary => { | |
api_key => '1234567890', | |
api_secret => 'your-super-s3cret', | |
cloud_name => 'your_cloud_name', | |
}; | |
# this need to be some sort of backend database | |
my $images = {}; | |
__PACKAGE__->attr(persistent_storage => sub { $images }); | |
get '/' => 'index'; | |
post '/image' => sub { | |
my $self = shift; | |
$self->render_later; | |
$self->cloudinary_upload({ | |
file => scalar $self->upload('some_file_field'), | |
on_success => sub { | |
my($res, $tx) = @_; | |
$self->stash(status => 'The image was uploaded'); | |
$self->app->persistent_storage->{$res->{'public_id'}} = time; | |
$self->render; | |
}, | |
on_error => sub { | |
$self->stash(status => 'Failed to upload the file'); | |
$self->render; | |
}, | |
}); | |
}; | |
get '/image/:public_id/delete' => sub { | |
my $self = shift; | |
my $id = $self->param('public_id'); | |
$self->render_later; | |
$self->cloudinary_destroy({ | |
public_id => $id, | |
on_success => sub { | |
$self->stash(status => 'The image was destroy'); | |
delete $self->app->persistent_storage->{$id}; | |
$self->render; | |
}, | |
on_error => sub { | |
$self->stash(status => 'Failed to destroy the file'); | |
$self->render; | |
}, | |
}); | |
}; | |
app->defaults(cloud_name => 'your_cloud_name', status => '', images => $images); | |
app->secret('yey!'); | |
app->start; | |
__DATA__ | |
@@ layouts/default.html.ep | |
<!DOCTYPE html> | |
<html> | |
<head><title><%= title %></title></head> | |
%= javascript '/js/jquery.js' | |
%= javascript 'http://raw.github.com/cloudinary/cloudinary_js/master/js/jquery.cloudinary.js' | |
%= javascript begin | |
$(document).ready(function() { | |
$.cloudinary.config('cloud_name', '<%= $cloud_name %>'); | |
$('img[data-src]').cloudinary(); | |
}); | |
% end | |
<body><%= content %></body> | |
</html> | |
@@ index.html.ep | |
% title 'Upload image example' | |
% layout 'default' | |
% for my $id (keys %$images) { | |
%= cloudinary_js_image "$id.jpg"; | |
% } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment