line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MojoMojo::Schema::Result::Attachment; |
2
|
|
|
|
|
|
|
|
3
|
40
|
|
|
40
|
|
56720
|
use strict; |
|
40
|
|
|
|
|
105
|
|
|
40
|
|
|
|
|
1111
|
|
4
|
40
|
|
|
40
|
|
222
|
use warnings; |
|
40
|
|
|
|
|
99
|
|
|
40
|
|
|
|
|
1126
|
|
5
|
|
|
|
|
|
|
|
6
|
40
|
|
|
40
|
|
213
|
use parent qw/MojoMojo::Schema::Base::Result/; |
|
40
|
|
|
|
|
90
|
|
|
40
|
|
|
|
|
286
|
|
7
|
|
|
|
|
|
|
|
8
|
40
|
|
|
40
|
|
23050
|
use Number::Format qw( format_bytes ); |
|
40
|
|
|
|
|
149532
|
|
|
40
|
|
|
|
|
19401
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
__PACKAGE__->load_components( |
11
|
|
|
|
|
|
|
qw/DateTime::Epoch TimeStamp Core/); |
12
|
|
|
|
|
|
|
__PACKAGE__->table("attachment"); |
13
|
|
|
|
|
|
|
__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
|
|
|
|
|
|
|
__PACKAGE__->set_primary_key("id"); |
39
|
|
|
|
|
|
|
__PACKAGE__->belongs_to( |
40
|
|
|
|
|
|
|
"page", |
41
|
|
|
|
|
|
|
"MojoMojo::Schema::Result::Page", |
42
|
|
|
|
|
|
|
{ id => "page" } |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
__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
|
|
|
|
|
|
|
sub delete { |
60
|
1
|
|
|
1
|
1
|
71
|
my ($self) = @_; |
61
|
|
|
|
|
|
|
|
62
|
1
|
50
|
|
|
|
7
|
unlink( $self->inline_filename ) if -f $self->inline_filename; |
63
|
1
|
50
|
|
|
|
58
|
unlink( $self->thumb_filename ) if -f $self->thumb_filename; |
64
|
1
|
|
|
|
|
34
|
$self->next::method(); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head2 filename |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Full path to this attachment. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub filename { |
74
|
10
|
|
|
10
|
1
|
26
|
my $self = shift; |
75
|
10
|
|
|
|
|
59
|
my $attachment_dir = $self->result_source->schema->attachment_dir; |
76
|
10
|
50
|
33
|
|
|
392
|
die "MojoMojo::Schema->attachment_dir must be set to a writable directory (Current: $attachment_dir)\n" |
77
|
|
|
|
|
|
|
unless -d $attachment_dir && -w $attachment_dir; |
78
|
10
|
|
|
|
|
252
|
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
|
|
|
1
|
1
|
6
|
sub inline_filename { shift->filename . '.inline'; } |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 thumb_filename |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Nmae of thumbnail of attachment. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |
94
|
|
|
|
|
|
|
|
95
|
1
|
|
|
1
|
1
|
5
|
sub thumb_filename { shift->filename . '.thumb'; } |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 make_photo |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Insert photo id and title into photo table. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub make_photo { |
104
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
105
|
0
|
|
|
|
|
0
|
my $photo = $self->result_source->related_source('photo')->resultset->new( |
106
|
|
|
|
|
|
|
{ |
107
|
|
|
|
|
|
|
id => $self->id, |
108
|
|
|
|
|
|
|
title => $self->name, |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
); |
111
|
0
|
|
|
|
|
0
|
$photo->description('Set your description'); |
112
|
0
|
0
|
|
|
|
0
|
$photo->extract_exif($self) if $self->contenttype eq 'image/jpeg'; |
113
|
0
|
|
|
|
|
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
|
|
|
|
|
|
|
sub is_image { |
123
|
18
|
|
|
18
|
1
|
39
|
my $self = shift; |
124
|
|
|
|
|
|
|
|
125
|
18
|
|
|
|
|
387
|
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
|
|
|
|
|
|
|
sub is_text { |
135
|
6
|
|
|
6
|
1
|
199
|
my $self = shift; |
136
|
|
|
|
|
|
|
|
137
|
6
|
|
|
|
|
100
|
return $self->contenttype =~ m{^text/}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 human_size |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Get a human readable size. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub human_size { |
147
|
6
|
|
|
6
|
1
|
16
|
my $self = shift; |
148
|
|
|
|
|
|
|
|
149
|
6
|
|
|
|
|
98
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub human_type { |
183
|
6
|
|
|
6
|
1
|
16
|
my $self = shift; |
184
|
|
|
|
|
|
|
|
185
|
6
|
|
33
|
|
|
101
|
return $mime_type_to_description{ $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
|
|
|
|
|
|
|
1; |