File Coverage

blib/lib/EB/Tools/Attachments.pm
Criterion Covered Total %
statement 18 93 19.3
branch 0 26 0.0
condition 0 35 0.0
subroutine 6 16 37.5
pod 0 10 0.0
total 24 180 13.3


line stmt bran cond sub pod time code
1             #! perl
2              
3 1     1   7 use utf8;
  1         3  
  1         5  
4              
5             # Author : Johan Vromans
6             # Created On : Tue Oct 6 13:55:54 2015
7             # Last Modified By: Johan Vromans
8             # Last Modified On: Fri Feb 3 21:26:38 2017
9             # Update Count : 92
10             # Status : Unknown, Use with caution!
11              
12             package main;
13              
14             our $dbh;
15             our $config;
16              
17             package EB::Tools::Attachments;
18              
19 1     1   58 use strict;
  1         2  
  1         27  
20 1     1   8 use warnings;
  1         1  
  1         31  
21 1     1   26 use Fcntl qw(O_RDONLY O_WRONLY O_CREAT);
  1         3  
  1         47  
22 1     1   776 use File::Temp;
  1         21268  
  1         75  
23              
24 1     1   10 use EB;
  1         2  
  1         1396  
25              
26             sub new {
27 0     0 0   my ( $pkg, %init ) = @_;
28 0           bless { id => 0,
29             name => undef, path => undef,
30             encoding => 0, checksum => undef,
31             content => undef, %init }, $pkg;
32             }
33              
34             sub store {
35 0     0 0   my ( $self, $id ) = @_;
36              
37 0   0       $self->{id} ||= $id || $dbh->get_sequence("attachments_id_seq");
      0        
38 0   0       $self->{encoding} ||= ATTENCODING_BASE64;
39             $self->{size} ||=
40             $self->{encoding} == ATTENCODING_URI
41             ? length( $self->{name} )
42 0 0 0       : length( ${ $self->{content} } );
  0            
43 0           my $att_id = $dbh->store_attachment($self);
44 0 0         $self->set_sequence( "attachments_id_seq", $self->{id} ) if $id;
45 0           return $self->{id};
46             }
47              
48             sub store_from_uri {
49 0     0 0   my ( $self, $uri, $id ) = @_;
50 0   0       $id ||= $self->{id};
51 0           $self->{encoding} = ATTENCODING_URI;
52 0           $self->{name} = $uri;
53 0           $self->store;
54             }
55              
56             sub store_from_file {
57 0     0 0   my ( $self, $filename, $id ) = @_;
58 0   0       $id ||= $self->{id};
59 0           my $file = $filename;
60 0 0         sysopen( my $fd, $file, O_RDONLY )
61             or die(__x("Bijlage {file} kan niet worden opgeslagen: {err}",
62             file => $filename, err => "".$!)."\n");
63              
64 0           my $cnt;
65 0           my $buf = "";
66 0           my $offset = 0;
67             # my $ctx = Digest::MD5->new;
68 0           while ( ( $cnt = sysread( $fd, $buf, 20480, $offset ) ) > 0 ) {
69              
70             =begin later
71              
72             unless ( defined $type ) {
73             if ( $buf =~ /^\%PDF-/ ) {
74             $type = ATTTYPE_PDF;
75             }
76             elsif ( $buf =~ /^\x89PNG\x0d\x0a\x1a\x0a/ ) {
77             $type = ATTTYPE_PNG;
78             }
79             elsif ( $buf =~ /^\xff\xd8/ ) {
80             $type = ATTTYPE_JPG;
81             }
82             elsif ( $buf =~ /^[[:print:]\s]*$/ ) {
83             $type = ATTTYPE_TEXT;
84             }
85             else {
86             die(__x("Bijlage {file} is van een niet-ondersteund type",
87             file => $filename)."\n");
88             }
89             }
90              
91             =cut
92              
93 0           $offset += $cnt;
94             }
95 0 0         die(__x("Bijlage {file} kon niet worden gelezen: {err}",
96             file => $filename, err => $!)."\n") unless $cnt == 0;
97 0           close($fd);
98              
99             # $ctx->add($buf);
100             # $self->{checksum} = $ctx->hexdigest;
101              
102 0           $self->{content} = \$buf;
103 0   0       $self->{name} ||= File::Basename::fileparse($file);
104 0           $self->{size} = $offset;
105 0           $self->store;
106             }
107              
108             sub drop {
109 0     0 0   my ( $self, $id ) = @_;
110 0   0       $dbh->drop_attachment( $id || $self->{id} );
111             }
112              
113             sub get {
114 0     0 0   my ( $self, $id ) = @_;
115 0   0       my $href = $dbh->get_attachment( $id || $self->{id} );
116             # { name => $name, encoding => $enc, content => \$data }
117 0           return $href;
118             }
119              
120             sub save_to_file {
121 0     0 0   my ( $self, $filename, $id ) = @_;
122 0   0       my $atts = $self->get( $id || $self->{id} ); # HashRef!
123 0           for ( qw( name content ) ) {
124 0           $self->{$_} = $atts->{$_};
125             }
126              
127 0 0         if ( $atts->{encoding} == ATTENCODING_URI ) {
128 0           my $content = $self->{name} . "\n";
129 0           $self->{content} = \$content;
130             }
131              
132 0           my $fd;
133 0 0         if ( $filename ) {
134 0 0         sysopen( $fd, $filename, O_WRONLY|O_CREAT, 0666 )
135             or die("?".__x("Fout bij aanmaken bestand {file}: {err}",
136             file => $filename, err => $!)."\n");
137             }
138             else {
139             $fd = File::Temp->new( UNLINK => 0,
140 0           SUFFIX => "__" . $self->{name} );
141 0           $filename = $fd->filename;
142             }
143              
144 0 0         syswrite( $fd, ${ $self->{content} }, length( ${ $self->{content} } ) ) == length( ${ $self->{content} } )
  0            
  0            
  0            
145             or die("?".__x("Fout bij schrijven bestand {file}: {err}",
146             file => $filename, err => $!)."\n");
147 0 0         $fd->close
148             or die("?".__x("Fout bij afsluiten bestand {file}: {err}",
149             file => $filename, err => $!)."\n");
150 0           return $filename;
151             }
152              
153             sub save_to_zip {
154 0     0 0   my ( $self, $zip, $membername, $id ) = @_;
155 0   0       my $atts = $self->get( $id || $self->{id} ); # HashRef!
156 0           my $m = $zip->addString( $atts->{content}, $membername );
157             # Error check not needed?
158 0           $m->desiredCompressionMethod(8);
159             }
160              
161             sub open {
162 0     0 0   my ( $self, $id, $output ) = @_;
163 0   0       $id ||= $self->{id};
164 0           my $href = EB::Tools::Attachments->new->get($id);
165              
166 0           my $file = $self->save_to_file( $output, $id );
167 0 0         return if defined $output;
168              
169 0 0         if ( $^O eq "MSWin32" ) {
    0          
170 0 0         if ( $Wx::VERSION ) {
171 0           Wx::LaunchDefaultBrowser("$file");
172             }
173             else {
174 0           system("start", $file); # ????
175             }
176 0           unlink($file);
177             }
178             elsif ( $^O eq "OSX" ) {
179             # Do we need to sleep here?
180 0           system("sh -c 'open \"$file\"; rm -f \"$file\"'&");
181             }
182             else {
183             # xdg-open spawns the right tool and exits immediately.
184 0           system("sh -c 'xdg-open \"$file\"; sleep 5; rm -f \"$file\"'&");
185             }
186             }
187              
188             sub attachments {
189 0     0 0   my ( $self ) = @_;
190 0           my $ret = [];
191 0           my $sth = $dbh->sql_exec("SELECT att_id,att_name,att_encoding FROM Attachments ORDER BY att_id");
192 0           while ( my $rr = $sth->fetchrow_arrayref ) {
193 0           push( @$ret, { id => $rr->[0], name => $rr->[1], encoding => $rr->[2] } );
194             }
195 0           $ret;
196             }
197              
198             1;