1 |
296 |
ahitrov |
<%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 |
|
|
return undef if $extension eq 'bin'; |
40 |
|
|
my $fh = $upload->fh(); |
41 |
|
|
my $size = (stat $fh)[7]; |
42 |
|
|
|
43 |
|
|
my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$extension) || die 'Can\'t open temporary file'; |
44 |
|
|
|
45 |
|
|
my $buffer; |
46 |
|
|
|
47 |
|
|
sysread $fh, $buffer, $size; |
48 |
|
|
syswrite $fh_tmp, $buffer, $size; |
49 |
|
|
|
50 |
|
|
undef $fh_tmp; |
51 |
|
|
my ($w, $h) = Image::Size::imgsize($filename_tmp.'.'.$extension); |
52 |
|
|
return undef unless $w && $h; |
53 |
|
|
|
54 |
|
|
if (Contenido::File::store($filename.'.'.$extension, $filename_tmp.'.'.$extension)) { |
55 |
|
|
if (ref $IMAGE eq 'HASH' and $IMAGE->{'filename'}) { |
56 |
|
|
Contenido::File::remove($IMAGE->{'filename'}); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
# hashref slice assigning - жжесть |
60 |
|
|
@{%$IMAGE}{'filename', 'width', 'height'} = ( |
61 |
|
|
$filename.'.'.$extension, |
62 |
|
|
Image::Size::imgsize($filename_tmp.'.'.$extension), |
63 |
|
|
); |
64 |
|
|
foreach my $suffix (@preview) { |
65 |
|
|
|
66 |
|
|
my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'\' -quality 80 '.$filename_tmp.'.'.$extension.' '.$filename_tmp.'.'.$suffix.'.'.$extension; |
67 |
|
|
my $result = `$c_line`; |
68 |
|
|
|
69 |
|
|
if (length $result > 0) { |
70 |
|
|
warn 'Contenido Error: При вызове "'.$c_line.'" произошла ошибка "'.$result.'" ('.$@.")\n"; |
71 |
|
|
return undef; |
72 |
|
|
} |
73 |
|
|
@{%{$IMAGE->{'mini'}{$suffix}}}{'filename', 'width', 'height'} = ( |
74 |
|
|
$filename.'.'.$suffix.'.'.$extension, |
75 |
|
|
Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$extension), |
76 |
|
|
); |
77 |
|
|
Contenido::File::store($filename.'.'.$suffix.'.'.$extension, $filename_tmp.'.'.$suffix.'.'.$extension); |
78 |
|
|
} |
79 |
|
|
@{%{$IMAGE->{'mini'}}}{'filename', 'width', 'height'} = @{%{$IMAGE->{'mini'}{$preview[0]}}}{'filename', 'width', 'height'}; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
unlink $filename_tmp.'.'.$extension if -e $filename_tmp.'.'.$extension; |
83 |
|
|
foreach my $suffix (@preview) { |
84 |
|
|
unlink $filename_tmp.'.'.$suffix.'.'.$extension if -e $filename_tmp.'.'.$suffix.'.'.$extension; |
85 |
|
|
} |
86 |
|
|
} else { |
87 |
|
|
warn 'ERROR in incoming POST form: "'.$field.' is not file upload (value: "'.$SETS->{$field}.'")'; |
88 |
|
|
} |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
# Дополнительные поля - берем все, что найдем... |
92 |
|
|
|
93 |
|
|
my $pattern = "^".$field.q|\.(.*)$|; |
94 |
|
|
|
95 |
|
|
foreach my $localfield (keys(%{ $SETS })) { |
96 |
|
|
if ($localfield =~ /$pattern/) { |
97 |
|
|
my $imagefield = $1; |
98 |
|
|
$IMAGE->{$imagefield} = $m->comp("/contenido/components/filter.msn", str => $SETS->{$localfield}); |
99 |
|
|
} |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
return $IMAGE; |
103 |
|
|
</%init> |