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 |
|
|
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 |
114 |
ahitrov |
} elsif ( ref $input eq 'Apache::Upload' ) { |
127 |
|
|
$fh = $input->fh; |
128 |
|
|
} elsif ((ref $input eq "GLOB") or (ref $input eq 'IO::File')) { |
129 |
3 |
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 |
170 |
ahitrov |
$scheme = 'http' if lc($scheme) eq 'https'; |
146 |
3 |
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 |
114 |
ahitrov |
sub store_image { |
171 |
|
|
my $input = shift; |
172 |
|
|
my (%opts) = @_; |
173 |
|
|
my $object = delete $opts{object} || return; |
174 |
|
|
my $attr = delete $opts{attr} || return; |
175 |
|
|
|
176 |
|
|
my ($prop) = grep { $_->{attr} eq $attr } $object->structure; |
177 |
|
|
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 |
121 |
ahitrov |
my $size = 1073741824; |
190 |
|
|
if ( not ref $input ) { |
191 |
114 |
ahitrov |
$ext = $input =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin'; |
192 |
121 |
ahitrov |
if ( scheme($input) eq 'file' ) { |
193 |
|
|
$size = (stat $fh)[7]; |
194 |
|
|
} |
195 |
114 |
ahitrov |
} elsif ( ref $input eq 'Apache::Upload' ) { |
196 |
|
|
$ext = $input->filename() =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin'; |
197 |
121 |
ahitrov |
$size = (stat $fh)[7]; |
198 |
|
|
} elsif ( $opts{filename} ) { |
199 |
|
|
$ext = $opts{filename} =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin'; |
200 |
114 |
ahitrov |
} |
201 |
127 |
ahitrov |
if ( ref $fh eq 'IO::Scalar' ) { |
202 |
|
|
$size = length("$fh"); |
203 |
|
|
} |
204 |
114 |
ahitrov |
$ext ||= 'bin'; |
205 |
|
|
|
206 |
|
|
my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$ext) || return; |
207 |
|
|
my $buffer; |
208 |
|
|
|
209 |
121 |
ahitrov |
$size = sysread $fh, $buffer, $size; |
210 |
114 |
ahitrov |
syswrite $fh_tmp, $buffer, $size; |
211 |
|
|
|
212 |
|
|
undef $fh_tmp; |
213 |
|
|
|
214 |
143 |
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 |
148 |
ahitrov |
} elsif ( !ref $image_info ) { |
219 |
|
|
unlink $filename_tmp.'.'.$ext; |
220 |
|
|
return undef; |
221 |
143 |
ahitrov |
} |
222 |
148 |
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 |
143 |
ahitrov |
|
261 |
114 |
ahitrov |
my $IMAGE; |
262 |
148 |
ahitrov |
my $stored = $transformed ? store($filename.'.'.$ext, $filename_tmp.'.transformed.'.$ext) : store($filename.'.'.$ext, $filename_tmp.'.'.$ext); |
263 |
|
|
if ( $stored ) { |
264 |
114 |
ahitrov |
$IMAGE = {}; |
265 |
150 |
ahitrov |
# hashref slice assigning - ������ |
266 |
148 |
ahitrov |
if ( $transformed && -e $filename_tmp.'.transformed.'.$ext ) { |
267 |
161 |
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 |
148 |
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 |
114 |
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 |
161 |
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 |
114 |
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 |
127 |
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 |
132 |
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 |
127 |
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 |
132 |
ahitrov |
$ext = $input =~ /\.([^\.]+)$/ ? lc($1) : 'bin'; |
450 |
127 |
ahitrov |
if ( scheme($input) eq 'file' ) { |
451 |
|
|
$size = (stat $fh)[7]; |
452 |
|
|
} |
453 |
|
|
} elsif ( ref $input eq 'Apache::Upload' ) { |
454 |
132 |
ahitrov |
$ext = $input->filename() =~ /\.([^\.]+)$/ ? lc($1) : 'bin'; |
455 |
127 |
ahitrov |
$size = (stat $fh)[7]; |
456 |
|
|
} elsif ( $opts{filename} ) { |
457 |
132 |
ahitrov |
$ext = $opts{filename} =~ /\.([^\.]+)$/ ? lc($1) : 'bin'; |
458 |
127 |
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 |
132 |
ahitrov |
@{$BINARY}{"filename", "ext", "size"} = ( |
475 |
|
|
$filename.".".$ext, $ext, $size |
476 |
|
|
); |
477 |
|
|
|
478 |
127 |
ahitrov |
unlink $filename_tmp.'.'.$ext if -e $filename_tmp.'.'.$ext; |
479 |
132 |
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 |
127 |
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 |
132 |
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 |
3 |
ahitrov@rambler.ru |
1; |