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