Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision bd727c21

Von Tamino Steinert vor 11 Monaten hinzugefügt

  • ID bd727c213d82ce4cf3f9d68fea5b811a6bf870c3
  • Vorgänger d0840cc3
  • Nachfolger 20a9061f

EmailJournal: Anhangsvorschau mit kopierbaren Text

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 img_tag html_tag);
6
use SL::Presenter::Tag         qw(link_tag html_tag div_tag);
7 7
use SL::Locale::String qw(t8);
8 8
use SL::SessionFile::Random;
9 9
use SL::DB::EmailJournalAttachment;
......
50 50
  my ($attachment_or_id, %params) = @_;
51 51

  
52 52
  if (! $attachment_or_id) {
53
    return is_escaped(html_tag('div', '', id => 'attachment_preview'));
54
  }
55
  my $attachment = ref $attachment_or_id ? $attachment_or_id
56
     : SL::DB::EmailJournalAttachment->new(id => $attachment_or_id)->load;
57

  
58
  # clean up mime_type
59
  my $mime_type = $attachment->mime_type;
60
  $mime_type =~ s/;.*//;
61

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

  
79
    my $file_name = $session_file->file_name;
80

  
81
    # files are created in session_files folder
82
    my $command = 'pdftoppm -forcenum -scale-to '
83
                  . $image_size . ' -png' . ' '
84
                  . $file_name . ' ' . $file_name;
85
    my $ans = system($command);
86
    if ($ans != 0) {
87
      return;
88
    }
89

  
90

  
91
    my @image_file_names = glob($file_name . '-*.png');
92
    unlink($file_name);
93

  
94
    my $image_count = scalar @image_file_names;
95
    my $counter = 1;
96
    foreach my $image_file_name (@image_file_names) {
97
      my $image_file = SL::SessionFile->new($image_file_name, mode => 'r');
98
      my $file_size = -s $image_file->file_name;
99
      my $image_content;
100
      read($image_file->fh, $image_content, $file_size);
101
      my $img_base64 = 'data:image/png;base64,' . MIME::Base64::encode_base64($image_content);
102
      my $name_ending = $image_count > 1 ? "-($counter/$image_count)" : '';
103
      my $image_tag = img_tag(
104
        src => $img_base64,
105
        alt => escape($attachment->name) . $name_ending,
106
        %params);
107
      unlink($image_file->file_name);
108
      $image_tags .= $image_tag;
109
    }
53
    return is_escaped(div_tag('', id => 'attachment_preview'));
110 54
  }
55
  my $attachment_id = ref $attachment_or_id ? $attachment_or_id->id
56
     : $attachment_or_id;
111 57

  
112
  my $attachment_preview = html_tag('div', $image_tags, id => 'attachment_preview');
58
  require SL::Controller::EmailJournal;
59
  my $src_url = SL::Controller::EmailJournal->new->url_for(
60
      action => 'show_attachment',
61
      attachment_id => $attachment_id
62
  );
113 63

  
64
  $params{style} .= "; display:flex; resize:both; overflow:hidden; padding-bottom:5px";
65
  my $attachment_preview = div_tag(
66
    html_tag('iframe', '', src => $src_url,
67
      width => "100%", height => '100%',
68
      "flex-grow" => '1',
69
      ),
70
    id => 'attachment_preview',
71
    %params
72
  );
114 73
  return is_escaped($attachment_preview);
115 74
}
116 75

  

Auch abrufbar als: Unified diff