| 1 | package MojoMojo::Schema::ResultSet::Attachment; | ||||||||||
| 2 | |||||||||||
| 3 | 40 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 40 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 40 | ▶ | use parent qw/MojoMojo::Schema::Base::ResultSet/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 40 | ▶ | use Archive::Zip qw(:ERROR_CODES); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 40 | ▶ | use File::MMagic; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 40 | ▶ | use FileHandle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 40 | ▶ | use File::Copy; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 40 | ▶ | use File::Temp qw/tempfile/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 40 | ▶ | use Imager; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 2 | ▶ | sub create_from_file { | ||||||||
|
Subroutine
create_from_file: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 29 | 2 | my ( $class, $page, $filename, $file ) = @_; | |||||||||
| 30 | 2 | 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 | my $self = $class->create({ | |||||||||
| 52 | name => $filename, | ||||||||||
| 53 | page => $page->id | ||||||||||
| 54 | }); | ||||||||||
| 55 | 2 | ▶ | die "Could not attach $filename to $page" if not $self; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | File::Copy::copy( $file, $self->filename ); | |||||||||
| 60 | |||||||||||
| 61 | 2 | my $fh = FileHandle->new( $self->filename . '' ); | |||||||||
| 62 | 2 | $self->contenttype( $mm->checktype_filehandle($fh) ); | |||||||||
| 63 | 2 | $self->size( -s $self->filename ); | |||||||||
| 64 | 2 | $self->update(); | |||||||||
| 65 | 2 | ▶ | $self->make_photo if ( $self->contenttype =~ m|^image/| ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 2 | 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 | 40 | 1; | |||||||||