This commit is contained in:
Luc Didry
2016-06-06 22:26:37 +02:00
parent 0a318dc4d4
commit ef6509a3bc
3 changed files with 38 additions and 2 deletions

View File

@@ -5,6 +5,17 @@ use LutimModel;
use Crypt::CBC;
use Data::Entropy qw(entropy_source);
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;
}
}
$ENV{MOJO_TMPDIR} = 'tmp';
mkdir($ENV{MOJO_TMPDIR}, 0700) unless (-d $ENV{MOJO_TMPDIR});
# This method will run once at server start
@@ -31,6 +42,7 @@ sub startup {
max_delay => 0,
token_length => 24,
crypto_key_length => 8,
thumbnail_size => 100,
}
});
@@ -43,7 +55,7 @@ sub startup {
$self->helper(
render_file => sub {
my $c = shift;
my ($filename, $path, $mediatype, $dl, $expires, $nocache, $key) = @_;
my ($filename, $path, $mediatype, $dl, $expires, $nocache, $key, $thumb) = @_;
$filename = quote($filename);
@@ -73,6 +85,18 @@ sub startup {
} else {
$asset = Mojo::Asset::File->new(path => $path);
}
if (defined $thumb && $im_loaded && $mediatype ne 'image/svg+xml') { # ImageMagick don't work in Debian with svg (for now?)
my $im = Image::Magick->new;
$im->BlobToImage($asset->slurp);
# Create the thumbnail
$im->Resize(geometry=>'x'.$c->config('thumbnail_size'));
# Replace the asset with the thumbnail
$asset = Mojo::Asset::Memory->new->add_chunk($im->ImageToBlob());
}
$c->res->content->asset($asset);
$headers->add('Content-Length' => $asset->size);

View File

@@ -499,6 +499,7 @@ sub short {
my $short = $c->param('short');
my $touit = $c->param('t');
my $key = $c->param('key');
my $thumb = $c->param('thumb');
my $dl = (defined($c->param('dl'))) ? 'attachment' : 'inline';
my @images = LutimModel::Lutim->select('WHERE short = ? AND ENABLED = 1 AND path IS NOT NULL', $short);
@@ -568,7 +569,7 @@ sub short {
$dt->set_time_zone('GMT');
$expires = $dt->strftime("%a, %d %b %Y %H:%M:%S GMT");
$test = $c->render_file($images[0]->filename, $images[0]->path, $images[0]->mediatype, $dl, $expires, $images[0]->delete_at_first_view, $key);
$test = $c->render_file($images[0]->filename, $images[0]->path, $images[0]->mediatype, $dl, $expires, $images[0]->delete_at_first_view, $key, $thumb);
}
}

View File

@@ -7,6 +7,9 @@
hypnotoad => {
# array of IP addresses and ports you want to listen to
listen => ['http://127.0.0.1:8080'],
# if you use Lutim behind a reverse proxy like Nginx, you want to set proxy to 1
# if you use Lutim directly, let it commented
#proxy => 1,
},
################
@@ -107,6 +110,14 @@
# optional, default is lutim.db
#db_path => 'lutim.db',
# define the heigth of the thumbnails generated at users' will
# this is not the height of the thumbnails send after upload,
# we're talking about thumbnails generated when someone asked for
# https://example.org/lutim/tesrinp?thumb
# this works only if you have ImageMagick
# optional, default is 100 (pixels)
#thumbnail_size => 100,
##########################
# Lutim cron jobs settings
##########################