Skip to content
Merged
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
154 changes: 154 additions & 0 deletions lib/LANraragi/Controller/Api/Stamp.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package LANraragi::Controller::Api::Stamp;
use Mojo::Base 'Mojolicious::Controller';

use Redis;
use Encode;

use LANraragi::Model::Stamp;
use LANraragi::Utils::Generic qw(render_api_response exec_with_lock exec_with_lock_pure);


sub get_stamp {

my $self = shift->openapi->valid_input or return;
my $stamp_id = $self->stash('id');

my ( $stamp, $err ) = LANraragi::Model::Stamp::get_stamp($stamp_id);

unless ($stamp) {
render_api_response($self, "get_stamp", "The given stamp does not exist.");
return;
}

$self->render( openapi => { result => $stamp } );
}

sub get_stamps_by_page {

my $self = shift->openapi->valid_input or return;
my $id = $self->stash('id');
my $index = $self->stash('index');

my ( $stamps, $err ) = LANraragi::Model::Stamp::get_stamps_by_page($id, $index);

$self->render( openapi => { result => $stamps } );
}

sub get_stamped_pages {

my $self = shift->openapi->valid_input or return;
my $id = $self->stash('id');

my ( $indexes, $err ) = LANraragi::Model::Stamp::get_stamped_pages( $id );

$self->render( openapi => { result => $indexes } );
}

sub add_stamp {

my $self = shift->openapi->valid_input or return;
my $id = $self->stash('id');
my $index = $self->stash('index');
my $content = $self->req->param('content') || "";
my $position = $self->req->param('position') || "";

unless ( defined $index ) {
return render_api_response( $self, "add_stamp", "No specified page for the stamp to attach to." );
}

return unless exec_with_lock(
$self,
"archive-write:$id",
"update_archive",
$id,
sub {
my ( $created_id, $err ) = LANraragi::Model::Stamp::add_stamp( $id, $index, $content, $position );

if ($created_id) {
$self->render(
openapi => {
operation => "add_stamp",
stamp_id => $created_id,
success => 1
}
);
} else {
$self->render(
openapi => {
operation => "add_stamp",
stamp_id => $created_id,
success => 0,
error => $err
}
);
}
}
);
}

sub update_stamp {

my $self = shift->openapi->valid_input or return;
my $stamp_id = $self->stash('id');
my $position = $self->req->param('position') || undef;
my $content = $self->req->param('content') || undef;

return unless exec_with_lock(
$self,
"stamp-write:$stamp_id",
"update_stamp",
$stamp_id,
sub {
my ( $result, $err ) = LANraragi::Model::Stamp::update_stamp( $stamp_id, $content, $position );

if ($result) {
my %stamp = LANraragi::Model::Stamp::get_stamp( $stamp_id );
my $successMessage = "Updated stamp \"$stamp_id\"!";

render_api_response( $self, "update_stamp", undef, $successMessage );
} else {
render_api_response( $self, "update_stamp", $err );
}
}
);

}

sub delete_stamp {

my $self = shift->openapi->valid_input or return;
my $stamp_id = $self->stash('id');

my ( $result, $id ) = LANraragi::Model::Stamp::get_stamp_archive_id($stamp_id);

if ( $result ) {
my ( $acquired, $response ) = exec_with_lock_pure(
[ "archive-write:$id", "stamp-write:$stamp_id" ],
sub {
my ( $result, $err ) = LANraragi::Model::Stamp::remove_stamp($stamp_id);

if ( $result ) {
render_api_response( $self, "delete_stamp" );
} else {
render_api_response( $self, "delete_stamp", $err );
}
}
);

if ( !$acquired ) {
$self->render(
json => {
operation => "delete_stamp",
success => 0,
error => "Locked resource"
},
status => 423
);
}
} else {
render_api_response( $self, "delete_stamp", $id );
}
}

1;

16 changes: 16 additions & 0 deletions lib/LANraragi/Model/Archive.pm
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,22 @@ sub delete_archive ($id) {
LANraragi::Model::Category::remove_from_category( $catid, $id );
}

# Remove Stamps
my $stamps = $redis->hget( $id, "stamps" );
my @stamps;

if ( $redis->hexists( $id, "stamps" )) {
eval { @stamps = @{ decode_json($stamps) } };
if ($@) {
die;
}
foreach my $stamp ( @stamps ) {
$redis->del($stamp);
}
} else {
# Stamps attribute was not set, do nothing.
}

$redis->del($id);
$redis->quit();

Expand Down
54 changes: 52 additions & 2 deletions lib/LANraragi/Model/Backup.pm
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,30 @@ sub build_backup_JSON {
push @{ $backup{tankoubons} }, \%tank;
}

# Backup stamps
my @stamp_ids = $redis->keys('STAMPS_*');

foreach my $stamp_id (@stamp_ids) {
eval {
my %stamp_hash = $redis->hgetall($stamp_id);
my ( $content, $position, $archive_id ) = @stamp_hash{qw(content position archive_id)};

( $_ = redis_decode($_) ) for ( $content, $position, $archive_id );
( $_ = trim_CRLF($_) ) for ( $content, $position, $archive_id );

my %stamp = (
stamp_id => $stamp_id,
content => $content,
position => $position,
archive_id => $archive_id
);

push @{ $backup{stamps} }, \%stamp;
};

$logger->trace("Backing up stamp $stamp_id: $@");
}

# Backup archives themselves next
my @keys = $redis->keys('????????????????????????????????????????'); #40-character long keys only => Archive IDs

Expand All @@ -80,7 +104,7 @@ sub build_backup_JSON {

eval {
my %hash = $redis->hgetall($id);
my ( $name, $title, $tags, $summary, $thumbhash ) = @hash{qw(name title tags summary thumbhash)};
my ( $name, $title, $tags, $summary, $thumbhash, $stamps ) = @hash{qw(name title tags summary thumbhash stamps)};

( $_ = redis_decode($_) ) for ( $name, $title, $tags, $summary );
( $_ = trim_CRLF($_) ) for ( $name, $title, $tags, $summary );
Expand All @@ -92,7 +116,8 @@ sub build_backup_JSON {
tags => $tags,
summary => $summary,
thumbhash => $thumbhash,
filename => $name
filename => $name,
stamps => $stamps
);

push @{ $backup{archives} }, \%arc;
Expand Down Expand Up @@ -171,6 +196,31 @@ sub restore_from_JSON {
$redis->hset( $id, "thumbhash", $thumbhash );
}

if ( defined $archive->{"stamps"} ) {
my $stamps = redis_encode( $archive->{"stamps"} );
$redis->hset( $id, "stamps", $stamps );
} else {
$redis->hset( $id, "stamps", "[]" );
}

}
}

foreach my $stamp ( @{ $json->{stamps} } ) {
my $stamp_id = $stamp->{"stamp_id"};

my $content = $stamp->{"content"};
my $position = $stamp->{"position"};
my $archive_id = $stamp->{"archive_id"};

#If the archive exists, restore metadata.
if ( $redis->exists($archive_id) ) {

( $_ = redis_encode($_) ) for ( $content, $position, $archive_id );

$redis->hset( $stamp_id, "content", $content);
$redis->hset( $stamp_id, "position", $position);
$redis->hset( $stamp_id, "archive_id", $archive_id);
}
}

Expand Down
Loading
Loading