[2597] | 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; |
---|
[3194] | 24 | use MIME::Base64 qw(encode_base64); |
---|
[2597] | 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; |
---|
[3230] | 30 | use Encode qw/encode decode is_utf8/; |
---|
[3194] | 31 | use POSIX qw(ceil floor); |
---|
[2597] | 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 |
---|
[2661] | 44 | qry_add_categories |
---|
[2597] | 45 | items |
---|
| 46 | tags |
---|
| 47 | categories |
---|
[2696] | 48 | site_high_file |
---|
| 49 | site_resized_file |
---|
| 50 | site_thumb_file |
---|
| 51 | site_image_name |
---|
[2597] | 52 | rank |
---|
[2696] | 53 | site_author |
---|
| 54 | site_comment |
---|
| 55 | site_img_date_creation |
---|
[2618] | 56 | uagent_response |
---|
| 57 | login_result |
---|
| 58 | action_result |
---|
[2696] | 59 | upload_high |
---|
[3194] | 60 | chunk_size |
---|
[2597] | 61 | / |
---|
| 62 | ); |
---|
| 63 | |
---|
| 64 | $|=1; |
---|
| 65 | |
---|
| 66 | sub 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 | |
---|
[3194] | 87 | $self->chunk_size( |
---|
| 88 | $self->{chunk_size}||500_000 |
---|
| 89 | ); |
---|
| 90 | |
---|
[2597] | 91 | $self->qry_list_categories( sprintf |
---|
| 92 | "%s/ws.php?format=json&method=%s&recursive=%s", |
---|
| 93 | $self->urlbase, |
---|
[3176] | 94 | # 'pwg.categories.getAdminList', |
---|
| 95 | 'pwg.categories.getList', |
---|
[2597] | 96 | 'true', |
---|
| 97 | ); |
---|
| 98 | |
---|
| 99 | my $form = { |
---|
| 100 | method => 'pwg.session.login', |
---|
| 101 | username => $self->username, |
---|
| 102 | password => $self->password, |
---|
| 103 | }; |
---|
| 104 | |
---|
[2618] | 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 | |
---|
[2597] | 132 | } |
---|
| 133 | |
---|
| 134 | sub 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 | |
---|
| 165 | sub UploadImage { |
---|
| 166 | my ( $self ) = @_; |
---|
| 167 | |
---|
[3230] | 168 | my $file_sum; |
---|
| 169 | |
---|
| 170 | eval { |
---|
| 171 | $file_sum = file_md5_hex( |
---|
| 172 | encode('iso-8859-1', $self->site_resized_file) |
---|
| 173 | ); |
---|
| 174 | }; |
---|
[2597] | 175 | |
---|
| 176 | |
---|
| 177 | my $thumbnail_sum = file_md5_hex( |
---|
[3230] | 178 | encode('iso-8859-1', $self->site_thumb_file), |
---|
[2597] | 179 | ); |
---|
| 180 | |
---|
[3158] | 181 | my $original_sum = file_md5_hex( |
---|
[3230] | 182 | encode('iso-8859-1', $self->site_high_file) |
---|
[3158] | 183 | ); |
---|
[2696] | 184 | |
---|
[2597] | 185 | my $form = { |
---|
[3162] | 186 | method => 'pwg.images.exist', |
---|
| 187 | md5sum_list => $original_sum, |
---|
[2597] | 188 | }; |
---|
| 189 | |
---|
[3162] | 190 | my $result = $self->uagent->post( |
---|
| 191 | $self->urlbase.'/ws.php?format=json', |
---|
| 192 | $form |
---|
| 193 | ); |
---|
[2728] | 194 | |
---|
[3162] | 195 | my $hresult = {}; |
---|
| 196 | eval { |
---|
| 197 | $hresult = from_json( |
---|
| 198 | $result->{_content} |
---|
| 199 | ); |
---|
| 200 | }; |
---|
[3194] | 201 | |
---|
[3162] | 202 | my $image_id = $hresult->{result}{$original_sum}; |
---|
| 203 | if(!defined($image_id)){ |
---|
[3194] | 204 | $self->send_chunks( |
---|
| 205 | { |
---|
[3230] | 206 | filepath => encode('iso-8859-1', $self->site_resized_file), |
---|
[3194] | 207 | type => 'file', |
---|
| 208 | original_sum => $original_sum, |
---|
| 209 | } |
---|
| 210 | ); |
---|
| 211 | |
---|
| 212 | $self->send_chunks( |
---|
| 213 | { |
---|
[3230] | 214 | filepath => encode('iso-8859-1', $self->site_thumb_file), |
---|
[3194] | 215 | type => 'thumb', |
---|
| 216 | original_sum => $original_sum, |
---|
| 217 | } |
---|
| 218 | ); |
---|
| 219 | |
---|
| 220 | |
---|
[3162] | 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){ |
---|
[3194] | 236 | $self->send_chunks( |
---|
| 237 | { |
---|
[3230] | 238 | filepath => encode('iso-8859-1', $self->site_high_file), |
---|
[3194] | 239 | type => 'high', |
---|
| 240 | original_sum => $original_sum, |
---|
| 241 | } |
---|
| 242 | ); |
---|
| 243 | |
---|
| 244 | $form->{high_sum} = $original_sum; |
---|
[3162] | 245 | } |
---|
[2696] | 246 | } |
---|
[3162] | 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 | } |
---|
[2696] | 259 | |
---|
[3162] | 260 | $result = $self->uagent->post( |
---|
[2597] | 261 | $self->urlbase.'/ws.php?format=json', |
---|
| 262 | $form |
---|
| 263 | ); |
---|
| 264 | |
---|
| 265 | return ( $result->is_success, $result->status_line ); |
---|
| 266 | } |
---|
| 267 | |
---|
[2661] | 268 | |
---|
| 269 | sub 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 | |
---|
[3194] | 286 | } |
---|
| 287 | |
---|
| 288 | |
---|
| 289 | sub send_chunks { |
---|
| 290 | my ( $self, $params ) = @_; |
---|
| 291 | |
---|
[3268] | 292 | my $content = read_file( |
---|
| 293 | $params->{filepath}, |
---|
| 294 | binmode => ':raw', |
---|
[3194] | 295 | ); |
---|
| 296 | |
---|
| 297 | my $content_length = length($content); |
---|
| 298 | my $nb_chunks = ceil($content_length / $self->chunk_size); |
---|
| 299 | |
---|
| 300 | my $chunk_pos = 0; |
---|
| 301 | my $chunk_id = 1; |
---|
| 302 | while ($chunk_pos < $content_length) { |
---|
| 303 | my $chunk = substr( |
---|
| 304 | $content, |
---|
| 305 | $chunk_pos, |
---|
| 306 | $self->chunk_size |
---|
| 307 | ); |
---|
| 308 | $chunk_pos += $self->chunk_size; |
---|
| 309 | |
---|
| 310 | my $response = $self->uagent->post( |
---|
| 311 | $self->urlbase.'/ws.php?format=json', |
---|
| 312 | { |
---|
| 313 | method => 'pwg.images.addChunk', |
---|
[3268] | 314 | data => encode_base64($chunk), |
---|
[3194] | 315 | original_sum => $params->{original_sum}, |
---|
| 316 | position => $chunk_id, |
---|
| 317 | type => $params->{type}, |
---|
| 318 | } |
---|
| 319 | ); |
---|
| 320 | |
---|
| 321 | #printf( |
---|
| 322 | # 'chunk %05u of %05u for %s "%s"'."\n", |
---|
| 323 | # $chunk_id, |
---|
| 324 | # $nb_chunks, |
---|
| 325 | # $params->{type}, |
---|
| 326 | # $params->{filepath} |
---|
| 327 | #); |
---|
| 328 | if ($response->code != 200) { |
---|
| 329 | printf("response code : %u\n", $response->code); |
---|
| 330 | printf("response message : %s\n", $response->message); |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | $chunk_id++; |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | |
---|
[2597] | 338 | 1; |
---|
| 339 | |
---|