Moved the test data around so more tests can use it properly. Added a IsValidArchive that is re-usable for all archive methods.
@ -25,11 +25,25 @@ namespace API.Tests.Services
|
|||||||
[InlineData("file in folder_alt.zip", true)]
|
[InlineData("file in folder_alt.zip", true)]
|
||||||
public void ArchiveNeedsFlatteningTest(string archivePath, bool expected)
|
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);
|
var file = Path.Join(testDirectory, archivePath);
|
||||||
using ZipArchive archive = ZipFile.OpenRead(file);
|
using ZipArchive archive = ZipFile.OpenRead(file);
|
||||||
Assert.Equal(expected, _archiveService.ArchiveNeedsFlattening(archive));
|
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]
|
[Theory]
|
||||||
[InlineData("v10.cbz", "v10.expected.jpg")]
|
[InlineData("v10.cbz", "v10.expected.jpg")]
|
||||||
@ -37,7 +51,7 @@ namespace API.Tests.Services
|
|||||||
[InlineData("v10 - nested folder.cbz", "v10 - nested folder.expected.jpg")]
|
[InlineData("v10 - nested folder.cbz", "v10 - nested folder.expected.jpg")]
|
||||||
public void GetCoverImageTest(string inputFile, string expectedOutputFile)
|
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));
|
var expectedBytes = File.ReadAllBytes(Path.Join(testDirectory, expectedOutputFile));
|
||||||
Assert.Equal(expectedBytes, _archiveService.GetCoverImage(Path.Join(testDirectory, inputFile)));
|
Assert.Equal(expectedBytes, _archiveService.GetCoverImage(Path.Join(testDirectory, inputFile)));
|
||||||
}
|
}
|
||||||
|
BIN
API.Tests/Services/Test Data/ArchiveService/Archives/empty.zip
Normal file
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 395 KiB After Width: | Height: | Size: 395 KiB |
Before Width: | Height: | Size: 385 KiB After Width: | Height: | Size: 385 KiB |
Before Width: | Height: | Size: 344 KiB After Width: | Height: | Size: 344 KiB |
Before Width: | Height: | Size: 385 KiB After Width: | Height: | Size: 385 KiB |
@ -8,5 +8,6 @@ namespace API.Interfaces
|
|||||||
void ExtractArchive(string archivePath, string extractPath);
|
void ExtractArchive(string archivePath, string extractPath);
|
||||||
int GetNumberOfPagesFromArchive(string archivePath);
|
int GetNumberOfPagesFromArchive(string archivePath);
|
||||||
byte[] GetCoverImage(string filepath, bool createThumbnail = false);
|
byte[] GetCoverImage(string filepath, bool createThumbnail = false);
|
||||||
|
bool IsValidArchive(string archivePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -24,17 +24,13 @@ namespace API.Services
|
|||||||
|
|
||||||
public int GetNumberOfPagesFromArchive(string archivePath)
|
public int GetNumberOfPagesFromArchive(string archivePath)
|
||||||
{
|
{
|
||||||
if (!File.Exists(archivePath) || !Parser.Parser.IsArchive(archivePath))
|
if (!IsValidArchive(archivePath)) return 0;
|
||||||
{
|
|
||||||
_logger.LogError($"Archive {archivePath} could not be found.");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.LogDebug($"Getting Page numbers from {archivePath}");
|
_logger.LogDebug($"Getting Page numbers from {archivePath}");
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using ZipArchive archive = ZipFile.OpenRead(archivePath); // ZIPFILE
|
using ZipArchive archive = ZipFile.OpenRead(archivePath);
|
||||||
return archive.Entries.Count(e => Parser.Parser.IsImage(e.FullName));
|
return archive.Entries.Count(e => Parser.Parser.IsImage(e.FullName));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -42,8 +38,6 @@ namespace API.Services
|
|||||||
_logger.LogError(ex, "There was an exception when reading archive stream.");
|
_logger.LogError(ex, "There was an exception when reading archive stream.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -58,7 +52,8 @@ namespace API.Services
|
|||||||
{
|
{
|
||||||
try
|
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}");
|
_logger.LogDebug($"Extracting Cover image from {filepath}");
|
||||||
using ZipArchive archive = ZipFile.OpenRead(filepath);
|
using ZipArchive archive = ZipFile.OpenRead(filepath);
|
||||||
@ -128,6 +123,31 @@ namespace API.Services
|
|||||||
archive.Entries.Any(e => e.FullName.Contains(Path.AltDirectorySeparatorChar));
|
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>
|
/// <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
|
||||||
@ -138,11 +158,7 @@ namespace API.Services
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public void ExtractArchive(string archivePath, string extractPath)
|
public void ExtractArchive(string archivePath, string extractPath)
|
||||||
{
|
{
|
||||||
if (!File.Exists(archivePath) || !Parser.Parser.IsArchive(archivePath))
|
if (!IsValidArchive(archivePath)) return;
|
||||||
{
|
|
||||||
_logger.LogError($"Archive {archivePath} could not be found.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Directory.Exists(extractPath))
|
if (Directory.Exists(extractPath))
|
||||||
{
|
{
|
||||||
|