source: extras/pLoader/trunk/src/Uploader/PWG/WebServices.pm @ 3230

Last change on this file since 3230 was 3230, checked in by ronosman, 15 years ago

Bug 952. Transfert of filenames with accentuated characters is aborted.

  • Property svn:eol-style set to LF
File size: 8.5 KB
Line 
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# +-----------------------------------------------------------------------+
20package Uploader::PWG::WebServices;
21 
22use strict;
23use warnings;
24use MIME::Base64 qw(encode_base64); 
25use JSON;
26use LWP::UserAgent;
27use Data::Dumper;
28use Digest::MD5::File qw/file_md5_hex md5_hex/;
29use File::Slurp;
30use Encode qw/encode decode is_utf8/;
31use POSIX qw(ceil floor);
32use 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
66sub Init {
67    my ( $self ) = @_ ;
68 
69    $self->uagent(
70        LWP::UserAgent->new
71    );
72   
73    $self->uagent->cookie_jar({});     
74
75    $self->urlbase(
76        $self->{site_url}
77    );
78   
79    $self->username(
80        $self->{site_username}
81    );
82   
83    $self->password(
84        $self->{site_password}
85    );
86   
87    $self->chunk_size(
88        $self->{chunk_size}||500_000
89    );
90   
91    $self->qry_list_categories( sprintf
92        "%s/ws.php?format=json&method=%s&recursive=%s",
93        $self->urlbase,
94#        'pwg.categories.getAdminList',
95        'pwg.categories.getList',
96        'true',
97    );
98
99    my $form = {
100        method => 'pwg.session.login',
101        username => $self->username,
102        password => $self->password,
103    };
104 
105    $self->uagent_response(
106        $self->uagent->post(
107            $self->urlbase.'/ws.php?format=json',
108            $form
109        )
110    );
111   
112    my $hresult = {} ;
113
114    if($self->uagent_response->is_success){
115        eval {
116            $hresult = from_json(
117                $self->uagent_response->content
118            );
119        };
120    }
121    else{
122        $hresult = {
123            'message' => $self->uagent_response->message,
124            'stat'    => 'fail',
125        };
126    }
127       
128    $self->login_result(
129        $hresult
130    );
131
132}
133
134sub GetCategories {
135    my ( $self ) = @_;
136
137    my $result;
138    eval {
139        $result = $self->uagent->get(
140            $self->qry_list_categories
141        );
142    };
143
144    if($@){
145        printf("An error occured in query execution %s\n%s",
146            $self->qry_list_categories,
147            $@,
148        );     
149    }
150    my $hresult;
151   
152    eval {
153        $hresult = from_json(
154            $result->content
155        );
156    };
157   
158    $hresult ||= {};
159    my $categories = $hresult->{result}{categories};
160
161
162    $categories;       
163}
164
165sub UploadImage {
166    my ( $self ) = @_;
167
168    my $file_sum;
169
170    eval {
171        $file_sum = file_md5_hex(
172            encode('iso-8859-1', $self->site_resized_file)
173        );
174    };
175 
176
177    my $thumbnail_sum = file_md5_hex(
178        encode('iso-8859-1', $self->site_thumb_file),
179    );
180
181    my $original_sum = file_md5_hex(
182        encode('iso-8859-1', $self->site_high_file)
183    );
184
185    my $form = {
186        method            => 'pwg.images.exist',
187        md5sum_list       => $original_sum,
188    };
189
190    my $result = $self->uagent->post(
191        $self->urlbase.'/ws.php?format=json',
192        $form
193    );
194
195    my $hresult = {};
196        eval {
197            $hresult = from_json(
198                $result->{_content}
199            );
200        };
201
202    my $image_id = $hresult->{result}{$original_sum};
203    if(!defined($image_id)){
204        $self->send_chunks(
205            {
206                filepath => encode('iso-8859-1', $self->site_resized_file),
207                type => 'file',
208                original_sum => $original_sum,
209            }
210        );
211
212        $self->send_chunks(
213            {
214                filepath => encode('iso-8859-1', $self->site_thumb_file),
215                type => 'thumb',
216                original_sum => $original_sum,
217            }
218        );
219       
220       
221        $form = {
222            method            => 'pwg.images.add',
223            original_sum      => $original_sum,
224            file_sum          => $file_sum,
225            thumbnail_sum     => $thumbnail_sum,
226            categories        => $self->categories,
227            name              => $self->site_image_name,
228            rank              => $self->rank,
229            author            => $self->site_author,
230            comment           => $self->site_comment,
231            date_creation     => $self->site_img_date_creation,
232           
233        };
234   
235        if($self->upload_high){
236            $self->send_chunks(
237                {
238                    filepath => encode('iso-8859-1', $self->site_high_file),
239                    type => 'high',
240                    original_sum => $original_sum,
241                }
242            );
243
244            $form->{high_sum}     = $original_sum;
245        }
246    }
247    else {
248        $form = {
249            method            => 'pwg.images.setInfo',
250            image_id          => $image_id,
251            categories        => $self->categories,
252            name              => $self->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
260    $result = $self->uagent->post(
261        $self->urlbase.'/ws.php?format=json',
262        $form
263    );
264
265    return ( $result->is_success, $result->status_line );
266}
267 
268
269sub AddCategories{
270    my ( $self, $name, $parentid ) = @_;
271
272    my $form = {
273        method            => 'pwg.categories.add',
274        name              => $name,
275        parent            => $parentid,
276       
277    };
278
279    my $result = $self->uagent->post(
280        $self->urlbase.'/ws.php?format=json',
281        $form
282    );
283
284    return ( $result->is_success, $result->status_line );
285       
286}
287
288
289sub send_chunks {
290    my ( $self, $params ) = @_;
291
292    my $content = encode_base64(
293        read_file(
294            $params->{filepath},
295            binmode => ':raw',
296        )
297    );
298
299    my $content_length = length($content);
300    my $nb_chunks = ceil($content_length / $self->chunk_size);
301
302    my $chunk_pos = 0;
303    my $chunk_id = 1;
304    while ($chunk_pos < $content_length) {
305        my $chunk = substr(
306            $content,
307            $chunk_pos,
308            $self->chunk_size
309        );
310        $chunk_pos += $self->chunk_size;
311
312        my $response = $self->uagent->post(
313            $self->urlbase.'/ws.php?format=json',
314            {
315                method       => 'pwg.images.addChunk',
316                data         => $chunk,
317                original_sum => $params->{original_sum},
318                position     => $chunk_id,
319                type         => $params->{type},
320            }
321        );
322
323        #printf(
324        #    'chunk %05u of %05u for %s "%s"'."\n",
325        #    $chunk_id,
326        #    $nb_chunks,
327        #    $params->{type},
328        #    $params->{filepath}
329        #);
330        if ($response->code != 200) {
331            printf("response code    : %u\n", $response->code);
332            printf("response message : %s\n", $response->message);
333        }
334
335        $chunk_id++;
336    }
337}
338
339 
3401;
341   
Note: See TracBrowser for help on using the repository browser.