Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

Moved the test data around so more tests can use it properly. Added a IsValidArchive that is re-usable for all archive methods.

Joseph Milazzoec64bf90c059
diff --git a/API.Tests/Services/ArchiveServiceTests.cs b/API.Tests/Services/ArchiveServiceTests.cs
index 44453a7f5..b3c4ac877 100644
--- a/API.Tests/Services/ArchiveServiceTests.cs
+++ b/API.Tests/Services/ArchiveServiceTests.cs
@@ -25,11 +25,25 @@ namespace API.Tests.Services
[InlineData("file in folder_alt.zip", true)]
public void ArchiveNeedsFlatteningTest(string archivePath, bool expected)
{
- var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService");
+ var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives");
var file = Path.Join(testDirectory, archivePath);
using ZipArchive archive = ZipFile.OpenRead(file);
Assert.Equal(expected, _archiveService.ArchiveNeedsFlattening(archive));
}
+
+ [Theory]
+ [InlineData("non existant file.zip", false)]
+ [InlineData("wrong extension.rar", false)]
+ [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)]
+ public void IsValidArchiveTest(string archivePath, bool expected)
+ {
+ var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives");
+ Assert.Equal(expected, _archiveService.IsValidArchive(Path.Join(testDirectory, archivePath)));
+ }
[Theory]
[InlineData("v10.cbz", "v10.expected.jpg")]
@@ -37,7 +51,7 @@ namespace API.Tests.Services
[InlineData("v10 - nested folder.cbz", "v10 - nested folder.expected.jpg")]
public void GetCoverImageTest(string inputFile, string expectedOutputFile)
{
- var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/CoverImageTests");
+ var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/CoverImages");
var expectedBytes = File.ReadAllBytes(Path.Join(testDirectory, expectedOutputFile));
Assert.Equal(expectedBytes, _archiveService.GetCoverImage(Path.Join(testDirectory, inputFile)));
}
diff --git a/API.Tests/Services/Test Data/ArchiveService/LICENSE.md b/API.Tests/Services/Test Data/ArchiveService/Archives/LICENSE.md
similarity index 100%
rename from API.Tests/Services/Test Data/ArchiveService/LICENSE.md
rename to API.Tests/Services/Test Data/ArchiveService/Archives/LICENSE.md
diff --git a/API.Tests/Services/Test Data/ArchiveService/Archives/empty.zip b/API.Tests/Services/Test Data/ArchiveService/Archives/empty.zip
new file mode 100644
index 000000000..15cb0ecb3
Binary files /dev/null and b/API.Tests/Services/Test Data/ArchiveService/Archives/empty.zip differ
diff --git a/API.Tests/Services/Test Data/ArchiveService/file in folder in folder.zip b/API.Tests/Services/Test Data/ArchiveService/Archives/file in folder in folder.zip
similarity index 100%
rename from API.Tests/Services/Test Data/ArchiveService/file in folder in folder.zip
rename to API.Tests/Services/Test Data/ArchiveService/Archives/file in folder in folder.zip
diff --git a/API.Tests/Services/Test Data/ArchiveService/file in folder.zip b/API.Tests/Services/Test Data/ArchiveService/Archives/file in folder.zip
similarity index 100%
rename from API.Tests/Services/Test Data/ArchiveService/file in folder.zip
rename to API.Tests/Services/Test Data/ArchiveService/Archives/file in folder.zip
diff --git a/API.Tests/Services/Test Data/ArchiveService/file in folder_alt.zip b/API.Tests/Services/Test Data/ArchiveService/Archives/file in folder_alt.zip
similarity index 100%
rename from API.Tests/Services/Test Data/ArchiveService/file in folder_alt.zip
rename to API.Tests/Services/Test Data/ArchiveService/Archives/file in folder_alt.zip
diff --git a/API.Tests/Services/Test Data/ArchiveService/flat file.zip b/API.Tests/Services/Test Data/ArchiveService/Archives/flat file.zip
similarity index 100%
rename from API.Tests/Services/Test Data/ArchiveService/flat file.zip
rename to API.Tests/Services/Test Data/ArchiveService/Archives/flat file.zip
diff --git a/API.Tests/Services/Test Data/CoverImageTests/thumbnail.expected.jpg b/API.Tests/Services/Test Data/ArchiveService/CoverImages/thumbnail.expected.jpg
similarity index 100%
rename from API.Tests/Services/Test Data/CoverImageTests/thumbnail.expected.jpg
rename to API.Tests/Services/Test Data/ArchiveService/CoverImages/thumbnail.expected.jpg
diff --git a/API.Tests/Services/Test Data/CoverImageTests/thumbnail.jpg b/API.Tests/Services/Test Data/ArchiveService/CoverImages/thumbnail.jpg
similarity index 100%
rename from API.Tests/Services/Test Data/CoverImageTests/thumbnail.jpg
rename to API.Tests/Services/Test Data/ArchiveService/CoverImages/thumbnail.jpg
diff --git a/API.Tests/Services/Test Data/CoverImageTests/v10 - nested folder.cbz b/API.Tests/Services/Test Data/ArchiveService/CoverImages/v10 - nested folder.cbz
similarity index 100%
rename from API.Tests/Services/Test Data/CoverImageTests/v10 - nested folder.cbz
rename to API.Tests/Services/Test Data/ArchiveService/CoverImages/v10 - nested folder.cbz
diff --git a/API.Tests/Services/Test Data/CoverImageTests/v10 - nested folder.expected.jpg b/API.Tests/Services/Test Data/ArchiveService/CoverImages/v10 - nested folder.expected.jpg
similarity index 100%
rename from API.Tests/Services/Test Data/CoverImageTests/v10 - nested folder.expected.jpg
rename to API.Tests/Services/Test Data/ArchiveService/CoverImages/v10 - nested folder.expected.jpg
diff --git a/API.Tests/Services/Test Data/CoverImageTests/v10 - with folder.cbz b/API.Tests/Services/Test Data/ArchiveService/CoverImages/v10 - with folder.cbz
similarity index 100%
rename from API.Tests/Services/Test Data/CoverImageTests/v10 - with folder.cbz
rename to API.Tests/Services/Test Data/ArchiveService/CoverImages/v10 - with folder.cbz
diff --git a/API.Tests/Services/Test Data/CoverImageTests/v10 - with folder.expected.jpg b/API.Tests/Services/Test Data/ArchiveService/CoverImages/v10 - with folder.expected.jpg
similarity index 100%
rename from API.Tests/Services/Test Data/CoverImageTests/v10 - with folder.expected.jpg
rename to API.Tests/Services/Test Data/ArchiveService/CoverImages/v10 - with folder.expected.jpg
diff --git a/API.Tests/Services/Test Data/CoverImageTests/v10.cbz b/API.Tests/Services/Test Data/ArchiveService/CoverImages/v10.cbz
similarity index 100%
rename from API.Tests/Services/Test Data/CoverImageTests/v10.cbz
rename to API.Tests/Services/Test Data/ArchiveService/CoverImages/v10.cbz
diff --git a/API.Tests/Services/Test Data/CoverImageTests/v10.expected.jpg b/API.Tests/Services/Test Data/ArchiveService/CoverImages/v10.expected.jpg
similarity index 100%
rename from API.Tests/Services/Test Data/CoverImageTests/v10.expected.jpg
rename to API.Tests/Services/Test Data/ArchiveService/CoverImages/v10.expected.jpg
diff --git a/API/Interfaces/IArchiveService.cs b/API/Interfaces/IArchiveService.cs
index 07d6b6206..3b3ab14cd 100644
--- a/API/Interfaces/IArchiveService.cs
+++ b/API/Interfaces/IArchiveService.cs
@@ -8,5 +8,6 @@ namespace API.Interfaces
void ExtractArchive(string archivePath, string extractPath);
int GetNumberOfPagesFromArchive(string archivePath);
byte[] GetCoverImage(string filepath, bool createThumbnail = false);
+ bool IsValidArchive(string archivePath);
}
}
\ No newline at end of file
diff --git a/API/Services/ArchiveService.cs b/API/Services/ArchiveService.cs
index 6ff593f59..b1615c705 100644
--- a/API/Services/ArchiveService.cs
+++ b/API/Services/ArchiveService.cs
@@ -24,17 +24,13 @@ namespace API.Services
public int GetNumberOfPagesFromArchive(string archivePath)
{
- if (!File.Exists(archivePath) || !Parser.Parser.IsArchive(archivePath))
- {
- _logger.LogError($"Archive {archivePath} could not be found.");
- return 0;
- }
-
+ if (!IsValidArchive(archivePath)) return 0;
+
_logger.LogDebug($"Getting Page numbers from {archivePath}");
try
{
- using ZipArchive archive = ZipFile.OpenRead(archivePath); // ZIPFILE
+ using ZipArchive archive = ZipFile.OpenRead(archivePath);
return archive.Entries.Count(e => Parser.Parser.IsImage(e.FullName));
}
catch (Exception ex)
@@ -42,8 +38,6 @@ namespace API.Services
_logger.LogError(ex, "There was an exception when reading archive stream.");
return 0;
}
-
-
}
/// <summary>
@@ -58,7 +52,8 @@ namespace API.Services
{
try
{
- if (string.IsNullOrEmpty(filepath) || !File.Exists(filepath) || !Parser.Parser.IsArchive(filepath)) return Array.Empty<byte>();
+ if (!IsValidArchive(filepath)) return Array.Empty<byte>();
+ //if (string.IsNullOrEmpty(filepath) || !File.Exists(filepath) || !Parser.Parser.IsArchive(filepath)) return Array.Empty<byte>();
_logger.LogDebug($"Extracting Cover image from {filepath}");
using ZipArchive archive = ZipFile.OpenRead(filepath);
@@ -128,6 +123,31 @@ namespace API.Services
archive.Entries.Any(e => e.FullName.Contains(Path.AltDirectorySeparatorChar));
}
+ /// <summary>
+ /// Test if the archive path exists and there are images inside it. This will log as an error.
+ /// </summary>
+ /// <param name="archivePath"></param>
+ /// <returns></returns>
+ public bool IsValidArchive(string archivePath)
+ {
+ if (!File.Exists(archivePath))
+ {
+ _logger.LogError($"Archive {archivePath} could not be found.");
+ return false;
+ }
+ if (!Parser.Parser.IsArchive(archivePath))
+ {
+ _logger.LogError($"Archive {archivePath} is not a valid archive.");
+ return false;
+ }
+
+ using var archive = ZipFile.OpenRead(archivePath);
+ if (archive.Entries.Any(e => Parser.Parser.IsImage(e.FullName))) return true;
+ _logger.LogError($"Archive {archivePath} contains no images.");
+ return false;
+
+ }
+
/// <summary>
/// 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
@@ -138,11 +158,7 @@ namespace API.Services
/// <returns></returns>
public void ExtractArchive(string archivePath, string extractPath)
{
- if (!File.Exists(archivePath) || !Parser.Parser.IsArchive(archivePath))
- {
- _logger.LogError($"Archive {archivePath} could not be found.");
- return;
- }
+ if (!IsValidArchive(archivePath)) return;
if (Directory.Exists(extractPath))
{