diff --git a/lib/LANraragi/Model/Archive.pm b/lib/LANraragi/Model/Archive.pm index 8c860a671..81205735f 100644 --- a/lib/LANraragi/Model/Archive.pm +++ b/lib/LANraragi/Model/Archive.pm @@ -166,8 +166,10 @@ sub serve_page { my $logger = get_logger( "File Serving", "lanraragi" ); + $logger->debug("Page /$id/$path was requested"); + my $tempfldr = get_temp(); - my $file = LANraragi::Utils::Archive::sanitize_filename( $tempfldr . "/$id/$path" ); + my $file = $tempfldr . "/$id/$path"; if ( -e $file ) { @@ -175,12 +177,17 @@ sub serve_page { # We have to wait before trying to serve them out... my $last_size = 0; my $size = -s $file; + my $timeout = 0; while (1) { $logger->debug("Waiting for file to be fully written ($size, previously $last_size)"); usleep(10000); # 10ms + $timeout += 10; # Sanity check in case the file remains at 0 bytes forever $last_size = $size; $size = -s $file; - last if ( $last_size eq $size ); # If the size hasn't changed since the last loop, it's likely the file is ready. + + # If the size hasn't changed since the last loop, it's likely the file is ready. + last + if ( $last_size eq $size && ( $size ne 0 || $timeout > 1000 ) ); } } else { diff --git a/lib/LANraragi/Model/Reader.pm b/lib/LANraragi/Model/Reader.pm index eb952d9da..a7a4ffa60 100644 --- a/lib/LANraragi/Model/Reader.pm +++ b/lib/LANraragi/Model/Reader.pm @@ -73,7 +73,7 @@ sub build_reader_JSON { # To investigate further, perhaps with custom sorting algorithms? @images = sort { &expand($a) cmp &expand($b) } @images; - $self->LRR_LOGGER->debug( "Files found in archive: \n " . Dumper @images ); + $self->LRR_LOGGER->debug( "Files found in archive (encoding might be incorrect): \n " . Dumper @images ); # Build a browser-compliant filepath array from @images my @images_browser; @@ -92,8 +92,6 @@ sub build_reader_JSON { # Then we bring the slashes back. $imgpath =~ s!%2F!/!g; - $self->LRR_LOGGER->debug("Will be extracted to disk as: $imgpath"); - # Bundle this path into an API call which will be used by the browser push @images_browser, "./api/archives/$id/page?path=$imgpath"; } diff --git a/lib/LANraragi/Utils/Archive.pm b/lib/LANraragi/Utils/Archive.pm index 4d28a77a2..31ba7c33d 100644 --- a/lib/LANraragi/Utils/Archive.pm +++ b/lib/LANraragi/Utils/Archive.pm @@ -47,30 +47,6 @@ sub generate_thumbnail { undef $img; } -# sanitize_filename(filename) -# Converts extracted filenames to an ascii variant to avoid extra filesystem headaches. -sub sanitize_filename { - - my $filename = $_[0]; - eval { - # Try a guess to regular japanese encodings first - $filename = decode( "Guess", $filename ); - }; - - # Fallback to utf8 - $filename = decode_utf8($filename) if $@; - - # Re-encode the result to ASCII and move the file to said result name. - # Use Encode's coderef feature to map non-ascii characters to their Unicode codepoint equivalent. - $filename = encode( "ascii", $filename, sub { sprintf "%04X", shift } ); - - if ( length $filename > 254 ) { - $filename = substr( $filename, 0, 254 ); - } - - return $filename; -} - # extract_archive(path, archive_to_extract, force) # Extract the given archive to the given path. # This sub won't re-extract files already present in the destination unless force = 1. @@ -78,6 +54,7 @@ sub extract_archive { my ( $destination, $to_extract, $force_extract ) = @_; my $logger = get_logger( "Archive", "lanraragi" ); + $logger->debug("Fully extracting archive $to_extract"); # PDFs are handled by Ghostscript (alas) if ( is_pdf($to_extract) ) { @@ -92,14 +69,16 @@ sub extract_archive { if ($force_extract) { return 1; } my $filename = $e->pathname; - $filename = sanitize_filename($filename); if ( -e "$destination/$filename" ) { $logger->debug("$filename already exists in $destination"); return 0; } + $logger->debug("Extracting $filename"); # Pre-emptively create the file to signal we're working on it - open( my $fh, ">", "$destination/$filename" ) or return 0; + open( my $fh, ">", "$destination/$filename" ) + or + $logger->error("Couldn't create placeholder file $destination/$filename (might be a folder?), moving on nonetheless"); close $fh; return 1; } @@ -112,16 +91,6 @@ sub extract_archive { my $result_dir = $ae->to; my $cwd = getcwd(); - # Rename extracted files and folders to an encoded version for easier handling - finddepth( - sub { - unless ( $_ eq '.' ) { - move( $_, sanitize_filename($_) ); - } - }, - $result_dir - ); - # chdir back to the base cwd in case finddepth died midway chdir $cwd; @@ -291,8 +260,19 @@ sub extract_single_file { } else { my $contents = ""; - my $peek = Archive::Libarchive::Peek->new( filename => $archive ); - $contents = $peek->file($filepath); + my $peek = Archive::Libarchive::Peek->new( filename => $archive ); + my @files = $peek->files; + + for my $name (@files) { + my $decoded_name = LANraragi::Utils::Database::redis_decode($name); + + # This sub can receive either encoded or raw filenames, so we have to test for both. + if ( $decoded_name eq $filepath || $name eq $filepath ) { + $logger->debug("Found file $filepath in archive $archive"); + $contents = $peek->file($name); + last; + } + } open( my $fh, '>', $outfile ) or die "Could not open file '$outfile' $!"; @@ -300,9 +280,7 @@ sub extract_single_file { close $fh; } - my $fixed_name = sanitize_filename($outfile); - move( $outfile, $fixed_name ); - return $fixed_name; + return $outfile; } # extract_file_from_archive($archive, $file) diff --git a/tools/Documentation/.gitignore b/tools/Documentation/.gitignore new file mode 100644 index 000000000..3cae616a8 --- /dev/null +++ b/tools/Documentation/.gitignore @@ -0,0 +1 @@ +.gitbook \ No newline at end of file diff --git a/tools/Documentation/basic-operations/archives.md b/tools/Documentation/basic-operations/archives.md index 0a0d8896f..55765f00e 100644 --- a/tools/Documentation/basic-operations/archives.md +++ b/tools/Documentation/basic-operations/archives.md @@ -11,6 +11,7 @@ You can customize display options to show the index in compact mode, as well as ![Customized compact mode to sort by ratings](../.screenshots/ratings.png) The topmost **carousel** view will show random archives from your current search in both thumbnail and compact modes -- It can also be configured to show New or Untagged archives instead. + ![Index page of a regular LRR install in compact mode]() {% hint style="info" %}