Line # Revision Author
1 3 ahitrov@rambler.ru package Contenido::File;
2
3 use strict;
4 use warnings;
5
6 use Contenido::Globals;
7 use URI;
8 use Data::Dumper;
9 use IO::File;
10 use IO::Scalar;
11 use Contenido::File::Scheme::HTTP;
12 use Contenido::File::Scheme::FILE;
13 use Contenido::DateTime;
14
15 our $IgnoreErrors = 1;
16
17 sub fetch {
18 my $filename = shift || return;
19 my $fh;
20
21 foreach my $dir (@{$state->{"files_dir"}}) {
22 my $path = $dir . '/' . $filename;
23
24 no strict "refs";
25 $fh = &{"Contenido::File::Scheme::".uc(scheme($path))."::fetch"}($path);
26 return $fh if $fh;
27 }
28 }
29
30 sub store {
31 my $filename = shift || return;
32 my $fh = get_fh(shift) || return;
33
34 my $dt = Contenido::DateTime->new()->set_locale('en')->set_time_zone("UTC");
35
36 my @successful;
37
38 #������� ��������� � ����� /
39 $filename =~ s#/+#/#g;
40 #������� ��������� /
41 $filename =~ s#^/##;
42
43 foreach my $dir (@{$state->{"files_dir"}}) {
44 seek $fh, 0, 0;
45 my $path = $dir . '/' . $filename;
46
47 no strict "refs";
48 my $return = &{"Contenido::File::Scheme::".uc(scheme($path))."::store"}($path, $fh, $dt);
49 push @successful, $path if $return;
50 }
51
52 if (
53 !@successful or
54 (
55 (scalar @successful != scalar @{$state->{"files_dir"}}) and
56 !$IgnoreErrors
57 )
58 ) {
59 foreach my $path (@successful) {
60 no strict "refs";
61 &{"Contenido::File::Scheme::".uc(scheme($path))."::remove"}($path);
62 }
63 return;
64 }
65 1;
66 }
67
68 sub size {
69 my $filename = shift;
70 my %args = @_;
71
72 my $size;
73
74 #������� ��������� � ����� /
75 $filename =~ s#/+#/#g;
76 #������� ��������� /
77 $filename =~ s#^/##;
78
79 foreach my $dir (@{$state->{"files_dir"}}) {
80 no strict "refs";
81 my $response = &{"Contenido::File::Scheme::".uc(scheme($dir))."::size"}($dir."/".$filename, %args);
82 return unless defined $response;
83 return if defined $size and $size != $response;
84
85 $size = $response;
86 #TODO
87 last;
88 }
89 return $size;
90 }
91
92 sub remove {
93 my $filename = shift || return;
94
95 #������� ��������� � ����� /
96 $filename =~ s#/+#/#g;
97 #������� ��������� /
98 $filename =~ s#^/##;
99
100 foreach my $dir (@{$state->{"files_dir"}}) {
101 no strict "refs";
102 &{"Contenido::File::Scheme::".uc(scheme($dir))."::remove"}($dir."/".$filename);
103 }
104
105 1;
106 }
107
108 sub get_fh {
109 my $input = shift;
110 my $fh;
111
112 if (not ref $input) {
113 no strict "refs";
114 $fh = &{"Contenido::File::Scheme::".uc(scheme($input))."::get_fh"}($input);
115 114 ahitrov } elsif ( ref $input eq 'Apache::Upload' ) {
116 $fh = $input->fh;
117 } elsif ((ref $input eq "GLOB") or (ref $input eq 'IO::File')) {
118 3 ahitrov@rambler.ru $fh = $input;
119 } elsif (ref $input eq "SCALAR") {
120 $fh = IO::Scalar->new($input);
121 } else {
122 $log->warning("Path, scalar ref or fh needed");
123 return;
124 }
125
126 return $fh
127 }
128
129 sub scheme {
130 my $uri = shift;
131 my $scheme;
132
133 $scheme = URI->new($uri)->scheme() || "file";
134
135 return $scheme;
136 }
137
138 # sub files_dir {
139 # return unless $_[0];
140 # my $dir;
141
142 # for (scheme($_[0])) {
143 # /http/ && do {
144 # $dir = URI->new($_[0])->canonical();
145 # $dir =~ s|/*$|/|;
146 # last;
147 # };
148 # /file/ && do {
149 # $dir = URI->new($_[0])->path();
150 # $dir =~ s|/*$|/|;
151 # last;
152 # };
153 # }
154
155 # return $dir;
156 # }
157
158 114 ahitrov sub store_image {
159 my $input = shift;
160 my (%opts) = @_;
161 my $object = delete $opts{object} || return;
162 my $attr = delete $opts{attr} || return;
163
164 my ($prop) = grep { $_->{attr} eq $attr } $object->structure;
165 return unless ref $prop;
166 my @preview = exists $prop->{'preview'} && ref $prop->{'preview'} eq 'ARRAY' ? @{$prop->{'preview'}} : exists $prop->{'preview'} && $prop->{'preview'} ? ($prop->{'preview'}) : ();
167 my @crops = exists $prop->{'crop'} && ref $prop->{'crop'} eq 'ARRAY' ? @{$prop->{'crop'}} : exists $prop->{'crop'} && $prop->{'crop'} ? ($prop->{'crop'}) : ();
168 my @shrinks = exists $prop->{'shrink'} && ref $prop->{'shrink'} eq 'ARRAY' ? @{$prop->{'shrink'}} : exists $prop->{'shrink'} && $prop->{'shrink'} ? ($prop->{'shrink'}) : ();
169
170 my $filename = '/images/'.$object->get_file_name() || return;
171 my $filename_tmp = $state->{'tmp_dir'}.'/'.join('_', split('/', $filename));
172
173 my $fh = get_fh($input);
174 return unless ref $fh;
175
176 my $ext;
177 121 ahitrov my $size = 1073741824;
178 if ( not ref $input ) {
179 114 ahitrov $ext = $input =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
180 121 ahitrov if ( scheme($input) eq 'file' ) {
181 $size = (stat $fh)[7];
182 }
183 114 ahitrov } elsif ( ref $input eq 'Apache::Upload' ) {
184 $ext = $input->filename() =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
185 121 ahitrov $size = (stat $fh)[7];
186 } elsif ( $opts{filename} ) {
187 $ext = $opts{filename} =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
188 114 ahitrov }
189 127 ahitrov if ( ref $fh eq 'IO::Scalar' ) {
190 $size = length("$fh");
191 }
192 114 ahitrov $ext ||= 'bin';
193
194 my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$ext) || return;
195 my $buffer;
196
197 121 ahitrov $size = sysread $fh, $buffer, $size;
198 114 ahitrov syswrite $fh_tmp, $buffer, $size;
199
200 undef $fh_tmp;
201
202 my $IMAGE;
203 if ( store($filename.'.'.$ext, $filename_tmp.'.'.$ext) ) {
204 $IMAGE = {};
205 # hashref slice assigning - ������
206 @{$IMAGE}{'filename', 'width', 'height'} = (
207 $filename.'.'.$ext,
208 Image::Size::imgsize($filename_tmp.'.'.$ext),
209 );
210
211 foreach my $suffix (@preview) {
212 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'\' -quality 80 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
213 my $result = `$c_line`;
214
215 if (length $result > 0) {
216 warn 'Contenido Error: ��� ������ "'.$c_line.'" ��������� ������ "'.$result.'" ('.$@.")\n";
217 return undef;
218 }
219 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
220 $filename.'.'.$suffix.'.'.$ext,
221 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$ext),
222 );
223 %{$IMAGE->{'resize'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
224 store($filename.'.'.$suffix.'.'.$ext, $filename_tmp.'.'.$suffix.'.'.$ext);
225 unlink $filename_tmp.'.'.$suffix.'.'.$ext if -e $filename_tmp.'.'.$suffix.'.'.$ext;
226 }
227 if ( @preview ) {
228 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'};
229 @{$IMAGE->{'resize'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'};
230 }
231
232 ########## CROPS
233 foreach my $suffix (@crops) {
234
235 my $shave_string;
236 my ($nwidth, $nheight) = $suffix =~ /(\d+)x(\d+)/i ? ($1, $2) : (0, 0);
237 if ( ($IMAGE->{width} / $IMAGE->{height}) > ($nwidth / $nheight) ) {
238 my $shave_pixels = (($IMAGE->{width} / $IMAGE->{height}) - ($nwidth / $nheight)) * $IMAGE->{height};
239 $shave_string = ' -shave '.int($shave_pixels / 2).'x0';
240 } elsif ( ($IMAGE->{height} / $IMAGE->{width}) > ($nheight / $nwidth) ) {
241 my $shave_pixels = (($IMAGE->{height} / $IMAGE->{width}) - ($nheight / $nwidth)) * $IMAGE->{width};
242 $shave_string = ' -shave 0x'.int($shave_pixels / 2);
243 }
244 if ( $shave_string ) {
245 my $c_line = $state->{"convert_binary"}." $shave_string $filename_tmp.$ext $filename_tmp.shaved.$ext";
246 my $result = `$c_line`;
247 if (length $result > 0) {
248 print "Contenido Error: ��� ������ '$c_line' ��������� ������ '$result' ($@)\n";
249 }
250 } else {
251 my $c_line = "cp $filename_tmp.$ext $filename_tmp.shaved.$ext";
252 my $result = `$c_line`;
253 if (length $result > 0) {
254 print "Contenido Error: ��� ������ '$c_line' ��������� ������ '$result' ($@)\n";
255 }
256 }
257
258 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 80 '.$filename_tmp.'.shaved.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
259 my $result = `$c_line`;
260
261 if (length $result > 0) {
262 warn 'Contenido Error: ��� ������ "'.$c_line.'" ��������� ������ "'.$result.'" ('.$@.")\n";
263 return undef;
264 }
265 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
266 $filename.'.'.$suffix.'.'.$ext,
267 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$ext),
268 );
269 %{$IMAGE->{'crop'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
270 store($filename.'.'.$suffix.'.'.$ext, $filename_tmp.'.'.$suffix.'.'.$ext);
271 unlink $filename_tmp.'.shaved.'.$ext if -e $filename_tmp.'.shaved.'.$ext;
272 unlink $filename_tmp.'.'.$suffix.'.'.$ext if -e $filename_tmp.'.'.$suffix.'.'.$ext;
273 }
274 if ( @crops ) {
275 if ( !exists $IMAGE->{'mini'}{'filename'} ) {
276 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$crops[0]}}{'filename', 'width', 'height'};
277 }
278 @{$IMAGE->{'crop'}}{'filename', 'width', 'height'} = @{$IMAGE->{'crop'}{$crops[0]}}{'filename', 'width', 'height'};
279 }
280
281
282 ########## SHRINKS
283 foreach my $suffix (@shrinks) {
284
285 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 80 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
286 my $result = `$c_line`;
287
288 if (length $result > 0) {
289 warn 'Contenido Error: ��� ������ "'.$c_line.'" ��������� ������ "'.$result.'" ('.$@.")\n";
290 return undef;
291 }
292 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
293 $filename.'.'.$suffix.'.'.$ext,
294 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$ext),
295 );
296 %{$IMAGE->{'shrink'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
297 store($filename.'.'.$suffix.'.'.$ext, $filename_tmp.'.'.$suffix.'.'.$ext);
298 unlink $filename_tmp.'.'.$suffix.'.'.$ext if -e $filename_tmp.'.'.$suffix.'.'.$ext;
299 }
300 if ( @shrinks && !exists $IMAGE->{'mini'}{'filename'} ) {
301 if ( !exists $IMAGE->{'mini'}{'filename'} ) {
302 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$shrinks[0]}}{'filename', 'width', 'height'};
303 }
304 @{$IMAGE->{'shrink'}}{'filename', 'width', 'height'} = @{$IMAGE->{'shrink'}{$shrinks[0]}}{'filename', 'width', 'height'};
305 }
306
307 unlink $filename_tmp.'.'.$ext if -e $filename_tmp.'.'.$ext;
308 }
309
310 return $IMAGE;
311 }
312
313 sub remove_image {
314 my $IMAGE = shift;
315
316 if ( ref $IMAGE eq 'HASH' && exists $IMAGE->{filename} ) {
317 remove($IMAGE->{'filename'}) || return;
318 }
319 if ( ref $IMAGE && exists $IMAGE->{mini} && ref $IMAGE->{mini} eq 'HASH' ) {
320 foreach my $val ( values %{$IMAGE->{mini}} ) {
321 if ( ref $val && exists $val->{filename} && $val->{filename} ) {
322 remove($val->{'filename'}) || return;
323 }
324 }
325 }
326 1;
327 }
328
329
330 127 ahitrov sub store_binary {
331 my $input = shift;
332 my (%opts) = @_;
333 my $object = delete $opts{object} || return;
334 my $attr = delete $opts{attr} || return;
335
336 my ($prop) = grep { $_->{attr} eq $attr } $object->structure;
337 return unless ref $prop;
338
339 my $filename = '/binary/'.$object->get_file_name() || return;
340 my $filename_tmp = $state->{'tmp_dir'}.'/'.join('_', split('/', $filename));
341
342 my $fh = get_fh($input);
343 return unless ref $fh;
344
345 my $ext;
346 my $size = 1073741824;
347 if ( not ref $input ) {
348 $ext = $input =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
349 if ( scheme($input) eq 'file' ) {
350 $size = (stat $fh)[7];
351 }
352 } elsif ( ref $input eq 'Apache::Upload' ) {
353 $ext = $input->filename() =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
354 $size = (stat $fh)[7];
355 } elsif ( $opts{filename} ) {
356 $ext = $opts{filename} =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
357 }
358 if ( ref $fh eq 'IO::Scalar' ) {
359 $size = length("$fh");
360 }
361 $ext ||= 'bin';
362
363 my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$ext) || return;
364 my $buffer;
365
366 $size = sysread $fh, $buffer, $size;
367 syswrite $fh_tmp, $buffer, $size;
368
369 undef $fh_tmp;
370
371 my $BINARY;
372 if ( store($filename.'.'.$ext, $filename_tmp.'.'.$ext) ) {
373 $BINARY = { filename => $filename.'.'.$ext };
374 unlink $filename_tmp.'.'.$ext if -e $filename_tmp.'.'.$ext;
375 }
376
377 return $BINARY;
378 }
379
380 sub remove_binary {
381 my $BINARY = shift;
382
383 if ( ref $BINARY eq 'HASH' && exists $BINARY->{filename} ) {
384 remove($BINARY->{'filename'}) || return;
385 }
386
387 1;
388 }
389
390
391 3 ahitrov@rambler.ru 1;