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 143 ahitrov use Image::Info qw(image_info dim);
15 3 ahitrov@rambler.ru
16 our $IgnoreErrors = 1;
17
18 132 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 3 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 204 ahitrov my (@successful, @failure);
48 my %result;
49 3 ahitrov@rambler.ru
50 #������� ��������� � ����� /
51 $filename =~ s#/+#/#g;
52 #������� ��������� /
53 $filename =~ s#^/##;
54
55 foreach my $dir (@{$state->{"files_dir"}}) {
56 204 ahitrov seek $fh, 0, 0;
57 my $path = $dir . '/' . $filename;
58 my $scheme = uc(scheme($path));
59 3 ahitrov@rambler.ru
60 204 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 3 ahitrov@rambler.ru }
72 204 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 3 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 218 ahitrov my $success = 0;
117 3 ahitrov@rambler.ru foreach my $dir (@{$state->{"files_dir"}}) {
118 218 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 3 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 114 ahitrov } elsif ( ref $input eq 'Apache::Upload' ) {
140 $fh = $input->fh;
141 } elsif ((ref $input eq "GLOB") or (ref $input eq 'IO::File')) {
142 3 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 170 ahitrov $scheme = 'http' if lc($scheme) eq 'https';
159 3 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 114 ahitrov sub store_image {
184 my $input = shift;
185 my (%opts) = @_;
186 my $object = delete $opts{object} || return;
187 171 ahitrov my $attr = delete $opts{attr};
188 114 ahitrov
189 171 ahitrov my ($prop) = exists $opts{prop} && ref $opts{prop} ? ($opts{prop}) : $attr ? grep { $_->{attr} eq $attr } $object->structure : (undef);
190 114 ahitrov return unless ref $prop;
191 364 ahitrov
192 warn Dumper \%opts if $DEBUG;
193 114 ahitrov my @preview = exists $prop->{'preview'} && ref $prop->{'preview'} eq 'ARRAY' ? @{$prop->{'preview'}} : exists $prop->{'preview'} && $prop->{'preview'} ? ($prop->{'preview'}) : ();
194 my @crops = exists $prop->{'crop'} && ref $prop->{'crop'} eq 'ARRAY' ? @{$prop->{'crop'}} : exists $prop->{'crop'} && $prop->{'crop'} ? ($prop->{'crop'}) : ();
195 my @shrinks = exists $prop->{'shrink'} && ref $prop->{'shrink'} eq 'ARRAY' ? @{$prop->{'shrink'}} : exists $prop->{'shrink'} && $prop->{'shrink'} ? ($prop->{'shrink'}) : ();
196
197 my $filename = '/images/'.$object->get_file_name() || return;
198 my $filename_tmp = $state->{'tmp_dir'}.'/'.join('_', split('/', $filename));
199
200 my $fh = get_fh($input);
201 return unless ref $fh;
202
203 my $ext;
204 121 ahitrov my $size = 1073741824;
205 if ( not ref $input ) {
206 114 ahitrov $ext = $input =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
207 121 ahitrov if ( scheme($input) eq 'file' ) {
208 $size = (stat $fh)[7];
209 }
210 114 ahitrov } elsif ( ref $input eq 'Apache::Upload' ) {
211 $ext = $input->filename() =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
212 121 ahitrov $size = (stat $fh)[7];
213 } elsif ( $opts{filename} ) {
214 $ext = $opts{filename} =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
215 114 ahitrov }
216 127 ahitrov if ( ref $fh eq 'IO::Scalar' ) {
217 $size = length("$fh");
218 }
219 114 ahitrov $ext ||= 'bin';
220
221 my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$ext) || return;
222 my $buffer;
223
224 121 ahitrov $size = sysread $fh, $buffer, $size;
225 114 ahitrov syswrite $fh_tmp, $buffer, $size;
226
227 undef $fh_tmp;
228
229 143 ahitrov my $image_info = image_info($filename_tmp.'.'.$ext);
230 183 ahitrov if ( !(ref $image_info && $image_info->{width} && $image_info->{height}) || (ref $image_info && $image_info->{error}) ) {
231 148 ahitrov unlink $filename_tmp.'.'.$ext;
232 return undef;
233 143 ahitrov }
234 183 ahitrov if ( $image_info->{file_ext} ne $ext ) {
235 rename $filename_tmp.'.'.$ext, $filename_tmp.'.'.$image_info->{file_ext};
236 $ext = $image_info->{file_ext};
237 }
238 221 ahitrov if ( $image_info->{color_type} eq 'CMYK' ) {
239 rename $filename_tmp.'.'.$ext, $filename_tmp.'.cmyk.'.$ext;
240 my $c_line = $state->{'convert_binary'}.' -colorspace RGB -quality 100 '.$filename_tmp.'.cmyk.'.$ext.' '.$filename_tmp.'.'.$ext;
241 `$c_line`;
242 unlink $filename_tmp.'.cmyk.'.$ext;
243 }
244 148 ahitrov my $transformed;
245 if ( exists $prop->{transform} && ref $prop->{transform} eq 'ARRAY' && scalar @{$prop->{transform}} == 2 && $prop->{transform}[0] =~ /(crop|resize|shrink)/ ) {
246 my $c_line;
247 if ( $prop->{transform}[0] eq 'resize' ) {
248 364 ahitrov $c_line = $state->{'convert_binary'}.' -adaptive-resize \''.$prop->{transform}[1].'>\' -quality 100 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.transformed.'.$ext;
249 148 ahitrov } elsif ( $prop->{transform}[0] eq 'crop' ) {
250 my $shave_string;
251 my ($nwidth, $nheight) = $prop->{transform}[1] =~ /(\d+)x(\d+)/i ? ($1, $2) : (0, 0);
252 if ( ($image_info->{width} / $image_info->{height}) > ($nwidth / $nheight) ) {
253 my $shave_pixels = (($image_info->{width} / $image_info->{height}) - ($nwidth / $nheight)) * $image_info->{height};
254 $shave_string = ' -shave '.int($shave_pixels / 2).'x0';
255 } elsif ( ($image_info->{height} / $image_info->{width}) > ($nheight / $nwidth) ) {
256 my $shave_pixels = (($image_info->{height} / $image_info->{width}) - ($nheight / $nwidth)) * $image_info->{width};
257 $shave_string = ' -shave 0x'.int($shave_pixels / 2);
258 }
259 if ( $shave_string ) {
260 my $c_line = $state->{"convert_binary"}." $shave_string $filename_tmp.$ext $filename_tmp.shaved.$ext";
261 my $result = `$c_line`;
262 if (length $result > 0) {
263 print "Contenido Error: ��� ������ '$c_line' ��������� ������ '$result' ($@)\n";
264 return undef;
265 }
266 } else {
267 my $c_line = "cp $filename_tmp.$ext $filename_tmp.shaved.$ext";
268 my $result = `$c_line`;
269 if (length $result > 0) {
270 print "Contenido Error: ��� ������ '$c_line' ��������� ������ '$result' ($@)\n";
271 return undef;
272 }
273 }
274 364 ahitrov $c_line = $state->{'convert_binary'}.' -adaptive-resize \''.$prop->{transform}[1].'!\' -quality 100 '.$filename_tmp.'.shaved.'.$ext.' '.$filename_tmp.'.transformed.'.$ext;
275 148 ahitrov } elsif ( $prop->{transform}[0] eq 'shrink' ) {
276 364 ahitrov $c_line = $state->{'convert_binary'}.' -adaptive-resize \''.$prop->{transform}[1].'!\' -quality 100 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.transformed.'.$ext;
277 148 ahitrov }
278 my $result = `$c_line`;
279 $transformed = 1;
280 unlink $filename_tmp.'.shaved.'.$ext if -e $filename_tmp.'.shaved.'.$ext;
281 }
282 143 ahitrov
283 364 ahitrov if ( exists $opts{watermark} && $opts{watermark} ) {
284 my $gravity = delete $opts{gravity} || 'Center';
285 my $source = $transformed ? $filename_tmp.'.transformed.'.$ext : $filename_tmp.'.'.$ext;
286 my $target = $filename_tmp.'.transformed.'.$ext;
287 my $offset = delete $opts{offset} || '+0+0';
288 my $c_line = $state->{'composite_binary'}." -geometry $offset -gravity $gravity -quality 99 $opts{watermark} $source $target";
289 warn "Watermark: $c_line\n" if $DEBUG;
290 my $result = `$c_line`;
291 $transformed = 1;
292 }
293
294 114 ahitrov my $IMAGE;
295 148 ahitrov my $stored = $transformed ? store($filename.'.'.$ext, $filename_tmp.'.transformed.'.$ext) : store($filename.'.'.$ext, $filename_tmp.'.'.$ext);
296 if ( $stored ) {
297 114 ahitrov $IMAGE = {};
298 150 ahitrov # hashref slice assigning - ������
299 148 ahitrov if ( $transformed && -e $filename_tmp.'.transformed.'.$ext ) {
300 161 ahitrov my ($tw, $th) = Image::Size::imgsize($filename_tmp.'.transformed.'.$ext);
301 my ($w, $h) = Image::Size::imgsize($filename_tmp.'.'.$ext);
302 @{$IMAGE}{'filename', 't_width', 't_height', 'width', 'height'} = (
303 $filename.'.'.$ext, $tw, $th, $w, $h
304 148 ahitrov );
305 unlink $filename_tmp.'.transformed.'.$ext;
306 } else {
307 @{$IMAGE}{'filename', 'width', 'height'} = (
308 $filename.'.'.$ext,
309 Image::Size::imgsize($filename_tmp.'.'.$ext),
310 );
311 }
312 114 ahitrov
313 foreach my $suffix (@preview) {
314 364 ahitrov my $c_line = $state->{'convert_binary'}.' -resize \''.$suffix.'>\' -quality 90 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
315 114 ahitrov my $result = `$c_line`;
316
317 if (length $result > 0) {
318 warn 'Contenido Error: ��� ������ "'.$c_line.'" ��������� ������ "'.$result.'" ('.$@.")\n";
319 return undef;
320 }
321 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
322 $filename.'.'.$suffix.'.'.$ext,
323 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$ext),
324 );
325 %{$IMAGE->{'resize'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
326 store($filename.'.'.$suffix.'.'.$ext, $filename_tmp.'.'.$suffix.'.'.$ext);
327 unlink $filename_tmp.'.'.$suffix.'.'.$ext if -e $filename_tmp.'.'.$suffix.'.'.$ext;
328 }
329 if ( @preview ) {
330 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'};
331 @{$IMAGE->{'resize'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'};
332 }
333
334 ########## CROPS
335 foreach my $suffix (@crops) {
336
337 my $shave_string;
338 my ($nwidth, $nheight) = $suffix =~ /(\d+)x(\d+)/i ? ($1, $2) : (0, 0);
339 if ( ($IMAGE->{width} / $IMAGE->{height}) > ($nwidth / $nheight) ) {
340 my $shave_pixels = (($IMAGE->{width} / $IMAGE->{height}) - ($nwidth / $nheight)) * $IMAGE->{height};
341 $shave_string = ' -shave '.int($shave_pixels / 2).'x0';
342 } elsif ( ($IMAGE->{height} / $IMAGE->{width}) > ($nheight / $nwidth) ) {
343 my $shave_pixels = (($IMAGE->{height} / $IMAGE->{width}) - ($nheight / $nwidth)) * $IMAGE->{width};
344 $shave_string = ' -shave 0x'.int($shave_pixels / 2);
345 }
346 if ( $shave_string ) {
347 my $c_line = $state->{"convert_binary"}." $shave_string $filename_tmp.$ext $filename_tmp.shaved.$ext";
348 my $result = `$c_line`;
349 if (length $result > 0) {
350 print "Contenido Error: ��� ������ '$c_line' ��������� ������ '$result' ($@)\n";
351 }
352 } else {
353 my $c_line = "cp $filename_tmp.$ext $filename_tmp.shaved.$ext";
354 my $result = `$c_line`;
355 if (length $result > 0) {
356 print "Contenido Error: ��� ������ '$c_line' ��������� ������ '$result' ($@)\n";
357 }
358 }
359
360 364 ahitrov my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 90 '.$filename_tmp.'.shaved.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
361 114 ahitrov my $result = `$c_line`;
362
363 if (length $result > 0) {
364 warn 'Contenido Error: ��� ������ "'.$c_line.'" ��������� ������ "'.$result.'" ('.$@.")\n";
365 return undef;
366 }
367 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
368 $filename.'.'.$suffix.'.'.$ext,
369 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$ext),
370 );
371 %{$IMAGE->{'crop'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
372 store($filename.'.'.$suffix.'.'.$ext, $filename_tmp.'.'.$suffix.'.'.$ext);
373 unlink $filename_tmp.'.shaved.'.$ext if -e $filename_tmp.'.shaved.'.$ext;
374 unlink $filename_tmp.'.'.$suffix.'.'.$ext if -e $filename_tmp.'.'.$suffix.'.'.$ext;
375 }
376 if ( @crops ) {
377 if ( !exists $IMAGE->{'mini'}{'filename'} ) {
378 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$crops[0]}}{'filename', 'width', 'height'};
379 }
380 @{$IMAGE->{'crop'}}{'filename', 'width', 'height'} = @{$IMAGE->{'crop'}{$crops[0]}}{'filename', 'width', 'height'};
381 }
382
383
384 ########## SHRINKS
385 foreach my $suffix (@shrinks) {
386
387 364 ahitrov my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 90 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
388 114 ahitrov my $result = `$c_line`;
389
390 if (length $result > 0) {
391 warn 'Contenido Error: ��� ������ "'.$c_line.'" ��������� ������ "'.$result.'" ('.$@.")\n";
392 return undef;
393 }
394 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
395 $filename.'.'.$suffix.'.'.$ext,
396 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$ext),
397 );
398 %{$IMAGE->{'shrink'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
399 store($filename.'.'.$suffix.'.'.$ext, $filename_tmp.'.'.$suffix.'.'.$ext);
400 unlink $filename_tmp.'.'.$suffix.'.'.$ext if -e $filename_tmp.'.'.$suffix.'.'.$ext;
401 }
402 if ( @shrinks && !exists $IMAGE->{'mini'}{'filename'} ) {
403 if ( !exists $IMAGE->{'mini'}{'filename'} ) {
404 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$shrinks[0]}}{'filename', 'width', 'height'};
405 }
406 @{$IMAGE->{'shrink'}}{'filename', 'width', 'height'} = @{$IMAGE->{'shrink'}{$shrinks[0]}}{'filename', 'width', 'height'};
407 }
408
409 unlink $filename_tmp.'.'.$ext if -e $filename_tmp.'.'.$ext;
410 161 ahitrov $IMAGE->{width} = delete $IMAGE->{t_width} if exists $IMAGE->{t_width};
411 $IMAGE->{height} = delete $IMAGE->{t_height} if exists $IMAGE->{t_height};
412 114 ahitrov }
413
414 return $IMAGE;
415 }
416
417 sub remove_image {
418 my $IMAGE = shift;
419
420 if ( ref $IMAGE eq 'HASH' && exists $IMAGE->{filename} ) {
421 remove($IMAGE->{'filename'}) || return;
422 }
423 if ( ref $IMAGE && exists $IMAGE->{mini} && ref $IMAGE->{mini} eq 'HASH' ) {
424 foreach my $val ( values %{$IMAGE->{mini}} ) {
425 if ( ref $val && exists $val->{filename} && $val->{filename} ) {
426 remove($val->{'filename'}) || return;
427 }
428 }
429 }
430 1;
431 }
432
433
434 127 ahitrov sub store_binary {
435 my $input = shift;
436 my (%opts) = @_;
437 my $object = delete $opts{object} || return;
438 my $attr = delete $opts{attr} || return;
439
440 my ($prop) = grep { $_->{attr} eq $attr } $object->structure;
441 return unless ref $prop;
442
443 my $filename = '/binary/'.$object->get_file_name() || return;
444 132 ahitrov if ( $prop->{softrename} ) {
445 my $oid = $object->id || int(rand(10000));
446 my $orig_name = '';
447 if ( ref $input eq 'Apache::Upload' ) {
448 $orig_name = $input->filename();
449 } elsif ( !ref $input ) {
450 $orig_name = $input;
451 }
452 if ( $orig_name ) {
453 if ( $orig_name =~ /\\([^\\]+)$/ ) {
454 $orig_name = $1;
455 } elsif ( $orig_name =~ /\/([^\/]+)$/ ) {
456 $orig_name = $1;
457 }
458 $orig_name =~ s/[\ \t]/_/g;
459 $orig_name = $oid.'_'.$orig_name;
460 $filename =~ s/\/([^\/]+)$//;
461 my $fname = $1;
462 unless ( $orig_name =~ /^[a-zA-Z_\d\.\-\,]+$/ ) {
463 $orig_name = translit( $orig_name );
464 }
465 warn "\n\n\n\n\nNew Name: [$orig_name]\n\n\n\n\n" if $DEBUG;
466 unless ( $orig_name =~ /^[a-zA-Z_\d\.\-\,]+$/ ) {
467 $orig_name = $fname;
468 }
469 $filename .= '/'.$orig_name;
470 $filename =~ s/\.([^\.]+)$//;
471 }
472 }
473
474 127 ahitrov my $filename_tmp = $state->{'tmp_dir'}.'/'.join('_', split('/', $filename));
475
476 my $fh = get_fh($input);
477 return unless ref $fh;
478
479 my $ext;
480 my $size = 1073741824;
481 if ( not ref $input ) {
482 132 ahitrov $ext = $input =~ /\.([^\.]+)$/ ? lc($1) : 'bin';
483 127 ahitrov if ( scheme($input) eq 'file' ) {
484 $size = (stat $fh)[7];
485 }
486 } elsif ( ref $input eq 'Apache::Upload' ) {
487 132 ahitrov $ext = $input->filename() =~ /\.([^\.]+)$/ ? lc($1) : 'bin';
488 127 ahitrov $size = (stat $fh)[7];
489 } elsif ( $opts{filename} ) {
490 132 ahitrov $ext = $opts{filename} =~ /\.([^\.]+)$/ ? lc($1) : 'bin';
491 127 ahitrov }
492 if ( ref $fh eq 'IO::Scalar' ) {
493 $size = length("$fh");
494 }
495 $ext ||= 'bin';
496
497 my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$ext) || return;
498 my $buffer;
499
500 $size = sysread $fh, $buffer, $size;
501 syswrite $fh_tmp, $buffer, $size;
502
503 undef $fh_tmp;
504
505 my $BINARY;
506 if ( store($filename.'.'.$ext, $filename_tmp.'.'.$ext) ) {
507 132 ahitrov @{$BINARY}{"filename", "ext", "size"} = (
508 $filename.".".$ext, $ext, $size
509 );
510
511 127 ahitrov unlink $filename_tmp.'.'.$ext if -e $filename_tmp.'.'.$ext;
512 132 ahitrov
513 if ( $ext =~ /(rar|7z|zip|arc|lha|arj|cab)/ ) {
514 $BINARY->{type} = 'archive';
515 } elsif ( $ext =~ /(doc|rtf)/ ) {
516 $BINARY->{type} = 'doc';
517 } elsif ( $ext eq 'xls' ) {
518 $BINARY->{type} = 'xls';
519 } elsif ( $ext =~ /(mdb|ppt)/ ) {
520 $BINARY->{type} = 'msoffice';
521 } elsif ( $ext =~ /(pdf)/ ) {
522 $BINARY->{type} = 'ebook';
523 } elsif ( $ext eq 'psd' ) {
524 $BINARY->{type} = 'psd';
525 } elsif ( $ext =~ /(exe|msi|cab)/ ) {
526 $BINARY->{type} = 'executable';
527 } else {
528 $BINARY->{type} = 'unknown';
529 }
530
531 127 ahitrov }
532
533 return $BINARY;
534 }
535
536 sub remove_binary {
537 my $BINARY = shift;
538
539 if ( ref $BINARY eq 'HASH' && exists $BINARY->{filename} ) {
540 remove($BINARY->{'filename'}) || return;
541 }
542
543 1;
544 }
545
546
547 132 ahitrov sub translit {
548 my $str = shift;
549 my @str = split (//, $str);
550 my $res = '';
551 while ( scalar @str ) {
552 my $alpha = shift @str;
553 if ( exists $translit{$alpha} ) {
554 $res .= $translit{$alpha};
555 } else {
556 $res .= $alpha;
557 }
558 }
559 return $res;
560 }
561
562
563 3 ahitrov@rambler.ru 1;