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