From 5349d327eddd29b0d6689a509f05a0ea99ecf2d2 Mon Sep 17 00:00:00 2001 From: Luc Didry Date: Fri, 7 Mar 2014 03:17:57 +0100 Subject: [PATCH] Fix #20 Thumbnails in response --- README.md | 8 +++++ lib/Lutim/Controller.pm | 51 ++++++++++++++++++++++++------- templates/index.html.ep | 33 ++++++++++++-------- templates/layouts/default.html.ep | 4 +-- 4 files changed, 71 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 3c2594d..6de22b8 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,14 @@ or sudo apt-get install carton ``` +###Thumbnails dependancy +If you want to provide thumbnails of uploaded images, you have to install the *ImageMagick* image manipulation software () and the Image::Magick CPAN module. + +On Debian, you can do: +```shell +sudo apt-get install perlmagick +``` + ##Installation After installing Carton : ```shell diff --git a/lib/Lutim/Controller.pm b/lib/Lutim/Controller.pm index 0e0b83f..865f9a3 100644 --- a/lib/Lutim/Controller.pm +++ b/lib/Lutim/Controller.pm @@ -1,13 +1,24 @@ # vim:set sw=4 ts=4 sts=4 expandtab: package Lutim::Controller; use Mojo::Base 'Mojolicious::Controller'; -use Mojo::Util qw(url_unescape); +use Mojo::Util qw(url_unescape b64_encode); use DateTime; use File::Type; use Digest::file qw(digest_file_hex); use Text::Unidecode; use Data::Validate::URI qw(is_http_uri is_https_uri); +use vars qw($im_loaded); +BEGIN { + eval "use Image::Magick"; + if ($@) { + warn "You don't have Image::Magick installed so you won't have thumbnails."; + $im_loaded = 0; + } else { + $im_loaded = 1; + } +} + sub home { my $c = shift; @@ -98,7 +109,7 @@ sub add { my $ip = $c->ip; - my ($msg, $short); + my ($msg, $short, $thumb); # Check file type if (index($mediatype, 'image/') >= 0) { # Create directory if needed @@ -126,6 +137,14 @@ sub add { my $filename = unidecode($upload->filename); my $ext = ($filename =~ m/([^.]+)$/)[0]; my $path = 'files/'.$records[0]->short.'.'.$ext; + if ($im_loaded) { + my $im = Image::Magick->new; + $im->BlobToImage($upload->slurp); + $im->Resize(geometry=>'x85'); + + $thumb = 'data:'.$mediatype.';base64,'; + $thumb .= b64_encode $im->ImageToBlob(); + } my $key; if ($c->param('crypt') || $c->config->{always_encrypt}) { ($upload, $key) = $c->crypt($upload, $filename); @@ -163,7 +182,8 @@ sub add { if (defined($short)) { $msg = { filename => $upload->filename, - short => $short + short => $short, + thumb => $thumb }; } else { $msg = { @@ -171,21 +191,30 @@ sub add { msg => $msg }; } - $c->render( + return $c->render( json => { success => (defined($short)) ? Mojo::JSON->true : Mojo::JSON->false, msg => $msg } ); } else { - $c->flash(msg => $msg) if (defined($msg)); - $c->flash(short => $short) if (defined($short)); - $c->flash(filename => $upload->filename); - $c->redirect_to('/'); + if ((defined($msg))) { + $c->flash(msg => $msg); + $c->flash(filename => $upload->filename); + return $c->redirect_to('/'); + } else { + $c->stash(short => $short) if (defined($short)); + $c->stash(thumb => $thumb); + $c->stash(filename => $upload->filename); + return $c->render( + template => 'index', + max_file_size => $c->req->max_message_size + ); + } } } else { if (defined($c->param('format')) && $c->param('format') eq 'json') { - $c->render( + return $c->render( json => { success => Mojo::JSON->false, msg => { @@ -197,7 +226,7 @@ sub add { } else { $c->flash(msg => $c->stash('stop_upload')); $c->flash(filename => $upload->filename); - $c->redirect_to('/'); + return $c->redirect_to('/'); } } } @@ -232,7 +261,7 @@ sub short { $test = 1; my $short = $images[0]->short; $short .= '/'.$key if (defined($key)); - $c->render( + return $c->render( template => 'twitter', layout => undef, short => $short, diff --git a/templates/index.html.ep b/templates/index.html.ep index d8bf15e..d532262 100644 --- a/templates/index.html.ep +++ b/templates/index.html.ep @@ -4,14 +4,19 @@ % if (config('always_encrypt')) {

<%=l 'always_encrypt' %>

% } -% if (defined(flash('short'))) { +% if (defined(stash('short'))) {
- <%= flash('filename') %> -
    -
  • <%= link_to url_for('/')->base->scheme($scheme)->to_abs.'/'.flash('short') => begin %><%= url_for('/')->base->scheme($scheme)->to_abs.'/'.flash('short') %><%= end %>
  • -
  • <%= link_to url_for('/')->base->scheme($scheme)->to_abs.'/'.flash('short').'?dl' => begin %><%= url_for('/')->base->scheme($scheme)->to_abs.'/'.flash('short').'?dl' %><%= end %>
  • -
  • <%= link_to url_for('/')->base->scheme($scheme)->to_abs.'/'.flash('short').'?t' => begin %><%= url_for('/')->base->scheme($scheme)->to_abs.'/'.flash('short').'?t' %><%= end %>
  • -
+% if (defined(stash('short'))) { + <%= stash('filename') %> thumbnail +% } +
+ <%= stash('filename') %> +
    +
  • <%= link_to url_for('/')->base->scheme($scheme)->to_abs.'/'.stash('short') => begin %><%= url_for('/')->base->scheme($scheme)->to_abs.'/'.stash('short') %><%= end %>
  • +
  • <%= link_to url_for('/')->base->scheme($scheme)->to_abs.'/'.stash('short').'?dl' => begin %><%= url_for('/')->base->scheme($scheme)->to_abs.'/'.stash('short').'?dl' %><%= end %>
  • +
  • <%= link_to url_for('/')->base->scheme($scheme)->to_abs.'/'.stash('short').'?t' => begin %><%= url_for('/')->base->scheme($scheme)->to_abs.'/'.stash('short').'?t' %><%= end %>
  • +
+
% } % if (defined(flash('msg'))) { @@ -134,15 +139,19 @@ } function message(success, msg) { if(success) { - return '
' + var thumb = (msg.thumb !== null) ? ''+msg.filename+' thumbnail' : '' + return '
' + +thumb + +'
' +msg.filename - +'
  • ' + +'' + +'
    •  ' +link(msg.short, '') - +'
    • ' + +'
    •  ' +link(msg.short, 'dl') - +'
    • ' + +'
    •  ' +link(msg.short, 't') - +'
'; + +'
'; } else { return '
<%=l 'some-bad' %>
' +msg.filename diff --git a/templates/layouts/default.html.ep b/templates/layouts/default.html.ep index 2c5646c..0baf7ed 100644 --- a/templates/layouts/default.html.ep +++ b/templates/layouts/default.html.ep @@ -32,8 +32,8 @@ display: none; } - ul.no-bullet { - list-style-type: none; + .thumbnail { + margin-right: 8px; } % if (config('always_encrypt')) {