| 1 | package MojoMojo::Schema::Result::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 | |||||||||||||||||||||||||||||
| 6 | 40 | ▶ | use parent qw/MojoMojo::Schema::Base::Result/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Number::Format qw( format_bytes ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 40 | __PACKAGE__->load_components( | |||||||||||||||||||||||||||
| 11 | qw/DateTime::Epoch TimeStamp Core/); | ||||||||||||||||||||||||||||
| 12 | 40 | __PACKAGE__->table("attachment"); | |||||||||||||||||||||||||||
| 13 | 40 | __PACKAGE__->add_columns( | |||||||||||||||||||||||||||
| 14 | "id", | ||||||||||||||||||||||||||||
| 15 | { | ||||||||||||||||||||||||||||
| 16 | data_type => "INTEGER", | ||||||||||||||||||||||||||||
| 17 | is_nullable => 0, | ||||||||||||||||||||||||||||
| 18 | size => undef, | ||||||||||||||||||||||||||||
| 19 | is_auto_increment => 1 | ||||||||||||||||||||||||||||
| 20 | }, | ||||||||||||||||||||||||||||
| 21 | "uploaded", | ||||||||||||||||||||||||||||
| 22 | { | ||||||||||||||||||||||||||||
| 23 | data_type => "BIGINT", | ||||||||||||||||||||||||||||
| 24 | is_nullable => 0, | ||||||||||||||||||||||||||||
| 25 | size => undef, | ||||||||||||||||||||||||||||
| 26 | inflate_datetime => 'epoch', | ||||||||||||||||||||||||||||
| 27 | set_on_create => 1 | ||||||||||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||||||||||
| 29 | "page", | ||||||||||||||||||||||||||||
| 30 | { data_type => "INTEGER", is_nullable => 0, size => undef }, | ||||||||||||||||||||||||||||
| 31 | "name", | ||||||||||||||||||||||||||||
| 32 | { data_type => "VARCHAR", is_nullable => 0, size => 100 }, | ||||||||||||||||||||||||||||
| 33 | "size", | ||||||||||||||||||||||||||||
| 34 | { data_type => "INTEGER", is_nullable => 1, size => undef }, | ||||||||||||||||||||||||||||
| 35 | "contenttype", | ||||||||||||||||||||||||||||
| 36 | { data_type => "VARCHAR", is_nullable => 1, size => 100 }, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | 40 | __PACKAGE__->set_primary_key("id"); | |||||||||||||||||||||||||||
| 39 | 40 | __PACKAGE__->belongs_to( | |||||||||||||||||||||||||||
| 40 | "page", | ||||||||||||||||||||||||||||
| 41 | "MojoMojo::Schema::Result::Page", | ||||||||||||||||||||||||||||
| 42 | { id => "page" } | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | 40 | __PACKAGE__->might_have( "photo", "MojoMojo::Schema::Result::Photo" ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 NAME | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | MojoMojo::Schema::Result::Attachment - store attachments | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head1 METHODS | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head2 delete | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Delete the inline and thumbnail versions but keep the original version | ||||||||||||||||||||||||||||
| 55 | (C<$self->filename>). | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | unlink( $self->inline_filename ) if -f $self->inline_filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | unlink( $self->thumb_filename ) if -f $self->thumb_filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1 | $self->next::method(); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head2 filename | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Full path to this attachment. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 10 | ▶ | sub filename { | ||||||||||||||||||||||||||
|
Subroutine
filename: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | 10 | my $attachment_dir = $self->result_source->schema->attachment_dir; | |||||||||||||||||||||||||||
| 76 | 10 | ▶ | die "MojoMojo::Schema->attachment_dir must be set to a writable directory (Current: $attachment_dir)\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-d $attachment_dir and -w $attachment_dir
MC/DC0%0/2
-d $attachment_dir and -w $attachment_dir
-d $attachment_dir
-w $attachment_dir
Truth table33%1/3
A and B
A= -d $attachment_dirB= -w $attachment_dir
| |||||||||||||||||||||||||||||
| 77 | unless -d $attachment_dir && -w $attachment_dir; | ||||||||||||||||||||||||||||
| 78 | 10 | return ( $attachment_dir . '/' . $self->id ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head2 inline_filename | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Name of attachment file when displayed inline. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | ▶ | sub inline_filename { shift->filename . '.inline'; } | ||||||||||||||||||||||||||
|
Subroutine
inline_filename: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head2 thumb_filename | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Nmae of thumbnail of attachment. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | sub thumb_filename { shift->filename . '.thumb'; } | ||||||||||||||||||||||||||
|
Subroutine
thumb_filename: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head2 make_photo | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | Insert photo id and title into photo table. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub make_photo { | ||||||||||||||||||||||||||
|
Subroutine
make_photo: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 105 | 0 | my $photo = $self->result_source->related_source('photo')->resultset->new( | |||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | id => $self->id, | ||||||||||||||||||||||||||||
| 108 | title => $self->name, | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | 0 | $photo->description('Set your description'); | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | $photo->extract_exif($self) if $self->contenttype eq 'image/jpeg'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | $photo->insert(); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head2 is_image | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Predicate to indicate is the contenttype is image or not. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 18 | ▶ | sub is_image { | ||||||||||||||||||||||||||
|
Subroutine
is_image: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 18 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 18 | return $self->contenttype =~ m{^image/}; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head2 is_text | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Predicate to indicate is the contenttype is text or not. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 6 | ▶ | sub is_text { | ||||||||||||||||||||||||||
|
Subroutine
is_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 6 | return $self->contenttype =~ m{^text/}; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head2 human_size | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Get a human readable size. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 6 | ▶ | sub human_size { | ||||||||||||||||||||||||||
|
Subroutine
human_size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 6 | return format_bytes( $self->size, precision => 1 ); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # It would be nice to find an external module/data source for this data, | ||||||||||||||||||||||||||||
| 153 | # e.g. http://en.kioskea.net/contents/courrier-electronique/mime.php3 | ||||||||||||||||||||||||||||
| 154 | # and/or bundle it into a separate module for CPAN. | ||||||||||||||||||||||||||||
| 155 | 40 | my %mime_type_to_description = ( | |||||||||||||||||||||||||||
| 156 | 'application/javascript' => 'Javascript', | ||||||||||||||||||||||||||||
| 157 | 'application/json' => 'JSON data', | ||||||||||||||||||||||||||||
| 158 | 'application/pdf' => 'PDF document', | ||||||||||||||||||||||||||||
| 159 | 'application/xhtml+xml' => 'Web page', | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 'audio/mpeg' => 'Sound file', | ||||||||||||||||||||||||||||
| 162 | 'audio/ogg' => 'Sound file', | ||||||||||||||||||||||||||||
| 163 | 'audio/vorbis' => 'Sound file', | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 'text/css' => 'Cascading style sheet', | ||||||||||||||||||||||||||||
| 166 | 'text/csv' => 'Comma separated values', | ||||||||||||||||||||||||||||
| 167 | 'text/html' => 'Web page', | ||||||||||||||||||||||||||||
| 168 | 'text/plain' => 'Plain text file', | ||||||||||||||||||||||||||||
| 169 | 'text/xml' => 'XML file', | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 'image/gif' => 'GIF image', | ||||||||||||||||||||||||||||
| 172 | 'image/jpeg' => 'JPEG image', | ||||||||||||||||||||||||||||
| 173 | 'image/png' => 'PNG image', | ||||||||||||||||||||||||||||
| 174 | ); | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head2 human_type | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Describe the mime type (in English?). | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 6 | ▶ | sub human_type { | ||||||||||||||||||||||||||
|
Subroutine
human_type: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||
| 183 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 6 | ▶ | return $mime_type_to_description{ $self->contenttype } | ||||||||||||||||||||||||||
|
Condition33%1/3
$mime_type_to_description{$self->contenttype} || $self->contenttype
MC/DC0%0/2
$mime_type_to_description{$self->contenttype} || $self->contenttype
$mime_type_to_description{$self->contenttype}
$self->contenttype
Truth table33%1/3
A || B
A= $mime_type_to_description{$self->contenttype}B= $self->contenttype
| |||||||||||||||||||||||||||||
| 186 | || $self->contenttype; | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Marcus Ramberg <mramberg@cpan.org> | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | This library is free software. You can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 196 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =cut | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 40 | 1; | |||||||||||||||||||||||||||