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