Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 1513c4c2

Von Tamino Steinert vor 7 Monaten hinzugefügt

  • ID 1513c4c2c50b00c3133e22ca2672855f6570aacb
  • Vorgänger 91d5a27e
  • Nachfolger 8635c0d6

Presenter: EmailJournal: Helferfunktion um E-mailanhänge anzuzeigen

Unterschiede anzeigen:

SL/Presenter/EmailJournal.pm
3 3
use strict;
4 4

  
5 5
use SL::Presenter::EscapedText qw(escape is_escaped);
6
use SL::Presenter::Tag         qw(link_tag);
6
use SL::Presenter::Tag         qw(link_tag img_tag html_tag);
7 7
use SL::Locale::String qw(t8);
8
use SL::SessionFile::Random;
8 9

  
9 10
use Exporter qw(import);
10
our @EXPORT_OK = qw(email_journal entry_status);
11
our @EXPORT_OK = qw(email_journal entry_status attachment_preview);
11 12

  
12 13
use Carp;
13 14

  
......
43 44
  return $text;
44 45
}
45 46

  
47
sub attachment_preview {
48
  my ($attachment, %params) = @_;
49

  
50
  # clean up mime_type
51
  my $mime_type = $attachment->mime_type;
52
  $mime_type =~ s/;.*//;
53

  
54
  # parse to img tag
55
  my $image_tags = '';
56
  if ($mime_type =~ m{^image/}) {
57
    my $image_content = $attachment->content;
58
    my $img_base64 = "data:$mime_type;base64," . MIME::Base64::encode_base64($image_content);
59
    my $image_tag = img_tag(
60
      src => $img_base64,
61
      alt => escape($attachment->name),
62
      %params);
63
    $image_tags .= $image_tag;
64
  } elsif ($mime_type =~ m{^application/pdf}) {
65
    my $pdf_content = $attachment->content;
66
    my $session_file = SL::SessionFile::Random->new(mode => 'w');
67
    $session_file->fh->print($pdf_content);
68
    $session_file->fh->close;
69
    my $image_size = 2048;
70

  
71
    my $file_name = $session_file->file_name;
72

  
73
    # files are created in session_files folder
74
    my $command = 'pdftoppm -forcenum -scale-to '
75
                  . $image_size . ' -png' . ' '
76
                  . $file_name . ' ' . $file_name;
77
    my $ans = system($command);
78
    if ($ans != 0) {
79
      return;
80
    }
81

  
82

  
83
    my @image_file_names = glob($file_name . '-*.png');
84
    unlink($file_name);
85

  
86
    my $image_count = scalar @image_file_names;
87
    my $counter = 1;
88
    foreach my $image_file_name (@image_file_names) {
89
      my $image_file = SL::SessionFile->new($image_file_name, mode => 'r');
90
      my $file_size = -s $image_file->file_name;
91
      my $image_content;
92
      read($image_file->fh, $image_content, $file_size);
93
      my $img_base64 = 'data:image/png;base64,' . MIME::Base64::encode_base64($image_content);
94
      my $name_ending = $image_count > 1 ? "-($counter/$image_count)" : '';
95
      my $image_tag = img_tag(
96
        src => $img_base64,
97
        alt => escape($attachment->name) . $name_ending,
98
        %params);
99
      unlink($image_file->file_name);
100
      $image_tags .= $image_tag;
101
    }
102
  }
103

  
104
  my $attachment_preview = html_tag('div', $image_tags, id => 'attachment_preview');
105

  
106
  return is_escaped($attachment_preview);
107
}
108

  
46 109
1;
47 110

  
48 111
__END__

Auch abrufbar als: Unified diff