Changeset 18577
- Timestamp:
- Oct 9, 2012, 4:35:26 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/piwigo_import_tree/piwigo_import_tree.pl
r15736 r18577 8 8 9 9 # make it compatible with Windows, but breaks Linux 10 use utf8;10 #use utf8; 11 11 12 12 use File::Find; … … 17 17 use Getopt::Long; 18 18 use Encode qw/is_utf8 decode/; 19 use Time::HiRes qw/gettimeofday tv_interval/; 20 use Digest::MD5 qw/md5 md5_hex/; 19 21 20 22 my %opt = (); … … 29 31 define=s% 30 32 quiet 33 only_write_cache 31 34 / 32 35 ); … … 200 203 } 201 204 205 print '[album '.$params{album_id}.'] '.$params{path}.' upload starts... '; 206 $| = 1; 207 my $t1 = [gettimeofday]; 208 202 209 piwigo_login(); 203 210 my $response = $ua->post( … … 206 213 'Content_Type' => 'form-data', 207 214 ); 215 216 my $elapsed = tv_interval($t1); 217 print ' upload completed ('.sprintf('%u ms', $elapsed * 1000).')'."\n"; 208 218 } 209 219 … … 229 239 foreach my $dir (split '/', $path) { 230 240 if (not defined $piwigo_albums{$up_dir.$dir}) { 231 print 'album "'.$up_dir.$dir.'" must be created'."\n"; 232 my $id = add_album(name => $dir, parent => $parent_id); 241 my $id = cached_album(dir => $up_dir.$dir); 242 # if the album is not in the cache OR if the id in the cache 243 # matches no album fetched by pwg.categories.getList, then 244 # we have to create the album first 245 if (not defined $id or not grep($_ eq $id, values(%piwigo_albums))) { 246 print 'album "'.$up_dir.$dir.'" must be created'."\n"; 247 $id = add_album(name => $dir, parent => $parent_id); 248 cache_add_album(dir => $up_dir.$dir, id => $id); 249 } 233 250 $piwigo_albums{$up_dir.$dir} = $id; 234 251 } … … 254 271 } 255 272 256 print $File::Find::name.' must be uploaded in "'.$album_key.'" (id='.$album_id.')'."\n"; 273 if (cached_photo(path => $File::Find::name, dir => $album_key)) { 274 return 1; 275 } 276 277 if ($opt{only_write_cache}) { 278 cache_add_photo(path => $File::Find::name, dir => $album_key); 279 return 1; 280 } 281 257 282 add_photo(path => $File::Find::name, album_id => $album_id); 283 cache_add_photo(path => $File::Find::name, dir => $album_key); 258 284 # print 'dirname = '.dirname($path)."\n\n"; 259 285 } 260 286 } 287 288 sub cache_add_photo { 289 my %params = @_; 290 291 if (cached_photo(path => $params{path}, dir => $params{dir})) { 292 if (not $opt{quiet}) { 293 print 'photo is in the cache, no upload'."\n"; 294 } 295 return 1; 296 } 297 298 $params{dir} =~ s{ / }{/}g; 299 300 my $filepath = $params{dir}.'/.piwigo_import_tree.txt'; 301 302 open(my $ofh, '>> '.$filepath) or die 'cannot open file "'.$filepath.'" for writing'; 303 print {$ofh} $conf{base_url}.' '.md5_hex(basename($params{path})); 304 305 if (defined $params{id}) { 306 print {$ofh} ' [id='.$params{id}.']'; 307 } 308 309 print {$ofh} "\n"; 310 close($ofh); 311 } 312 313 sub cached_photo { 314 my %params = @_; 315 316 $params{dir} =~ s{ / }{/}g; 317 318 my $filepath = $params{dir}.'/.piwigo_import_tree.txt'; 319 320 if (not -f $filepath) { 321 return undef; 322 } 323 324 my $photo_id = undef; 325 my $photo_filename_md5 = md5_hex(basename($params{path})); 326 327 open(my $ifh, '<'.$filepath) or die 'cannot open file "'.$filepath.'" for reading'; 328 while (my $line = <$ifh>) { 329 chomp $line; 330 if ($line =~ m/$photo_filename_md5/) { 331 # TODO if needed : search the [id=(\d+)] for photo_id 332 return 1; 333 } 334 } 335 close($ifh); 336 337 return undef; 338 } 339 340 sub cache_add_album { 341 my %params = @_; 342 343 $params{dir} =~ s{ / }{/}g; 344 345 my $filepath = $params{dir}.'/.piwigo_import_tree.txt'; 346 347 open(my $ofh, '>> '.$filepath) or die 'cannot open file "'.$filepath.'" for writing'; 348 print {$ofh} $conf{base_url}.' album_id = '.$params{id}."\n"; 349 print $conf{base_url}.' album_id = '.$params{id}."\n"; 350 close($ofh); 351 } 352 353 sub cached_album { 354 my %params = @_; 355 356 $params{dir} =~ s{ / }{/}g; 357 358 my $filepath = $params{dir}.'/.piwigo_import_tree.txt'; 359 360 if (not -f $filepath) { 361 return undef; 362 } 363 364 my $album_id = undef; 365 366 open(my $ifh, '<'.$filepath) or die 'cannot open file "'.$filepath.'" for reading'; 367 while (my $line = <$ifh>) { 368 chomp $line; 369 if ($line =~ m/album_id = (\d+)/) { 370 $album_id = $1; 371 } 372 } 373 close($ifh); 374 375 print 'directory "'.$params{dir}.'" was found as album '.$album_id."\n"; 376 377 return $album_id; 378 }
Note: See TracChangeset
for help on using the changeset viewer.