Changeset 18577


Ignore:
Timestamp:
Oct 9, 2012, 4:35:26 PM (12 years ago)
Author:
plg
Message:

add a local cache system, to avoid problem of photo/album name matching with accents

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/piwigo_import_tree/piwigo_import_tree.pl

    r15736 r18577  
    88
    99# make it compatible with Windows, but breaks Linux
    10 use utf8;
     10#use utf8;
    1111
    1212use File::Find;
     
    1717use Getopt::Long;
    1818use Encode qw/is_utf8 decode/;
     19use Time::HiRes qw/gettimeofday tv_interval/;
     20use Digest::MD5 qw/md5 md5_hex/;
    1921
    2022my %opt = ();
     
    2931          define=s%
    3032          quiet
     33          only_write_cache
    3134      /
    3235);
     
    200203    }
    201204
     205    print '[album '.$params{album_id}.'] '.$params{path}.' upload starts... ';
     206    $| = 1;
     207    my $t1 = [gettimeofday];
     208
    202209    piwigo_login();
    203210    my $response = $ua->post(
     
    206213        'Content_Type' => 'form-data',
    207214    );
     215
     216    my $elapsed = tv_interval($t1);
     217    print ' upload completed ('.sprintf('%u ms', $elapsed * 1000).')'."\n";
    208218}
    209219
     
    229239        foreach my $dir (split '/', $path) {
    230240            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                }
    233250                $piwigo_albums{$up_dir.$dir} = $id;
    234251            }
     
    254271        }
    255272
    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
    257282        add_photo(path => $File::Find::name, album_id => $album_id);
     283        cache_add_photo(path => $File::Find::name, dir => $album_key);
    258284        # print 'dirname = '.dirname($path)."\n\n";
    259285    }
    260286}
     287
     288sub 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
     313sub 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
     340sub 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
     353sub 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.