1 |
8 |
ahitrov@rambler.ru |
<%args> |
2 |
|
|
|
3 |
|
|
$SETS => undef |
4 |
|
|
$field => undef |
5 |
|
|
$default => undef |
6 |
|
|
$object => undef |
7 |
|
|
$prop => undef |
8 |
|
|
|
9 |
|
|
</%args> |
10 |
|
|
|
11 |
|
|
<%init> |
12 |
|
|
use IO::File; |
13 |
|
|
use Data::Dumper; |
14 |
|
|
|
15 |
|
|
return undef unless defined $SETS and defined $field; |
16 |
|
|
|
17 |
|
|
# -------------------------------------------------------------------------------------- |
18 |
|
|
# Добавление одной картинки |
19 |
|
|
|
20 |
|
|
my $IMAGE = $default; |
21 |
|
|
|
22 |
|
|
if ($SETS->{$field}) { |
23 |
|
|
|
24 |
|
|
my $preview = $prop->{'preview'} || $keeper->{'preview'}; |
25 |
|
|
my @preview = ref $preview && ref $preview eq 'ARRAY' ? @{$preview} : ref $preview ? ($keeper->{'preview'}) : ($preview); |
26 |
|
|
|
27 |
|
|
if (ref $IMAGE eq 'HASH') { |
28 |
|
|
foreach my $suffix (@preview) { |
29 |
|
|
Contenido::File::remove(${$IMAGE->{mini}}{$suffix}->{'filename'}) if ${$IMAGE->{mini}}{$suffix} and ${$IMAGE->{mini}}{$suffix}->{'filename'}; |
30 |
|
|
} |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
my $filename = '/images/'.$object->get_file_name() || return; |
34 |
|
|
my $filename_tmp = $state->{'tmp_dir'}.'/'.join('_', split('/', $filename)); |
35 |
|
|
|
36 |
|
|
my $upload = $r->upload($field); |
37 |
|
|
if ($upload) { |
38 |
|
|
my $extension = $upload->filename() =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin'; |
39 |
|
|
my $fh = $upload->fh(); |
40 |
|
|
my $size = (stat $fh)[7]; |
41 |
|
|
|
42 |
|
|
my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$extension) || die 'Can\'t open temporary file'; |
43 |
|
|
|
44 |
|
|
my $buffer; |
45 |
|
|
|
46 |
|
|
sysread $fh, $buffer, $size; |
47 |
|
|
syswrite $fh_tmp, $buffer, $size; |
48 |
|
|
|
49 |
|
|
undef $fh_tmp; |
50 |
|
|
|
51 |
|
|
if (Contenido::File::store($filename.'.'.$extension, $filename_tmp.'.'.$extension)) { |
52 |
|
|
if (ref $IMAGE eq 'HASH' and $IMAGE->{'filename'}) { |
53 |
|
|
Contenido::File::remove($IMAGE->{'filename'}); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
# hashref slice assigning - жжесть |
57 |
|
|
@{$IMAGE}{'filename', 'width', 'height'} = ( |
58 |
|
|
$filename.'.'.$extension, |
59 |
|
|
Image::Size::imgsize($filename_tmp.'.'.$extension), |
60 |
|
|
); |
61 |
|
|
foreach my $suffix (@preview) { |
62 |
|
|
|
63 |
|
|
my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'\' -quality 80 '.$filename_tmp.'.'.$extension.' '.$filename_tmp.'.'.$suffix.'.'.$extension; |
64 |
|
|
my $result = `$c_line`; |
65 |
|
|
|
66 |
|
|
if (length $result > 0) { |
67 |
|
|
warn 'Contenido Error: При вызове "'.$c_line.'" произошла ошибка "'.$result.'" ('.$@.")\n"; |
68 |
|
|
return undef; |
69 |
|
|
} |
70 |
|
|
@{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = ( |
71 |
|
|
$filename.'.'.$suffix.'.'.$extension, |
72 |
|
|
Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$extension), |
73 |
|
|
); |
74 |
|
|
Contenido::File::store($filename.'.'.$suffix.'.'.$extension, $filename_tmp.'.'.$suffix.'.'.$extension); |
75 |
|
|
} |
76 |
|
|
@{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'}; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
unlink $filename_tmp.'.'.$extension if -e $filename_tmp.'.'.$extension; |
80 |
|
|
foreach my $suffix (@preview) { |
81 |
|
|
unlink $filename_tmp.'.'.$suffix.'.'.$extension if -e $filename_tmp.'.'.$suffix.'.'.$extension; |
82 |
|
|
} |
83 |
|
|
} else { |
84 |
|
|
warn 'ERROR in incoming POST form: "'.$field.' is not file upload (value: "'.$SETS->{$field}.'")'; |
85 |
|
|
} |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
# Дополнительные поля - берем все, что найдем... |
89 |
|
|
|
90 |
|
|
my $pattern = "^".$field.q|\.(.*)$|; |
91 |
|
|
|
92 |
|
|
foreach my $localfield (keys(%{ $SETS })) { |
93 |
|
|
if ($localfield =~ /$pattern/) { |
94 |
|
|
my $imagefield = $1; |
95 |
|
|
$IMAGE->{$imagefield} = $m->comp("/contenido/components/filter.msn", str => $SETS->{$localfield}); |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
return $IMAGE; |
100 |
|
|
</%init> |