annotate lib/IMPL/Web/DOM/FileNode.pm @ 417:3ed0c58e9da3 ref20150831

working on di container, tests
author cin
date Mon, 02 Nov 2015 01:56:53 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Web::DOM::FileNode;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use parent qw(IMPL::DOM::Node);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 __PACKAGE__->PassThroughArgs;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::Class::Property;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use File::Temp qw(tempfile);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 public property parameterName => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 get => sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 $this->_parameterName() or
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 $this->_parameterName(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 join '/', ( map {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 (defined $_->nodeProperty('instanceId')) ?
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 $_->nodeName . '['.$_->nodeProperty('instanceId').']':
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 $_->nodeName
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 } $this->_selectParents, $this )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 private property _parameterName => prop_all;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 public property fileName => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 get => sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 return $this->document->query->param($this->parameterName);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 public property fileHandle => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 get => sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 return $this->document->query->upload($this->parameterName);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 sub invokeTempFile {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 my ($this,$sub,$target) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 die new IMPL::InvalidArgumentException("A reference to a function should be specified") unless $sub && ref $sub eq 'CODE';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 $target ||= $this;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 my $query = $this->document->nodeProperty('query') or die new IMPL::InvalidOperationException("Failed to get a CGI query from the document");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 my $hFile = $query->upload($this->parameterName) or die new IMPL::IOException("Failed to open the uploaded file",$query->cgi_error,$this->parameterName,$this->nodeProperty('instanceId'));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 my ($hTemp,$tempFileName) = tempfile();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 binmode($hTemp);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 print $hTemp $_ while <$hFile>;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 $hTemp->flush();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 seek $hTemp, 0,0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 local $_ = $tempFileName;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 $sub->($this,$tempFileName,$hTemp);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 sub _selectParents {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 my ($node) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 my @result;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 unshift @result, $node while $node = $node->parentNode;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 return @result;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 C<IMPL::Web::DOM::FileNode> - узел, использующийся для представления параметра запроса в котором передан файл.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 =head1 SINOPSYS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 =begin code xml
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 <!-- input.schema.xml -->
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 <schema>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 <SimpleType type="file" nativeType="IMPL::Web::DOM::FileNode"/>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 <ComplexNode name="user">
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 <Node type="file" name="avatar"/>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 </ComplexNode>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 </schema>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 =end code xml
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 # handle.pl
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 use IMPL::DOM::Transform::PostToDOM ();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 use IMPL::DOM::Schema;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 use CGI;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 use File::Copy qw(copy);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 my $t = new IMPL::DOM::Transform::PostToDOM(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 IMPL::DOM::Schema->LoadSchema('input.schema.xml'),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 'user'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 my $doc = $t->Transform(CGI->new());
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 if ($t->Errors->Count) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 # handle errors
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 $doc->selectSingleNode('avatar')->invokeTempFile(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 my($node,$fname,$fhandle) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 # do smth with file
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 copy($_,'avatar.jpg');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 # same thing
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 # copy($fname,'avatar.jpg');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 Данный класс используется для представлении параметров C<CGI> запросов при преобзаовании
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 запроса в ДОМ документ преобразованием C<IMPL::DOM::Transform::PostToDOM>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 Узлы данного типа расширяют стандатрный C<IMPL::DOM::Node> несколькими свойствами и
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 методами для доступа к файлу, переданному в виде параметра запроса.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 =head1 MEMBERS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 =head2 PROPERTIES
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 =item C<[get] parameterName>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 Имя параметра C<CGI> запроса соответствующего данному узлу.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 =item C<[get] fileName>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 Имя файла из параметра запроса
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 =item C<[get] fileHandle>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 Указатель на файл из параметра запроса
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157 =head2 METHODS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 =item C<invokeTempFile($callback,$target)>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 Сохраняет файл, переданный в запросе во временный, вызывает C<$callback> для обработки временного файла.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167 =item C<$callback>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169 Ссылка на функцию которая будет вызвана для обработки временного файла. C<callback($target,$fname,$fhandle)>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173 =item C<$fname>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175 Имя временного файла
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177 =item C<$fhandle>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179 Указатель на временный файл
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 Также пременная C<$_> содержит имя временного файла.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185 =item C<$target>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187 Значение этого параметра будет передано первым параметром функции C<$callback>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
189 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
190
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
192
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
193 =cut