Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 86dcebf0

Von Bernd Bleßmann vor etwa 3 Jahren hinzugefügt

  • ID 86dcebf00db1421fc5208a1ab407293a18a03e0e
  • Vorgänger d04d57fd
  • Nachfolger ca96627e

Dateimanagement: Thumbnails erzeugen und anzeigen

Unterschiede anzeigen:

SL/Controller/File.pm
8 8

  
9 9
use utf8;
10 10
use Encode qw(decode);
11
use English qw( -no_match_vars );
11 12
use URI::Escape;
12 13
use Cwd;
13 14
use DateTime;
14 15
use File::stat;
16
use File::Slurp qw(slurp);
15 17
use File::Spec::Unix;
16 18
use File::Spec::Win32;
17 19
use File::MimeInfo::Magic;
......
29 31
use SL::Helper::CreatePDF qw(:all);
30 32
use SL::Locale::String;
31 33
use SL::SessionFile;
34
use SL::SessionFile::Random;
32 35
use SL::File;
33
use SL::Controller::Helper::ThumbnailCreator qw(file_probe_image_type);
36
use SL::Controller::Helper::ThumbnailCreator qw(file_probe_image_type file_probe_type);
34 37

  
35 38
use constant DO_DELETE   => 0;
36 39
use constant DO_UNIMPORT => 1;
......
393 396
  }
394 397
  $self->files(\@files);
395 398

  
399
  $_->{thumbnail} = _create_thumbnail($_) for @files;
400

  
396 401
  if($self->object_type eq 'shop_image'){
397 402
    $self->js
398 403
      ->run('kivi.ShopPart.show_images', $self->object_id)
......
601 606
  return @sources;
602 607
}
603 608

  
609
# ignores all errros
610
# todo: cache thumbs?
611
sub _create_thumbnail {
612
  my ($file) = @_;
613

  
614
  my $filename;
615
  if (!eval { $filename = $file->get_file(); 1; }) {
616
    $::lxdebug->message(LXDebug::WARN(), "SL::File::_create_thumbnail get_file failed: " . $EVAL_ERROR);
617
    return;
618
  }
619

  
620
  # Workaround for pfds which are not handled by file_probe_type.
621
  # Maybe use mime info stored in db?
622
  my $mime_type = File::MimeInfo::Magic::magic($filename);
623
  if ($mime_type =~ m{pdf}) {
624
    $filename = _convert_pdf_to_png($filename);
625
  }
626
  return if !$filename;
627

  
628
  my $content;
629
  if (!eval { $content = slurp $filename; 1; }) {
630
    $::lxdebug->message(LXDebug::WARN(), "SL::File::_create_thumbnail slurp failed: " . $EVAL_ERROR);
631
    return;
632
  }
633

  
634
  my $ret;
635
  if (!eval { $ret = file_probe_type($content); 1; }) {
636
    $::lxdebug->message(LXDebug::WARN(), "SL::File::_create_thumbnail file_probe_type failed: " . $EVAL_ERROR);
637
    return;
638
  }
639

  
640
  # file_probe_type returns a hash ref with thumbnail info and content
641
  # or an error message
642
  if ('HASH' ne ref $ret) {
643
    $::lxdebug->message(LXDebug::WARN(), "SL::File::_create_thumbnail file_probe_type returned an error: " . $ret);
644
    return;
645
  }
646

  
647
  return $ret;
648
}
649

  
650
sub _convert_pdf_to_png {
651
  my ($filename) = @_;
652

  
653
  my $sfile = SL::SessionFile::Random->new();
654

  
655
  my $command = 'pdftoppm -singlefile -scale-to 64 -png' . ' ' . $filename . ' ' . $sfile->file_name;
656

  
657
  if (system($command) == -1) {
658
    $::lxdebug->message(LXDebug::WARN(), "SL::File::_convert_pdf_to_png: system call failed: " . $ERRNO);
659
    return;
660
  }
661
  if ($CHILD_ERROR) {
662
    $::lxdebug->message(LXDebug::WARN(), "SL::File::_convert_pdf_to_png: pdftoppm failed with error code: " . ($CHILD_ERROR >> 8));
663
    return;
664
  }
665

  
666
  return $sfile->file_name . '.png';
667
}
668

  
604 669
1;
605 670

  
606 671
__END__
templates/webpages/file/list.html
1 1
[%- USE LxERP -%][% USE L %]
2 2
[% USE T8 %]
3
[% USE Base64 %]
4
[% USE HTML %]
3 5
[%- IF ! json %]
4 6
 <div id="[% file_type %]_list_[% object_type %]">
5 7
[%- END %]
......
36 38
      </th>
37 39
      <th class="listheading" width="30%"><b>[%  LxERP.t8('Description') %]</b></th>
38 40
     [%- ELSE %]
39
      <th class="listheading" width="40%"></th>
41
      <th class="listheading" width="40%"><b>[%  LxERP.t8('ImagePreview') %]</b></th>
40 42
     [%- END %]
41 43
    </tr>
42 44
   </thead>
......
61 63
       </td>
62 64
       <td>[% file.description %]</td>
63 65
      [%- ELSE %]
64
       <td></td>
66
       <td align="left">
67
        [%- IF file.thumbnail %]
68
         <a href="controller.pl?action=File/download&id=[% file.id %][%- IF file.version %]&version=[%- file.version %][%- END %]">
69
          <img src="data:[% HTML.escape(file.thumbnail.thumbnail_img_content_type) %];base64,[% file.thumbnail.thumbnail_img_content.encode_base64 %]" alt="[% file.file_name %]">
70
         </a>
71
        [%- ELSE %]
72
         -
73
        [%- END %]
74
       </td>
65 75
      [%- END %]
66 76
     </tr>
67 77
    [%- END # FOREACH file %]

Auch abrufbar als: Unified diff