File Coverage

blib/lib/MojoMojo/Schema/ResultSet/Attachment.pm
Criterion Covered Total %
statement 38 38 100.0
branch 2 4 50.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 51 53 96.2


line stmt bran cond sub pod time code
1             package MojoMojo::Schema::ResultSet::Attachment;
2              
3 40     40   45577 use strict;
  40         107  
  40         1020  
4 40     40   194 use warnings;
  40         143  
  40         967  
5 40     40   187 use parent qw/MojoMojo::Schema::Base::ResultSet/;
  40         85  
  40         195  
6 40     40   7497 use Archive::Zip qw(:ERROR_CODES);
  40         562076  
  40         5146  
7 40     40   23158 use File::MMagic;
  40         172149  
  40         753  
8 40     40   1075 use FileHandle;
  40         104  
  40         238  
9 40     40   9913 use File::Copy;
  40         103  
  40         2397  
10 40     40   278 use File::Temp qw/tempfile/;
  40         90  
  40         1847  
11 40     40   27968 use Imager;
  40         976100  
  40         362  
12              
13             =head1 NAME
14              
15             MojoMojo::Schema::ResultSet::Attachment - resulset methods on attachments
16              
17             =head1 METHODS
18              
19             =cut
20              
21             =head2 create_from_file (page, filename, storage_callback)
22              
23             Create an instance from a given file. Takes a page to attach to, the
24             client-supplied filename, and the actual file.
25              
26             =cut
27              
28             sub create_from_file {
29 2     2 1 1092 my ( $class, $page, $filename, $file ) = @_;
30 2         22 my $mm = File::MMagic->new();
31             #if ( $mm->checktype_filename($filename) eq 'application/zip' ) {
32             # TODO: the file type returned for a ZIP is 'application/x-zip' (not 'application-zip'),
33             # so this has never actually worked. It also never worked because $filename is
34             # the client-supplied filename, not the actually uploaded file.
35             # Anyway, unpacking the ZIP willy-nilly is a silly idea.
36             # Commented out until a UI option to unpack uploaded ZIP(s) is added.
37             # --dandv
38             #my $zip;
39             #$zip = Archive::Zip->new($file);
40             #return unless $zip;
41             #my @atts;
42             #foreach my $member ( $zip->members ) {
43             # next if $member->isDirectory;
44             # my $tmpfile = tempfile;
45             # $member->extractToFileNamed($tmpfile);
46             # push @atts, $class->create_from_file( $page, $member->fileName, $tmpfile );
47             #}
48             #return @atts;
49             #}
50              
51 2         1030 my $self = $class->create({
52             name => $filename,
53             page => $page->id
54             });
55 2 50       26271 die "Could not attach $filename to $page" if not $self;
56              
57             # copy the passed $file (usually from the temporary upload directory), to the attachments directory,
58             # with the filename set to MojoMojo Schema::Result::Attachment->filename (currently the row id)
59 2         23 File::Copy::copy( $file, $self->filename );
60              
61 2         789 my $fh = FileHandle->new( $self->filename . '' );
62 2         217 $self->contenttype( $mm->checktype_filehandle($fh) );
63 2         49799 $self->size( -s $self->filename );
64 2         293 $self->update();
65 2 50       17444 $self->make_photo if ( $self->contenttype =~ m|^image/| );
66 2         321 return $self;
67             }
68              
69             =head1 AUTHOR
70              
71             Marcus Ramberg <mramberg@cpan.org>
72              
73             =head1 LICENSE
74              
75             This library is free software. You can redistribute it and/or modify
76             it under the same terms as Perl itself.
77              
78             =cut
79              
80             1;