Skip to content

Commit

Permalink
Use trim instead of remove_spaces everywhere
Browse files Browse the repository at this point in the history
And move remove_newlines and trim_url to Utils::String
  • Loading branch information
Difegue committed Aug 29, 2023
1 parent cbbe868 commit 36c6812
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 220 deletions.
12 changes: 7 additions & 5 deletions lib/LANraragi/Controller/Config.pm
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package LANraragi::Controller::Config;
use Mojo::Base 'Mojolicious::Controller';

use LANraragi::Utils::Generic qw(generate_themes_header remove_spaces remove_newlines);
use LANraragi::Utils::Generic qw(generate_themes_header);
use LANraragi::Utils::String qw(trim trim_CRLF);
use LANraragi::Utils::Database qw(redis_encode save_computed_tagrules);
use LANraragi::Utils::TempFolder qw(get_tempsize);
use LANraragi::Utils::Tags qw(tags_rules_to_array replace_CRLF restore_CRLF);
Expand Down Expand Up @@ -127,10 +128,11 @@ sub save_config {

# Clean up the user's inputs for non-toggle options and encode for redis insertion
foreach my $key ( keys %confhash ) {
remove_spaces( $confhash{$key} );
remove_newlines( $confhash{$key} );
$confhash{$key} = redis_encode( $confhash{$key} );
$self->LRR_LOGGER->debug( "Saving $key with value " . $confhash{$key} );
my $value = $confhash{$key};
$value = trim($value);
$value = trim_CRLF($value);
$value = redis_encode($value);
$self->LRR_LOGGER->debug( "Saving $key with value " . $value );
}

#for all keys of the hash, add them to the redis config hash with the matching keys.
Expand Down
31 changes: 17 additions & 14 deletions lib/LANraragi/Model/Archive.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use File::Basename;
use File::Copy "cp";
use File::Path qw(make_path);

use LANraragi::Utils::Generic qw(remove_spaces remove_newlines render_api_response);
use LANraragi::Utils::Generic qw(render_api_response);
use LANraragi::Utils::String qw(trim trim_CRLF);
use LANraragi::Utils::TempFolder qw(get_temp);
use LANraragi::Utils::Logging qw(get_logger);
use LANraragi::Utils::Archive qw(extract_single_file extract_thumbnail);
Expand All @@ -27,14 +28,14 @@ use LANraragi::Utils::Database
sub get_title($id) {

my $logger = get_logger( "Archives", "lanraragi" );
my $redis = LANraragi::Model::Config->get_redis;
my $redis = LANraragi::Model::Config->get_redis;

if ( $id eq "" ) {
$logger->debug("No archive ID provided.");
return ();
}

return redis_decode($redis->hget( $id, "title" ));
return redis_decode( $redis->hget( $id, "title" ) );
}

# Functions used when dealing with archives.
Expand All @@ -57,8 +58,8 @@ sub update_thumbnail {
$page = 1 unless $page;

my $thumbdir = LANraragi::Model::Config->get_thumbdir;
my $use_jxl = LANraragi::Model::Config->get_jxlthumbpages;
my $format = $use_jxl ? 'jxl' : 'jpg';
my $use_jxl = LANraragi::Model::Config->get_jxlthumbpages;
my $format = $use_jxl ? 'jxl' : 'jpg';

# Thumbnails are stored in the content directory, thumb subfolder.
# Another subfolder with the first two characters of the id is used for FS optimization.
Expand Down Expand Up @@ -100,18 +101,18 @@ sub serve_thumbnail {
my $no_fallback = $self->req->param('no_fallback');
$no_fallback = ( $no_fallback && $no_fallback eq "true" ) || "0"; # Prevent undef warnings by checking the variable first

my $thumbdir = LANraragi::Model::Config->get_thumbdir;
my $use_jxl = LANraragi::Model::Config->get_jxlthumbpages;
my $format = $use_jxl ? 'jxl' : 'jpg';
my $thumbdir = LANraragi::Model::Config->get_thumbdir;
my $use_jxl = LANraragi::Model::Config->get_jxlthumbpages;
my $format = $use_jxl ? 'jxl' : 'jpg';
my $fallback_format = $format eq 'jxl' ? 'jpg' : 'jxl';

# Thumbnails are stored in the content directory, thumb subfolder.
# Another subfolder with the first two characters of the id is used for FS optimization.
my $subfolder = substr( $id, 0, 2 );

# Check for the page and set the appropriate thumbnail name and fallback thumbnail name
my $thumbbase = ( $page - 1 > 0 ) ? "$thumbdir/$subfolder/$id/$page" : "$thumbdir/$subfolder/$id";
my $thumbname = "$thumbbase.$format";
my $thumbbase = ( $page - 1 > 0 ) ? "$thumbdir/$subfolder/$id/$page" : "$thumbdir/$subfolder/$id";
my $thumbname = "$thumbbase.$format";
my $fallback_thumbname = "$thumbbase.$fallback_format";

# Check if the preferred format thumbnail exists, if not, try the alternate format
Expand All @@ -123,7 +124,7 @@ sub serve_thumbnail {
unless ( -e $thumbname ) {
my $job_id = $self->minion->enqueue( thumbnail_task => [ $thumbdir, $id, $page ] => { priority => 0, attempts => 3 } );

if ( $no_fallback ) {
if ($no_fallback) {
$self->render(
json => {
operation => "serve_thumbnail",
Expand All @@ -133,11 +134,13 @@ sub serve_thumbnail {
status => 202 # 202 Accepted
);
} else {

# If the thumbnail doesn't exist, serve the default thumbnail.
$self->render_file( filepath => "./public/img/noThumb.png" );
}
return;
} else {

# Simply serve the thumbnail.
$self->render_file( filepath => $thumbname );
}
Expand Down Expand Up @@ -176,7 +179,7 @@ sub serve_page {

# Extract the file from the parent archive if it doesn't exist
$logger->debug("Extracting missing file");
my $redis = LANraragi::Model::Config->get_redis;
my $redis = LANraragi::Model::Config->get_redis;
my $archive = $redis->hget( $id, "file" );
$redis->quit();

Expand Down Expand Up @@ -249,8 +252,8 @@ sub update_metadata {
}

# Clean up the user's inputs and encode them.
( remove_spaces($_) ) for ( $title, $tags );
( remove_newlines($_) ) for ( $title, $tags );
( $_ = trim($_) ) for ( $title, $tags );
( $_ = trim_CRLF($_) ) for ( $title, $tags );

if ( defined $title ) {
set_title( $id, $title );
Expand Down
6 changes: 3 additions & 3 deletions lib/LANraragi/Model/Backup.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use Mojo::JSON qw(decode_json encode_json);

use LANraragi::Model::Category;
use LANraragi::Utils::Database;
use LANraragi::Utils::Generic qw(remove_newlines);
use LANraragi::Utils::String qw(trim_CRLF);
use LANraragi::Utils::Database qw(redis_encode redis_decode invalidate_cache set_title set_tags);
use LANraragi::Utils::Logging qw(get_logger);

#build_backup_JSON()
#Goes through the Redis archive IDs and builds a JSON string containing their metadata.
sub build_backup_JSON {
my $redis = LANraragi::Model::Config->get_redis;
my $redis = LANraragi::Model::Config->get_redis;
my $logger = get_logger( "Backup/Restore", "lanraragi" );

# Basic structure of the backup object
Expand Down Expand Up @@ -65,7 +65,7 @@ sub build_backup_JSON {
my ( $name, $title, $tags, $thumbhash ) = @hash{qw(name title tags thumbhash)};

( $_ = redis_decode($_) ) for ( $name, $title, $tags );
( remove_newlines($_) ) for ( $name, $title, $tags );
( $_ = trim_CRLF($_) ) for ( $name, $title, $tags );

# Backup all user-generated metadata, alongside the unique ID.
my %arc = (
Expand Down
6 changes: 3 additions & 3 deletions lib/LANraragi/Model/Plugins.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use Mojo::JSON qw(decode_json encode_json);
use Mojo::UserAgent;
use Data::Dumper;

use LANraragi::Utils::Generic qw(remove_spaces remove_newlines);
use LANraragi::Utils::String qw(trim);
use LANraragi::Utils::Database qw(set_tags set_title);
use LANraragi::Utils::Archive qw(extract_thumbnail);
use LANraragi::Utils::Logging qw(get_logger);
Expand All @@ -20,7 +20,7 @@ use LANraragi::Utils::Tags qw(rewrite_tags split_tags_to_array);
# Sub used by Auto-Plugin.
sub exec_enabled_plugins_on_file {

my $id = shift;
my $id = shift;
my $logger = get_logger( "Auto-Plugin", "lanraragi" );

$logger->info("Executing enabled metadata plugins on archive with id $id.");
Expand Down Expand Up @@ -273,7 +273,7 @@ sub exec_metadata_plugin {
if ( exists $newmetadata{title} ) {

my $newtitle = $newmetadata{title};
remove_spaces($newtitle);
$newtitle = trim($newtitle);
$returnhash{title} = $newtitle;
}
return %returnhash;
Expand Down
21 changes: 11 additions & 10 deletions lib/LANraragi/Model/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use Redis;
use Storable qw/ nfreeze thaw /;
use Sort::Naturally;

use LANraragi::Utils::Generic qw(split_workload_by_cpu remove_spaces);
use LANraragi::Utils::Generic qw(split_workload_by_cpu);
use LANraragi::Utils::String qw(trim);
use LANraragi::Utils::Database qw(redis_decode redis_encode);
use LANraragi::Utils::Logging qw(get_logger);

Expand All @@ -22,7 +23,7 @@ sub do_search {

my ( $filter, $category_id, $start, $sortkey, $sortorder, $newonly, $untaggedonly ) = @_;

my $redis = LANraragi::Model::Config->get_redis_search;
my $redis = LANraragi::Model::Config->get_redis_search;
my $logger = get_logger( "Search Engine", "lanraragi" );

unless ( $redis->exists("LAST_JOB_TIME") ) {
Expand Down Expand Up @@ -64,7 +65,7 @@ sub do_search {
sub check_cache {

my ( $cachekey, $cachekey_inv ) = @_;
my $redis = LANraragi::Model::Config->get_redis_search;
my $redis = LANraragi::Model::Config->get_redis_search;
my $logger = get_logger( "Search Cache", "lanraragi" );

my @filtered = ();
Expand Down Expand Up @@ -190,7 +191,7 @@ sub search_uncached {
# If the tag has a namespace, We don't add a wildcard at the start of the tag to keep it intact.
# Otherwise, we add a wildcard at the start to match all namespaces.
my $indexkey = $tag =~ /:/ ? "INDEX_$tag*" : "INDEX_*$tag*";
my @keys = $redis->keys($indexkey);
my @keys = $redis->keys($indexkey);

# Get the list of IDs for each key
foreach my $key (@keys) {
Expand All @@ -202,7 +203,7 @@ sub search_uncached {

# Append fuzzy title search
my $namesearch = $isexact ? "$tag\x00*" : "*$tag*";
my $scan = -1;
my $scan = -1;
while ( $scan != 0 ) {

# First iteration
Expand Down Expand Up @@ -373,7 +374,7 @@ sub compute_search_filter {
# Escape already present regex characters
$logger->debug("Pre-escaped tag: $tag");

remove_spaces($tag);
$tag = trim($tag);

# Escape characters according to redis zscan rules
$tag =~ s/([\[\]\^\\])/\\$1/g;
Expand Down Expand Up @@ -405,10 +406,10 @@ sub sort_results {
# (If no tag, defaults to "zzzz")
my %tmpfilter = map { $_ => ( $redis->hget( $_, "tags" ) =~ m/.*${re}:(.*)(\,.*|$)/ ) ? $1 : "zzzz" } @filtered;

my @sorted = map { $_->[0] } # Map back to only having the ID
sort { ncmp( $a->[1], $b->[1] ) } # Sort by the tag
map { [ $_, lc( $tmpfilter{$_} ) ] } # Map to an array containing the ID and the lowercased tag
keys %tmpfilter; # List of IDs
my @sorted = map { $_->[0] } # Map back to only having the ID
sort { ncmp( $a->[1], $b->[1] ) } # Sort by the tag
map { [ $_, lc( $tmpfilter{$_} ) ] } # Map to an array containing the ID and the lowercased tag
keys %tmpfilter; # List of IDs

if ($sortorder) {
@sorted = reverse @sorted;
Expand Down
24 changes: 12 additions & 12 deletions lib/LANraragi/Model/Stats.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use Redis;
use File::Find;
use Mojo::JSON qw(encode_json);

use LANraragi::Utils::Generic qw(remove_spaces remove_newlines is_archive trim_url);
use LANraragi::Utils::Generic qw(is_archive);
use LANraragi::Utils::String qw(trim trim_CRLF trim_url);
use LANraragi::Utils::Database qw(redis_decode redis_encode);
use LANraragi::Utils::Logging qw(get_logger);

Expand Down Expand Up @@ -38,7 +39,7 @@ sub get_archive_count {
sub get_page_stat {

my $redis = LANraragi::Model::Config->get_redis_config;
my $stat = $redis->get("LRR_TOTALPAGESTAT") || 0;
my $stat = $redis->get("LRR_TOTALPAGESTAT") || 0;
$redis->quit();

return $stat;
Expand Down Expand Up @@ -83,20 +84,19 @@ sub build_stat_hashes {
my $rawtags = $redis->hget( $id, "tags" );

# Split tags by comma
my @tags = split( /,\s?/, redis_decode($rawtags) );
my @tags = split( /,\s?/, redis_decode($rawtags) );
my $has_tags = 0;

foreach my $t (@tags) {
remove_spaces($t);
remove_newlines($t);
$t = trim($t);
$t = trim_CRLF($t);

# The following are basic and therefore don't count as "tagged"
$has_tags = 1 unless $t =~ /(artist|parody|series|language|event|group|date_added|timestamp):.*/;

# If the tag is a source: tag, add it to the URL index
if ( $t =~ /source:(.*)/i ) {
my $url = $1;
trim_url($url);
my $url = trim_url($1);
$logger->trace("Adding $url as an URL for $id");
$redistx->hset( "LRR_URLMAP", $url, $id ); # No need to encode the value, as URLs are already encoded by design
}
Expand All @@ -123,8 +123,8 @@ sub build_stat_hashes {

# Decode and lowercase the title
$title = lc( redis_decode($title) );
remove_spaces($title);
remove_newlines($title);
$title = trim($title);
$title = trim_CRLF($title);
$title = redis_encode($title);

# The LRR_TITLES lexicographically sorted set contains both the title and the id under the form $title\x00$id.
Expand Down Expand Up @@ -156,7 +156,7 @@ sub is_url_recorded {
$logger->debug("Checking if url $url is in the url map.");

# Trim last slash from url if it's present
trim_url($url);
$url = trim_url($url);

if ( $redis->hexists( "LRR_URLMAP", $url ) ) {
$id = $redis->hget( "LRR_URLMAP", $url );
Expand All @@ -169,11 +169,11 @@ sub is_url_recorded {
sub build_tag_stats {

my $minscore = shift;
my $logger = get_logger( "Tag Stats", "lanraragi" );
my $logger = get_logger( "Tag Stats", "lanraragi" );
$logger->debug("Serving tag statistics with a minimum weight of $minscore");

# Login to Redis and grab the stats sorted set
my $redis = LANraragi::Model::Config->get_redis_search;
my $redis = LANraragi::Model::Config->get_redis_search;
my %tagcloud = $redis->zrangebyscore( "LRR_STATS", $minscore, "+inf", "WITHSCORES" );
$redis->quit();

Expand Down
15 changes: 8 additions & 7 deletions lib/LANraragi/Model/Upload.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use File::Copy qw(move);
use LANraragi::Utils::Database qw(invalidate_cache compute_id);
use LANraragi::Utils::Logging qw(get_logger);
use LANraragi::Utils::Database qw(redis_encode);
use LANraragi::Utils::Generic qw(is_archive remove_spaces remove_newlines trim_url get_bytelength);
use LANraragi::Utils::Generic qw(is_archive get_bytelength);
use LANraragi::Utils::String qw(trim trim_CRLF trim_url);

use LANraragi::Model::Config;
use LANraragi::Model::Plugins;
Expand All @@ -31,8 +32,8 @@ use LANraragi::Model::Category;
# Returns a status value, the ID and title of the file, and a status message.
sub handle_incoming_file {

my ( $tempfile, $catid, $tags ) = @_;
my ( $filename, $dirs, $suffix ) = fileparse( $tempfile, qr/\.[^.]*/ );
my ( $tempfile, $catid, $tags ) = @_;
my ( $filename, $dirs, $suffix ) = fileparse( $tempfile, qr/\.[^.]*/ );
$filename = $filename . $suffix;
my $logger = get_logger( "File Upload/Download", "lanraragi" );

Expand All @@ -57,7 +58,7 @@ sub handle_incoming_file {
my $isdupe = $redis->exists($id) && -e $redis->hget( $id, "file" );

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

# Trash temporary file
unlink $tempfile;
Expand All @@ -75,7 +76,7 @@ sub handle_incoming_file {
# 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 );
LANraragi::Utils::Database::delete_archive($id);
}

# Add the file to the database ourselves so Shinobu doesn't do it
Expand All @@ -91,8 +92,8 @@ sub handle_incoming_file {
my @tags = split( /,\s?/, $tags );

foreach my $t (@tags) {
remove_spaces($t);
remove_newlines($t);
$t = trim($t);
$t = trim_CRLF($t);

# If the tag is a source: tag, add it to the URL index
if ( $t =~ /source:(.*)/i ) {
Expand Down
Loading

0 comments on commit 36c6812

Please sign in to comment.