Skip to content

Commit

Permalink
Added an option to replace existing archive when an existing one is e…
Browse files Browse the repository at this point in the history
…ncountered.
  • Loading branch information
qhua948 committed Feb 18, 2023
1 parent 8909bd2 commit 7519921
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/LANraragi/Controller/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ sub index {
hqthumbpages => $self->LRR_CONF->get_hqthumbpages,
csshead => generate_themes_header($self),
tempsize => get_tempsize
replacedupe => $self->LRR_CONF->get_replacedupe,
);
}

Expand Down Expand Up @@ -84,6 +85,7 @@ sub save_config {
usedatemodified => ( scalar $self->req->param('usedatemodified') ? '1' : '0' ),
enablecryptofs => ( scalar $self->req->param('enablecryptofs') ? '1' : '0' ),
hqthumbpages => ( scalar $self->req->param('hqthumbpages') ? '1' : '0' ),
replacedupe => ( scalar $self->req->param('replacedupe') ? '1' : '0' ),
);

# Only add newpassword field as password if enablepass = 1
Expand Down
1 change: 1 addition & 0 deletions lib/LANraragi/Model/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,6 @@ sub enable_dateadded { return &get_redis_conf( "usedateadded", "1" ) }
sub use_lastmodified { return &get_redis_conf( "usedatemodified", "0" ) }
sub enable_cryptofs { return &get_redis_conf( "enablecryptofs", "0" ) }
sub get_hqthumbpages { return &get_redis_conf( "hqthumbpages", "0" ) }
sub get_replacedupe { return &get_redis_conf( "replacedupe", "0" ) }

1;
18 changes: 13 additions & 5 deletions lib/LANraragi/Model/Upload.pm
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,31 @@ sub handle_incoming_file {
#that the file it references still exists on the filesystem
my $redis = LANraragi::Model::Config->get_redis;
my $redis_search = LANraragi::Model::Config->get_redis_search;
my $replace_dupe = LANraragi::Model::Config->get_replacedupe;
my $isdupe = $redis->exists($id) && -e $redis->hget( $id, "file" );

# Stop here if file is a dupe.
if ( -e $output_file || $isdupe ) {
# Stop here if file is a dupe and replacement is turned off.
if ((-e $output_file || $isdupe) && !$replace_dupe) {

# Trash temporary file
unlink $tempfile;

# The file already exists
my $suffix = " Enable replace duplicated archive in config to replace old ones."
my $msg =
$isdupe
? "This file already exists in the Library."
: "A file with the same name is present in the Library.";
? "This file already exists in the Library." . $suffix
: "A file with the same name is present in the Library." . $suffix;

return ( 0, $id, $filename, $msg );
}

# If we are replacing an existing one, just remove the old one first.
if ($replace_dupe) {
$logger->debug("Delete archive $id before replacing it.");
LANraragi::Utils::Database::delete_archive( $id );
}

# Add the file to the database ourselves so Shinobu doesn't do it
# This allows autoplugin to be ran ASAP.
my $name = LANraragi::Utils::Database::add_archive_to_redis( $id, $output_file, $redis );
Expand Down Expand Up @@ -100,7 +108,7 @@ sub handle_incoming_file {
}

# Move the file to the content folder.
# Move to a .tmp first in case copy to the content folder takes a while...
# Move to a .upload first in case copy to the content folder takes a while...
move( $tempfile, $output_file . ".upload" );

# Then rename inside the content folder itself to proc Shinobu.
Expand Down
2 changes: 1 addition & 1 deletion lib/LANraragi/Utils/Database.pm
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ sub build_json {
return $arcdata;
}

#Deletes the archive with the given id from redis, and the matching archive file/thumbnail.
# Deletes the archive with the given id from redis, and the matching archive file/thumbnail.
sub delete_archive {

my $id = $_[0];
Expand Down
16 changes: 15 additions & 1 deletion templates/templates_config/config_files.html.tt2
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,18 @@
If you want to clear those flags, click this button. <br>
<br>
</td>
</tr>
</tr>

<tr>
<td class="option-td">
<h2 class="ih">Replace duplicated archive</h2>
</td>
<td class="config-td">
[% IF replacedupe %]
<input id="replacedupe" name="replacedupe" class="fa" type="checkbox" checked> [% ELSE %]
<input id="replacedupe" name="replacedupe" class="fa" type="checkbox"> [% END %]
<label for="replacedupe">
<br>If enabled, LANraragi will replace old archive when a duplicated one (of the same name) is added.
</label>
</td>
</tr>

0 comments on commit 7519921

Please sign in to comment.