Skip to content

Commit

Permalink
LANraragi 0.8.2
Browse files Browse the repository at this point in the history
Merge pull request #535 from Difegue/dev
  • Loading branch information
Difegue authored Nov 22, 2021
2 parents c4fcc49 + 6584cd8 commit de298af
Show file tree
Hide file tree
Showing 83 changed files with 2,726 additions and 1,799 deletions.
12 changes: 12 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
"ecmaVersion": 12,
"sourceType": "module"
},
"globals": {
"LRR": "readonly",
"Common": "readonly",
"Server": "readonly",
"tagger": "readonly",
"marked": "readonly",
"Awesomplete": "readonly",
"tippy": "readonly",
"Index": "readonly",
"IndexTable": "readonly",
"Swiper": "readonly"
},
"rules": {
"func-names": ["error", "never"],
"indent": ["error", 4],
Expand Down
18 changes: 10 additions & 8 deletions lib/LANraragi/Controller/Api/Archive.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Mojo::JSON qw(decode_json);
use Scalar::Util qw(looks_like_number);

use LANraragi::Utils::Generic qw(render_api_response);
use LANraragi::Utils::Database;
use LANraragi::Utils::Database qw(get_archive_json);

use LANraragi::Model::Archive;
use LANraragi::Model::Category;
Expand Down Expand Up @@ -46,7 +46,7 @@ sub serve_metadata {
my $id = check_id_parameter( $self, "metadata" ) || return;
my $redis = $self->LRR_CONF->get_redis;

my $arcdata = LANraragi::Utils::Database::build_archive_JSON( $redis, $id );
my $arcdata = get_archive_json( $redis, $id );
$redis->quit;

if ($arcdata) {
Expand Down Expand Up @@ -113,18 +113,20 @@ sub serve_page {
LANraragi::Model::Archive::serve_page( $self, $id, $path );
}

sub extract_archive {
sub get_file_list {
my $self = shift;
my $id = check_id_parameter( $self, "extract_archive" ) || return;
my $readerjson;
my $id = check_id_parameter( $self, "get_file_list" ) || return;

eval { $readerjson = LANraragi::Model::Reader::build_reader_JSON( $self, $id, "0", "0" ); };
my $force = $self->req->param('force') eq "true" || "0";
my $reader_json;

eval { $reader_json = LANraragi::Model::Reader::build_reader_JSON( $self, $id, $force ); };
my $err = $@;

if ($err) {
render_api_response( $self, "extract_archive", $err );
render_api_response( $self, "get_file_list", $err );
} else {
$self->render( json => decode_json($readerjson) );
$self->render( json => decode_json($reader_json) );
}
}

Expand Down
46 changes: 37 additions & 9 deletions lib/LANraragi/Controller/Api/Search.pm
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package LANraragi::Controller::Api::Search;
use Mojo::Base 'Mojolicious::Controller';

use List::Util qw(min);

use LANraragi::Model::Search;
use LANraragi::Utils::Generic qw(render_api_response);
use LANraragi::Utils::Database qw(invalidate_cache);
use LANraragi::Utils::Database qw(invalidate_cache get_archive_json_multi);

# Undocumented API matching the Datatables spec.
sub handle_datatables {
Expand Down Expand Up @@ -90,21 +92,47 @@ sub clear_cache {
render_api_response( shift, "clear_cache" );
}

# Pull random archives out of the given search
sub get_random_archives {

my $self = shift;
my $redis = $self->LRR_CONF->get_redis();
my $req = $self->req;

my $filter = $req->param('filter');
my $category = $req->param('category') || "";
my $random_count = $req->param('count') || 5;

# Use the search engine to get IDs matching the filter/category selection, with start=-1 to get all data
# This method could be extended later to also use isnew/untagged filters.
my ( $total, $filtered, @ids ) = LANraragi::Model::Search::do_search( $filter, $category, -1, "title", 0, "", "" );
my @random_ids;

$random_count = min( $random_count, scalar(@ids) );

while ( $random_count > 0 ) {
my $random_id = $ids[ int( rand( scalar @ids ) ) ];
next if ( grep { $_ eq $random_id } @random_ids );

push @random_ids, $random_id;
$random_count--;
}

$self->render( json => { data => \@random_ids } );
$redis->quit();
}

# get_datatables_object($draw, $total, $totalsearched, @pagedkeys)
# Creates a Datatables-compatible json from the given data.
sub get_datatables_object {

my ( $draw, $redis, $total, $filtered, @keys ) = @_;

# Get archive data from keys
my @data = ();
foreach my $key (@keys) {
my $arcdata = LANraragi::Utils::Database::build_archive_JSON( $redis, $key->{id} );
# Get IDs from keys
my @ids = map { $_->{id} } @keys;

if ($arcdata) {
push @data, $arcdata;
}
}
# Get archive data
my @data = get_archive_json_multi(@ids);

# Create json object matching the datatables structure
return {
Expand Down
3 changes: 1 addition & 2 deletions lib/LANraragi/Controller/Backup.pm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package LANraragi::Controller::Backup;
use Mojo::Base 'Mojolicious::Controller';

use LANraragi::Utils::Generic qw(generate_themes_selector generate_themes_header);
use LANraragi::Utils::Generic qw(generate_themes_header);
use LANraragi::Model::Backup;

# This action will render a template
Expand Down Expand Up @@ -30,7 +30,6 @@ sub index {
template => "backup",
title => $self->LRR_CONF->get_htmltitle,
descstr => $self->LRR_DESC,
cssdrop => generate_themes_selector,
csshead => generate_themes_header($self),
version => $self->LRR_VERSION
);
Expand Down
174 changes: 110 additions & 64 deletions lib/LANraragi/Controller/Batch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,33 @@ use Mojo::Base 'Mojolicious::Controller';

use Redis;
use Encode;
use Mojo::Util qw(xml_escape);
use Mojo::JSON qw(decode_json);

use LANraragi::Utils::Generic qw(generate_themes_selector generate_themes_header);
use LANraragi::Utils::Database qw(redis_decode);
use LANraragi::Utils::Generic qw(generate_themes_header);
use LANraragi::Utils::Tags qw(rewrite_tags split_tags_to_array restore_CRLF);
use LANraragi::Utils::Database qw(redis_decode get_computed_tagrules);
use LANraragi::Utils::Plugins qw(get_plugins get_plugin get_plugin_parameters);
use LANraragi::Utils::Logging qw(get_logger);

# This action will render a template
sub index {
my $self = shift;
my $redis = $self->LRR_CONF->get_redis();

#Then complete it with the rest from the database.
#40-character long keys only => Archive IDs
my @keys = $redis->keys('????????????????????????????????????????');

#Parse the archive list and build <li> elements accordingly.
my $arclist = "";

#Only show IDs that still have their files present.
foreach my $id (@keys) {
my $zipfile = $redis->hget( $id, "file" );
my $title = $redis->hget( $id, "title" );
$title = redis_decode($title);
$title = xml_escape($title);

if ( -e $zipfile ) {
$arclist .= "<li><input type='checkbox' name='archive' id='$id' class='archive' >";
$arclist .= "<label for='$id'> $title</label></li>";
}
}

$redis->quit();
my $self = shift;

#Build plugin listing
my @pluginlist = get_plugins("metadata");

$self->render(
template => "batch",
arclist => $arclist,
plugins => \@pluginlist,
title => $self->LRR_CONF->get_htmltitle,
descstr => $self->LRR_DESC,
cssdrop => generate_themes_selector,
csshead => generate_themes_header($self),
tagrules => restore_CRLF( $self->LRR_CONF->get_tagrules ),
version => $self->LRR_VERSION
);
}

#Websocket server receiving a list of IDs as a JSON and calling the specified plugin on them.
# Websocket server receiving a list of IDs as a JSON and calling the specified plugin on them.
sub socket {

my $self = shift;
Expand All @@ -72,70 +48,111 @@ sub socket {
message => sub {
my ( $self, $msg ) = @_;

#JSON-decode message and grab the plugin
# JSON-decode message and perform the requested action
my $command = decode_json($msg);
my $operation = $command->{'operation'};
my $pluginname = $command->{"plugin"};
my $id = $command->{"archive"};

my $plugin = get_plugin($pluginname);
unless ($id) {
$client->finish( 1001 => 'No archives provided.' );
return;
}
$logger->debug("Processing $id");

if ( $operation eq "plugin" ) {

#Global arguments can come from the database or the user override
my @args = @{ $command->{"args"} };
my $timeout = $command->{"timeout"} || 0;
my $plugin = get_plugin($pluginname);
unless ($plugin) {
$client->finish( 1001 => 'Plugin not found.' );
return;
}

if ($plugin) {
# Global arguments can come from the database or the user override
my @args = @{ $command->{"args"} };

#If the array is empty(no overrides)
if ( !@args ) {
$logger->debug("No user overrides given.");

# Try getting the saved defaults
@args = get_plugin_parameters($pluginname);
}

# Run plugin with args on id
my $id = $command->{"archive"};
unless ($id) {
$client->finish( 1001 => 'No archives provided.' );
return;
}
$logger->debug("Processing $id");
# Send reply message for completed archive
$client->send( { json => batch_plugin( $id, $plugin, @args ) } );
return;
}

my %plugin_result;
eval { %plugin_result = LANraragi::Model::Plugins::exec_metadata_plugin( $plugin, $id, "", @args ); };
if ( $operation eq "clearnew" ) {
$redis->hset( $id, "isnew", "false" );

if ($@) {
$plugin_result{error} = $@;
}
$client->send(
{ json => {
id => $id,
success => 1,
}
}
);
return;
}

#If the plugin exec returned tags, add them
unless ( exists $plugin_result{error} ) {
LANraragi::Utils::Database::add_tags( $id, $plugin_result{new_tags} );
if ( $operation eq "tagrules" ) {

if ( exists $plugin_result{title} ) {
LANraragi::Utils::Database::set_title( $id, $plugin_result{title} );
}
}
$logger->debug("Applying tag rules to $id...");
my $tags = $redis->hget( $id, "tags" );

my @tagarray = split_tags_to_array($tags);
my @rules = get_computed_tagrules();
@tagarray = rewrite_tags( \@tagarray, \@rules );

# Merge array with commas
my $newtags = join( ', ', @tagarray );
$logger->debug("New tags: $newtags");
$redis->hset( $id, "tags", $newtags );

# Send reply message for completed archive
$client->send(
{ json => {
id => $id,
success => exists $plugin_result{error} ? 0 : 1,
message => $plugin_result{error},
tags => $plugin_result{new_tags},
title => exists $plugin_result{title} ? $plugin_result{title} : ""
success => 1,
tags => $newtags,
}
}
);
return;
}

if ( $operation eq "delete" ) {
$logger->debug("Deleting $id...");

my $delStatus = LANraragi::Utils::Database::delete_archive($id);

$client->send(
{ json => {
id => $id,
filename => $delStatus,
message => $delStatus ? "Archive deleted." : "Archive not found.",
success => $delStatus ? 1 : 0
}
}
);
} else {
$client->finish( 1001 => 'This plugin does not exist' );
return;
}

# Unknown operation
$client->send(
{ json => {
id => $id,
message => "Unknown operation type $operation.",
success => 0
}
}
);
}
);

$self->on(

#If the client doesn't respond, halt processing
# If the client doesn't respond, halt processing
finish => sub {
$logger->info('Client disconnected, halting remaining operations');
$cancelled = 1;
Expand All @@ -145,4 +162,33 @@ sub socket {

}

sub batch_plugin {
my ( $id, $plugin, @args ) = @_;

# Run plugin with args on id
my %plugin_result;
eval { %plugin_result = LANraragi::Model::Plugins::exec_metadata_plugin( $plugin, $id, "", @args ); };

if ($@) {
$plugin_result{error} = $@;
}

# If the plugin exec returned tags, add them
unless ( exists $plugin_result{error} ) {
LANraragi::Utils::Database::add_tags( $id, $plugin_result{new_tags} );

if ( exists $plugin_result{title} ) {
LANraragi::Utils::Database::set_title( $id, $plugin_result{title} );
}
}

return {
id => $id,
success => exists $plugin_result{error} ? 0 : 1,
message => $plugin_result{error},
tags => $plugin_result{new_tags},
title => exists $plugin_result{title} ? $plugin_result{title} : ""
};
}

1;
Loading

0 comments on commit de298af

Please sign in to comment.