- Timestamp:
- Mar 13, 2009, 10:15:21 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extras/pLoader/trunk/src/Uploader/PWG/WebServices.pm
r3176 r3194 22 22 use strict; 23 23 use warnings; 24 use MIME::Base64 ;24 use MIME::Base64 qw(encode_base64); 25 25 use JSON; 26 26 use LWP::UserAgent; … … 29 29 use File::Slurp; 30 30 use Encode qw/encode decode/; 31 use POSIX qw(ceil floor); 31 32 use base qw/ 32 33 Uploader::Object … … 57 58 action_result 58 59 upload_high 60 chunk_size 59 61 / 60 62 ); … … 81 83 $self->password( 82 84 $self->{site_password} 85 ); 86 87 $self->chunk_size( 88 $self->{chunk_size}||500_000 83 89 ); 84 90 … … 159 165 sub UploadImage { 160 166 my ( $self ) = @_; 161 162 163 164 my $file_content = encode_base64(165 read_file(166 $self->site_resized_file,167 binmode => ':raw',168 )169 );170 167 171 168 my $file_sum = file_md5_hex( … … 173 170 ); 174 171 175 my $thumbnail_content = encode_base64(176 read_file(177 $self->site_thumb_file,178 binmode => ':raw',179 )180 );181 172 182 173 my $thumbnail_sum = file_md5_hex( … … 198 189 ); 199 190 200 #print Dumper $result;201 191 my $hresult = {}; 202 192 eval { … … 205 195 ); 206 196 }; 207 #print Dumper $hresult; 197 208 198 my $image_id = $hresult->{result}{$original_sum}; 209 199 if(!defined($image_id)){ 200 $self->send_chunks( 201 { 202 filepath => $self->site_resized_file, 203 type => 'file', 204 original_sum => $original_sum, 205 } 206 ); 207 208 $self->send_chunks( 209 { 210 filepath => $self->site_thumb_file, 211 type => 'thumb', 212 original_sum => $original_sum, 213 } 214 ); 215 216 210 217 $form = { 211 218 method => 'pwg.images.add', 212 219 original_sum => $original_sum, 213 220 file_sum => $file_sum, 214 file_content => $file_content,215 221 thumbnail_sum => $thumbnail_sum, 216 thumbnail_content => $thumbnail_content,217 222 categories => $self->categories, 218 223 name => $self->site_image_name, … … 225 230 226 231 if($self->upload_high){ 227 $form->{high_content} = encode_base64( 228 read_file( 229 $self->site_high_file, 230 binmode => ':raw', 231 ) 232 ); 233 234 $form->{high_sum} = $original_sum; 232 $self->send_chunks( 233 { 234 filepath => $self->site_high_file, 235 type => 'high', 236 original_sum => $original_sum, 237 } 238 ); 239 240 $form->{high_sum} = $original_sum; 235 241 } 236 242 } … … 274 280 return ( $result->is_success, $result->status_line ); 275 281 276 } 282 } 283 284 285 sub send_chunks { 286 my ( $self, $params ) = @_; 287 288 my $content = encode_base64( 289 read_file( 290 $params->{filepath}, 291 binmode => ':raw', 292 ) 293 ); 294 295 my $content_length = length($content); 296 my $nb_chunks = ceil($content_length / $self->chunk_size); 297 298 my $chunk_pos = 0; 299 my $chunk_id = 1; 300 while ($chunk_pos < $content_length) { 301 my $chunk = substr( 302 $content, 303 $chunk_pos, 304 $self->chunk_size 305 ); 306 $chunk_pos += $self->chunk_size; 307 308 my $response = $self->uagent->post( 309 $self->urlbase.'/ws.php?format=json', 310 { 311 method => 'pwg.images.addChunk', 312 data => $chunk, 313 original_sum => $params->{original_sum}, 314 position => $chunk_id, 315 type => $params->{type}, 316 } 317 ); 318 319 #printf( 320 # 'chunk %05u of %05u for %s "%s"'."\n", 321 # $chunk_id, 322 # $nb_chunks, 323 # $params->{type}, 324 # $params->{filepath} 325 #); 326 if ($response->code != 200) { 327 printf("response code : %u\n", $response->code); 328 printf("response message : %s\n", $response->message); 329 } 330 331 $chunk_id++; 332 } 333 } 334 335 277 336 1; 278 337
Note: See TracChangeset
for help on using the changeset viewer.