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
15 our $IgnoreErrors = 1;
16
17 sub fetch {
18 my $filename = shift || return;
19 my $fh;
20
21 foreach my $dir (@{$state->{"files_dir"}}) {
22 my $path = $dir . '/' . $filename;
23
24 no strict "refs";
25 $fh = &{"Contenido::File::Scheme::".uc(scheme($path))."::fetch"}($path);
26 return $fh if $fh;
27 }
28 }
29
30 sub store {
31 my $filename = shift || return;
32 my $fh = get_fh(shift) || return;
33
34 my $dt = Contenido::DateTime->new()->set_locale('en')->set_time_zone("UTC");
35
36 my @successful;
37
38 #������� ��������� � ����� /
39 $filename =~ s#/+#/#g;
40 #������� ��������� /
41 $filename =~ s#^/##;
42
43 foreach my $dir (@{$state->{"files_dir"}}) {
44 seek $fh, 0, 0;
45 my $path = $dir . '/' . $filename;
46
47 no strict "refs";
48 my $return = &{"Contenido::File::Scheme::".uc(scheme($path))."::store"}($path, $fh, $dt);
49 push @successful, $path if $return;
50 }
51
52 if (
53 !@successful or
54 (
55 (scalar @successful != scalar @{$state->{"files_dir"}}) and
56 !$IgnoreErrors
57 )
58 ) {
59 foreach my $path (@successful) {
60 no strict "refs";
61 &{"Contenido::File::Scheme::".uc(scheme($path))."::remove"}($path);
62 }
63 return;
64 }
65 1;
66 }
67
68 sub size {
69 my $filename = shift;
70 my %args = @_;
71
72 my $size;
73
74 #������� ��������� � ����� /
75 $filename =~ s#/+#/#g;
76 #������� ��������� /
77 $filename =~ s#^/##;
78
79 foreach my $dir (@{$state->{"files_dir"}}) {
80 no strict "refs";
81 my $response = &{"Contenido::File::Scheme::".uc(scheme($dir))."::size"}($dir."/".$filename, %args);
82 return unless defined $response;
83 return if defined $size and $size != $response;
84
85 $size = $response;
86 #TODO
87 last;
88 }
89 return $size;
90 }
91
92 sub remove {
93 my $filename = shift || return;
94
95 #������� ��������� � ����� /
96 $filename =~ s#/+#/#g;
97 #������� ��������� /
98 $filename =~ s#^/##;
99
100 foreach my $dir (@{$state->{"files_dir"}}) {
101 no strict "refs";
102 &{"Contenido::File::Scheme::".uc(scheme($dir))."::remove"}($dir."/".$filename);
103 }
104
105 1;
106 }
107
108 sub get_fh {
109 my $input = shift;
110 my $fh;
111
112 if (not ref $input) {
113 no strict "refs";
114 $fh = &{"Contenido::File::Scheme::".uc(scheme($input))."::get_fh"}($input);
115 114 ahitrov } elsif ( ref $input eq 'Apache::Upload' ) {
116 $fh = $input->fh;
117 } elsif ((ref $input eq "GLOB") or (ref $input eq 'IO::File')) {
118 3 ahitrov@rambler.ru $fh = $input;
119 } elsif (ref $input eq "SCALAR") {
120 $fh = IO::Scalar->new($input);
121 } else {
122 $log->warning("Path, scalar ref or fh needed");
123 return;
124 }
125
126 return $fh
127 }
128
129 sub scheme {
130 my $uri = shift;
131 my $scheme;
132
133 $scheme = URI->new($uri)->scheme() || "file";
134
135 return $scheme;
136 }
137
138 # sub files_dir {
139 # return unless $_[0];
140 # my $dir;
141
142 # for (scheme($_[0])) {
143 # /http/ && do {
144 # $dir = URI->new($_[0])->canonical();
145 # $dir =~ s|/*$|/|;
146 # last;
147 # };
148 # /file/ && do {
149 # $dir = URI->new($_[0])->path();
150 # $dir =~ s|/*$|/|;
151 # last;
152 # };
153 # }
154
155 # return $dir;
156 # }
157
158 114 ahitrov sub store_image {
159 my $input = shift;
160 my (%opts) = @_;
161 my $object = delete $opts{object} || return;
162 my $attr = delete $opts{attr} || return;
163
164 my ($prop) = grep { $_->{attr} eq $attr } $object->structure;
165 return unless ref $prop;
166 my @preview = exists $prop->{'preview'} && ref $prop->{'preview'} eq 'ARRAY' ? @{$prop->{'preview'}} : exists $prop->{'preview'} && $prop->{'preview'} ? ($prop->{'preview'}) : ();
167 my @crops = exists $prop->{'crop'} && ref $prop->{'crop'} eq 'ARRAY' ? @{$prop->{'crop'}} : exists $prop->{'crop'} && $prop->{'crop'} ? ($prop->{'crop'}) : ();
168 my @shrinks = exists $prop->{'shrink'} && ref $prop->{'shrink'} eq 'ARRAY' ? @{$prop->{'shrink'}} : exists $prop->{'shrink'} && $prop->{'shrink'} ? ($prop->{'shrink'}) : ();
169
170 my $filename = '/images/'.$object->get_file_name() || return;
171 my $filename_tmp = $state->{'tmp_dir'}.'/'.join('_', split('/', $filename));
172
173 my $fh = get_fh($input);
174 return unless ref $fh;
175
176 my $size = (stat $fh)[7];
177 my $ext;
178 if ( $opts{filename} ) {
179 $ext = $opts{filename} =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
180 } elsif ( not ref $input ) {
181 $ext = $input =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
182 } elsif ( ref $input eq 'Apache::Upload' ) {
183 $ext = $input->filename() =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
184 }
185 $ext ||= 'bin';
186
187 my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$ext) || return;
188 my $buffer;
189
190 sysread $fh, $buffer, $size;
191 syswrite $fh_tmp, $buffer, $size;
192
193 undef $fh_tmp;
194
195 my $IMAGE;
196 if ( store($filename.'.'.$ext, $filename_tmp.'.'.$ext) ) {
197 $IMAGE = {};
198 # hashref slice assigning - ������
199 @{$IMAGE}{'filename', 'width', 'height'} = (
200 $filename.'.'.$ext,
201 Image::Size::imgsize($filename_tmp.'.'.$ext),
202 );
203
204 foreach my $suffix (@preview) {
205 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'\' -quality 80 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
206 my $result = `$c_line`;
207
208 if (length $result > 0) {
209 warn 'Contenido Error: ��� ������ "'.$c_line.'" ��������� ������ "'.$result.'" ('.$@.")\n";
210 return undef;
211 }
212 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
213 $filename.'.'.$suffix.'.'.$ext,
214 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$ext),
215 );
216 %{$IMAGE->{'resize'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
217 store($filename.'.'.$suffix.'.'.$ext, $filename_tmp.'.'.$suffix.'.'.$ext);
218 unlink $filename_tmp.'.'.$suffix.'.'.$ext if -e $filename_tmp.'.'.$suffix.'.'.$ext;
219 }
220 if ( @preview ) {
221 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'};
222 @{$IMAGE->{'resize'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'};
223 }
224
225 ########## CROPS
226 foreach my $suffix (@crops) {
227
228 my $shave_string;
229 my ($nwidth, $nheight) = $suffix =~ /(\d+)x(\d+)/i ? ($1, $2) : (0, 0);
230 if ( ($IMAGE->{width} / $IMAGE->{height}) > ($nwidth / $nheight) ) {
231 my $shave_pixels = (($IMAGE->{width} / $IMAGE->{height}) - ($nwidth / $nheight)) * $IMAGE->{height};
232 $shave_string = ' -shave '.int($shave_pixels / 2).'x0';
233 } elsif ( ($IMAGE->{height} / $IMAGE->{width}) > ($nheight / $nwidth) ) {
234 my $shave_pixels = (($IMAGE->{height} / $IMAGE->{width}) - ($nheight / $nwidth)) * $IMAGE->{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 }
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 }
249 }
250
251 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 80 '.$filename_tmp.'.shaved.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
252 my $result = `$c_line`;
253
254 if (length $result > 0) {
255 warn 'Contenido Error: ��� ������ "'.$c_line.'" ��������� ������ "'.$result.'" ('.$@.")\n";
256 return undef;
257 }
258 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
259 $filename.'.'.$suffix.'.'.$ext,
260 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$ext),
261 );
262 %{$IMAGE->{'crop'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
263 store($filename.'.'.$suffix.'.'.$ext, $filename_tmp.'.'.$suffix.'.'.$ext);
264 unlink $filename_tmp.'.shaved.'.$ext if -e $filename_tmp.'.shaved.'.$ext;
265 unlink $filename_tmp.'.'.$suffix.'.'.$ext if -e $filename_tmp.'.'.$suffix.'.'.$ext;
266 }
267 if ( @crops ) {
268 if ( !exists $IMAGE->{'mini'}{'filename'} ) {
269 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$crops[0]}}{'filename', 'width', 'height'};
270 }
271 @{$IMAGE->{'crop'}}{'filename', 'width', 'height'} = @{$IMAGE->{'crop'}{$crops[0]}}{'filename', 'width', 'height'};
272 }
273
274
275 ########## SHRINKS
276 foreach my $suffix (@shrinks) {
277
278 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 80 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
279 my $result = `$c_line`;
280
281 if (length $result > 0) {
282 warn 'Contenido Error: ��� ������ "'.$c_line.'" ��������� ������ "'.$result.'" ('.$@.")\n";
283 return undef;
284 }
285 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
286 $filename.'.'.$suffix.'.'.$ext,
287 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$ext),
288 );
289 %{$IMAGE->{'shrink'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
290 store($filename.'.'.$suffix.'.'.$ext, $filename_tmp.'.'.$suffix.'.'.$ext);
291 unlink $filename_tmp.'.'.$suffix.'.'.$ext if -e $filename_tmp.'.'.$suffix.'.'.$ext;
292 }
293 if ( @shrinks && !exists $IMAGE->{'mini'}{'filename'} ) {
294 if ( !exists $IMAGE->{'mini'}{'filename'} ) {
295 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$shrinks[0]}}{'filename', 'width', 'height'};
296 }
297 @{$IMAGE->{'shrink'}}{'filename', 'width', 'height'} = @{$IMAGE->{'shrink'}{$shrinks[0]}}{'filename', 'width', 'height'};
298 }
299
300 unlink $filename_tmp.'.'.$ext if -e $filename_tmp.'.'.$ext;
301 }
302
303 return $IMAGE;
304 }
305
306 sub remove_image {
307 my $IMAGE = shift;
308
309 if ( ref $IMAGE eq 'HASH' && exists $IMAGE->{filename} ) {
310 remove($IMAGE->{'filename'}) || return;
311 }
312 if ( ref $IMAGE && exists $IMAGE->{mini} && ref $IMAGE->{mini} eq 'HASH' ) {
313 foreach my $val ( values %{$IMAGE->{mini}} ) {
314 if ( ref $val && exists $val->{filename} && $val->{filename} ) {
315 remove($val->{'filename'}) || return;
316 }
317 }
318 }
319 1;
320 }
321
322
323 3 ahitrov@rambler.ru 1;