From 86dcebf00db1421fc5208a1ab407293a18a03e0e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= Date: Wed, 24 Feb 2021 13:10:01 +0100 Subject: [PATCH] Dateimanagement: Thumbnails erzeugen und anzeigen --- SL/Controller/File.pm | 67 ++++++++++++++++++++++++++++++- templates/webpages/file/list.html | 14 ++++++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/SL/Controller/File.pm b/SL/Controller/File.pm index 51dd5c0ac..1429912c8 100644 --- a/SL/Controller/File.pm +++ b/SL/Controller/File.pm @@ -8,10 +8,12 @@ use List::Util qw(first max); use utf8; use Encode qw(decode); +use English qw( -no_match_vars ); use URI::Escape; use Cwd; use DateTime; use File::stat; +use File::Slurp qw(slurp); use File::Spec::Unix; use File::Spec::Win32; use File::MimeInfo::Magic; @@ -29,8 +31,9 @@ use SL::JSON; use SL::Helper::CreatePDF qw(:all); use SL::Locale::String; use SL::SessionFile; +use SL::SessionFile::Random; use SL::File; -use SL::Controller::Helper::ThumbnailCreator qw(file_probe_image_type); +use SL::Controller::Helper::ThumbnailCreator qw(file_probe_image_type file_probe_type); use constant DO_DELETE => 0; use constant DO_UNIMPORT => 1; @@ -393,6 +396,8 @@ sub _do_list { } $self->files(\@files); + $_->{thumbnail} = _create_thumbnail($_) for @files; + if($self->object_type eq 'shop_image'){ $self->js ->run('kivi.ShopPart.show_images', $self->object_id) @@ -601,6 +606,66 @@ sub _get_sources { return @sources; } +# ignores all errros +# todo: cache thumbs? +sub _create_thumbnail { + my ($file) = @_; + + my $filename; + if (!eval { $filename = $file->get_file(); 1; }) { + $::lxdebug->message(LXDebug::WARN(), "SL::File::_create_thumbnail get_file failed: " . $EVAL_ERROR); + return; + } + + # Workaround for pfds which are not handled by file_probe_type. + # Maybe use mime info stored in db? + my $mime_type = File::MimeInfo::Magic::magic($filename); + if ($mime_type =~ m{pdf}) { + $filename = _convert_pdf_to_png($filename); + } + return if !$filename; + + my $content; + if (!eval { $content = slurp $filename; 1; }) { + $::lxdebug->message(LXDebug::WARN(), "SL::File::_create_thumbnail slurp failed: " . $EVAL_ERROR); + return; + } + + my $ret; + if (!eval { $ret = file_probe_type($content); 1; }) { + $::lxdebug->message(LXDebug::WARN(), "SL::File::_create_thumbnail file_probe_type failed: " . $EVAL_ERROR); + return; + } + + # file_probe_type returns a hash ref with thumbnail info and content + # or an error message + if ('HASH' ne ref $ret) { + $::lxdebug->message(LXDebug::WARN(), "SL::File::_create_thumbnail file_probe_type returned an error: " . $ret); + return; + } + + return $ret; +} + +sub _convert_pdf_to_png { + my ($filename) = @_; + + my $sfile = SL::SessionFile::Random->new(); + + my $command = 'pdftoppm -singlefile -scale-to 64 -png' . ' ' . $filename . ' ' . $sfile->file_name; + + if (system($command) == -1) { + $::lxdebug->message(LXDebug::WARN(), "SL::File::_convert_pdf_to_png: system call failed: " . $ERRNO); + return; + } + if ($CHILD_ERROR) { + $::lxdebug->message(LXDebug::WARN(), "SL::File::_convert_pdf_to_png: pdftoppm failed with error code: " . ($CHILD_ERROR >> 8)); + return; + } + + return $sfile->file_name . '.png'; +} + 1; __END__ diff --git a/templates/webpages/file/list.html b/templates/webpages/file/list.html index dfb59f3a2..cf5666572 100644 --- a/templates/webpages/file/list.html +++ b/templates/webpages/file/list.html @@ -1,5 +1,7 @@ [%- USE LxERP -%][% USE L %] [% USE T8 %] +[% USE Base64 %] +[% USE HTML %] [%- IF ! json %]
[%- END %] @@ -36,7 +38,7 @@ [% LxERP.t8('Description') %] [%- ELSE %] - + [% LxERP.t8('ImagePreview') %] [%- END %] @@ -61,7 +63,15 @@ [% file.description %] [%- ELSE %] - + + [%- IF file.thumbnail %] + + [% file.file_name %] + + [%- ELSE %] + - + [%- END %] + [%- END %] [%- END # FOREACH file %] -- 2.20.1