Line # Revision Author
1 8 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 142 ahitrov use Image::Info qw(image_info dim);
15 8 ahitrov@rambler.ru
16 our $IgnoreErrors = 1;
17
18 128 ahitrov my %translit = (
19 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'e', 'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'y',
20 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h',
21 'ц' => 'ts', 'ч' => '4', 'ш' => 'sh', 'щ' => 'sch', 'ъ' => 'y', 'ы' => 'i', 'ь' => 'y', 'э' => 'e', 'ю' => 'u', 'я' => 'a', 'А' => 'A',
22 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'E', 'Ж' => 'ZH', 'З' => 'Z', 'И' => 'I', 'Й' => 'Y', 'К' => 'K', 'Л' => 'L',
23 'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'TS', 'Ч' => '4',
24 'Ш' => 'SH', 'Щ' => 'SCH', 'Ъ' => 'Y', 'Ы' => 'I', 'Ь' => 'Y', 'Э' => 'E', 'Ю' => 'U', 'Я' => 'YA',
25 );
26
27
28 8 ahitrov@rambler.ru sub fetch {
29 my $filename = shift || return;
30 my $fh;
31
32 foreach my $dir (@{$state->{"files_dir"}}) {
33 my $path = $dir . '/' . $filename;
34
35 no strict "refs";
36 $fh = &{"Contenido::File::Scheme::".uc(scheme($path))."::fetch"}($path);
37 return $fh if $fh;
38 }
39 }
40
41 sub store {
42 my $filename = shift || return;
43 my $fh = get_fh(shift) || return;
44
45 my $dt = Contenido::DateTime->new()->set_locale('en')->set_time_zone("UTC");
46
47 203 ahitrov my (@successful, @failure);
48 my %result;
49 8 ahitrov@rambler.ru
50 #убираем сдвоенные и более /
51 $filename =~ s#/+#/#g;
52 #убираем начальный /
53 $filename =~ s#^/##;
54
55 foreach my $dir (@{$state->{"files_dir"}}) {
56 203 ahitrov seek $fh, 0, 0;
57 my $path = $dir . '/' . $filename;
58 my $scheme = uc(scheme($path));
59 8 ahitrov@rambler.ru
60 203 ahitrov if ( $scheme eq 'FILE' || $state->{file_web_storage} eq 'separate' || ($state->{file_web_storage} eq 'common' && !exists $result{HTTP}{success}) ) {
61 no strict "refs";
62 my $return = &{"Contenido::File::Scheme::".$scheme."::store"}($path, $fh, $dt);
63 if ( $return ) {
64 push @successful, $path;
65 push @{$result{$scheme}{success}}, $path;
66 } else {
67 push @failure, $path;
68 push @{$result{$scheme}{fail}}, $path;
69 }
70 }
71 8 ahitrov@rambler.ru }
72 203 ahitrov if (!$IgnoreErrors && ( exists $result{FILE}{fail} || ($state->{file_web_storage} eq 'separate' && exists $result{HTTP}{fail})
73 || ($state->{file_web_storage} eq 'common' && exists $result{HTTP}{fail} && !exists($result{HTTP}{success})) ) ) {
74 foreach my $path (@successful) {
75 no strict "refs";
76 &{"Contenido::File::Scheme::".uc(scheme($path))."::remove"}($path);
77 }
78 return;
79 }
80 8 ahitrov@rambler.ru
81 1;
82 }
83
84 sub size {
85 my $filename = shift;
86 my %args = @_;
87
88 my $size;
89
90 #убираем сдвоенные и более /
91 $filename =~ s#/+#/#g;
92 #убираем начальный /
93 $filename =~ s#^/##;
94
95 foreach my $dir (@{$state->{"files_dir"}}) {
96 no strict "refs";
97 my $response = &{"Contenido::File::Scheme::".uc(scheme($dir))."::size"}($dir."/".$filename, %args);
98 return unless defined $response;
99 return if defined $size and $size != $response;
100
101 $size = $response;
102 #TODO
103 last;
104 }
105 return $size;
106 }
107
108 sub remove {
109 my $filename = shift || return;
110
111 #убираем сдвоенные и более /
112 $filename =~ s#/+#/#g;
113 #убираем начальный /
114 $filename =~ s#^/##;
115
116 217 ahitrov my $success = 0;
117 8 ahitrov@rambler.ru foreach my $dir (@{$state->{"files_dir"}}) {
118 217 ahitrov no strict "refs";
119 my $scheme = uc(scheme($dir));
120 my $res;
121 if ( !$success || $scheme eq 'FILE' ) {
122 $res = &{"Contenido::File::Scheme::".$scheme."::remove"}($dir."/".$filename);
123 }
124 if ( $res && $scheme eq 'HTTP' && $state->{file_web_storage} eq 'common' ) {
125 $success = 1;
126 }
127 8 ahitrov@rambler.ru }
128
129 1;
130 }
131
132 sub get_fh {
133 my $input = shift;
134 my $fh;
135
136 if (not ref $input) {
137 no strict "refs";
138 $fh = &{"Contenido::File::Scheme::".uc(scheme($input))."::get_fh"}($input);
139 113 ahitrov } elsif ( ref $input eq 'Apache::Upload' ) {
140 $fh = $input->fh;
141 } elsif ((ref $input eq "GLOB") or (ref $input eq 'IO::File')) {
142 8 ahitrov@rambler.ru $fh = $input;
143 } elsif (ref $input eq "SCALAR") {
144 $fh = IO::Scalar->new($input);
145 } else {
146 $log->warning("Path, scalar ref or fh needed");
147 return;
148 }
149
150 return $fh
151 }
152
153 sub scheme {
154 my $uri = shift;
155 my $scheme;
156
157 $scheme = URI->new($uri)->scheme() || "file";
158 172 ahitrov $scheme = 'http' if lc($scheme) eq 'https';
159 8 ahitrov@rambler.ru
160 return $scheme;
161 }
162
163 # sub files_dir {
164 # return unless $_[0];
165 # my $dir;
166
167 # for (scheme($_[0])) {
168 # /http/ && do {
169 # $dir = URI->new($_[0])->canonical();
170 # $dir =~ s|/*$|/|;
171 # last;
172 # };
173 # /file/ && do {
174 # $dir = URI->new($_[0])->path();
175 # $dir =~ s|/*$|/|;
176 # last;
177 # };
178 # }
179
180 # return $dir;
181 # }
182
183 113 ahitrov sub store_image {
184 my $input = shift;
185 my (%opts) = @_;
186 my $object = delete $opts{object} || return;
187 169 ahitrov my $attr = delete $opts{attr};
188 113 ahitrov
189 169 ahitrov my ($prop) = exists $opts{prop} && ref $opts{prop} ? ($opts{prop}) : $attr ? grep { $_->{attr} eq $attr } $object->structure : (undef);
190 113 ahitrov return unless ref $prop;
191 my @preview = exists $prop->{'preview'} && ref $prop->{'preview'} eq 'ARRAY' ? @{$prop->{'preview'}} : exists $prop->{'preview'} && $prop->{'preview'} ? ($prop->{'preview'}) : ();
192 my @crops = exists $prop->{'crop'} && ref $prop->{'crop'} eq 'ARRAY' ? @{$prop->{'crop'}} : exists $prop->{'crop'} && $prop->{'crop'} ? ($prop->{'crop'}) : ();
193 my @shrinks = exists $prop->{'shrink'} && ref $prop->{'shrink'} eq 'ARRAY' ? @{$prop->{'shrink'}} : exists $prop->{'shrink'} && $prop->{'shrink'} ? ($prop->{'shrink'}) : ();
194
195 my $filename = '/images/'.$object->get_file_name() || return;
196 my $filename_tmp = $state->{'tmp_dir'}.'/'.join('_', split('/', $filename));
197
198 my $fh = get_fh($input);
199 return unless ref $fh;
200
201 my $ext;
202 117 ahitrov my $size = 1073741824;
203 if ( not ref $input ) {
204 113 ahitrov $ext = $input =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
205 117 ahitrov if ( scheme($input) eq 'file' ) {
206 $size = (stat $fh)[7];
207 }
208 113 ahitrov } elsif ( ref $input eq 'Apache::Upload' ) {
209 $ext = $input->filename() =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
210 117 ahitrov $size = (stat $fh)[7];
211 } elsif ( $opts{filename} ) {
212 $ext = $opts{filename} =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
213 113 ahitrov }
214 125 ahitrov if ( ref $fh eq 'IO::Scalar' ) {
215 $size = length("$fh");
216 }
217 113 ahitrov $ext ||= 'bin';
218
219 my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$ext) || return;
220 my $buffer;
221
222 117 ahitrov $size = sysread $fh, $buffer, $size;
223 113 ahitrov syswrite $fh_tmp, $buffer, $size;
224
225 undef $fh_tmp;
226
227 142 ahitrov my $image_info = image_info($filename_tmp.'.'.$ext);
228 182 ahitrov if ( !(ref $image_info && $image_info->{width} && $image_info->{height}) || (ref $image_info && $image_info->{error}) ) {
229 147 ahitrov unlink $filename_tmp.'.'.$ext;
230 return undef;
231 142 ahitrov }
232 182 ahitrov if ( $image_info->{file_ext} ne $ext ) {
233 rename $filename_tmp.'.'.$ext, $filename_tmp.'.'.$image_info->{file_ext};
234 $ext = $image_info->{file_ext};
235 }
236 220 ahitrov if ( $image_info->{color_type} eq 'CMYK' ) {
237 rename $filename_tmp.'.'.$ext, $filename_tmp.'.cmyk.'.$ext;
238 my $c_line = $state->{'convert_binary'}.' -colorspace RGB -quality 100 '.$filename_tmp.'.cmyk.'.$ext.' '.$filename_tmp.'.'.$ext;
239 `$c_line`;
240 unlink $filename_tmp.'.cmyk.'.$ext;
241 }
242 147 ahitrov my $transformed;
243 if ( exists $prop->{transform} && ref $prop->{transform} eq 'ARRAY' && scalar @{$prop->{transform}} == 2 && $prop->{transform}[0] =~ /(crop|resize|shrink)/ ) {
244 my $c_line;
245 if ( $prop->{transform}[0] eq 'resize' ) {
246 348 ahitrov $c_line = $state->{'convert_binary'}.' -adaptive-resize \''.$prop->{transform}[1].'>\' -quality 100 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.transformed.'.$ext;
247 147 ahitrov } elsif ( $prop->{transform}[0] eq 'crop' ) {
248 my $shave_string;
249 my ($nwidth, $nheight) = $prop->{transform}[1] =~ /(\d+)x(\d+)/i ? ($1, $2) : (0, 0);
250 if ( ($image_info->{width} / $image_info->{height}) > ($nwidth / $nheight) ) {
251 my $shave_pixels = (($image_info->{width} / $image_info->{height}) - ($nwidth / $nheight)) * $image_info->{height};
252 $shave_string = ' -shave '.int($shave_pixels / 2).'x0';
253 } elsif ( ($image_info->{height} / $image_info->{width}) > ($nheight / $nwidth) ) {
254 my $shave_pixels = (($image_info->{height} / $image_info->{width}) - ($nheight / $nwidth)) * $image_info->{width};
255 $shave_string = ' -shave 0x'.int($shave_pixels / 2);
256 }
257 if ( $shave_string ) {
258 my $c_line = $state->{"convert_binary"}." $shave_string $filename_tmp.$ext $filename_tmp.shaved.$ext";
259 my $result = `$c_line`;
260 if (length $result > 0) {
261 print "Contenido Error: При вызове '$c_line' произошла ошибка '$result' ($@)\n";
262 return undef;
263 }
264 } else {
265 my $c_line = "cp $filename_tmp.$ext $filename_tmp.shaved.$ext";
266 my $result = `$c_line`;
267 if (length $result > 0) {
268 print "Contenido Error: При вызове '$c_line' произошла ошибка '$result' ($@)\n";
269 return undef;
270 }
271 }
272 348 ahitrov $c_line = $state->{'convert_binary'}.' -adaptive_resize \''.$prop->{transform}[1].'!\' -quality 100 '.$filename_tmp.'.shaved.'.$ext.' '.$filename_tmp.'.transformed.'.$ext;
273 147 ahitrov } elsif ( $prop->{transform}[0] eq 'shrink' ) {
274 348 ahitrov $c_line = $state->{'convert_binary'}.' -adaptive_resize \''.$prop->{transform}[1].'!\' -quality 100 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.transformed.'.$ext;
275 147 ahitrov }
276 my $result = `$c_line`;
277 $transformed = 1;
278 unlink $filename_tmp.'.shaved.'.$ext if -e $filename_tmp.'.shaved.'.$ext;
279 }
280 142 ahitrov
281 113 ahitrov my $IMAGE;
282 147 ahitrov my $stored = $transformed ? store($filename.'.'.$ext, $filename_tmp.'.transformed.'.$ext) : store($filename.'.'.$ext, $filename_tmp.'.'.$ext);
283 if ( $stored ) {
284 113 ahitrov $IMAGE = {};
285 149 ahitrov # hashref slice assigning - жжесть
286 147 ahitrov if ( $transformed && -e $filename_tmp.'.transformed.'.$ext ) {
287 160 ahitrov my ($tw, $th) = Image::Size::imgsize($filename_tmp.'.transformed.'.$ext);
288 my ($w, $h) = Image::Size::imgsize($filename_tmp.'.'.$ext);
289 @{$IMAGE}{'filename', 't_width', 't_height', 'width', 'height'} = (
290 $filename.'.'.$ext, $tw, $th, $w, $h
291 147 ahitrov );
292 unlink $filename_tmp.'.transformed.'.$ext;
293 } else {
294 @{$IMAGE}{'filename', 'width', 'height'} = (
295 $filename.'.'.$ext,
296 Image::Size::imgsize($filename_tmp.'.'.$ext),
297 );
298 }
299 113 ahitrov
300 foreach my $suffix (@preview) {
301 348 ahitrov my $c_line = $state->{'convert_binary'}.' -resize \''.$suffix.'>\' -quality 90 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
302 113 ahitrov my $result = `$c_line`;
303
304 if (length $result > 0) {
305 warn 'Contenido Error: При вызове "'.$c_line.'" произошла ошибка "'.$result.'" ('.$@.")\n";
306 return undef;
307 }
308 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
309 $filename.'.'.$suffix.'.'.$ext,
310 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$ext),
311 );
312 %{$IMAGE->{'resize'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
313 store($filename.'.'.$suffix.'.'.$ext, $filename_tmp.'.'.$suffix.'.'.$ext);
314 unlink $filename_tmp.'.'.$suffix.'.'.$ext if -e $filename_tmp.'.'.$suffix.'.'.$ext;
315 }
316 if ( @preview ) {
317 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'};
318 @{$IMAGE->{'resize'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'};
319 }
320
321 ########## CROPS
322 foreach my $suffix (@crops) {
323
324 my $shave_string;
325 my ($nwidth, $nheight) = $suffix =~ /(\d+)x(\d+)/i ? ($1, $2) : (0, 0);
326 if ( ($IMAGE->{width} / $IMAGE->{height}) > ($nwidth / $nheight) ) {
327 my $shave_pixels = (($IMAGE->{width} / $IMAGE->{height}) - ($nwidth / $nheight)) * $IMAGE->{height};
328 $shave_string = ' -shave '.int($shave_pixels / 2).'x0';
329 } elsif ( ($IMAGE->{height} / $IMAGE->{width}) > ($nheight / $nwidth) ) {
330 my $shave_pixels = (($IMAGE->{height} / $IMAGE->{width}) - ($nheight / $nwidth)) * $IMAGE->{width};
331 $shave_string = ' -shave 0x'.int($shave_pixels / 2);
332 }
333 if ( $shave_string ) {
334 my $c_line = $state->{"convert_binary"}." $shave_string $filename_tmp.$ext $filename_tmp.shaved.$ext";
335 my $result = `$c_line`;
336 if (length $result > 0) {
337 print "Contenido Error: При вызове '$c_line' произошла ошибка '$result' ($@)\n";
338 }
339 } else {
340 my $c_line = "cp $filename_tmp.$ext $filename_tmp.shaved.$ext";
341 my $result = `$c_line`;
342 if (length $result > 0) {
343 print "Contenido Error: При вызове '$c_line' произошла ошибка '$result' ($@)\n";
344 }
345 }
346
347 348 ahitrov my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 90 '.$filename_tmp.'.shaved.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
348 113 ahitrov my $result = `$c_line`;
349
350 if (length $result > 0) {
351 warn 'Contenido Error: При вызове "'.$c_line.'" произошла ошибка "'.$result.'" ('.$@.")\n";
352 return undef;
353 }
354 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
355 $filename.'.'.$suffix.'.'.$ext,
356 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$ext),
357 );
358 %{$IMAGE->{'crop'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
359 store($filename.'.'.$suffix.'.'.$ext, $filename_tmp.'.'.$suffix.'.'.$ext);
360 unlink $filename_tmp.'.shaved.'.$ext if -e $filename_tmp.'.shaved.'.$ext;
361 unlink $filename_tmp.'.'.$suffix.'.'.$ext if -e $filename_tmp.'.'.$suffix.'.'.$ext;
362 }
363 if ( @crops ) {
364 if ( !exists $IMAGE->{'mini'}{'filename'} ) {
365 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$crops[0]}}{'filename', 'width', 'height'};
366 }
367 @{$IMAGE->{'crop'}}{'filename', 'width', 'height'} = @{$IMAGE->{'crop'}{$crops[0]}}{'filename', 'width', 'height'};
368 }
369
370
371 ########## SHRINKS
372 foreach my $suffix (@shrinks) {
373
374 348 ahitrov my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 90 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
375 113 ahitrov my $result = `$c_line`;
376
377 if (length $result > 0) {
378 warn 'Contenido Error: При вызове "'.$c_line.'" произошла ошибка "'.$result.'" ('.$@.")\n";
379 return undef;
380 }
381 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
382 $filename.'.'.$suffix.'.'.$ext,
383 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$ext),
384 );
385 %{$IMAGE->{'shrink'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
386 store($filename.'.'.$suffix.'.'.$ext, $filename_tmp.'.'.$suffix.'.'.$ext);
387 unlink $filename_tmp.'.'.$suffix.'.'.$ext if -e $filename_tmp.'.'.$suffix.'.'.$ext;
388 }
389 if ( @shrinks && !exists $IMAGE->{'mini'}{'filename'} ) {
390 if ( !exists $IMAGE->{'mini'}{'filename'} ) {
391 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$shrinks[0]}}{'filename', 'width', 'height'};
392 }
393 @{$IMAGE->{'shrink'}}{'filename', 'width', 'height'} = @{$IMAGE->{'shrink'}{$shrinks[0]}}{'filename', 'width', 'height'};
394 }
395
396 unlink $filename_tmp.'.'.$ext if -e $filename_tmp.'.'.$ext;
397 160 ahitrov $IMAGE->{width} = delete $IMAGE->{t_width} if exists $IMAGE->{t_width};
398 $IMAGE->{height} = delete $IMAGE->{t_height} if exists $IMAGE->{t_height};
399 113 ahitrov }
400
401 return $IMAGE;
402 }
403
404 sub remove_image {
405 my $IMAGE = shift;
406
407 if ( ref $IMAGE eq 'HASH' && exists $IMAGE->{filename} ) {
408 remove($IMAGE->{'filename'}) || return;
409 }
410 if ( ref $IMAGE && exists $IMAGE->{mini} && ref $IMAGE->{mini} eq 'HASH' ) {
411 foreach my $val ( values %{$IMAGE->{mini}} ) {
412 if ( ref $val && exists $val->{filename} && $val->{filename} ) {
413 remove($val->{'filename'}) || return;
414 }
415 }
416 }
417 1;
418 }
419
420
421 125 ahitrov sub store_binary {
422 my $input = shift;
423 my (%opts) = @_;
424 my $object = delete $opts{object} || return;
425 my $attr = delete $opts{attr} || return;
426
427 my ($prop) = grep { $_->{attr} eq $attr } $object->structure;
428 return unless ref $prop;
429
430 my $filename = '/binary/'.$object->get_file_name() || return;
431 347 ahitrov my $orig_name = '';
432 if ( ref $input eq 'Apache::Upload' ) {
433 $orig_name = $input->filename();
434 } elsif ( !ref $input ) {
435 $orig_name = $input;
436 }
437 if ( $orig_name ) {
438 if ( $orig_name =~ /\\([^\\]+)$/ ) {
439 $orig_name = $1;
440 } elsif ( $orig_name =~ /\/([^\/]+)$/ ) {
441 $orig_name = $1;
442 }
443 }
444 128 ahitrov if ( $prop->{softrename} ) {
445 my $oid = $object->id || int(rand(10000));
446 if ( $orig_name ) {
447 347 ahitrov my $set_name = $orig_name;
448 $set_name =~ s/[\ \t]/_/g;
449 $set_name = $oid.'_'.$set_name;
450 128 ahitrov $filename =~ s/\/([^\/]+)$//;
451 my $fname = $1;
452 347 ahitrov unless ( $set_name =~ /^[a-zA-Z_\d\.\-\,]+$/ ) {
453 $set_name = translit( $set_name );
454 128 ahitrov }
455 347 ahitrov warn "\n\n\n\n\nNew Name: [$set_name]\n\n\n\n\n" if $DEBUG;
456 unless ( $set_name =~ /^[a-zA-Z_\d\.\-\,]+$/ ) {
457 $set_name = $fname;
458 128 ahitrov }
459 347 ahitrov $filename .= '/'.$set_name;
460 128 ahitrov $filename =~ s/\.([^\.]+)$//;
461 }
462 }
463
464 125 ahitrov my $filename_tmp = $state->{'tmp_dir'}.'/'.join('_', split('/', $filename));
465
466 my $fh = get_fh($input);
467 return unless ref $fh;
468
469 my $ext;
470 my $size = 1073741824;
471 if ( not ref $input ) {
472 128 ahitrov $ext = $input =~ /\.([^\.]+)$/ ? lc($1) : 'bin';
473 125 ahitrov if ( scheme($input) eq 'file' ) {
474 $size = (stat $fh)[7];
475 }
476 } elsif ( ref $input eq 'Apache::Upload' ) {
477 128 ahitrov $ext = $input->filename() =~ /\.([^\.]+)$/ ? lc($1) : 'bin';
478 125 ahitrov $size = (stat $fh)[7];
479 } elsif ( $opts{filename} ) {
480 128 ahitrov $ext = $opts{filename} =~ /\.([^\.]+)$/ ? lc($1) : 'bin';
481 125 ahitrov }
482 if ( ref $fh eq 'IO::Scalar' ) {
483 $size = length("$fh");
484 }
485 $ext ||= 'bin';
486
487 my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$ext) || return;
488 my $buffer;
489
490 $size = sysread $fh, $buffer, $size;
491 syswrite $fh_tmp, $buffer, $size;
492
493 undef $fh_tmp;
494
495 my $BINARY;
496 if ( store($filename.'.'.$ext, $filename_tmp.'.'.$ext) ) {
497 347 ahitrov @{$BINARY}{"filename", "ext", "size", "sourcename"} = (
498 $filename.".".$ext, $ext, $size, $orig_name
499 128 ahitrov );
500
501 125 ahitrov unlink $filename_tmp.'.'.$ext if -e $filename_tmp.'.'.$ext;
502 128 ahitrov
503 if ( $ext =~ /(rar|7z|zip|arc|lha|arj|cab)/ ) {
504 $BINARY->{type} = 'archive';
505 347 ahitrov } elsif ( $ext =~ /(doc|docx|rtf)/ ) {
506 128 ahitrov $BINARY->{type} = 'doc';
507 347 ahitrov } elsif ( $ext =~ /(xls|xlsx)/ ) {
508 128 ahitrov $BINARY->{type} = 'xls';
509 } elsif ( $ext =~ /(mdb|ppt)/ ) {
510 $BINARY->{type} = 'msoffice';
511 347 ahitrov } elsif ( $ext =~ /(pdf|fb2|djvu)/ ) {
512 128 ahitrov $BINARY->{type} = 'ebook';
513 347 ahitrov } elsif ( $ext =~ /(psd|cdr)/ ) {
514 $BINARY->{type} = 'graphics';
515 } elsif ( $ext eq 'ico' ) {
516 $BINARY->{type} = 'icon';
517 128 ahitrov } elsif ( $ext =~ /(exe|msi|cab)/ ) {
518 $BINARY->{type} = 'executable';
519 } else {
520 $BINARY->{type} = 'unknown';
521 }
522
523 125 ahitrov }
524
525 return $BINARY;
526 }
527
528 sub remove_binary {
529 my $BINARY = shift;
530
531 if ( ref $BINARY eq 'HASH' && exists $BINARY->{filename} ) {
532 remove($BINARY->{'filename'}) || return;
533 }
534
535 1;
536 }
537
538
539 128 ahitrov sub translit {
540 my $str = shift;
541 my @str = split (//, $str);
542 my $res = '';
543 while ( scalar @str ) {
544 my $alpha = shift @str;
545 if ( exists $translit{$alpha} ) {
546 $res .= $translit{$alpha};
547 } else {
548 $res .= $alpha;
549 }
550 }
551 return $res;
552 }
553
554
555 8 ahitrov@rambler.ru 1;