Permalink
Browse files

Implement simple update of existing fgallery

This fails when sanitizing filenames results in duplicates (which are
then resolved by appending "_$n"). As long as your gallery has files
with names such as IMG_123.JPG you're fine. Problematic would be a
gallery containing both IMG_123 1.JPG and IMG_123_1.JPG -- the first
one gets sanitized to IMG_123_1.JPG, and the second one then has a
name collision so it gets renamed to IMG_123_1_1.JPG. In this case, if
IMG_123 1.JPG is an existing file in your gallery and IMG_123_1.JPG
is a new file in your gallery, the update will skip it since the code
checks whether IMG_123_1.JPG exists (it does) without knowing that it
should be checking IMG_123_1_1.JPG instead. In order to fix this, we
would have to store more information in data.json (or write a new file
containing this information).
  • Loading branch information...
kensanata committed Oct 25, 2017
1 parent aea3c2a commit b6db8addc7361084b9440fa6c85866b5bb87ad92
Showing with 43 additions and 6 deletions.
  1. +43 −6 fgallery
View
@@ -33,7 +33,7 @@ use Time::Piece;
my $JSON_cls = eval { require Cpanel::JSON::XS; "Cpanel::JSON::XS"; } //
eval { require JSON::PP; "JSON::PP"; } //
fatal("either Cpanel::JSON::XS or JSON::PP is required");
$JSON_cls->import(qw{encode_json});
$JSON_cls->import(qw{encode_json decode_json});
# constants
our $VERSION = "1.8.2";
@@ -69,6 +69,7 @@ my @capmethods = ("txt", "xmp", "exif");
my $galleryTitle = "";
my $galleryDescription = "";
my $galleryUrl = "";
my $update = 0;
# support functions
sub fatal
@@ -241,7 +242,7 @@ sub encode
my ($msg) = @_;
lock($cnt);
my $pc = ($cnt++ / $total * 100);
my $pc = ($cnt++ * 100 / $total);
my $str = sprintf("%s %2.0f%%", $act, $pc);
if(defined($msg)) {
$str .= ": " . $msg;
@@ -353,6 +354,7 @@ sub print_help
-k do not modify files, keep original
-t do not time-sort
-r reverse album order
-u update existing gallery
-p do not automatically include full-sized panoramas
-d do not generate a full album download
-f improve thumbnail cutting by performing face detection
@@ -391,6 +393,7 @@ my ($ret, @ARGS) = GetOptions(
'r' => sub { $revsort = 1; },
's' => sub { $slim = 1; },
't' => sub { $timesort = 0; },
'u' => sub { $update = 1; },
'v' => sub { $verbose = 1; },
'max-full=s' => sub { @maxfull = parse_wh(@_); },
'max-thumb=s' => sub { @maxthumb = parse_wh(@_); },
@@ -503,10 +506,12 @@ my $backblur = int(($minthumb[0] + $minthumb[1]) / 2 * 0.1);
my @backsize = (int($minthumb[0] * 4), int($minthumb[1] * 3));
# cleanup target paths
for my $path("$out/thumbs", "$out/blurs", "$out/imgs", "$out/files")
{
remove_tree($path);
make_path($path);
unless ($update) {
for my $path("$out/thumbs", "$out/blurs", "$out/imgs", "$out/files")
{
remove_tree($path);
make_path($path);
}
}
# disable sub-process parallelism when threading ourselves
@@ -516,6 +521,22 @@ if($workers)
$ENV{OMP_NUM_THREADS} = 1;
}
# updates require data from the JSON file
my @odata;
my %ofiles;
if ($update) {
# FIXME: use slurp but avoid wide character warning?
my $fn = "$out/data.json";
open(my $fd, '<:raw', $fn) or fatal("cannot read $fn: $!");
local $/;
my $json = <$fd>;
my $data = decode_json($json);
@odata = @{$data->{data}};
foreach(@odata) {
$ofiles{$_->{img}->[0]} = 1; # this is $fdata{img}
}
}
# 1st pass: extract/prepare input file data
sub analyze_file
{
@@ -535,6 +556,14 @@ sub analyze_file
$sane =~ s/[^\w\-]/_/gu;
my $root = $sane;
# FIXME: need a way to identify existing files with the
# autoincrement below. For the moment, this breaks for all filenames
# that result in duplicates after sanitzing.
if ($update && exists $ofiles{"imgs/$root.$ext"}) {
return undef;
}
for(my $c = 0;; ++$c)
{
my $tmp = "$out/imgs/$root.$ext";
@@ -618,6 +647,10 @@ for(my $n = 0; $n <= $#files;)
splice(@aprops, $n, 1);
}
unless(@files) {
fatal("nothing to be done");
}
# gather some statistics
my $amp = 0;
my $ostamp = 0;
@@ -884,6 +917,10 @@ foreach my $fdata(@adata)
push(@{$json{data}}, \%data);
}
if ($update) {
$json{data} = [sort { $a->{stamp} <=> $b->{stamp} } @{$json{data}}, @odata];
}
my $fd;
unless(open($fd, '>:raw', "$out/data.json")) {
fatal("cannot write data file: $!");

0 comments on commit b6db8ad

Please sign in to comment.