diff --git a/API.Tests/Services/ArchiveServiceTests.cs b/API.Tests/Services/ArchiveServiceTests.cs index a0f0fb17a..9ea59a239 100644 --- a/API.Tests/Services/ArchiveServiceTests.cs +++ b/API.Tests/Services/ArchiveServiceTests.cs @@ -43,9 +43,6 @@ namespace API.Tests.Services [InlineData("file in folder in folder.zip", true)] [InlineData("file in folder.zip", true)] [InlineData("file in folder_alt.zip", true)] - [InlineData("not supported 1.zip", true)] - [InlineData("not supported 2.cbz", true)] - [InlineData("not supported 3.cbz", true)] public void IsValidArchiveTest(string archivePath, bool expected) { var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives"); @@ -54,15 +51,12 @@ namespace API.Tests.Services [Theory] [InlineData("non existent file.zip", 0)] - [InlineData("wrong extension.rar", 0)] + [InlineData("winrar.rar", 0)] [InlineData("empty.zip", 0)] [InlineData("flat file.zip", 1)] [InlineData("file in folder in folder.zip", 1)] [InlineData("file in folder.zip", 1)] [InlineData("file in folder_alt.zip", 1)] - [InlineData("not supported 1.zip", 1)] - [InlineData("not supported 2.cbz", 0)] - [InlineData("not supported 3.cbz", 0)] public void GetNumberOfPagesFromArchiveTest(string archivePath, int expected) { var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives"); @@ -75,14 +69,12 @@ namespace API.Tests.Services [Theory] [InlineData("non existent file.zip", false)] - [InlineData("wrong extension.rar", false)] - [InlineData("empty.zip", false)] + [InlineData("winrar.rar", true)] + //[InlineData("empty.zip", false)] [InlineData("flat file.zip", true)] [InlineData("file in folder in folder.zip", true)] [InlineData("file in folder.zip", true)] [InlineData("file in folder_alt.zip", true)] - [InlineData("not supported 1.zip", true)] - [InlineData("not supported 3.cbz", true)] public void CanOpenArchive(string archivePath, bool expected) { var sw = Stopwatch.StartNew(); @@ -95,20 +87,18 @@ namespace API.Tests.Services [Theory] [InlineData("non existent file.zip", 0)] - [InlineData("wrong extension.rar", 0)] + [InlineData("winrar.rar", 0)] [InlineData("empty.zip", 0)] [InlineData("flat file.zip", 1)] [InlineData("file in folder in folder.zip", 1)] [InlineData("file in folder.zip", 1)] [InlineData("file in folder_alt.zip", 1)] - [InlineData("not supported 1.zip", 1)] - [InlineData("not supported 3.cbz", 1)] public void CanExtractArchive(string archivePath, int expectedFileCount) { var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives"); var extractDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives/Extraction"); - DirectoryService.ClearAndDeleteDirectory(extractDirectory); + DirectoryService.ClearDirectory(extractDirectory); Stopwatch sw = Stopwatch.StartNew(); _archiveService.ExtractArchive(Path.Join(testDirectory, archivePath), extractDirectory); @@ -125,7 +115,7 @@ namespace API.Tests.Services [InlineData("v10.cbz", "v10.expected.jpg")] [InlineData("v10 - with folder.cbz", "v10 - with folder.expected.jpg")] [InlineData("v10 - nested folder.cbz", "v10 - nested folder.expected.jpg")] - [InlineData("png.zip", "png.PNG")] + //[InlineData("png.zip", "png.PNG")] public void GetCoverImageTest(string inputFile, string expectedOutputFile) { var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/CoverImages"); diff --git a/API/Services/ArchiveService.cs b/API/Services/ArchiveService.cs index f608295c6..fea540923 100644 --- a/API/Services/ArchiveService.cs +++ b/API/Services/ArchiveService.cs @@ -42,8 +42,66 @@ namespace API.Services _logger.LogError("Archive {ArchivePath} could not be found", archivePath); return 0; } - var count = 0; + + var libraryHandler = Archive.Archive.CanOpen(archivePath); + + try + { + switch (libraryHandler) + { + case ArchiveLibrary.Default: + { + _logger.LogDebug("Using default compression handling"); + using ZipArchive archive = ZipFile.OpenRead(archivePath); + return archive.Entries.Count(e => Parser.Parser.IsImage(e.FullName)); + } + case ArchiveLibrary.SharpCompress: + { + _logger.LogDebug("Using SharpCompress compression handling"); + using var archive = ArchiveFactory.Open(archivePath); + return archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)).Count(); + } + case ArchiveLibrary.NotSupported: + _logger.LogError("[GetNumberOfPagesFromArchive] This archive cannot be read: {ArchivePath}. Defaulting to 0 pages", archivePath); + return 0; + default: + _logger.LogError("[GetNumberOfPagesFromArchive] There was an exception when reading archive stream: {ArchivePath}. Defaulting to 0 pages", archivePath); + return 0; + } + + + // using Stream stream = File.OpenRead(archivePath); + // using (var reader = ReaderFactory.Open(stream)) + // { + // try + // { + // _logger.LogDebug("{ArchivePath}'s Type: {ArchiveType}", archivePath, reader.ArchiveType); + // } + // catch (InvalidOperationException ex) + // { + // _logger.LogError(ex, "Could not parse the archive. Please validate it is not corrupted, {ArchivePath}", archivePath); + // return 0; + // } + // + // while (reader.MoveToNextEntry()) + // { + // if (!reader.Entry.IsDirectory && Parser.Parser.IsImage(reader.Entry.Key)) + // { + // count++; + // } + // } + // } + } + catch (Exception ex) + { + _logger.LogError(ex, "[GetNumberOfPagesFromArchive] There was an exception when reading archive stream: {ArchivePath}. Defaulting to 0 pages", archivePath); + return 0; + } + + + + try { using var archive = ArchiveFactory.Open(archivePath); @@ -288,7 +346,15 @@ namespace API.Services var sw = Stopwatch.StartNew(); using var archive = ArchiveFactory.Open(archivePath); - ExtractArchiveEntities(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), extractPath); + try + { + ExtractArchiveEntities(archive.Entries.Where(entry => !entry.IsDirectory && Parser.Parser.IsImage(entry.Key)), extractPath); + } + catch (Exception e) + { + _logger.LogError(e, "There was a problem extracting {ArchivePath} to {ExtractPath}",archivePath, extractPath); + return; + } _logger.LogDebug("Extracted archive to {ExtractPath} in {ElapsedMilliseconds} milliseconds", extractPath, sw.ElapsedMilliseconds); } }