Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/LANraragi/Controller/Api/Archive.pm
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ sub add_toc {
my $page = $self->req->param('page');
my $title = $self->req->param('title');

unless ( defined $page && defined $title ) {
return render_api_response( $self, "add_toc", "Missing page and/or title." );
}

return unless exec_with_lock(
$self,
"archive-write:$id",
Expand All @@ -403,6 +407,10 @@ sub remove_toc {

my $page = $self->req->param('page');

unless ( defined $page ) {
return render_api_response( $self, "remove_toc", "Please specify a page to remove" );
}

return unless exec_with_lock(
$self,
"archive-write:$id",
Expand Down
15 changes: 3 additions & 12 deletions lib/LANraragi/Model/Archive.pm
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,6 @@ sub update_metadata {
sub add_toc_entry {
my ( $id, $page, $title ) = @_;

unless ( defined $page and defined $title ) {
return "Missing page and/or title.";
}

my $redis = LANraragi::Model::Config->get_redis;
my $logger = get_logger( "Archives", "lanraragi" );
my $toc = $redis->hget( $id, "toc" );
Expand All @@ -338,16 +334,15 @@ sub add_toc_entry {
$toc = decode_json($toc);
$toc->{$page} = $title;
$toc = encode_json($toc);
$redis->hset( $id, "toc", $toc );
} catch ($e) {
$logger->warn(
"Error while updating ToC: $e -- Will overwrite with a ToC containing the new data. (This is normal if this ID had no ToC yet.)"
);
$toc = {};
$toc->{$page} = $title;
$toc = encode_json($toc);
$redis->hset( $id, "toc", "{}" );
}
$redis->hset( $id, "toc", $toc );

$redis->quit();
return "";
Expand All @@ -356,10 +351,6 @@ sub add_toc_entry {
sub remove_toc_entry {
my ( $id, $page ) = @_;

unless ( defined $page ) {
return "Please specify a page to remove";
}

my $redis = LANraragi::Model::Config->get_redis;
my $logger = get_logger( "Archives", "lanraragi" );
my $toc = $redis->hget( $id, "toc" );
Expand All @@ -369,11 +360,11 @@ sub remove_toc_entry {
$toc = decode_json($toc);
delete $toc->{$page};
$toc = encode_json($toc);
$redis->hset( $id, "toc", $toc );
} catch ($e) {
$logger->warn("Error while updating ToC: $e -- Will overwrite with a blank ToC.");
$redis->hset( $id, "toc", "{}" );
$toc = "{}";
}
$redis->hset( $id, "toc", $toc );

$redis->quit();
return "";
Expand Down
12 changes: 10 additions & 2 deletions lib/LANraragi/Utils/Database.pm
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,16 @@ sub build_json ( $id, %hash ) {
if ( defined $toc ) {
eval { $toc = decode_json($toc) };

foreach my $page ( keys %$toc ) {
push @chapters, { page => $page + 0, name => $toc->{$page} };
if ( my $decode_error = $@ ) {
get_logger( "Archive", "lanraragi" )->error("Failed to parse ToC JSON for archive $id: $decode_error");
$toc = undef;
}
if ( defined $toc && ref($toc) eq 'HASH' ) {
foreach my $page ( keys %$toc ) {
push @chapters, { page => $page + 0, name => $toc->{$page} };
}
} elsif ( defined $toc ) {
get_logger( "Archive", "lanraragi" )->error("ToC is not a hash: $toc");
}

# Sort chapters by page number
Expand Down
Loading