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