1 | # +-----------------------------------------------------------------------+ |
---|
2 | # | pLoader - a Perl photo uploader for Piwigo | |
---|
3 | # +-----------------------------------------------------------------------+ |
---|
4 | # | Copyright(C) 2008 Piwigo Team http://piwigo.org | |
---|
5 | # +-----------------------------------------------------------------------+ |
---|
6 | # | This program is free software; you can redistribute it and/or modify | |
---|
7 | # | it under the terms of the GNU General Public License as published by | |
---|
8 | # | the Free Software Foundation | |
---|
9 | # | | |
---|
10 | # | This program is distributed in the hope that it will be useful, but | |
---|
11 | # | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
12 | # | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
13 | # | General Public License for more details. | |
---|
14 | # | | |
---|
15 | # | You should have received a copy of the GNU General Public License | |
---|
16 | # | along with this program; if not, write to the Free Software | |
---|
17 | # | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
18 | # | USA. | |
---|
19 | # +-----------------------------------------------------------------------+ |
---|
20 | package Uploader::PWG::WebServices; |
---|
21 | |
---|
22 | use strict; |
---|
23 | use warnings; |
---|
24 | use MIME::Base64 qw(encode_base64); |
---|
25 | use JSON; |
---|
26 | use LWP::UserAgent; |
---|
27 | use Data::Dumper; |
---|
28 | use Digest::MD5::File qw/file_md5_hex md5_hex/; |
---|
29 | use File::Slurp; |
---|
30 | use Encode qw/encode decode is_utf8/; |
---|
31 | use POSIX qw(ceil floor); |
---|
32 | use base qw/ |
---|
33 | Uploader::Object |
---|
34 | Class::Accessor::Fast |
---|
35 | /; |
---|
36 | |
---|
37 | __PACKAGE__->mk_accessors( |
---|
38 | qw/ |
---|
39 | uagent |
---|
40 | urlbase |
---|
41 | username |
---|
42 | password |
---|
43 | qry_list_categories |
---|
44 | qry_add_categories |
---|
45 | items |
---|
46 | tags |
---|
47 | categories |
---|
48 | site_high_file |
---|
49 | site_resized_file |
---|
50 | site_thumb_file |
---|
51 | site_image_name |
---|
52 | rank |
---|
53 | site_author |
---|
54 | site_comment |
---|
55 | site_img_date_creation |
---|
56 | uagent_response |
---|
57 | login_result |
---|
58 | action_result |
---|
59 | upload_high |
---|
60 | chunk_size |
---|
61 | / |
---|
62 | ); |
---|
63 | |
---|
64 | $|=1; |
---|
65 | |
---|
66 | sub Init { |
---|
67 | my ( $self ) = @_ ; |
---|
68 | |
---|
69 | $self->uagent( |
---|
70 | LWP::UserAgent->new |
---|
71 | ); |
---|
72 | |
---|
73 | |
---|
74 | $self->uagent->cookie_jar({}); |
---|
75 | |
---|
76 | $self->urlbase( |
---|
77 | $self->{site_url} |
---|
78 | ); |
---|
79 | |
---|
80 | $self->username( |
---|
81 | $self->{site_username} |
---|
82 | ); |
---|
83 | |
---|
84 | $self->password( |
---|
85 | $self->{site_password} |
---|
86 | ); |
---|
87 | |
---|
88 | |
---|
89 | $self->uagent->default_headers->authorization_basic( |
---|
90 | $self->{http_username}||$self->username, |
---|
91 | $self->{http_password}||$self->password |
---|
92 | ); |
---|
93 | |
---|
94 | $self->chunk_size( |
---|
95 | $self->{chunk_size}||500_000 |
---|
96 | ); |
---|
97 | |
---|
98 | $self->qry_list_categories( sprintf |
---|
99 | "%s/ws.php?format=json&method=%s&recursive=%s", |
---|
100 | $self->urlbase, |
---|
101 | # 'pwg.categories.getAdminList', |
---|
102 | 'pwg.categories.getList', |
---|
103 | 'true', |
---|
104 | ); |
---|
105 | |
---|
106 | my $form = { |
---|
107 | method => 'pwg.session.login', |
---|
108 | username => $self->username, |
---|
109 | password => $self->password, |
---|
110 | }; |
---|
111 | |
---|
112 | $self->uagent_response( |
---|
113 | $self->uagent->post( |
---|
114 | $self->urlbase.'/ws.php?format=json', |
---|
115 | $form |
---|
116 | ) |
---|
117 | ); |
---|
118 | |
---|
119 | my $hresult = {} ; |
---|
120 | |
---|
121 | if($self->uagent_response->is_success){ |
---|
122 | eval { |
---|
123 | $hresult = from_json( |
---|
124 | $self->uagent_response->content |
---|
125 | ); |
---|
126 | }; |
---|
127 | } |
---|
128 | else{ |
---|
129 | $hresult = { |
---|
130 | 'message' => $self->uagent_response->message, |
---|
131 | 'stat' => 'fail', |
---|
132 | }; |
---|
133 | } |
---|
134 | |
---|
135 | $self->login_result( |
---|
136 | $hresult |
---|
137 | ); |
---|
138 | |
---|
139 | } |
---|
140 | |
---|
141 | sub GetCategories { |
---|
142 | my ( $self ) = @_; |
---|
143 | |
---|
144 | my $result; |
---|
145 | eval { |
---|
146 | $result = $self->uagent->get( |
---|
147 | $self->qry_list_categories |
---|
148 | ); |
---|
149 | }; |
---|
150 | |
---|
151 | if($@){ |
---|
152 | printf("An error occured in query execution %s\n%s", |
---|
153 | $self->qry_list_categories, |
---|
154 | $@, |
---|
155 | ); |
---|
156 | } |
---|
157 | my $hresult; |
---|
158 | |
---|
159 | eval { |
---|
160 | $hresult = from_json( |
---|
161 | $result->content |
---|
162 | ); |
---|
163 | }; |
---|
164 | |
---|
165 | $hresult ||= {}; |
---|
166 | |
---|
167 | my $categories = $hresult->{result}{categories}; |
---|
168 | |
---|
169 | $categories; |
---|
170 | } |
---|
171 | |
---|
172 | sub UploadImage { |
---|
173 | my ( $self ) = @_; |
---|
174 | |
---|
175 | my $file_sum; |
---|
176 | |
---|
177 | eval { |
---|
178 | $file_sum = file_md5_hex( |
---|
179 | encode('iso-8859-1', $self->site_resized_file) |
---|
180 | ); |
---|
181 | }; |
---|
182 | |
---|
183 | |
---|
184 | my $thumbnail_sum = file_md5_hex( |
---|
185 | encode('iso-8859-1', $self->site_thumb_file), |
---|
186 | ); |
---|
187 | |
---|
188 | my $original_sum = file_md5_hex( |
---|
189 | encode('iso-8859-1', $self->site_high_file) |
---|
190 | ); |
---|
191 | |
---|
192 | my $form = { |
---|
193 | method => 'pwg.images.exist', |
---|
194 | md5sum_list => $original_sum, |
---|
195 | }; |
---|
196 | |
---|
197 | my $result = $self->uagent->post( |
---|
198 | $self->urlbase.'/ws.php?format=json', |
---|
199 | $form |
---|
200 | ); |
---|
201 | |
---|
202 | my $hresult = {}; |
---|
203 | eval { |
---|
204 | $hresult = from_json( |
---|
205 | $result->{_content} |
---|
206 | ); |
---|
207 | }; |
---|
208 | |
---|
209 | my $site_image_name = is_utf8($self->site_image_name) ? |
---|
210 | $self->site_image_name : |
---|
211 | encode('utf8', $self->site_image_name); |
---|
212 | |
---|
213 | my $image_id = $hresult->{result}{$original_sum}; |
---|
214 | my $status = 0; |
---|
215 | my $status_line = "An error has occured during upload."; |
---|
216 | while(1){ |
---|
217 | if(!defined($image_id)){ |
---|
218 | |
---|
219 | my $resized_params = { |
---|
220 | filepath => encode('iso-8859-1', $self->site_resized_file), |
---|
221 | type => 'file', |
---|
222 | original_sum => $original_sum, |
---|
223 | }; |
---|
224 | |
---|
225 | $self->send_chunks( |
---|
226 | $resized_params |
---|
227 | ); |
---|
228 | |
---|
229 | $status_line = $resized_params->{message}; |
---|
230 | last if !$resized_params->{ok}; |
---|
231 | |
---|
232 | my $thumb_params = { |
---|
233 | filepath => encode('iso-8859-1', $self->site_thumb_file), |
---|
234 | type => 'thumb', |
---|
235 | original_sum => $original_sum, |
---|
236 | }; |
---|
237 | |
---|
238 | $self->send_chunks( |
---|
239 | $thumb_params |
---|
240 | ); |
---|
241 | |
---|
242 | $status_line = $thumb_params->{message}; |
---|
243 | last if !$thumb_params->{ok}; |
---|
244 | |
---|
245 | |
---|
246 | $form = { |
---|
247 | method => 'pwg.images.add', |
---|
248 | original_sum => $original_sum, |
---|
249 | file_sum => $file_sum, |
---|
250 | thumbnail_sum => $thumbnail_sum, |
---|
251 | categories => $self->categories, |
---|
252 | name => $site_image_name, |
---|
253 | rank => $self->rank, |
---|
254 | author => $self->site_author, |
---|
255 | comment => $self->site_comment, |
---|
256 | date_creation => $self->site_img_date_creation, |
---|
257 | |
---|
258 | }; |
---|
259 | my $high_params = { |
---|
260 | filepath => encode('iso-8859-1', $self->site_high_file), |
---|
261 | type => 'high', |
---|
262 | original_sum => $original_sum, |
---|
263 | }; |
---|
264 | |
---|
265 | if($self->upload_high){ |
---|
266 | $form->{high_sum} = $original_sum; |
---|
267 | |
---|
268 | $self->send_chunks( |
---|
269 | $high_params |
---|
270 | ); |
---|
271 | |
---|
272 | $status_line = $high_params->{message}; |
---|
273 | last if !$high_params->{ok}; |
---|
274 | |
---|
275 | } |
---|
276 | } |
---|
277 | else { |
---|
278 | $form = { |
---|
279 | method => 'pwg.images.setInfo', |
---|
280 | image_id => $image_id, |
---|
281 | categories => $self->categories, |
---|
282 | name => $site_image_name, |
---|
283 | rank => $self->rank, |
---|
284 | author => $self->site_author, |
---|
285 | comment => $self->site_comment, |
---|
286 | date_creation => $self->site_img_date_creation, |
---|
287 | }; |
---|
288 | } |
---|
289 | |
---|
290 | $result = $self->uagent->post( |
---|
291 | $self->urlbase.'/ws.php?format=json', |
---|
292 | $form |
---|
293 | ); |
---|
294 | |
---|
295 | $status = $result->is_success; |
---|
296 | $status_line = $result->status_line; |
---|
297 | last; |
---|
298 | } |
---|
299 | |
---|
300 | return ( $status, $status_line); |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | sub AddCategories{ |
---|
305 | my ( $self, $name, $parentid ) = @_; |
---|
306 | |
---|
307 | my $form = { |
---|
308 | method => 'pwg.categories.add', |
---|
309 | name => $name, |
---|
310 | parent => $parentid, |
---|
311 | |
---|
312 | }; |
---|
313 | |
---|
314 | my $result = $self->uagent->post( |
---|
315 | $self->urlbase.'/ws.php?format=json', |
---|
316 | $form |
---|
317 | ); |
---|
318 | |
---|
319 | my $content = {}; |
---|
320 | eval { |
---|
321 | $content = from_json( |
---|
322 | $result->content |
---|
323 | ); |
---|
324 | }; |
---|
325 | |
---|
326 | return ( $result->is_success, $result->status_line, $content ); |
---|
327 | |
---|
328 | } |
---|
329 | |
---|
330 | sub SetInfoCategories{ |
---|
331 | my ( $self, $name, $comment, $parentid ) = @_; |
---|
332 | |
---|
333 | my $form = { |
---|
334 | method => 'pwg.categories.setInfo', |
---|
335 | name => $name, |
---|
336 | comment => $comment, |
---|
337 | category_id => $parentid, |
---|
338 | |
---|
339 | }; |
---|
340 | |
---|
341 | my $result = $self->uagent->post( |
---|
342 | $self->urlbase.'/ws.php?format=json', |
---|
343 | $form |
---|
344 | ); |
---|
345 | |
---|
346 | my $content = {}; |
---|
347 | eval { |
---|
348 | $content = from_json( |
---|
349 | $result->content |
---|
350 | ); |
---|
351 | }; |
---|
352 | |
---|
353 | return ( $result->is_success, $result->status_line, $content ); |
---|
354 | |
---|
355 | } |
---|
356 | |
---|
357 | |
---|
358 | sub send_chunks { |
---|
359 | my ( $self, $params ) = @_; |
---|
360 | |
---|
361 | my $content = read_file( |
---|
362 | $params->{filepath}, |
---|
363 | binmode => ':raw', |
---|
364 | ); |
---|
365 | |
---|
366 | my $content_length = length($content); |
---|
367 | my $nb_chunks = ceil($content_length / $self->chunk_size); |
---|
368 | |
---|
369 | my $chunk_pos = 0; |
---|
370 | my $chunk_id = 1; |
---|
371 | while ($chunk_pos < $content_length) { |
---|
372 | my $chunk = substr( |
---|
373 | $content, |
---|
374 | $chunk_pos, |
---|
375 | $self->chunk_size |
---|
376 | ); |
---|
377 | $chunk_pos += $self->chunk_size; |
---|
378 | |
---|
379 | my $response = $self->uagent->post( |
---|
380 | $self->urlbase.'/ws.php?format=json', |
---|
381 | { |
---|
382 | method => 'pwg.images.addChunk', |
---|
383 | data => encode_base64($chunk), |
---|
384 | original_sum => $params->{original_sum}, |
---|
385 | position => $chunk_id, |
---|
386 | type => $params->{type}, |
---|
387 | } |
---|
388 | ); |
---|
389 | |
---|
390 | $params->{ok} = 1; |
---|
391 | if ($response->code != 200) { |
---|
392 | printf("response code : %u\n", $response->code); |
---|
393 | printf("response message : %s\n", $response->message); |
---|
394 | $params->{ok} = 0; |
---|
395 | $params->{message} = $response->message; |
---|
396 | $params->{code} = $response->code; |
---|
397 | last; |
---|
398 | } |
---|
399 | |
---|
400 | $chunk_id++; |
---|
401 | } |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | 1; |
---|
406 | |
---|