Tried SharpCompress but had 20x speed decrement. Not using.

This commit is contained in:
Joseph Milazzo 2021-01-25 18:18:07 -06:00
parent e7f2baaa94
commit 6cc11d5c4c
5 changed files with 85 additions and 18 deletions

View File

@ -74,6 +74,7 @@ namespace API.Tests
[InlineData("Ichinensei_ni_Nacchattara_v01_ch01_[Taruby]_v1.1.zip", "Ichinensei ni Nacchattara")] [InlineData("Ichinensei_ni_Nacchattara_v01_ch01_[Taruby]_v1.1.zip", "Ichinensei ni Nacchattara")]
[InlineData("Chrno_Crusade_Dragon_Age_All_Stars[AS].zip", "")] [InlineData("Chrno_Crusade_Dragon_Age_All_Stars[AS].zip", "")]
[InlineData("Ichiban_Ushiro_no_Daimaou_v04_ch34_[VISCANS].zip", "Ichiban Ushiro no Daimaou")] [InlineData("Ichiban_Ushiro_no_Daimaou_v04_ch34_[VISCANS].zip", "Ichiban Ushiro no Daimaou")]
[InlineData("Rent a Girlfriend v01.cbr", "Rent a Girlfriend")]
//[InlineData("[Tempus Edax Rerum] Epigraph of the Closed Curve - Chapter 6.zip", "Epigraph of the Closed Curve")] //[InlineData("[Tempus Edax Rerum] Epigraph of the Closed Curve - Chapter 6.zip", "Epigraph of the Closed Curve")]
public void ParseSeriesTest(string filename, string expected) public void ParseSeriesTest(string filename, string expected)
{ {

View File

@ -52,7 +52,6 @@ namespace API.Extensions
var newName = $"{file.Directory.Name}_{file.Name}"; var newName = $"{file.Directory.Name}_{file.Name}";
var newPath = Path.Join(root.FullName, newName); var newPath = Path.Join(root.FullName, newName);
file.MoveTo(newPath); file.MoveTo(newPath);
} }
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
@ -41,10 +42,10 @@ namespace API.Services
return null; return null;
} }
Volume volume = await _unitOfWork.SeriesRepository.GetVolumeAsync(volumeId); Volume volume = await _unitOfWork.SeriesRepository.GetVolumeAsync(volumeId);
foreach (var file in volume.Files) foreach (var file in volume.Files)
{ {
var extractPath = GetVolumeCachePath(volumeId, file); var extractPath = GetVolumeCachePath(volumeId, file);
ExtractArchive(file.FilePath, extractPath); ExtractArchive(file.FilePath, extractPath);
} }
@ -91,6 +92,74 @@ namespace API.Services
_logger.LogInformation("Cache directory purged"); _logger.LogInformation("Cache directory purged");
} }
// private void ExtractArchive(string archivePath, string extractPath, int expectedPages)
// {
// if (!File.Exists(archivePath) || !Parser.Parser.IsArchive(archivePath))
// {
// _logger.LogError($"Archive {archivePath} could not be found.");
// }
//
// var extractDirectoryInfo = new DirectoryInfo(extractPath);
//
// // If the extraction and flattening aren't perfect, adding extra cases adds a serious amount of extra time.
// if (Directory.Exists(extractPath)) // && extractDirectoryInfo.EnumerateFiles().Count() >= expectedPages
// {
// _logger.LogDebug($"Archive {archivePath} has already been extracted. Returning existing folder.");
// return;
// }
//
// Stopwatch sw = Stopwatch.StartNew();
// var needsFlattening = false;
// var options = new ExtractionOptions
// {
// ExtractFullPath = true,
// Overwrite = true
// };
//
// using Stream stream = File.OpenRead(archivePath);
// using var reader = ReaderFactory.Open(stream);
// while (reader.MoveToNextEntry())
// {
// if (!reader.Entry.IsDirectory)
// {
// try
// {
// reader.WriteEntryToDirectory(extractPath, options);
// }
// catch (Exception e)
// {
// _logger.LogError(e, "There was an issue with extracting image.");
// }
// }
//
// if (!needsFlattening)
// {
// needsFlattening = reader.Entry.IsDirectory;
// }
// }
//
//
// _logger.LogDebug($"Extracted archive to {extractPath} in {sw.ElapsedMilliseconds} milliseconds.");
//
//
// if (needsFlattening)
// {
// sw = Stopwatch.StartNew();
// _logger.LogInformation("Extracted archive is nested in root folder, flattening...");
// try
// {
// extractDirectoryInfo.Flatten();
// }
// catch (Exception ex)
// {
// _logger.LogError(ex, "There was an issue extracting archive.");
// return;
// }
// _logger.LogInformation($"Flattened in {sw.ElapsedMilliseconds} milliseconds");
// }
// }
/// <summary> /// <summary>
/// Extracts an archive to a temp cache directory. Returns path to new directory. If temp cache directory already exists, /// Extracts an archive to a temp cache directory. Returns path to new directory. If temp cache directory already exists,
/// will return that without performing an extraction. Returns empty string if there are any invalidations which would /// will return that without performing an extraction. Returns empty string if there are any invalidations which would
@ -99,35 +168,34 @@ namespace API.Services
/// <param name="archivePath">A valid file to an archive file.</param> /// <param name="archivePath">A valid file to an archive file.</param>
/// <param name="extractPath">Path to extract to</param> /// <param name="extractPath">Path to extract to</param>
/// <returns></returns> /// <returns></returns>
private string ExtractArchive(string archivePath, string extractPath) private void ExtractArchive(string archivePath, string extractPath)
{ {
// NOTE: This is used by Cache Service
if (!File.Exists(archivePath) || !Parser.Parser.IsArchive(archivePath)) if (!File.Exists(archivePath) || !Parser.Parser.IsArchive(archivePath))
{ {
_logger.LogError($"Archive {archivePath} could not be found."); _logger.LogError($"Archive {archivePath} could not be found.");
return "";
} }
if (Directory.Exists(extractPath)) if (Directory.Exists(extractPath))
{ {
_logger.LogDebug($"Archive {archivePath} has already been extracted. Returning existing folder."); _logger.LogDebug($"Archive {archivePath} has already been extracted. Returning existing folder.");
return extractPath;
} }
Stopwatch sw = Stopwatch.StartNew();
using ZipArchive archive = ZipFile.OpenRead(archivePath); using ZipArchive archive = ZipFile.OpenRead(archivePath);
// TODO: Throw error if we couldn't extract // TODO: Throw error if we couldn't extract
var needsFlattening = archive.Entries.Count > 0 && !Path.HasExtension(archive.Entries.ElementAt(0).FullName); var needsFlattening = archive.Entries.Count > 0 && !Path.HasExtension(archive.Entries.ElementAt(0).FullName);
if (!archive.HasFiles() && !needsFlattening) return ""; if (!archive.HasFiles() && !needsFlattening) return;
archive.ExtractToDirectory(extractPath); archive.ExtractToDirectory(extractPath);
_logger.LogDebug($"Extracting archive to {extractPath}"); _logger.LogDebug($"[OLD] Extracted archive to {extractPath} in {sw.ElapsedMilliseconds} milliseconds.");
if (!needsFlattening) return extractPath; if (needsFlattening)
{
_logger.LogInformation("Extracted archive is nested in root folder, flattening..."); sw = Stopwatch.StartNew();
new DirectoryInfo(extractPath).Flatten(); _logger.LogInformation("Extracted archive is nested in root folder, flattening...");
new DirectoryInfo(extractPath).Flatten();
return extractPath; _logger.LogInformation($"[OLD] Flattened in {sw.ElapsedMilliseconds} milliseconds");
}
} }

View File

@ -6,7 +6,6 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using API.Entities; using API.Entities;
using API.Extensions; using API.Extensions;

View File

@ -1,13 +1,13 @@
{ {
"ConnectionStrings": { "ConnectionStrings": {
"DefaultConnection": "Data source=kavita.db", "DefaultConnection": "Data source=kavita.db"
}, },
"TokenKey": "super secret unguessable key", "TokenKey": "super secret unguessable key",
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Debug", "Default": "Debug",
"Microsoft": "Information", "Microsoft": "Error",
"Microsoft.Hosting.Lifetime": "Information", "Microsoft.Hosting.Lifetime": "Error",
"Hangfire": "Information" "Hangfire": "Information"
}, },
"File": { "File": {