mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-07-09 03:04:19 -04:00
Refactored and cleaned up GetCoverImage code.
This commit is contained in:
parent
ec64bf90c0
commit
1d61d1057e
@ -32,7 +32,7 @@ namespace API.Tests.Services
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("non existant file.zip", false)]
|
||||
[InlineData("non existent file.zip", false)]
|
||||
[InlineData("wrong extension.rar", false)]
|
||||
[InlineData("empty.zip", false)]
|
||||
[InlineData("flat file.zip", true)]
|
||||
@ -45,6 +45,20 @@ namespace API.Tests.Services
|
||||
Assert.Equal(expected, _archiveService.IsValidArchive(Path.Join(testDirectory, archivePath)));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("non existent file.zip", 0)]
|
||||
[InlineData("wrong extension.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)]
|
||||
public void GetNumberOfPagesFromArchiveTest(string archivePath, int expected)
|
||||
{
|
||||
var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives");
|
||||
Assert.Equal(expected, _archiveService.GetNumberOfPagesFromArchive(Path.Join(testDirectory, archivePath)));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("v10.cbz", "v10.expected.jpg")]
|
||||
[InlineData("v10 - with folder.cbz", "v10 - with folder.expected.jpg")]
|
||||
|
@ -11,11 +11,12 @@ using NetVips;
|
||||
namespace API.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Responsible for manipulating Archive files. Used by <see cref="CacheService"/> almost exclusively.
|
||||
/// Responsible for manipulating Archive files. Used by <see cref="CacheService"/> and <see cref="ScannerService"/>
|
||||
/// </summary>
|
||||
public class ArchiveService : IArchiveService
|
||||
{
|
||||
private readonly ILogger<ArchiveService> _logger;
|
||||
private const int ThumbnailWidth = 320;
|
||||
|
||||
public ArchiveService(ILogger<ArchiveService> logger)
|
||||
{
|
||||
@ -25,7 +26,6 @@ namespace API.Services
|
||||
public int GetNumberOfPagesFromArchive(string archivePath)
|
||||
{
|
||||
if (!IsValidArchive(archivePath)) return 0;
|
||||
|
||||
_logger.LogDebug($"Getting Page numbers from {archivePath}");
|
||||
|
||||
try
|
||||
@ -53,53 +53,43 @@ namespace API.Services
|
||||
try
|
||||
{
|
||||
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);
|
||||
if (!archive.HasFiles()) return Array.Empty<byte>();
|
||||
|
||||
var folder = archive.Entries.SingleOrDefault(x => Path.GetFileNameWithoutExtension(x.Name).ToLower() == "folder");
|
||||
var entries = archive.Entries.Where(x => Path.HasExtension(x.FullName) && Parser.Parser.IsImage(x.FullName)).OrderBy(x => x.FullName).ToList();
|
||||
ZipArchiveEntry entry;
|
||||
var entry = folder ?? entries[0];
|
||||
|
||||
if (folder != null)
|
||||
{
|
||||
entry = folder;
|
||||
} else if (!entries.Any())
|
||||
{
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
else
|
||||
{
|
||||
entry = entries[0];
|
||||
}
|
||||
|
||||
|
||||
if (createThumbnail)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var stream = entry.Open();
|
||||
var thumbnail = Image.ThumbnailStream(stream, 320);
|
||||
return thumbnail.WriteToBuffer(".jpg");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "There was a critical error and prevented thumbnail generation.");
|
||||
}
|
||||
}
|
||||
|
||||
return ExtractEntryToImage(entry);
|
||||
return createThumbnail ? CreateThumbnail(entry) : ConvertEntryToByteArray(entry);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "There was an exception when reading archive stream.");
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] ExtractEntryToImage(ZipArchiveEntry entry)
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
|
||||
private byte[] CreateThumbnail(ZipArchiveEntry entry)
|
||||
{
|
||||
var coverImage = Array.Empty<byte>();
|
||||
try
|
||||
{
|
||||
using var stream = entry.Open();
|
||||
using var thumbnail = Image.ThumbnailStream(stream, ThumbnailWidth);
|
||||
coverImage = thumbnail.WriteToBuffer(".jpg");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "There was a critical error and prevented thumbnail generation. Defaulting to no cover image.");
|
||||
}
|
||||
|
||||
return coverImage;
|
||||
}
|
||||
|
||||
private static byte[] ConvertEntryToByteArray(ZipArchiveEntry entry)
|
||||
{
|
||||
using var stream = entry.Open();
|
||||
using var ms = new MemoryStream();
|
||||
|
Loading…
x
Reference in New Issue
Block a user