mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-07-09 03:04:19 -04:00
Refactored GetCoverImage to a static class and unit tested it. Enhanced the method by defaulting to folder.jpg file if exists, else takes first entry.
This commit is contained in:
parent
451d459473
commit
7f5a1d0518
@ -23,4 +23,8 @@
|
||||
<ProjectReference Include="..\API\API.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Services\Test Data" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -82,5 +82,15 @@ namespace API.Tests
|
||||
{
|
||||
Assert.Equal(expected, CleanTitle(input));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("test.cbz", true)]
|
||||
[InlineData("test.cbr", true)]
|
||||
[InlineData("test.zip", true)]
|
||||
[InlineData("test.rar", true)]
|
||||
public void IsArchiveTest(string input, bool expected)
|
||||
{
|
||||
Assert.Equal(expected, IsArchive(input));
|
||||
}
|
||||
}
|
||||
}
|
21
API.Tests/Services/ImageProviderTest.cs
Normal file
21
API.Tests/Services/ImageProviderTest.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using API.IO;
|
||||
using Xunit;
|
||||
|
||||
namespace API.Tests.Services
|
||||
{
|
||||
public class ImageProviderTest
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("v10.cbz", "v10.expected.jpg")]
|
||||
[InlineData("v10 - with folder.cbz", "v10 - with folder.expected.jpg")]
|
||||
public void GetCoverImageTest(string inputFile, string expectedOutputFile)
|
||||
{
|
||||
var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ImageProvider");
|
||||
var expectedBytes = File.ReadAllBytes(Path.Join(testDirectory, expectedOutputFile));
|
||||
|
||||
Assert.Equal(expectedBytes, ImageProvider.GetCoverImage(Path.Join(testDirectory, inputFile)));
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 385 KiB |
BIN
API.Tests/Services/Test Data/ImageProvider/v10 - with folder.cbz
Normal file
BIN
API.Tests/Services/Test Data/ImageProvider/v10 - with folder.cbz
Normal file
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 344 KiB |
BIN
API.Tests/Services/Test Data/ImageProvider/v10.cbz
Normal file
BIN
API.Tests/Services/Test Data/ImageProvider/v10.cbz
Normal file
Binary file not shown.
BIN
API.Tests/Services/Test Data/ImageProvider/v10.expected.jpg
Normal file
BIN
API.Tests/Services/Test Data/ImageProvider/v10.expected.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 385 KiB |
45
API/IO/ImageProvider.cs
Normal file
45
API/IO/ImageProvider.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
|
||||
namespace API.IO
|
||||
{
|
||||
public static class ImageProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates byte array of cover image.
|
||||
/// Given a path to a compressed file (zip, rar, cbz, cbr, etc), will ensure the first image is returned unless
|
||||
/// a folder.extension exists in the root directory of the compressed file.
|
||||
/// </summary>
|
||||
/// <param name="filepath"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] GetCoverImage(string filepath)
|
||||
{
|
||||
if (!File.Exists(filepath) || !Parser.Parser.IsArchive(filepath)) return Array.Empty<byte>();
|
||||
|
||||
using ZipArchive archive = ZipFile.OpenRead(filepath);
|
||||
if (archive.Entries.Count <= 0) return Array.Empty<byte>();
|
||||
|
||||
var folder = archive.Entries.SingleOrDefault(x => Path.GetFileNameWithoutExtension(x.Name).ToLower() == "folder");
|
||||
var entry = archive.Entries[0];
|
||||
|
||||
if (folder != null)
|
||||
{
|
||||
entry = folder;
|
||||
}
|
||||
|
||||
return ExtractEntryToImage(entry);
|
||||
}
|
||||
|
||||
private static byte[] ExtractEntryToImage(ZipArchiveEntry entry)
|
||||
{
|
||||
var stream = entry.Open();
|
||||
using var ms = new MemoryStream();
|
||||
stream.CopyTo(ms);
|
||||
var data = ms.ToArray();
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace API.Parser
|
||||
@ -226,5 +227,12 @@ namespace API.Parser
|
||||
{
|
||||
return title.TrimStart(new[] { '0' });
|
||||
}
|
||||
|
||||
public static bool IsArchive(string filePath)
|
||||
{
|
||||
var fileInfo = new FileInfo(filePath);
|
||||
|
||||
return MangaFileExtensions.Contains(fileInfo.Extension);
|
||||
}
|
||||
}
|
||||
}
|
@ -4,13 +4,13 @@ using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using API.Entities;
|
||||
using API.Interfaces;
|
||||
using API.IO;
|
||||
using API.Parser;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
@ -183,7 +183,7 @@ namespace API.Services
|
||||
{
|
||||
Name = info.Volumes,
|
||||
Number = Int32.Parse(info.Volumes),
|
||||
CoverImage = getCoverImage(info.FullFilePath),
|
||||
CoverImage = ImageProvider.GetCoverImage(info.FullFilePath),
|
||||
Files = new List<MangaFile>()
|
||||
{
|
||||
new MangaFile()
|
||||
@ -352,31 +352,8 @@ namespace API.Services
|
||||
Console.WriteLine("Processed {0} files in {1} milliseconds", fileCount, sw.ElapsedMilliseconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates byte array of cover image.
|
||||
/// Looks for first valid image file (folder.png, first jpg/png file)
|
||||
/// </summary>
|
||||
/// <param name="filepath"></param>
|
||||
/// <returns></returns>
|
||||
private byte[] getCoverImage(string filepath)
|
||||
{
|
||||
// TODO: Sort and file type
|
||||
// if file not zip (folder or txt)
|
||||
using (ZipArchive archive = ZipFile.OpenRead(filepath))
|
||||
{
|
||||
|
||||
|
||||
if (archive.Entries.Count <= 0) {return null;}
|
||||
var stream = archive.Entries[0].Open();
|
||||
byte[] data;
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
stream.CopyTo(ms);
|
||||
data = ms.ToArray();
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user