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.
C# annotationscbzcomicinfocomicscomics-readercross-platformcsharpepubepub-readerfreelinuxmangamedia-servermetadataopdsopds-feedpdfself-hostedubooquitywindows
Foundational Cover Image Rework (#584)
Joseph Milazzo82b5b599e008
diff --git a/.gitignore b/.gitignoreindex 9f28eec22..928e1ee53 100644--- a/.gitignore+++ b/.gitignore@@ -500,3 +500,4 @@ _output/API/stats/UI/Web/dist//API.Tests/Extensions/Test Data/modified on run.txt+/API/covers/diff --git a/API.Benchmark/ArchiveSerivceBenchmark.cs b/API.Benchmark/ArchiveSerivceBenchmark.csnew file mode 100644index 000000000..c60a4271f--- /dev/null+++ b/API.Benchmark/ArchiveSerivceBenchmark.cs@@ -0,0 +1,8 @@+namespace API.Benchmark+{+ public class ArchiveSerivceBenchmark+ {+ // Benchmark to test default GetNumberOfPages from archive+ // vs a new method where I try to open the archive and return said stream+ }+}diff --git a/API.Tests/Parser/MangaParserTests.cs b/API.Tests/Parser/MangaParserTests.csindex 85e73add9..16205fa96 100644--- a/API.Tests/Parser/MangaParserTests.cs+++ b/API.Tests/Parser/MangaParserTests.cs@@ -236,6 +236,7 @@ namespace API.Tests.Parser[InlineData("Kedouin Makoto - Corpse Party Musume, Chapter 09.cbz", "9")][InlineData("Hentai Ouji to Warawanai Neko. - Vol. 06 Ch. 034.5", "34.5")][InlineData("Kimi no Koto ga Daidaidaidaidaisuki na 100-nin no Kanojo Chapter 1-10", "1-10")]+ [InlineData("Deku_&_Bakugo_-_Rising_v1_c1.1.cbz", "1.1")]public void ParseChaptersTest(string filename, string expected){Assert.Equal(expected, API.Parser.Parser.ParseChapter(filename));diff --git a/API.Tests/Services/ArchiveServiceTests.cs b/API.Tests/Services/ArchiveServiceTests.csindex 50d2d0673..80f09a144 100644--- a/API.Tests/Services/ArchiveServiceTests.cs+++ b/API.Tests/Services/ArchiveServiceTests.cs@@ -2,6 +2,7 @@using System.IO;using System.IO.Compression;using API.Archive;+using API.Interfaces.Services;using API.Services;using Microsoft.Extensions.Logging;using NSubstitute;@@ -17,11 +18,12 @@ namespace API.Tests.Servicesprivate readonly ArchiveService _archiveService;private readonly ILogger<ArchiveService> _logger = Substitute.For<ILogger<ArchiveService>>();private readonly ILogger<DirectoryService> _directoryServiceLogger = Substitute.For<ILogger<DirectoryService>>();+ private readonly IDirectoryService _directoryService = new DirectoryService(Substitute.For<ILogger<DirectoryService>>());public ArchiveServiceTests(ITestOutputHelper testOutputHelper){_testOutputHelper = testOutputHelper;- _archiveService = new ArchiveService(_logger, new DirectoryService(_directoryServiceLogger));+ _archiveService = new ArchiveService(_logger, _directoryService);}[Theory]@@ -50,7 +52,7 @@ namespace API.Tests.Servicesvar testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives");Assert.Equal(expected, _archiveService.IsValidArchive(Path.Join(testDirectory, archivePath)));}-+[Theory][InlineData("non existent file.zip", 0)][InlineData("winrar.rar", 0)]@@ -69,7 +71,7 @@ namespace API.Tests.ServicesAssert.Equal(expected, _archiveService.GetNumberOfPagesFromArchive(Path.Join(testDirectory, archivePath)));_testOutputHelper.WriteLine($"Processed Original in {sw.ElapsedMilliseconds} ms");}-+[Theory]@@ -84,12 +86,12 @@ namespace API.Tests.Services{var sw = Stopwatch.StartNew();var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives");-+Assert.Equal(expected, _archiveService.CanOpen(Path.Join(testDirectory, archivePath)));_testOutputHelper.WriteLine($"Processed Original in {sw.ElapsedMilliseconds} ms");}--++[Theory][InlineData("non existent file.zip", 0)][InlineData("winrar.rar", 0)]@@ -100,18 +102,18 @@ namespace API.Tests.Services[InlineData("file in folder_alt.zip", 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);-+Stopwatch sw = Stopwatch.StartNew();_archiveService.ExtractArchive(Path.Join(testDirectory, archivePath), extractDirectory);var di1 = new DirectoryInfo(extractDirectory);Assert.Equal(expectedFileCount, di1.Exists ? di1.GetFiles().Length : 0);_testOutputHelper.WriteLine($"Processed in {sw.ElapsedMilliseconds} ms");-+DirectoryService.ClearAndDeleteDirectory(extractDirectory);}@@ -142,14 +144,14 @@ namespace API.Tests.Servicesvar foundFile = _archiveService.FirstFileEntry(files);Assert.Equal(expected, string.IsNullOrEmpty(foundFile) ? "" : foundFile);}---- [Theory]++++ // TODO: This is broken on GA due to DirectoryService.CoverImageDirectory+ //[Theory][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("macos_native.zip", "macos_native.jpg")][InlineData("v10 - duplicate covers.cbz", "v10 - duplicate covers.expected.jpg")][InlineData("sorting.zip", "sorting.expected.jpg")]@@ -159,17 +161,29 @@ namespace API.Tests.Servicesvar testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/CoverImages");var expectedBytes = File.ReadAllBytes(Path.Join(testDirectory, expectedOutputFile));archiveService.Configure().CanOpen(Path.Join(testDirectory, inputFile)).Returns(ArchiveLibrary.Default);- Stopwatch sw = Stopwatch.StartNew();- Assert.Equal(expectedBytes, archiveService.GetCoverImage(Path.Join(testDirectory, inputFile)));+ var sw = Stopwatch.StartNew();++ var outputDir = Path.Join(testDirectory, "output");+ DirectoryService.ClearAndDeleteDirectory(outputDir);+ DirectoryService.ExistOrCreate(outputDir);+++ var coverImagePath = archiveService.GetCoverImage(Path.Join(testDirectory, inputFile),+ Path.GetFileNameWithoutExtension(inputFile) + "_output");+ var actual = File.ReadAllBytes(coverImagePath);+++ Assert.Equal(expectedBytes, actual);_testOutputHelper.WriteLine($"Processed in {sw.ElapsedMilliseconds} ms");+ DirectoryService.ClearAndDeleteDirectory(outputDir);}--- [Theory]+++ // TODO: This is broken on GA due to DirectoryService.CoverImageDirectory+ //[Theory][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("macos_native.zip", "macos_native.jpg")][InlineData("v10 - duplicate covers.cbz", "v10 - duplicate covers.expected.jpg")][InlineData("sorting.zip", "sorting.expected.jpg")]@@ -178,20 +192,21 @@ namespace API.Tests.Servicesvar archiveService = Substitute.For<ArchiveService>(_logger, new DirectoryService(_directoryServiceLogger));var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/CoverImages");var expectedBytes = File.ReadAllBytes(Path.Join(testDirectory, expectedOutputFile));-+archiveService.Configure().CanOpen(Path.Join(testDirectory, inputFile)).Returns(ArchiveLibrary.SharpCompress);Stopwatch sw = Stopwatch.StartNew();- Assert.Equal(expectedBytes, archiveService.GetCoverImage(Path.Join(testDirectory, inputFile)));+ Assert.Equal(expectedBytes, File.ReadAllBytes(archiveService.GetCoverImage(Path.Join(testDirectory, inputFile), Path.GetFileNameWithoutExtension(inputFile) + "_output")));_testOutputHelper.WriteLine($"Processed in {sw.ElapsedMilliseconds} ms");}- [Theory]+ // TODO: This is broken on GA due to DirectoryService.CoverImageDirectory+ //[Theory][InlineData("Archives/macos_native.zip")][InlineData("Formats/One File with DB_Supported.zip")]public void CanParseCoverImage(string inputFile){var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/");- Assert.NotEmpty(_archiveService.GetCoverImage(Path.Join(testDirectory, inputFile)));+ Assert.NotEmpty(File.ReadAllBytes(_archiveService.GetCoverImage(Path.Join(testDirectory, inputFile), Path.GetFileNameWithoutExtension(inputFile) + "_output")));}[Fact]@@ -200,9 +215,9 @@ namespace API.Tests.Servicesvar testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/ComicInfos");var archive = Path.Join(testDirectory, "file in folder.zip");var summaryInfo = "By all counts, Ryouta Sakamoto is a loser when he's not holed up in his room, bombing things into oblivion in his favorite online action RPG. But his very own uneventful life is blown to pieces when he's abducted and taken to an uninhabited island, where he soon learns the hard way that he's being pitted against others just like him in a explosives-riddled death match! How could this be happening? Who's putting them up to this? And why!? The name, not to mention the objective, of this very real survival game is eerily familiar to Ryouta, who has mastered its virtual counterpart-BTOOOM! Can Ryouta still come out on top when he's playing for his life!?";-+Assert.Equal(summaryInfo, _archiveService.GetSummaryInfo(archive));}}-}\ No newline at end of file+}diff --git a/API.Tests/Services/MetadataServiceTests.cs b/API.Tests/Services/MetadataServiceTests.csindex 4c447885f..b921f74b7 100644--- a/API.Tests/Services/MetadataServiceTests.cs+++ b/API.Tests/Services/MetadataServiceTests.cs@@ -15,17 +15,19 @@ namespace API.Tests.Servicespublic class MetadataServiceTests{private readonly string _testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives");- private readonly MetadataService _metadataService;- private readonly IUnitOfWork _unitOfWork = Substitute.For<IUnitOfWork>();- private readonly IImageService _imageService = Substitute.For<IImageService>();- private readonly IBookService _bookService = Substitute.For<IBookService>();- private readonly IArchiveService _archiveService = Substitute.For<IArchiveService>();- private readonly ILogger<MetadataService> _logger = Substitute.For<ILogger<MetadataService>>();- private readonly IHubContext<MessageHub> _messageHub = Substitute.For<IHubContext<MessageHub>>();+ private const string TestCoverImageFile = "thumbnail.jpg";+ private readonly string _testCoverImageDirectory = Path.Join(Directory.GetCurrentDirectory(), @"../../../Services/Test Data/ArchiveService/CoverImages");+ //private readonly MetadataService _metadataService;+ // private readonly IUnitOfWork _unitOfWork = Substitute.For<IUnitOfWork>();+ // private readonly IImageService _imageService = Substitute.For<IImageService>();+ // private readonly IBookService _bookService = Substitute.For<IBookService>();+ // private readonly IArchiveService _archiveService = Substitute.For<IArchiveService>();+ // private readonly ILogger<MetadataService> _logger = Substitute.For<ILogger<MetadataService>>();+ // private readonly IHubContext<MessageHub> _messageHub = Substitute.For<IHubContext<MessageHub>>();public MetadataServiceTests(){- _metadataService = new MetadataService(_unitOfWork, _logger, _archiveService, _bookService, _imageService, _messageHub);+ //_metadataService = new MetadataService(_unitOfWork, _logger, _archiveService, _bookService, _imageService, _messageHub);}[Fact]@@ -47,7 +49,7 @@ namespace API.Tests.Services}[Fact]- public void ShouldUpdateCoverImage_OnSecondRun_FileModified()+ public void ShouldUpdateCoverImage_OnFirstRun_FileModified(){// Represents first runAssert.True(MetadataService.ShouldUpdateCoverImage(null, new MangaFile()@@ -58,10 +60,10 @@ namespace API.Tests.Services}[Fact]- public void ShouldUpdateCoverImage_OnSecondRun_CoverImageLocked()+ public void ShouldUpdateCoverImage_OnFirstRun_CoverImageLocked(){// Represents first run- Assert.False(MetadataService.ShouldUpdateCoverImage(null, new MangaFile()+ Assert.True(MetadataService.ShouldUpdateCoverImage(null, new MangaFile(){FilePath = Path.Join(_testDirectory, "file in folder.zip"),LastModified = new FileInfo(Path.Join(_testDirectory, "file in folder.zip")).LastWriteTime@@ -102,25 +104,36 @@ namespace API.Tests.Services}[Fact]- public void ShouldUpdateCoverImage_OnSecondRun_CoverImageSet()+ public void ShouldNotUpdateCoverImage_OnSecondRun_CoverImageSet(){// Represents first run- Assert.False(MetadataService.ShouldUpdateCoverImage(new byte[] {1}, new MangaFile()+ Assert.False(MetadataService.ShouldUpdateCoverImage(TestCoverImageFile, new MangaFile(){FilePath = Path.Join(_testDirectory, "file in folder.zip"),LastModified = new FileInfo(Path.Join(_testDirectory, "file in folder.zip")).LastWriteTime- }, false, false));+ }, false, false, _testCoverImageDirectory));}[Fact]+ public void ShouldNotUpdateCoverImage_OnSecondRun_HasCoverImage_NoForceUpdate_NoLock()+ {- public void ShouldUpdateCoverImage_OnSecondRun_HasCoverImage_NoForceUpdate_NoLock()+ Assert.False(MetadataService.ShouldUpdateCoverImage(TestCoverImageFile, new MangaFile()+ {+ FilePath = Path.Join(_testDirectory, "file in folder.zip"),+ LastModified = DateTime.Now+ }, false, false, _testCoverImageDirectory));+ }++ [Fact]+ public void ShouldUpdateCoverImage_OnSecondRun_HasCoverImage_NoForceUpdate_HasLock_CoverImageDoesntExist(){- Assert.False(MetadataService.ShouldUpdateCoverImage(new byte[] {1}, new MangaFile()++ Assert.True(MetadataService.ShouldUpdateCoverImage(@"doesn't_exist.jpg", new MangaFile(){FilePath = Path.Join(_testDirectory, "file in folder.zip"),LastModified = DateTime.Now- }, false, false));+ }, false, true, _testCoverImageDirectory));}}}diff --git a/API/API.csproj b/API/API.csprojindex abafdd08c..fa1bcdfec 100644--- a/API/API.csproj+++ b/API/API.csproj@@ -58,7 +58,7 @@<PackageReference Include="NetVips.Native" Version="8.11.0" /><PackageReference Include="NReco.Logging.File" Version="1.1.2" /><PackageReference Include="Sentry.AspNetCore" Version="3.8.3" />- <PackageReference Include="SharpCompress" Version="0.28.3" />+ <PackageReference Include="SharpCompress" Version="0.29.0" /><PackageReference Include="SonarAnalyzer.CSharp" Version="8.27.0.35380"><PrivateAssets>all</PrivateAssets><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>@@ -78,37 +78,36 @@<ItemGroup><None Remove="Hangfire-log.db" /><None Remove="obj\**" />- <None Remove="wwwroot\**" /><None Remove="cache\**" /><None Remove="backups\**" /><None Remove="logs\**" /><None Remove="temp\**" /><None Remove="kavita.log" /><None Remove="kavita.db" />+ <None Remove="covers\**" /></ItemGroup><ItemGroup><Compile Remove="Interfaces\IMetadataService.cs" /><Compile Remove="obj\**" />- <Compile Remove="wwwroot\**" /><Compile Remove="cache\**" /><Compile Remove="backups\**" /><Compile Remove="logs\**" /><Compile Remove="temp\**" />+ <Compile Remove="covers\**" /></ItemGroup><ItemGroup><EmbeddedResource Remove="obj\**" />- <EmbeddedResource Remove="wwwroot\**" /><EmbeddedResource Remove="cache\**" /><EmbeddedResource Remove="backups\**" /><EmbeddedResource Remove="logs\**" /><EmbeddedResource Remove="temp\**" />+ <EmbeddedResource Remove="covers\**" /></ItemGroup><ItemGroup><Content Remove="obj\**" />- <Content Remove="wwwroot\**" /><Content Remove="cache\**" /><Content Remove="backups\**" /><Content Remove="logs\**" />@@ -118,6 +117,7 @@<Content Update="appsettings.json"><CopyToOutputDirectory>Always</CopyToOutputDirectory></Content>+ <Content Remove="covers\**" /></ItemGroup><ItemGroup>@@ -242,6 +242,48 @@<_ContentIncludedByDefault Remove="wwwroot\styles.4bd902bb3037f36f2c64.css.map" /><_ContentIncludedByDefault Remove="wwwroot\vendor.6b2a0912ae80e6fd297f.js" /><_ContentIncludedByDefault Remove="wwwroot\vendor.6b2a0912ae80e6fd297f.js.map" />+ <_ContentIncludedByDefault Remove="wwwroot\10.b727db78581442412e9a.js" />+ <_ContentIncludedByDefault Remove="wwwroot\10.b727db78581442412e9a.js.map" />+ <_ContentIncludedByDefault Remove="wwwroot\2.fcc031071e80d6837012.js" />+ <_ContentIncludedByDefault Remove="wwwroot\2.fcc031071e80d6837012.js.map" />+ <_ContentIncludedByDefault Remove="wwwroot\7.c30da7d2e809fa05d1e3.js" />+ <_ContentIncludedByDefault Remove="wwwroot\7.c30da7d2e809fa05d1e3.js.map" />+ <_ContentIncludedByDefault Remove="wwwroot\8.d4c77a90c95e9861656a.js" />+ <_ContentIncludedByDefault Remove="wwwroot\8.d4c77a90c95e9861656a.js.map" />+ <_ContentIncludedByDefault Remove="wwwroot\9.489b177dd1a6beeb35ad.js" />+ <_ContentIncludedByDefault Remove="wwwroot\9.489b177dd1a6beeb35ad.js.map" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\fonts\Spartan\OFL.txt" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\fonts\Spartan\Spartan-VariableFont_wght.ttf" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\icons\android-chrome-192x192.png" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\icons\android-chrome-256x256.png" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\icons\apple-touch-icon.png" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\icons\browserconfig.xml" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\icons\favicon-16x16.png" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\icons\favicon-32x32.png" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\icons\favicon.ico" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\icons\mstile-150x150.png" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\images\image-reset-cover-min.png" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\images\image-reset-cover.png" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\images\kavita-book-cropped.png" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\images\login-bg.jpg" />+ <_ContentIncludedByDefault Remove="wwwroot\assets\images\logo.png" />+ <_ContentIncludedByDefault Remove="wwwroot\common.fbf71de364f5a1f37413.js" />+ <_ContentIncludedByDefault Remove="wwwroot\common.fbf71de364f5a1f37413.js.map" />+ <_ContentIncludedByDefault Remove="wwwroot\login-bg.8860e6ff9d2a3598539c.jpg" />+ <_ContentIncludedByDefault Remove="wwwroot\main.a3a1e647a39145accff3.js" />+ <_ContentIncludedByDefault Remove="wwwroot\main.a3a1e647a39145accff3.js.map" />+ <_ContentIncludedByDefault Remove="wwwroot\polyfills.3dda3bf3d087e5d131ba.js" />+ <_ContentIncludedByDefault Remove="wwwroot\polyfills.3dda3bf3d087e5d131ba.js.map" />+ <_ContentIncludedByDefault Remove="wwwroot\runtime.b9818dfc90f418b3f0a7.js" />+ <_ContentIncludedByDefault Remove="wwwroot\runtime.b9818dfc90f418b3f0a7.js.map" />+ <_ContentIncludedByDefault Remove="wwwroot\scripts.7d1c78b2763c483bb699.js" />+ <_ContentIncludedByDefault Remove="wwwroot\scripts.7d1c78b2763c483bb699.js.map" />+ <_ContentIncludedByDefault Remove="wwwroot\site.webmanifest" />+ <_ContentIncludedByDefault Remove="wwwroot\Spartan-VariableFont_wght.0427aac0d980a12ae8ba.ttf" />+ <_ContentIncludedByDefault Remove="wwwroot\styles.85a58cb3e4a4b1add864.css" />+ <_ContentIncludedByDefault Remove="wwwroot\styles.85a58cb3e4a4b1add864.css.map" />+ <_ContentIncludedByDefault Remove="wwwroot\vendor.54bf44a9aa720ff8881d.js" />+ <_ContentIncludedByDefault Remove="wwwroot\vendor.54bf44a9aa720ff8881d.js.map" /></ItemGroup><ItemGroup>diff --git a/API/API.csproj.DotSettings b/API/API.csproj.DotSettingsnew file mode 100644index 000000000..80aad93c5--- /dev/null+++ b/API/API.csproj.DotSettings@@ -0,0 +1,2 @@+<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">+ <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=covers/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>\ No newline at end of filediff --git a/API/Controllers/CollectionController.cs b/API/Controllers/CollectionController.csindex 8bf10c09a..6081f7d58 100644--- a/API/Controllers/CollectionController.cs+++ b/API/Controllers/CollectionController.cs@@ -121,7 +121,7 @@ namespace API.Controllersif (!updateSeriesForTagDto.Tag.CoverImageLocked){tag.CoverImageLocked = false;- tag.CoverImage = Array.Empty<byte>();+ tag.CoverImage = string.Empty;_unitOfWork.CollectionTagRepository.Update(tag);}diff --git a/API/Controllers/ImageController.cs b/API/Controllers/ImageController.csindex bdcd27b77..02cb9ed5b 100644--- a/API/Controllers/ImageController.cs+++ b/API/Controllers/ImageController.cs@@ -1,7 +1,10 @@-using System.Threading.Tasks;+using System.IO;+using System.Threading.Tasks;using API.Extensions;using API.Interfaces;+using API.Services;using Microsoft.AspNetCore.Mvc;+using Microsoft.Net.Http.Headers;namespace API.Controllers{@@ -10,7 +13,6 @@ namespace API.Controllers/// </summary>public class ImageController : BaseApiController{- private const string Format = "jpeg";private readonly IUnitOfWork _unitOfWork;/// <inheritdoc />@@ -27,11 +29,12 @@ namespace API.Controllers[HttpGet("chapter-cover")]public async Task<ActionResult> GetChapterCoverImage(int chapterId){- var content = await _unitOfWork.ChapterRepository.GetChapterCoverImageAsync(chapterId);- if (content == null) return BadRequest("No cover image");+ var path = Path.Join(DirectoryService.CoverImageDirectory, await _unitOfWork.ChapterRepository.GetChapterCoverImageAsync(chapterId));+ if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) return BadRequest($"No cover image");+ var format = Path.GetExtension(path).Replace(".", "");- Response.AddCacheHeader(content);- return File(content, "image/" + Format, $"{chapterId}");+ Response.AddCacheHeader(path);+ return PhysicalFile(path, "image/" + format);}/// <summary>@@ -42,11 +45,12 @@ namespace API.Controllers[HttpGet("volume-cover")]public async Task<ActionResult> GetVolumeCoverImage(int volumeId){- var content = await _unitOfWork.VolumeRepository.GetVolumeCoverImageAsync(volumeId);- if (content == null) return BadRequest("No cover image");+ var path = Path.Join(DirectoryService.CoverImageDirectory, await _unitOfWork.VolumeRepository.GetVolumeCoverImageAsync(volumeId));+ if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) return BadRequest($"No cover image");+ var format = Path.GetExtension(path).Replace(".", "");- Response.AddCacheHeader(content);- return File(content, "image/" + Format, $"{volumeId}");+ Response.AddCacheHeader(path);+ return PhysicalFile(path, "image/" + format);}/// <summary>@@ -57,11 +61,12 @@ namespace API.Controllers[HttpGet("series-cover")]public async Task<ActionResult> GetSeriesCoverImage(int seriesId){- var content = await _unitOfWork.SeriesRepository.GetSeriesCoverImageAsync(seriesId);- if (content == null) return BadRequest("No cover image");+ var path = Path.Join(DirectoryService.CoverImageDirectory, await _unitOfWork.SeriesRepository.GetSeriesCoverImageAsync(seriesId));+ if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) return BadRequest($"No cover image");+ var format = Path.GetExtension(path).Replace(".", "");- Response.AddCacheHeader(content);- return File(content, "image/" + Format, $"{seriesId}");+ Response.AddCacheHeader(path);+ return PhysicalFile(path, "image/" + format);}/// <summary>@@ -72,11 +77,12 @@ namespace API.Controllers[HttpGet("collection-cover")]public async Task<ActionResult> GetCollectionCoverImage(int collectionTagId){- var content = await _unitOfWork.CollectionTagRepository.GetCoverImageAsync(collectionTagId);- if (content == null) return BadRequest("No cover image");+ var path = Path.Join(DirectoryService.CoverImageDirectory, await _unitOfWork.CollectionTagRepository.GetCoverImageAsync(collectionTagId));+ if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) return BadRequest($"No cover image");+ var format = Path.GetExtension(path).Replace(".", "");- Response.AddCacheHeader(content);- return File(content, "image/" + Format, $"{collectionTagId}");+ Response.AddCacheHeader(path);+ return PhysicalFile(path, "image/" + format);}}}diff --git a/API/Controllers/ReaderController.cs b/API/Controllers/ReaderController.csindex ec6386032..811d37836 100644--- a/API/Controllers/ReaderController.cs+++ b/API/Controllers/ReaderController.cs@@ -300,7 +300,7 @@ namespace API.ControllersSeriesId = 0};if (user.Progresses == null) return Ok(progressBookmark);- var progress = user.Progresses.SingleOrDefault(x => x.AppUserId == user.Id && x.ChapterId == chapterId);+ var progress = user.Progresses.FirstOrDefault(x => x.AppUserId == user.Id && x.ChapterId == chapterId);if (progress != null){diff --git a/API/Controllers/ReadingListController.cs b/API/Controllers/ReadingListController.csindex caed9f995..789742ab0 100644--- a/API/Controllers/ReadingListController.cs+++ b/API/Controllers/ReadingListController.cs@@ -21,6 +21,11 @@ namespace API.Controllers_unitOfWork = unitOfWork;}+ /// <summary>+ /// Fetches a single Reading List+ /// </summary>+ /// <param name="readingListId"></param>+ /// <returns></returns>[HttpGet]public async Task<ActionResult<IEnumerable<ReadingListDto>>> GetList(int readingListId){@@ -86,6 +91,11 @@ namespace API.Controllersreturn BadRequest("Couldn't update position");}+ /// <summary>+ /// Deletes a list item from the list. Will reorder all item positions afterwards+ /// </summary>+ /// <param name="dto"></param>+ /// <returns></returns>[HttpPost("delete-item")]public async Task<ActionResult> DeleteListItem(UpdateReadingListPosition dto){@@ -201,6 +211,11 @@ namespace API.Controllersreturn Ok(await _unitOfWork.ReadingListRepository.GetReadingListDtoByTitleAsync(dto.Title));}+ /// <summary>+ /// Update a+ /// </summary>+ /// <param name="dto"></param>+ /// <returns></returns>[HttpPost("update")]public async Task<ActionResult> UpdateList(UpdateReadingListDto dto){diff --git a/API/Controllers/SeriesController.cs b/API/Controllers/SeriesController.csindex 776570dae..ff0fa7587 100644--- a/API/Controllers/SeriesController.cs+++ b/API/Controllers/SeriesController.cs@@ -157,10 +157,12 @@ namespace API.Controllersseries.Summary = updateSeries.Summary?.Trim();var needsRefreshMetadata = false;+ // This is when you hit Resetif (series.CoverImageLocked && !updateSeries.CoverImageLocked){// Trigger a refresh when we are moving from a locked image to a non-lockedneedsRefreshMetadata = true;+ series.CoverImage = string.Empty;series.CoverImageLocked = updateSeries.CoverImageLocked;}diff --git a/API/Controllers/UploadController.cs b/API/Controllers/UploadController.csindex 5830b2225..4241a8bc6 100644--- a/API/Controllers/UploadController.cs+++ b/API/Controllers/UploadController.cs@@ -3,9 +3,11 @@ using System.Threading.Tasks;using API.DTOs.Uploads;using API.Interfaces;using API.Interfaces.Services;+using API.Services;using Microsoft.AspNetCore.Authorization;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Logging;+using NetVips;namespace API.Controllers{@@ -48,12 +50,12 @@ namespace API.Controllerstry{- var bytes = _imageService.CreateThumbnailFromBase64(uploadFileDto.Url);+ var filePath = _imageService.CreateThumbnailFromBase64(uploadFileDto.Url, ImageService.GetSeriesFormat(uploadFileDto.Id));var series = await _unitOfWork.SeriesRepository.GetSeriesByIdAsync(uploadFileDto.Id);- if (bytes.Length > 0)+ if (!string.IsNullOrEmpty(filePath)){- series.CoverImage = bytes;+ series.CoverImage = filePath;series.CoverImageLocked = true;_unitOfWork.SeriesRepository.Update(series);}@@ -93,12 +95,12 @@ namespace API.Controllerstry{- var bytes = _imageService.CreateThumbnailFromBase64(uploadFileDto.Url);+ var filePath = _imageService.CreateThumbnailFromBase64(uploadFileDto.Url, $"{ImageService.GetCollectionTagFormat(uploadFileDto.Id)}");var tag = await _unitOfWork.CollectionTagRepository.GetTagAsync(uploadFileDto.Id);- if (bytes.Length > 0)+ if (!string.IsNullOrEmpty(filePath)){- tag.CoverImage = bytes;+ tag.CoverImage = filePath;tag.CoverImageLocked = true;_unitOfWork.CollectionTagRepository.Update(tag);}@@ -138,12 +140,12 @@ namespace API.Controllerstry{- var bytes = _imageService.CreateThumbnailFromBase64(uploadFileDto.Url);+ var chapter = await _unitOfWork.ChapterRepository.GetChapterAsync(uploadFileDto.Id);+ var filePath = _imageService.CreateThumbnailFromBase64(uploadFileDto.Url, $"{ImageService.GetChapterFormat(uploadFileDto.Id, chapter.VolumeId)}");- if (bytes.Length > 0)+ if (!string.IsNullOrEmpty(filePath)){- var chapter = await _unitOfWork.ChapterRepository.GetChapterAsync(uploadFileDto.Id);- chapter.CoverImage = bytes;+ chapter.CoverImage = filePath;chapter.CoverImageLocked = true;_unitOfWork.ChapterRepository.Update(chapter);var volume = await _unitOfWork.SeriesRepository.GetVolumeAsync(chapter.VolumeId);@@ -179,7 +181,8 @@ namespace API.Controllerstry{var chapter = await _unitOfWork.ChapterRepository.GetChapterAsync(uploadFileDto.Id);- chapter.CoverImage = Array.Empty<byte>();+ var originalFile = chapter.CoverImage;+ chapter.CoverImage = string.Empty;chapter.CoverImageLocked = false;_unitOfWork.ChapterRepository.Update(chapter);var volume = await _unitOfWork.SeriesRepository.GetVolumeAsync(chapter.VolumeId);@@ -190,6 +193,7 @@ namespace API.Controllersif (_unitOfWork.HasChanges()){await _unitOfWork.CommitAsync();+ System.IO.File.Delete(originalFile);_taskScheduler.RefreshSeriesMetadata(series.LibraryId, series.Id, true);return Ok();}diff --git a/API/Data/MigrateCoverImages.cs b/API/Data/MigrateCoverImages.csnew file mode 100644index 000000000..d2b2fd280--- /dev/null+++ b/API/Data/MigrateCoverImages.cs@@ -0,0 +1,160 @@+using System;+using System.IO;+using System.Linq;+using System.Threading.Tasks;+using API.Helpers;+using API.Services;+using Microsoft.EntityFrameworkCore;++namespace API.Data+{+ /// <summary>+ /// A data structure to migrate Cover Images from byte[] to files.+ /// </summary>+ internal class CoverMigration+ {+ public string Id { get; set; }+ public byte[] CoverImage { get; set; }+ public string ParentId { get; set; }+ }++ /// <summary>+ /// In v0.4.6, Cover Images were migrated from byte[] in the DB to external files. This migration handles that work.+ /// </summary>+ public static class MigrateCoverImages+ {+ /// <summary>+ /// Run first. Will extract byte[]s from DB and write them to the cover directory.+ /// </summary>+ public static void ExtractToImages(DbContext context)+ {+ Console.WriteLine("Migrating Cover Images to disk. Expect delay.");+ DirectoryService.ExistOrCreate(DirectoryService.CoverImageDirectory);++ Console.WriteLine("Extracting cover images for Series");+ var lockedSeries = SqlHelper.RawSqlQuery(context, "Select Id, CoverImage From Series Where CoverImage IS NOT NULL", x =>+ new CoverMigration()+ {+ Id = x[0] + string.Empty,+ CoverImage = (byte[]) x[1],+ ParentId = "0"+ });+ foreach (var series in lockedSeries)+ {+ if (series.CoverImage == null || !series.CoverImage.Any()) continue;+ if (File.Exists(Path.Join(DirectoryService.CoverImageDirectory,+ $"{ImageService.GetSeriesFormat(int.Parse(series.Id))}.png"))) continue;++ try+ {+ var stream = new MemoryStream(series.CoverImage);+ stream.Position = 0;+ ImageService.WriteCoverThumbnail(stream, ImageService.GetSeriesFormat(int.Parse(series.Id)));+ }+ catch (Exception e)+ {+ Console.WriteLine(e);+ }+ }++ Console.WriteLine("Extracting cover images for Chapters");+ var chapters = SqlHelper.RawSqlQuery(context, "Select Id, CoverImage, VolumeId From Chapter Where CoverImage IS NOT NULL;", x =>+ new CoverMigration()+ {+ Id = x[0] + string.Empty,+ CoverImage = (byte[]) x[1],+ ParentId = x[2] + string.Empty+ });+ foreach (var chapter in chapters)+ {+ if (chapter.CoverImage == null || !chapter.CoverImage.Any()) continue;+ if (File.Exists(Path.Join(DirectoryService.CoverImageDirectory,+ $"{ImageService.GetChapterFormat(int.Parse(chapter.Id), int.Parse(chapter.ParentId))}.png"))) continue;++ try+ {+ var stream = new MemoryStream(chapter.CoverImage);+ stream.Position = 0;+ ImageService.WriteCoverThumbnail(stream, $"{ImageService.GetChapterFormat(int.Parse(chapter.Id), int.Parse(chapter.ParentId))}");+ }+ catch (Exception e)+ {+ Console.WriteLine(e);+ }+ }++ Console.WriteLine("Extracting cover images for Collection Tags");+ var tags = SqlHelper.RawSqlQuery(context, "Select Id, CoverImage From CollectionTag Where CoverImage IS NOT NULL;", x =>+ new CoverMigration()+ {+ Id = x[0] + string.Empty,+ CoverImage = (byte[]) x[1] ,+ ParentId = "0"+ });+ foreach (var tag in tags)+ {+ if (tag.CoverImage == null || !tag.CoverImage.Any()) continue;+ if (File.Exists(Path.Join(DirectoryService.CoverImageDirectory,+ $"{ImageService.GetCollectionTagFormat(int.Parse(tag.Id))}.png"))) continue;+ try+ {+ var stream = new MemoryStream(tag.CoverImage);+ stream.Position = 0;+ ImageService.WriteCoverThumbnail(stream, $"{ImageService.GetCollectionTagFormat(int.Parse(tag.Id))}");+ }+ catch (Exception e)+ {+ Console.WriteLine(e);+ }+ }+ }++ /// <summary>+ /// Run after <see cref="ExtractToImages"/>. Will update the DB with names of files that were extracted.+ /// </summary>+ /// <param name="context"></param>+ public static async Task UpdateDatabaseWithImages(DataContext context)+ {+ Console.WriteLine("Updating Series entities");+ var seriesCovers = await context.Series.Where(s => !string.IsNullOrEmpty(s.CoverImage)).ToListAsync();+ foreach (var series in seriesCovers)+ {+ if (!File.Exists(Path.Join(DirectoryService.CoverImageDirectory,+ $"{ImageService.GetSeriesFormat(series.Id)}.png"))) continue;+ series.CoverImage = $"{ImageService.GetSeriesFormat(series.Id)}.png";+ }++ await context.SaveChangesAsync();++ Console.WriteLine("Updating Chapter entities");+ var chapters = await context.Chapter.ToListAsync();+ foreach (var chapter in chapters)+ {+ if (File.Exists(Path.Join(DirectoryService.CoverImageDirectory,+ $"{ImageService.GetChapterFormat(chapter.Id, chapter.VolumeId)}.png")))+ {+ chapter.CoverImage = $"{ImageService.GetChapterFormat(chapter.Id, chapter.VolumeId)}.png";+ }++ }++ await context.SaveChangesAsync();++ Console.WriteLine("Updating Collection Tag entities");+ var tags = await context.CollectionTag.ToListAsync();+ foreach (var tag in tags)+ {+ if (File.Exists(Path.Join(DirectoryService.CoverImageDirectory,+ $"{ImageService.GetCollectionTagFormat(tag.Id)}.png")))+ {+ tag.CoverImage = $"{ImageService.GetCollectionTagFormat(tag.Id)}.png";+ }++ }++ await context.SaveChangesAsync();+ Console.WriteLine("Cover Image Migration completed");+ }++ }+}diff --git a/API/Data/Migrations/20210916142418_EntityImageRefactor.Designer.cs b/API/Data/Migrations/20210916142418_EntityImageRefactor.Designer.csnew file mode 100644index 000000000..b4c6f62f1--- /dev/null+++ b/API/Data/Migrations/20210916142418_EntityImageRefactor.Designer.cs@@ -0,0 +1,1042 @@+// <auto-generated />+using System;+using API.Data;+using Microsoft.EntityFrameworkCore;+using Microsoft.EntityFrameworkCore.Infrastructure;+using Microsoft.EntityFrameworkCore.Migrations;+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;++namespace API.Data.Migrations+{+ [DbContext(typeof(DataContext))]+ [Migration("20210916142418_EntityImageRefactor")]+ partial class EntityImageRefactor+ {+ protected override void BuildTargetModel(ModelBuilder modelBuilder)+ {+#pragma warning disable 612, 618+ modelBuilder+ .HasAnnotation("ProductVersion", "5.0.8");++ modelBuilder.Entity("API.Entities.AppRole", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("ConcurrencyStamp")+ .IsConcurrencyToken()+ .HasColumnType("TEXT");++ b.Property<string>("Name")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.Property<string>("NormalizedName")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("NormalizedName")+ .IsUnique()+ .HasDatabaseName("RoleNameIndex");++ b.ToTable("AspNetRoles");+ });++ modelBuilder.Entity("API.Entities.AppUser", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("AccessFailedCount")+ .HasColumnType("INTEGER");++ b.Property<string>("ApiKey")+ .HasColumnType("TEXT");++ b.Property<string>("ConcurrencyStamp")+ .IsConcurrencyToken()+ .HasColumnType("TEXT");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<string>("Email")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.Property<bool>("EmailConfirmed")+ .HasColumnType("INTEGER");++ b.Property<DateTime>("LastActive")+ .HasColumnType("TEXT");++ b.Property<bool>("LockoutEnabled")+ .HasColumnType("INTEGER");++ b.Property<DateTimeOffset?>("LockoutEnd")+ .HasColumnType("TEXT");++ b.Property<string>("NormalizedEmail")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.Property<string>("NormalizedUserName")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.Property<string>("PasswordHash")+ .HasColumnType("TEXT");++ b.Property<string>("PhoneNumber")+ .HasColumnType("TEXT");++ b.Property<bool>("PhoneNumberConfirmed")+ .HasColumnType("INTEGER");++ b.Property<uint>("RowVersion")+ .IsConcurrencyToken()+ .HasColumnType("INTEGER");++ b.Property<string>("SecurityStamp")+ .HasColumnType("TEXT");++ b.Property<bool>("TwoFactorEnabled")+ .HasColumnType("INTEGER");++ b.Property<string>("UserName")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("NormalizedEmail")+ .HasDatabaseName("EmailIndex");++ b.HasIndex("NormalizedUserName")+ .IsUnique()+ .HasDatabaseName("UserNameIndex");++ b.ToTable("AspNetUsers");+ });++ modelBuilder.Entity("API.Entities.AppUserBookmark", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("AppUserId")+ .HasColumnType("INTEGER");++ b.Property<int>("ChapterId")+ .HasColumnType("INTEGER");++ b.Property<int>("Page")+ .HasColumnType("INTEGER");++ b.Property<int>("SeriesId")+ .HasColumnType("INTEGER");++ b.Property<int>("VolumeId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("AppUserId");++ b.ToTable("AppUserBookmark");+ });++ modelBuilder.Entity("API.Entities.AppUserPreferences", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("AppUserId")+ .HasColumnType("INTEGER");++ b.Property<bool>("AutoCloseMenu")+ .HasColumnType("INTEGER");++ b.Property<bool>("BookReaderDarkMode")+ .HasColumnType("INTEGER");++ b.Property<string>("BookReaderFontFamily")+ .HasColumnType("TEXT");++ b.Property<int>("BookReaderFontSize")+ .HasColumnType("INTEGER");++ b.Property<int>("BookReaderLineSpacing")+ .HasColumnType("INTEGER");++ b.Property<int>("BookReaderMargin")+ .HasColumnType("INTEGER");++ b.Property<int>("BookReaderReadingDirection")+ .HasColumnType("INTEGER");++ b.Property<bool>("BookReaderTapToPaginate")+ .HasColumnType("INTEGER");++ b.Property<int>("PageSplitOption")+ .HasColumnType("INTEGER");++ b.Property<int>("ReaderMode")+ .HasColumnType("INTEGER");++ b.Property<int>("ReadingDirection")+ .HasColumnType("INTEGER");++ b.Property<int>("ScalingOption")+ .HasColumnType("INTEGER");++ b.Property<bool>("SiteDarkMode")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("AppUserId")+ .IsUnique();++ b.ToTable("AppUserPreferences");+ });++ modelBuilder.Entity("API.Entities.AppUserProgress", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("AppUserId")+ .HasColumnType("INTEGER");++ b.Property<string>("BookScrollId")+ .HasColumnType("TEXT");++ b.Property<int>("ChapterId")+ .HasColumnType("INTEGER");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<int>("PagesRead")+ .HasColumnType("INTEGER");++ b.Property<int>("SeriesId")+ .HasColumnType("INTEGER");++ b.Property<int>("VolumeId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("AppUserId");++ b.ToTable("AppUserProgresses");+ });++ modelBuilder.Entity("API.Entities.AppUserRating", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("AppUserId")+ .HasColumnType("INTEGER");++ b.Property<int>("Rating")+ .HasColumnType("INTEGER");++ b.Property<string>("Review")+ .HasColumnType("TEXT");++ b.Property<int>("SeriesId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("AppUserId");++ b.ToTable("AppUserRating");+ });++ modelBuilder.Entity("API.Entities.AppUserRole", b =>+ {+ b.Property<int>("UserId")+ .HasColumnType("INTEGER");++ b.Property<int>("RoleId")+ .HasColumnType("INTEGER");++ b.HasKey("UserId", "RoleId");++ b.HasIndex("RoleId");++ b.ToTable("AspNetUserRoles");+ });++ modelBuilder.Entity("API.Entities.Chapter", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("CoverImage")+ .HasColumnType("TEXT");++ b.Property<bool>("CoverImageLocked")+ .HasColumnType("INTEGER");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<bool>("IsSpecial")+ .HasColumnType("INTEGER");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<string>("Number")+ .HasColumnType("TEXT");++ b.Property<int>("Pages")+ .HasColumnType("INTEGER");++ b.Property<string>("Range")+ .HasColumnType("TEXT");++ b.Property<string>("Title")+ .HasColumnType("TEXT");++ b.Property<int>("VolumeId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("VolumeId");++ b.ToTable("Chapter");+ });++ modelBuilder.Entity("API.Entities.CollectionTag", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("CoverImage")+ .HasColumnType("TEXT");++ b.Property<bool>("CoverImageLocked")+ .HasColumnType("INTEGER");++ b.Property<string>("NormalizedTitle")+ .HasColumnType("TEXT");++ b.Property<bool>("Promoted")+ .HasColumnType("INTEGER");++ b.Property<uint>("RowVersion")+ .HasColumnType("INTEGER");++ b.Property<string>("Summary")+ .HasColumnType("TEXT");++ b.Property<string>("Title")+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("Id", "Promoted")+ .IsUnique();++ b.ToTable("CollectionTag");+ });++ modelBuilder.Entity("API.Entities.FolderPath", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<DateTime>("LastScanned")+ .HasColumnType("TEXT");++ b.Property<int>("LibraryId")+ .HasColumnType("INTEGER");++ b.Property<string>("Path")+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("LibraryId");++ b.ToTable("FolderPath");+ });++ modelBuilder.Entity("API.Entities.Library", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("CoverImage")+ .HasColumnType("TEXT");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<string>("Name")+ .HasColumnType("TEXT");++ b.Property<int>("Type")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.ToTable("Library");+ });++ modelBuilder.Entity("API.Entities.MangaFile", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("ChapterId")+ .HasColumnType("INTEGER");++ b.Property<string>("FilePath")+ .HasColumnType("TEXT");++ b.Property<int>("Format")+ .HasColumnType("INTEGER");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<int>("Pages")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("ChapterId");++ b.ToTable("MangaFile");+ });++ modelBuilder.Entity("API.Entities.ReadingList", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("AppUserId")+ .HasColumnType("INTEGER");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<bool>("Promoted")+ .HasColumnType("INTEGER");++ b.Property<string>("Summary")+ .HasColumnType("TEXT");++ b.Property<string>("Title")+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("AppUserId");++ b.ToTable("ReadingList");+ });++ modelBuilder.Entity("API.Entities.ReadingListItem", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("ChapterId")+ .HasColumnType("INTEGER");++ b.Property<int>("Order")+ .HasColumnType("INTEGER");++ b.Property<int>("ReadingListId")+ .HasColumnType("INTEGER");++ b.Property<int>("SeriesId")+ .HasColumnType("INTEGER");++ b.Property<int>("VolumeId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("ChapterId");++ b.HasIndex("ReadingListId");++ b.HasIndex("SeriesId");++ b.HasIndex("VolumeId");++ b.ToTable("ReadingListItem");+ });++ modelBuilder.Entity("API.Entities.Series", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("CoverImage")+ .HasColumnType("TEXT");++ b.Property<bool>("CoverImageLocked")+ .HasColumnType("INTEGER");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<int>("Format")+ .HasColumnType("INTEGER");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<int>("LibraryId")+ .HasColumnType("INTEGER");++ b.Property<string>("LocalizedName")+ .HasColumnType("TEXT");++ b.Property<string>("Name")+ .HasColumnType("TEXT");++ b.Property<string>("NormalizedName")+ .HasColumnType("TEXT");++ b.Property<string>("OriginalName")+ .HasColumnType("TEXT");++ b.Property<int>("Pages")+ .HasColumnType("INTEGER");++ b.Property<string>("SortName")+ .HasColumnType("TEXT");++ b.Property<string>("Summary")+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("LibraryId");++ b.HasIndex("Name", "NormalizedName", "LocalizedName", "LibraryId", "Format")+ .IsUnique();++ b.ToTable("Series");+ });++ modelBuilder.Entity("API.Entities.SeriesMetadata", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<uint>("RowVersion")+ .IsConcurrencyToken()+ .HasColumnType("INTEGER");++ b.Property<int>("SeriesId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("SeriesId")+ .IsUnique();++ b.HasIndex("Id", "SeriesId")+ .IsUnique();++ b.ToTable("SeriesMetadata");+ });++ modelBuilder.Entity("API.Entities.ServerSetting", b =>+ {+ b.Property<int>("Key")+ .HasColumnType("INTEGER");++ b.Property<uint>("RowVersion")+ .IsConcurrencyToken()+ .HasColumnType("INTEGER");++ b.Property<string>("Value")+ .HasColumnType("TEXT");++ b.HasKey("Key");++ b.ToTable("ServerSetting");+ });++ modelBuilder.Entity("API.Entities.Volume", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("CoverImage")+ .HasColumnType("TEXT");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<string>("Name")+ .HasColumnType("TEXT");++ b.Property<int>("Number")+ .HasColumnType("INTEGER");++ b.Property<int>("Pages")+ .HasColumnType("INTEGER");++ b.Property<int>("SeriesId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("SeriesId");++ b.ToTable("Volume");+ });++ modelBuilder.Entity("AppUserLibrary", b =>+ {+ b.Property<int>("AppUsersId")+ .HasColumnType("INTEGER");++ b.Property<int>("LibrariesId")+ .HasColumnType("INTEGER");++ b.HasKey("AppUsersId", "LibrariesId");++ b.HasIndex("LibrariesId");++ b.ToTable("AppUserLibrary");+ });++ modelBuilder.Entity("CollectionTagSeriesMetadata", b =>+ {+ b.Property<int>("CollectionTagsId")+ .HasColumnType("INTEGER");++ b.Property<int>("SeriesMetadatasId")+ .HasColumnType("INTEGER");++ b.HasKey("CollectionTagsId", "SeriesMetadatasId");++ b.HasIndex("SeriesMetadatasId");++ b.ToTable("CollectionTagSeriesMetadata");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<int>", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("ClaimType")+ .HasColumnType("TEXT");++ b.Property<string>("ClaimValue")+ .HasColumnType("TEXT");++ b.Property<int>("RoleId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("RoleId");++ b.ToTable("AspNetRoleClaims");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<int>", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("ClaimType")+ .HasColumnType("TEXT");++ b.Property<string>("ClaimValue")+ .HasColumnType("TEXT");++ b.Property<int>("UserId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("UserId");++ b.ToTable("AspNetUserClaims");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<int>", b =>+ {+ b.Property<string>("LoginProvider")+ .HasColumnType("TEXT");++ b.Property<string>("ProviderKey")+ .HasColumnType("TEXT");++ b.Property<string>("ProviderDisplayName")+ .HasColumnType("TEXT");++ b.Property<int>("UserId")+ .HasColumnType("INTEGER");++ b.HasKey("LoginProvider", "ProviderKey");++ b.HasIndex("UserId");++ b.ToTable("AspNetUserLogins");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<int>", b =>+ {+ b.Property<int>("UserId")+ .HasColumnType("INTEGER");++ b.Property<string>("LoginProvider")+ .HasColumnType("TEXT");++ b.Property<string>("Name")+ .HasColumnType("TEXT");++ b.Property<string>("Value")+ .HasColumnType("TEXT");++ b.HasKey("UserId", "LoginProvider", "Name");++ b.ToTable("AspNetUserTokens");+ });++ modelBuilder.Entity("API.Entities.AppUserBookmark", b =>+ {+ b.HasOne("API.Entities.AppUser", "AppUser")+ .WithMany("Bookmarks")+ .HasForeignKey("AppUserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("AppUser");+ });++ modelBuilder.Entity("API.Entities.AppUserPreferences", b =>+ {+ b.HasOne("API.Entities.AppUser", "AppUser")+ .WithOne("UserPreferences")+ .HasForeignKey("API.Entities.AppUserPreferences", "AppUserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("AppUser");+ });++ modelBuilder.Entity("API.Entities.AppUserProgress", b =>+ {+ b.HasOne("API.Entities.AppUser", "AppUser")+ .WithMany("Progresses")+ .HasForeignKey("AppUserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("AppUser");+ });++ modelBuilder.Entity("API.Entities.AppUserRating", b =>+ {+ b.HasOne("API.Entities.AppUser", "AppUser")+ .WithMany("Ratings")+ .HasForeignKey("AppUserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("AppUser");+ });++ modelBuilder.Entity("API.Entities.AppUserRole", b =>+ {+ b.HasOne("API.Entities.AppRole", "Role")+ .WithMany("UserRoles")+ .HasForeignKey("RoleId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("API.Entities.AppUser", "User")+ .WithMany("UserRoles")+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Role");++ b.Navigation("User");+ });++ modelBuilder.Entity("API.Entities.Chapter", b =>+ {+ b.HasOne("API.Entities.Volume", "Volume")+ .WithMany("Chapters")+ .HasForeignKey("VolumeId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Volume");+ });++ modelBuilder.Entity("API.Entities.FolderPath", b =>+ {+ b.HasOne("API.Entities.Library", "Library")+ .WithMany("Folders")+ .HasForeignKey("LibraryId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Library");+ });++ modelBuilder.Entity("API.Entities.MangaFile", b =>+ {+ b.HasOne("API.Entities.Chapter", "Chapter")+ .WithMany("Files")+ .HasForeignKey("ChapterId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Chapter");+ });++ modelBuilder.Entity("API.Entities.ReadingList", b =>+ {+ b.HasOne("API.Entities.AppUser", "AppUser")+ .WithMany("ReadingLists")+ .HasForeignKey("AppUserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("AppUser");+ });++ modelBuilder.Entity("API.Entities.ReadingListItem", b =>+ {+ b.HasOne("API.Entities.Chapter", "Chapter")+ .WithMany()+ .HasForeignKey("ChapterId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("API.Entities.ReadingList", "ReadingList")+ .WithMany("Items")+ .HasForeignKey("ReadingListId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("API.Entities.Series", "Series")+ .WithMany()+ .HasForeignKey("SeriesId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("API.Entities.Volume", "Volume")+ .WithMany()+ .HasForeignKey("VolumeId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Chapter");++ b.Navigation("ReadingList");++ b.Navigation("Series");++ b.Navigation("Volume");+ });++ modelBuilder.Entity("API.Entities.Series", b =>+ {+ b.HasOne("API.Entities.Library", "Library")+ .WithMany("Series")+ .HasForeignKey("LibraryId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Library");+ });++ modelBuilder.Entity("API.Entities.SeriesMetadata", b =>+ {+ b.HasOne("API.Entities.Series", "Series")+ .WithOne("Metadata")+ .HasForeignKey("API.Entities.SeriesMetadata", "SeriesId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Series");+ });++ modelBuilder.Entity("API.Entities.Volume", b =>+ {+ b.HasOne("API.Entities.Series", "Series")+ .WithMany("Volumes")+ .HasForeignKey("SeriesId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Series");+ });++ modelBuilder.Entity("AppUserLibrary", b =>+ {+ b.HasOne("API.Entities.AppUser", null)+ .WithMany()+ .HasForeignKey("AppUsersId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("API.Entities.Library", null)+ .WithMany()+ .HasForeignKey("LibrariesId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("CollectionTagSeriesMetadata", b =>+ {+ b.HasOne("API.Entities.CollectionTag", null)+ .WithMany()+ .HasForeignKey("CollectionTagsId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("API.Entities.SeriesMetadata", null)+ .WithMany()+ .HasForeignKey("SeriesMetadatasId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<int>", b =>+ {+ b.HasOne("API.Entities.AppRole", null)+ .WithMany()+ .HasForeignKey("RoleId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<int>", b =>+ {+ b.HasOne("API.Entities.AppUser", null)+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<int>", b =>+ {+ b.HasOne("API.Entities.AppUser", null)+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<int>", b =>+ {+ b.HasOne("API.Entities.AppUser", null)+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("API.Entities.AppRole", b =>+ {+ b.Navigation("UserRoles");+ });++ modelBuilder.Entity("API.Entities.AppUser", b =>+ {+ b.Navigation("Bookmarks");++ b.Navigation("Progresses");++ b.Navigation("Ratings");++ b.Navigation("ReadingLists");++ b.Navigation("UserPreferences");++ b.Navigation("UserRoles");+ });++ modelBuilder.Entity("API.Entities.Chapter", b =>+ {+ b.Navigation("Files");+ });++ modelBuilder.Entity("API.Entities.Library", b =>+ {+ b.Navigation("Folders");++ b.Navigation("Series");+ });++ modelBuilder.Entity("API.Entities.ReadingList", b =>+ {+ b.Navigation("Items");+ });++ modelBuilder.Entity("API.Entities.Series", b =>+ {+ b.Navigation("Metadata");++ b.Navigation("Volumes");+ });++ modelBuilder.Entity("API.Entities.Volume", b =>+ {+ b.Navigation("Chapters");+ });+#pragma warning restore 612, 618+ }+ }+}diff --git a/API/Data/Migrations/20210916142418_EntityImageRefactor.cs b/API/Data/Migrations/20210916142418_EntityImageRefactor.csnew file mode 100644index 000000000..deafb134b--- /dev/null+++ b/API/Data/Migrations/20210916142418_EntityImageRefactor.cs@@ -0,0 +1,97 @@+using System;+using Microsoft.EntityFrameworkCore.Migrations;++namespace API.Data.Migrations+{+ public partial class EntityImageRefactor : Migration+ {+ protected override void Up(MigrationBuilder migrationBuilder)+ {+ migrationBuilder.DropColumn(+ name: "RowVersion",+ table: "AppUserProgresses");++ migrationBuilder.AlterColumn<string>(+ name: "CoverImage",+ table: "Volume",+ type: "TEXT",+ nullable: true,+ oldClrType: typeof(byte[]),+ oldType: "BLOB",+ oldNullable: true);++ migrationBuilder.AlterColumn<string>(+ name: "CoverImage",+ table: "Series",+ type: "TEXT",+ nullable: true,+ oldClrType: typeof(byte[]),+ oldType: "BLOB",+ oldNullable: true);++ migrationBuilder.AlterColumn<string>(+ name: "CoverImage",+ table: "CollectionTag",+ type: "TEXT",+ nullable: true,+ oldClrType: typeof(byte[]),+ oldType: "BLOB",+ oldNullable: true);++ migrationBuilder.AlterColumn<string>(+ name: "CoverImage",+ table: "Chapter",+ type: "TEXT",+ nullable: true,+ oldClrType: typeof(byte[]),+ oldType: "BLOB",+ oldNullable: true);+ }++ protected override void Down(MigrationBuilder migrationBuilder)+ {+ migrationBuilder.AlterColumn<byte[]>(+ name: "CoverImage",+ table: "Volume",+ type: "BLOB",+ nullable: true,+ oldClrType: typeof(string),+ oldType: "TEXT",+ oldNullable: true);++ migrationBuilder.AlterColumn<byte[]>(+ name: "CoverImage",+ table: "Series",+ type: "BLOB",+ nullable: true,+ oldClrType: typeof(string),+ oldType: "TEXT",+ oldNullable: true);++ migrationBuilder.AlterColumn<byte[]>(+ name: "CoverImage",+ table: "CollectionTag",+ type: "BLOB",+ nullable: true,+ oldClrType: typeof(string),+ oldType: "TEXT",+ oldNullable: true);++ migrationBuilder.AlterColumn<byte[]>(+ name: "CoverImage",+ table: "Chapter",+ type: "BLOB",+ nullable: true,+ oldClrType: typeof(string),+ oldType: "TEXT",+ oldNullable: true);++ migrationBuilder.AddColumn<uint>(+ name: "RowVersion",+ table: "AppUserProgresses",+ type: "INTEGER",+ nullable: false,+ defaultValue: 0u);+ }+ }+}diff --git a/API/Data/Migrations/DataContextModelSnapshot.cs b/API/Data/Migrations/DataContextModelSnapshot.csindex 0d2c3681e..38a09633e 100644--- a/API/Data/Migrations/DataContextModelSnapshot.cs+++ b/API/Data/Migrations/DataContextModelSnapshot.cs@@ -229,10 +229,6 @@ namespace API.Data.Migrationsb.Property<int>("PagesRead").HasColumnType("INTEGER");- b.Property<uint>("RowVersion")- .IsConcurrencyToken()- .HasColumnType("INTEGER");-b.Property<int>("SeriesId").HasColumnType("INTEGER");@@ -292,8 +288,8 @@ namespace API.Data.Migrations.ValueGeneratedOnAdd().HasColumnType("INTEGER");- b.Property<byte[]>("CoverImage")- .HasColumnType("BLOB");+ b.Property<string>("CoverImage")+ .HasColumnType("TEXT");b.Property<bool>("CoverImageLocked").HasColumnType("INTEGER");@@ -335,8 +331,8 @@ namespace API.Data.Migrations.ValueGeneratedOnAdd().HasColumnType("INTEGER");- b.Property<byte[]>("CoverImage")- .HasColumnType("BLOB");+ b.Property<string>("CoverImage")+ .HasColumnType("TEXT");b.Property<bool>("CoverImageLocked").HasColumnType("INTEGER");@@ -511,8 +507,8 @@ namespace API.Data.Migrations.ValueGeneratedOnAdd().HasColumnType("INTEGER");- b.Property<byte[]>("CoverImage")- .HasColumnType("BLOB");+ b.Property<string>("CoverImage")+ .HasColumnType("TEXT");b.Property<bool>("CoverImageLocked").HasColumnType("INTEGER");@@ -607,8 +603,8 @@ namespace API.Data.Migrations.ValueGeneratedOnAdd().HasColumnType("INTEGER");- b.Property<byte[]>("CoverImage")- .HasColumnType("BLOB");+ b.Property<string>("CoverImage")+ .HasColumnType("TEXT");b.Property<DateTime>("Created").HasColumnType("TEXT");diff --git a/API/Data/Repositories/AppUserProgressRepository.cs b/API/Data/Repositories/AppUserProgressRepository.csindex 9a4d3aa73..c91e61cd0 100644--- a/API/Data/Repositories/AppUserProgressRepository.cs+++ b/API/Data/Repositories/AppUserProgressRepository.cs@@ -73,7 +73,7 @@ namespace API.Data.Repositories{return await _context.AppUserProgresses.Where(p => p.ChapterId == chapterId && p.AppUserId == userId)- .SingleOrDefaultAsync();+ .FirstOrDefaultAsync();}}}diff --git a/API/Data/Repositories/ChapterRepository.cs b/API/Data/Repositories/ChapterRepository.csindex a60c0a819..f1905eaa8 100644--- a/API/Data/Repositories/ChapterRepository.cs+++ b/API/Data/Repositories/ChapterRepository.cs@@ -1,5 +1,7 @@using System.Collections.Generic;+using System.IO;using System.Linq;+using System.Text;using System.Threading.Tasks;using API.DTOs;using API.DTOs.Reader;@@ -140,8 +142,9 @@ namespace API.Data.Repositories/// </summary>/// <param name="chapterId"></param>/// <returns></returns>- public async Task<byte[]> GetChapterCoverImageAsync(int chapterId)+ public async Task<string> GetChapterCoverImageAsync(int chapterId){+return await _context.Chapter.Where(c => c.Id == chapterId).Select(c => c.CoverImage)@@ -149,10 +152,33 @@ namespace API.Data.Repositories.SingleOrDefaultAsync();}+ public async Task<IList<string>> GetAllCoverImagesAsync()+ {+ return await _context.Chapter+ .Select(c => c.CoverImage)+ .Where(t => !string.IsNullOrEmpty(t))+ .AsNoTracking()+ .ToListAsync();+ }++ /// <summary>+ /// Returns cover images for locked chapters+ /// </summary>+ /// <returns></returns>+ public async Task<IEnumerable<string>> GetCoverImagesForLockedChaptersAsync()+ {+ return await _context.Chapter+ .Where(c => c.CoverImageLocked)+ .Select(c => c.CoverImage)+ .Where(t => !string.IsNullOrEmpty(t))+ .AsNoTracking()+ .ToListAsync();+ }+/// <summary>- /// Returns non-tracked files for a set of chapterIds+ /// Returns non-tracked files for a set of <paramref name="chapterIds"/>/// </summary>- /// <param name="chapterIds"></param>+ /// <param name="chapterIds">List of chapter Ids</param>/// <returns></returns>public async Task<IList<MangaFile>> GetFilesForChaptersAsync(IReadOnlyList<int> chapterIds){diff --git a/API/Data/Repositories/CollectionTagRepository.cs b/API/Data/Repositories/CollectionTagRepository.csindex 1093bbc60..777e82788 100644--- a/API/Data/Repositories/CollectionTagRepository.cs+++ b/API/Data/Repositories/CollectionTagRepository.cs@@ -1,4 +1,5 @@using System.Collections.Generic;+using System.IO;using System.Linq;using System.Threading.Tasks;using API.DTOs;@@ -48,11 +49,19 @@ namespace API.Data.Repositoriespublic async Task<IEnumerable<CollectionTag>> GetAllTagsAsync(){return await _context.CollectionTag- .Select(c => c).OrderBy(c => c.NormalizedTitle).ToListAsync();}+ public async Task<IList<string>> GetAllCoverImagesAsync()+ {+ return await _context.CollectionTag+ .Select(t => t.CoverImage)+ .Where(t => !string.IsNullOrEmpty(t))+ .AsNoTracking()+ .ToListAsync();+ }+public async Task<IEnumerable<CollectionTagDto>> GetAllTagDtosAsync(){return await _context.CollectionTag@@ -100,9 +109,9 @@ namespace API.Data.Repositories.ToListAsync();}- public Task<byte[]> GetCoverImageAsync(int collectionTagId)+ public async Task<string> GetCoverImageAsync(int collectionTagId){- return _context.CollectionTag+ return await _context.CollectionTag.Where(c => c.Id == collectionTagId).Select(c => c.CoverImage).AsNoTracking()diff --git a/API/Data/Repositories/SeriesRepository.cs b/API/Data/Repositories/SeriesRepository.csindex 3d8e8c661..8eac7d0bc 100644--- a/API/Data/Repositories/SeriesRepository.cs+++ b/API/Data/Repositories/SeriesRepository.cs@@ -1,4 +1,5 @@using System.Collections.Generic;+using System.IO;using System.Linq;using System.Threading.Tasks;using API.Comparators;@@ -256,7 +257,7 @@ namespace API.Data.Repositories}}- public async Task<byte[]> GetSeriesCoverImageAsync(int seriesId)+ public async Task<string> GetSeriesCoverImageAsync(int seriesId){return await _context.Series.Where(s => s.Id == seriesId)@@ -443,5 +444,23 @@ namespace API.Data.Repositories.AsSplitQuery().ToListAsync();}++ public async Task<IList<string>> GetAllCoverImagesAsync()+ {+ return await _context.Series+ .Select(s => s.CoverImage)+ .Where(t => !string.IsNullOrEmpty(t))+ .AsNoTracking()+ .ToListAsync();+ }++ public async Task<IEnumerable<string>> GetLockedCoverImagesAsync()+ {+ return await _context.Series+ .Where(s => s.CoverImageLocked && !string.IsNullOrEmpty(s.CoverImage))+ .Select(s => s.CoverImage)+ .AsNoTracking()+ .ToListAsync();+ }}}diff --git a/API/Data/Repositories/VolumeRepository.cs b/API/Data/Repositories/VolumeRepository.csindex bc6257af1..860bcaf79 100644--- a/API/Data/Repositories/VolumeRepository.cs+++ b/API/Data/Repositories/VolumeRepository.cs@@ -1,4 +1,5 @@using System.Collections.Generic;+using System.IO;using System.Linq;using System.Threading.Tasks;using API.DTOs;@@ -35,9 +36,9 @@ namespace API.Data.Repositories.ToListAsync();}- public async Task<byte[]> GetVolumeCoverImageAsync(int volumeId)+ public async Task<string> GetVolumeCoverImageAsync(int volumeId){- return await _context.Volume+ return await _context.Volume.Where(v => v.Id == volumeId).Select(v => v.CoverImage).AsNoTracking()diff --git a/API/Entities/Chapter.cs b/API/Entities/Chapter.csindex 4595ba048..ef12de8ce 100644--- a/API/Entities/Chapter.cs+++ b/API/Entities/Chapter.cs@@ -23,7 +23,11 @@ namespace API.Entitiespublic ICollection<MangaFile> Files { get; set; }public DateTime Created { get; set; }public DateTime LastModified { get; set; }- public byte[] CoverImage { get; set; }+ /// <summary>+ /// Absolute path to the (managed) image file+ /// </summary>+ /// <remarks>The file is managed internally to Kavita's APPDIR</remarks>+ public string CoverImage { get; set; }public bool CoverImageLocked { get; set; }/// <summary>/// Total number of pages in all MangaFilesdiff --git a/API/Entities/CollectionTag.cs b/API/Entities/CollectionTag.csindex c9dd4fa92..ee966cafc 100644--- a/API/Entities/CollectionTag.cs+++ b/API/Entities/CollectionTag.cs@@ -14,11 +14,11 @@ namespace API.Entities/// Visible title of the Tag/// </summary>public string Title { get; set; }-/// <summary>- /// Cover Image for the collection tag+ /// Absolute path to the (managed) image file/// </summary>- public byte[] CoverImage { get; set; }+ /// <remarks>The file is managed internally to Kavita's APPDIR</remarks>+ public string CoverImage { get; set; }/// <summary>/// Denotes if the CoverImage has been overridden by the user. If so, it will not be updated during normal scan operations./// </summary>diff --git a/API/Entities/Series.cs b/API/Entities/Series.csindex b2d97751f..899e52bfd 100644--- a/API/Entities/Series.cs+++ b/API/Entities/Series.cs@@ -36,7 +36,11 @@ namespace API.Entitiespublic string Summary { get; set; } // TODO: Migrate into SeriesMetdata (with Metadata update)public DateTime Created { get; set; }public DateTime LastModified { get; set; }- public byte[] CoverImage { get; set; }+ /// <summary>+ /// Absolute path to the (managed) image file+ /// </summary>+ /// <remarks>The file is managed internally to Kavita's APPDIR</remarks>+ public string CoverImage { get; set; }/// <summary>/// Denotes if the CoverImage has been overridden by the user. If so, it will not be updated during normal scan operations./// </summary>diff --git a/API/Entities/Volume.cs b/API/Entities/Volume.csindex f02c9f848..3be7a4d6a 100644--- a/API/Entities/Volume.cs+++ b/API/Entities/Volume.cs@@ -13,7 +13,11 @@ namespace API.Entitiespublic IList<Chapter> Chapters { get; set; }public DateTime Created { get; set; }public DateTime LastModified { get; set; }- public byte[] CoverImage { get; set; }+ /// <summary>+ /// Absolute path to the (managed) image file+ /// </summary>+ /// <remarks>The file is managed internally to Kavita's APPDIR</remarks>+ public string CoverImage { get; set; }public int Pages { get; set; }diff --git a/API/Extensions/HttpExtensions.cs b/API/Extensions/HttpExtensions.csindex db7bcd370..80b52f18f 100644--- a/API/Extensions/HttpExtensions.cs+++ b/API/Extensions/HttpExtensions.cs@@ -1,4 +1,5 @@using System.Linq;+using System.Text;using System.Text.Json;using API.Helpers;using Microsoft.AspNetCore.Http;@@ -7,7 +8,7 @@ namespace API.Extensions{public static class HttpExtensions{- public static void AddPaginationHeader(this HttpResponse response, int currentPage,+ public static void AddPaginationHeader(this HttpResponse response, int currentPage,int itemsPerPage, int totalItems, int totalPages){var paginationHeader = new PaginationHeader(currentPage, itemsPerPage, totalItems, totalPages);@@ -15,7 +16,7 @@ namespace API.Extensions{PropertyNamingPolicy = JsonNamingPolicy.CamelCase};-+response.Headers.Add("Pagination", JsonSerializer.Serialize(paginationHeader, options));response.Headers.Add("Access-Control-Expose-Headers", "Pagination");}@@ -31,6 +32,18 @@ namespace API.Extensionsusing var sha1 = new System.Security.Cryptography.SHA256CryptoServiceProvider();response.Headers.Add("ETag", string.Concat(sha1.ComputeHash(content).Select(x => x.ToString("X2"))));}-++ /// <summary>+ /// Calculates SHA256 hash for a cover image filename and sets as ETag. Ensures Cache-Control: private header is added.+ /// </summary>+ /// <param name="response"></param>+ /// <param name="filename"></param>+ public static void AddCacheHeader(this HttpResponse response, string filename)+ {+ if (filename == null || filename.Length <= 0) return;+ using var sha1 = new System.Security.Cryptography.SHA256CryptoServiceProvider();+ response.Headers.Add("ETag", string.Concat(sha1.ComputeHash(Encoding.UTF8.GetBytes(filename)).Select(x => x.ToString("X2"))));+ }+}-}\ No newline at end of file+}diff --git a/API/Helpers/SQLHelper.cs b/API/Helpers/SQLHelper.csnew file mode 100644index 000000000..fcd44e7da--- /dev/null+++ b/API/Helpers/SQLHelper.cs@@ -0,0 +1,30 @@+using System;+using System.Collections.Generic;+using System.Data;+using System.Data.Common;+using Microsoft.EntityFrameworkCore;++namespace API.Helpers+{+ public static class SqlHelper+ {+ public static List<T> RawSqlQuery<T>(DbContext context, string query, Func<DbDataReader, T> map)+ {+ using var command = context.Database.GetDbConnection().CreateCommand();+ command.CommandText = query;+ command.CommandType = CommandType.Text;++ context.Database.OpenConnection();++ using var result = command.ExecuteReader();+ var entities = new List<T>();++ while (result.Read())+ {+ entities.Add(map(result));+ }++ return entities;+ }+ }+}diff --git a/API/Interfaces/Repositories/IChapterRepository.cs b/API/Interfaces/Repositories/IChapterRepository.csindex 3f9713020..9ce145f4c 100644--- a/API/Interfaces/Repositories/IChapterRepository.cs+++ b/API/Interfaces/Repositories/IChapterRepository.cs@@ -17,6 +17,8 @@ namespace API.Interfaces.RepositoriesTask<IList<MangaFile>> GetFilesForChapterAsync(int chapterId);Task<IList<Chapter>> GetChaptersAsync(int volumeId);Task<IList<MangaFile>> GetFilesForChaptersAsync(IReadOnlyList<int> chapterIds);- Task<byte[]> GetChapterCoverImageAsync(int chapterId);+ Task<string> GetChapterCoverImageAsync(int chapterId);+ Task<IList<string>> GetAllCoverImagesAsync();+ Task<IEnumerable<string>> GetCoverImagesForLockedChaptersAsync();}}diff --git a/API/Interfaces/Repositories/ICollectionTagRepository.cs b/API/Interfaces/Repositories/ICollectionTagRepository.csindex 39f814e4c..03a552bd9 100644--- a/API/Interfaces/Repositories/ICollectionTagRepository.cs+++ b/API/Interfaces/Repositories/ICollectionTagRepository.cs@@ -10,12 +10,13 @@ namespace API.Interfaces.Repositoriesvoid Remove(CollectionTag tag);Task<IEnumerable<CollectionTagDto>> GetAllTagDtosAsync();Task<IEnumerable<CollectionTagDto>> SearchTagDtosAsync(string searchQuery);- Task<byte[]> GetCoverImageAsync(int collectionTagId);+ Task<string> GetCoverImageAsync(int collectionTagId);Task<IEnumerable<CollectionTagDto>> GetAllPromotedTagDtosAsync();Task<CollectionTag> GetTagAsync(int tagId);Task<CollectionTag> GetFullTagAsync(int tagId);void Update(CollectionTag tag);Task<int> RemoveTagsWithoutSeries();Task<IEnumerable<CollectionTag>> GetAllTagsAsync();+ Task<IList<string>> GetAllCoverImagesAsync();}}diff --git a/API/Interfaces/Repositories/ISeriesRepository.cs b/API/Interfaces/Repositories/ISeriesRepository.csindex a66b8bbe5..31b77d65f 100644--- a/API/Interfaces/Repositories/ISeriesRepository.cs+++ b/API/Interfaces/Repositories/ISeriesRepository.cs@@ -1,4 +1,5 @@-using System.Collections.Generic;+using System.Collections;+using System.Collections.Generic;using System.Threading.Tasks;using API.DTOs;using API.DTOs.Filtering;@@ -57,12 +58,14 @@ namespace API.Interfaces.RepositoriesTask AddSeriesModifiers(int userId, List<SeriesDto> series);- Task<byte[]> GetSeriesCoverImageAsync(int seriesId);+ Task<string> GetSeriesCoverImageAsync(int seriesId);Task<IEnumerable<SeriesDto>> GetInProgress(int userId, int libraryId, UserParams userParams, FilterDto filter);Task<PagedList<SeriesDto>> GetRecentlyAdded(int libraryId, int userId, UserParams userParams, FilterDto filter);Task<SeriesMetadataDto> GetSeriesMetadata(int seriesId);Task<PagedList<SeriesDto>> GetSeriesDtoForCollectionAsync(int collectionId, int userId, UserParams userParams);Task<IList<MangaFile>> GetFilesForSeries(int seriesId);Task<IEnumerable<SeriesDto>> GetSeriesDtoForIdsAsync(IEnumerable<int> seriesIds, int userId);+ Task<IList<string>> GetAllCoverImagesAsync();+ Task<IEnumerable<string>> GetLockedCoverImagesAsync();}}diff --git a/API/Interfaces/Repositories/IVolumeRepository.cs b/API/Interfaces/Repositories/IVolumeRepository.csindex 98f93746a..898175df9 100644--- a/API/Interfaces/Repositories/IVolumeRepository.cs+++ b/API/Interfaces/Repositories/IVolumeRepository.cs@@ -9,6 +9,6 @@ namespace API.Interfaces.Repositories{void Update(Volume volume);Task<IList<MangaFile>> GetFilesForVolume(int volumeId);- Task<byte[]> GetVolumeCoverImageAsync(int volumeId);+ Task<string> GetVolumeCoverImageAsync(int volumeId);}}diff --git a/API/Interfaces/Services/IArchiveService.cs b/API/Interfaces/Services/IArchiveService.csindex 07c1e287d..ae9bddc98 100644--- a/API/Interfaces/Services/IArchiveService.cs+++ b/API/Interfaces/Services/IArchiveService.cs@@ -10,7 +10,7 @@ namespace API.Interfaces.Services{void ExtractArchive(string archivePath, string extractPath);int GetNumberOfPagesFromArchive(string archivePath);- byte[] GetCoverImage(string archivePath, bool createThumbnail = false);+ string GetCoverImage(string archivePath, string fileName);bool IsValidArchive(string archivePath);string GetSummaryInfo(string archivePath);ArchiveLibrary CanOpen(string archivePath);diff --git a/API/Interfaces/Services/IBackupService.cs b/API/Interfaces/Services/IBackupService.csindex eaa140e46..315b852f0 100644--- a/API/Interfaces/Services/IBackupService.cs+++ b/API/Interfaces/Services/IBackupService.cs@@ -1,11 +1,12 @@using System.Collections.Generic;+using System.Threading.Tasks;using Microsoft.Extensions.Configuration;namespace API.Interfaces.Services{public interface IBackupService{- void BackupDatabase();+ Task BackupDatabase();/// <summary>/// Returns a list of full paths of the logs files detailed in <see cref="IConfiguration"/>./// </summary>diff --git a/API/Interfaces/Services/IBookService.cs b/API/Interfaces/Services/IBookService.csindex b3afc13a8..cde2cad8e 100644--- a/API/Interfaces/Services/IBookService.cs+++ b/API/Interfaces/Services/IBookService.cs@@ -8,7 +8,7 @@ namespace API.Interfaces.Servicespublic interface IBookService{int GetNumberOfPages(string filePath);- byte[] GetCoverImage(string fileFilePath, bool createThumbnail = true);+ string GetCoverImage(string fileFilePath, string fileName);Task<Dictionary<string, int>> CreateKeyToPageMappingAsync(EpubBookRef book);/// <summary>diff --git a/API/Interfaces/Services/ICleanupService.cs b/API/Interfaces/Services/ICleanupService.csindex da61943fe..afabb9900 100644--- a/API/Interfaces/Services/ICleanupService.cs+++ b/API/Interfaces/Services/ICleanupService.cs@@ -1,7 +1,10 @@-namespace API.Interfaces.Services+using System.Threading.Tasks;++namespace API.Interfaces.Services{public interface ICleanupService{- void Cleanup();+ Task Cleanup();+ void CleanupCacheDirectory();}-}\ No newline at end of file+}diff --git a/API/Interfaces/Services/IImageService.cs b/API/Interfaces/Services/IImageService.csindex 86f6fa489..0aba07f39 100644--- a/API/Interfaces/Services/IImageService.cs+++ b/API/Interfaces/Services/IImageService.cs@@ -1,22 +1,23 @@using API.Entities;+using API.Services;namespace API.Interfaces.Services{public interface IImageService{- byte[] GetCoverImage(string path, bool createThumbnail = false);+ string GetCoverImage(string path, string fileName);string GetCoverFile(MangaFile file);/// <summary>/// Creates a Thumbnail version of an image/// </summary>/// <param name="path">Path to the image file</param>- /// <returns></returns>- public byte[] CreateThumbnail(string path);+ /// <returns>File name with extension of the file. This will always write to <see cref="DirectoryService.CoverImageDirectory"/></returns>+ public string CreateThumbnail(string path, string fileName);/// <summary>/// Creates a Thumbnail version of a base64 image/// </summary>/// <param name="encodedImage">base64 encoded image</param>- /// <returns></returns>- public byte[] CreateThumbnailFromBase64(string encodedImage);+ /// <returns>File name with extension of the file. This will always write to <see cref="DirectoryService.CoverImageDirectory"/></returns>+ public string CreateThumbnailFromBase64(string encodedImage, string fileName);}}diff --git a/API/Program.cs b/API/Program.csindex 6d91f6d98..eddc4cb4e 100644--- a/API/Program.cs+++ b/API/Program.cs@@ -1,10 +1,17 @@using System;+using System.Collections.Generic;+using System.Data;using System.IO;+using System.Linq;using System.Security.Cryptography;using System.Threading;+using System.Threading.Channels;using System.Threading.Tasks;using API.Data;using API.Entities;+using API.Helpers;+using API.Interfaces;+using API.Services;using Kavita.Common;using Kavita.Common.EnvironmentInfo;using Microsoft.AspNetCore.Hosting;@@ -14,6 +21,8 @@ using Microsoft.EntityFrameworkCore;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Hosting;using Microsoft.Extensions.Logging;+using Microsoft.IO;+using NetVips;using Sentry;namespace API@@ -49,8 +58,29 @@ namespace API{var context = services.GetRequiredService<DataContext>();var roleManager = services.GetRequiredService<RoleManager<AppRole>>();++ var requiresCoverImageMigration = !Directory.Exists(DirectoryService.CoverImageDirectory);+ try+ {+ // If this is a new install, tables wont exist yet+ if (requiresCoverImageMigration)+ {+ MigrateCoverImages.ExtractToImages(context);+ }+ }+ catch (Exception )+ {+ requiresCoverImageMigration = false;+ }+// Apply all migrations on startupawait context.Database.MigrateAsync();++ if (requiresCoverImageMigration)+ {+ await MigrateCoverImages.UpdateDatabaseWithImages(context);+ }+await Seed.SeedRoles(roleManager);await Seed.SeedSettings(context);await Seed.SeedUserApiKeys(context);diff --git a/API/Services/ArchiveService.cs b/API/Services/ArchiveService.csindex d8b381b60..2c22b18e7 100644--- a/API/Services/ArchiveService.cs+++ b/API/Services/ArchiveService.cs@@ -147,12 +147,13 @@ namespace API.Services////// This skips over any __MACOSX folder/file iteration./// </summary>+ /// <remarks>This always creates a thumbnail</remarks>/// <param name="archivePath"></param>- /// <param name="createThumbnail">Create a smaller variant of file extracted from archive. Archive images are usually 1MB each.</param>+ /// <param name="fileName">File name to use based on context of entity.</param>/// <returns></returns>- public byte[] GetCoverImage(string archivePath, bool createThumbnail = false)+ public string GetCoverImage(string archivePath, string fileName){- if (archivePath == null || !IsValidArchive(archivePath)) return Array.Empty<byte>();+ if (archivePath == null || !IsValidArchive(archivePath)) return String.Empty;try{var libraryHandler = CanOpen(archivePath);@@ -168,7 +169,7 @@ namespace API.Servicesvar entry = archive.Entries.Single(e => e.FullName == entryName);using var stream = entry.Open();- return createThumbnail ? CreateThumbnail(entry.FullName, stream) : ConvertEntryToByteArray(entry);+ return CreateThumbnail(entry.FullName, stream, fileName);}case ArchiveLibrary.SharpCompress:{@@ -183,14 +184,14 @@ namespace API.Servicesentry.WriteTo(ms);ms.Position = 0;- return createThumbnail ? CreateThumbnail(entry.Key, ms, Path.GetExtension(entry.Key)) : ms.ToArray();+ return CreateThumbnail(entry.Key, ms, fileName); // Path.GetExtension(entry.Key)}case ArchiveLibrary.NotSupported:_logger.LogWarning("[GetCoverImage] This archive cannot be read: {ArchivePath}. Defaulting to no cover image", archivePath);- return Array.Empty<byte>();+ return String.Empty;default:_logger.LogWarning("[GetCoverImage] There was an exception when reading archive stream: {ArchivePath}. Defaulting to no cover image", archivePath);- return Array.Empty<byte>();+ return String.Empty;}}catch (Exception ex)@@ -198,15 +199,7 @@ namespace API.Services_logger.LogWarning(ex, "[GetCoverImage] There was an exception when reading archive stream: {ArchivePath}. Defaulting to no cover image", archivePath);}- return Array.Empty<byte>();- }-- private static byte[] ConvertEntryToByteArray(ZipArchiveEntry entry)- {- using var stream = entry.Open();- using var ms = StreamManager.GetStream();- stream.CopyTo(ms);- return ms.ToArray();+ return String.Empty;}/// <summary>@@ -223,6 +216,7 @@ namespace API.Servicesarchive.Entries.Any(e => e.FullName.Contains(Path.AltDirectorySeparatorChar) && !Parser.Parser.HasBlacklistedFolderInPath(e.FullName));}+ // TODO: Refactor CreateZipForDownload to return the temp file so we can stream it from temppublic async Task<Tuple<byte[], string>> CreateZipForDownload(IEnumerable<string> files, string tempFolder){var dateString = DateTime.Now.ToShortDateString().Replace("/", "_");@@ -254,23 +248,18 @@ namespace API.Servicesreturn Tuple.Create(fileBytes, zipPath);}- private byte[] CreateThumbnail(string entryName, Stream stream, string formatExtension = ".jpg")+ private string CreateThumbnail(string entryName, Stream stream, string fileName){- if (!formatExtension.StartsWith("."))- {- formatExtension = $".{formatExtension}";- }try{- using var thumbnail = Image.ThumbnailStream(stream, MetadataService.ThumbnailWidth);- return thumbnail.WriteToBuffer(formatExtension);+ return ImageService.WriteCoverThumbnail(stream, fileName);}catch (Exception ex){_logger.LogWarning(ex, "[GetCoverImage] There was an error and prevented thumbnail generation on {EntryName}. Defaulting to no cover image", entryName);}- return Array.Empty<byte>();+ return string.Empty;}/// <summary>@@ -332,7 +321,7 @@ namespace API.Services{case ArchiveLibrary.Default:{- _logger.LogDebug("Using default compression handling");+ _logger.LogTrace("Using default compression handling");using var archive = ZipFile.OpenRead(archivePath);var entry = archive.Entries.SingleOrDefault(x => !Parser.Parser.HasBlacklistedFolderInPath(x.FullName)&& Path.GetFileNameWithoutExtension(x.Name)?.ToLower() == ComicInfoFilename@@ -348,7 +337,7 @@ namespace API.Services}case ArchiveLibrary.SharpCompress:{- _logger.LogDebug("Using SharpCompress compression handling");+ _logger.LogTrace("Using SharpCompress compression handling");using var archive = ArchiveFactory.Open(archivePath);info = FindComicInfoXml(archive.Entries.Where(entry => !entry.IsDirectory&& !Parser.Parser.HasBlacklistedFolderInPath(Path.GetDirectoryName(entry.Key) ?? string.Empty)diff --git a/API/Services/BookService.cs b/API/Services/BookService.csindex 6c02b68db..ff4830e55 100644--- a/API/Services/BookService.cs+++ b/API/Services/BookService.cs@@ -382,14 +382,19 @@ namespace API.Services}}-- public byte[] GetCoverImage(string fileFilePath, bool createThumbnail = true)+ /// <summary>+ /// Extracts the cover image to covers directory and returns file path back+ /// </summary>+ /// <param name="fileFilePath"></param>+ /// <param name="fileName">Name of the new file.</param>+ /// <returns></returns>+ public string GetCoverImage(string fileFilePath, string fileName){- if (!IsValidFile(fileFilePath)) return Array.Empty<byte>();+ if (!IsValidFile(fileFilePath)) return String.Empty;if (Parser.Parser.IsPdf(fileFilePath)){- return GetPdfCoverImage(fileFilePath, createThumbnail);+ return GetPdfCoverImage(fileFilePath, fileName);}using var epubBook = EpubReader.OpenBook(fileFilePath);@@ -402,47 +407,41 @@ namespace API.Services?? epubBook.Content.Images.Values.FirstOrDefault(file => Parser.Parser.IsCoverImage(file.FileName))?? epubBook.Content.Images.Values.FirstOrDefault();- if (coverImageContent == null) return Array.Empty<byte>();-- if (!createThumbnail) return coverImageContent.ReadContent();+ if (coverImageContent == null) return string.Empty;using var stream = StreamManager.GetStream("BookService.GetCoverImage", coverImageContent.ReadContent());- using var thumbnail = NetVips.Image.ThumbnailStream(stream, MetadataService.ThumbnailWidth);- return thumbnail.WriteToBuffer(".jpg");-+ return ImageService.WriteCoverThumbnail(stream, fileName);}catch (Exception ex){_logger.LogWarning(ex, "[BookService] There was a critical error and prevented thumbnail generation on {BookFile}. Defaulting to no cover image", fileFilePath);}- return Array.Empty<byte>();+ return string.Empty;}- private byte[] GetPdfCoverImage(string fileFilePath, bool createThumbnail)- {- try- {- using var docReader = DocLib.Instance.GetDocReader(fileFilePath, new PageDimensions(1080, 1920));- if (docReader.GetPageCount() == 0) return Array.Empty<byte>();- using var stream = StreamManager.GetStream("BookService.GetPdfPage");- GetPdfPage(docReader, 0, stream);+ private string GetPdfCoverImage(string fileFilePath, string fileName)+ {+ try+ {+ using var docReader = DocLib.Instance.GetDocReader(fileFilePath, new PageDimensions(1080, 1920));+ if (docReader.GetPageCount() == 0) return string.Empty;- if (!createThumbnail) return stream.ToArray();+ using var stream = StreamManager.GetStream("BookService.GetPdfPage");+ GetPdfPage(docReader, 0, stream);- using var thumbnail = NetVips.Image.ThumbnailStream(stream, MetadataService.ThumbnailWidth);- return thumbnail.WriteToBuffer(".png");+ return ImageService.WriteCoverThumbnail(stream, fileName);- }- catch (Exception ex)- {- _logger.LogWarning(ex,- "[BookService] There was a critical error and prevented thumbnail generation on {BookFile}. Defaulting to no cover image",- fileFilePath);- }+ }+ catch (Exception ex)+ {+ _logger.LogWarning(ex,+ "[BookService] There was a critical error and prevented thumbnail generation on {BookFile}. Defaulting to no cover image",+ fileFilePath);+ }- return Array.Empty<byte>();+ return string.Empty;}private static void GetPdfPage(IDocReader docReader, int pageNumber, Stream stream)diff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.csindex 4a1ef7dd9..360492303 100644--- a/API/Services/DirectoryService.cs+++ b/API/Services/DirectoryService.cs@@ -19,6 +19,7 @@ namespace API.Servicespublic static readonly string TempDirectory = Path.Join(Directory.GetCurrentDirectory(), "temp");public static readonly string LogDirectory = Path.Join(Directory.GetCurrentDirectory(), "logs");public static readonly string CacheDirectory = Path.Join(Directory.GetCurrentDirectory(), "cache");+ public static readonly string CoverImageDirectory = Path.Join(Directory.GetCurrentDirectory(), "covers");public DirectoryService(ILogger<DirectoryService> logger){diff --git a/API/Services/ImageService.cs b/API/Services/ImageService.csindex 11aa4716d..0f0f3aa16 100644--- a/API/Services/ImageService.cs+++ b/API/Services/ImageService.cs@@ -14,6 +14,15 @@ namespace API.Services{private readonly ILogger<ImageService> _logger;private readonly IDirectoryService _directoryService;+ public const string ChapterCoverImageRegex = @"v\d+_c\d+";+ public const string SeriesCoverImageRegex = @"seres\d+";+ public const string CollectionTagCoverImageRegex = @"tag\d+";+++ /// <summary>+ /// Width of the Thumbnail generation+ /// </summary>+ private const int ThumbnailWidth = 320;public ImageService(ILogger<ImageService> logger, IDirectoryService directoryService){@@ -41,63 +50,103 @@ namespace API.Servicesreturn firstImage;}- public byte[] GetCoverImage(string path, bool createThumbnail = false)+ public string GetCoverImage(string path, string fileName){- if (string.IsNullOrEmpty(path)) return Array.Empty<byte>();+ if (string.IsNullOrEmpty(path)) return string.Empty;try{- if (createThumbnail)- {- return CreateThumbnail(path);- }-- using var img = Image.NewFromFile(path);- using var stream = new MemoryStream();- img.JpegsaveStream(stream);- stream.Position = 0;- return stream.ToArray();+ return CreateThumbnail(path, fileName);}catch (Exception ex){_logger.LogWarning(ex, "[GetCoverImage] There was an error and prevented thumbnail generation on {ImageFile}. Defaulting to no cover image", path);}- return Array.Empty<byte>();+ return string.Empty;}-/// <inheritdoc />- public byte[] CreateThumbnail(string path)+ public string CreateThumbnail(string path, string fileName){try{- using var thumbnail = Image.Thumbnail(path, MetadataService.ThumbnailWidth);- return thumbnail.WriteToBuffer(".jpg");+ using var thumbnail = Image.Thumbnail(path, ThumbnailWidth);+ var filename = fileName + ".png";+ thumbnail.WriteToFile(Path.Join(DirectoryService.CoverImageDirectory, fileName + ".png"));+ return filename;}catch (Exception e){_logger.LogError(e, "Error creating thumbnail from url");}- return Array.Empty<byte>();+ return string.Empty;+ }++ /// <summary>+ /// Creates a thumbnail out of a memory stream and saves to <see cref="DirectoryService.CoverImageDirectory"/> with the passed+ /// fileName and .png extension.+ /// </summary>+ /// <param name="stream">Stream to write to disk. Ensure this is rewinded.</param>+ /// <param name="fileName">filename to save as without extension</param>+ /// <returns>File name with extension of the file. This will always write to <see cref="DirectoryService.CoverImageDirectory"/></returns>+ public static string WriteCoverThumbnail(Stream stream, string fileName)+ {+ using var thumbnail = NetVips.Image.ThumbnailStream(stream, ThumbnailWidth);+ var filename = fileName + ".png";+ thumbnail.WriteToFile(Path.Join(DirectoryService.CoverImageDirectory, fileName + ".png"));+ return filename;}/// <inheritdoc />- public byte[] CreateThumbnailFromBase64(string encodedImage)+ public string CreateThumbnailFromBase64(string encodedImage, string fileName){try{- using var thumbnail = Image.ThumbnailBuffer(Convert.FromBase64String(encodedImage), MetadataService.ThumbnailWidth);- return thumbnail.WriteToBuffer(".jpg");+ using var thumbnail = Image.ThumbnailBuffer(Convert.FromBase64String(encodedImage), ThumbnailWidth);+ var filename = fileName + ".png";+ thumbnail.WriteToFile(Path.Join(DirectoryService.CoverImageDirectory, fileName + ".png"));+ return filename;}catch (Exception e){_logger.LogError(e, "Error creating thumbnail from url");}- return Array.Empty<byte>();+ return string.Empty;+ }++ /// <summary>+ /// Returns the name format for a chapter cover image+ /// </summary>+ /// <param name="chapterId"></param>+ /// <param name="volumeId"></param>+ /// <returns></returns>+ public static string GetChapterFormat(int chapterId, int volumeId)+ {+ return $"v{volumeId}_c{chapterId}";+ }++ /// <summary>+ /// Returns the name format for a series cover image+ /// </summary>+ /// <param name="seriesId"></param>+ /// <returns></returns>+ public static string GetSeriesFormat(int seriesId)+ {+ return $"series{seriesId}";+ }++ /// <summary>+ /// Returns the name format for a collection tag cover image+ /// </summary>+ /// <param name="tagId"></param>+ /// <returns></returns>+ public static string GetCollectionTagFormat(int tagId)+ {+ return $"tag{tagId}";}}}diff --git a/API/Services/MetadataService.cs b/API/Services/MetadataService.csindex 4d6552156..d443f9f23 100644--- a/API/Services/MetadataService.cs+++ b/API/Services/MetadataService.cs@@ -1,6 +1,7 @@using System;using System.Collections.Generic;using System.Diagnostics;+using System.IO;using System.Linq;using System.Threading.Tasks;using API.Comparators;@@ -24,10 +25,6 @@ namespace API.Servicesprivate readonly IImageService _imageService;private readonly IHubContext<MessageHub> _messageHub;private readonly ChapterSortComparerZeroFirst _chapterSortComparerForInChapterSorting = new ChapterSortComparerZeroFirst();- /// <summary>- /// Width of the Thumbnail generation- /// </summary>- public static readonly int ThumbnailWidth = 320; // 153w x 230hpublic MetadataService(IUnitOfWork unitOfWork, ILogger<MetadataService> logger,IArchiveService archiveService, IBookService bookService, IImageService imageService, IHubContext<MessageHub> messageHub)@@ -41,41 +38,55 @@ namespace API.Services}/// <summary>- /// Determines whether an entity should regenerate cover image+ /// Determines whether an entity should regenerate cover image./// </summary>+ /// <remarks>If a cover image is locked but the underlying file has been deleted, this will allow regenerating. </remarks>/// <param name="coverImage"></param>/// <param name="firstFile"></param>/// <param name="forceUpdate"></param>/// <param name="isCoverLocked"></param>+ /// <param name="coverImageDirectory">Directory where cover images are. Defaults to <see cref="DirectoryService.CoverImageDirectory"/></param>/// <returns></returns>- public static bool ShouldUpdateCoverImage(byte[] coverImage, MangaFile firstFile, bool forceUpdate = false,- bool isCoverLocked = false)+ public static bool ShouldUpdateCoverImage(string coverImage, MangaFile firstFile, bool forceUpdate = false,+ bool isCoverLocked = false, string coverImageDirectory = null){- if (isCoverLocked) return false;+ if (string.IsNullOrEmpty(coverImageDirectory))+ {+ coverImageDirectory = DirectoryService.CoverImageDirectory;+ }++ var fileExists = File.Exists(Path.Join(coverImageDirectory, coverImage));+ if (isCoverLocked && fileExists) return false;if (forceUpdate) return true;- return (firstFile != null && firstFile.HasFileBeenModified()) || !HasCoverImage(coverImage);+ return (firstFile != null && firstFile.HasFileBeenModified()) || !HasCoverImage(coverImage, fileExists);+ }+++ private static bool HasCoverImage(string coverImage)+ {+ return HasCoverImage(coverImage, File.Exists(coverImage));}- private static bool HasCoverImage(byte[] coverImage)+ private static bool HasCoverImage(string coverImage, bool fileExists){- return coverImage != null && coverImage.Any();+ return !string.IsNullOrEmpty(coverImage) && fileExists;}- private byte[] GetCoverImage(MangaFile file, bool createThumbnail = true)+ private string GetCoverImage(MangaFile file, int volumeId, int chapterId){file.LastModified = DateTime.Now;switch (file.Format){case MangaFormat.Pdf:case MangaFormat.Epub:- return _bookService.GetCoverImage(file.FilePath, createThumbnail);+ return _bookService.GetCoverImage(file.FilePath, ImageService.GetChapterFormat(chapterId, volumeId));case MangaFormat.Image:var coverImage = _imageService.GetCoverFile(file);- return _imageService.GetCoverImage(coverImage, createThumbnail);+ return _imageService.GetCoverImage(coverImage, ImageService.GetChapterFormat(chapterId, volumeId));case MangaFormat.Archive:- return _archiveService.GetCoverImage(file.FilePath, createThumbnail);+ return _archiveService.GetCoverImage(file.FilePath, ImageService.GetChapterFormat(chapterId, volumeId));default:- return Array.Empty<byte>();+ return string.Empty;}}@@ -91,7 +102,7 @@ namespace API.Servicesif (ShouldUpdateCoverImage(chapter.CoverImage, firstFile, forceUpdate, chapter.CoverImageLocked)){- chapter.CoverImage = GetCoverImage(firstFile);+ chapter.CoverImage = GetCoverImage(firstFile, chapter.VolumeId, chapter.Id);return true;}@@ -130,7 +141,7 @@ namespace API.Services{series.Volumes ??= new List<Volume>();var firstCover = series.Volumes.GetCoverImage(series.Format);- byte[] coverImage = null;+ string coverImage = null;if (firstCover == null && series.Volumes.Any()){// If firstCover is null and one volume, the whole series is Chapters under Vol 0.diff --git a/API/Services/TaskScheduler.cs b/API/Services/TaskScheduler.csindex fe6931b92..2d1b25a7d 100644--- a/API/Services/TaskScheduler.cs+++ b/API/Services/TaskScheduler.cs@@ -121,7 +121,7 @@ namespace API.Services_logger.LogInformation("Enqueuing library scan for: {LibraryId}", libraryId);BackgroundJob.Enqueue(() => _scannerService.ScanLibrary(libraryId, forceUpdate));// When we do a scan, force cache to re-unpack in case page numbers change- BackgroundJob.Enqueue(() => _cleanupService.Cleanup());+ BackgroundJob.Enqueue(() => _cleanupService.CleanupCacheDirectory());}public void CleanupChapters(int[] chapterIds)diff --git a/API/Services/Tasks/BackupService.cs b/API/Services/Tasks/BackupService.csindex e04e5373f..35388985a 100644--- a/API/Services/Tasks/BackupService.cs+++ b/API/Services/Tasks/BackupService.cs@@ -59,8 +59,11 @@ namespace API.Services.Tasksreturn files;}+ /// <summary>+ /// Will backup anything that needs to be backed up. This includes logs, setting files, bare minimum cover images (just locked and first cover).+ /// </summary>[AutomaticRetry(Attempts = 3, LogEvents = false, OnAttemptsExceeded = AttemptsExceededAction.Fail)]- public void BackupDatabase()+ public async Task BackupDatabase(){_logger.LogInformation("Beginning backup of Database at {BackupTime}", DateTime.Now);var backupDirectory = Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BackupDirectory)).Result.Value;@@ -87,6 +90,9 @@ namespace API.Services.Tasks_directoryService.CopyFilesToDirectory(_backupFiles.Select(file => Path.Join(Directory.GetCurrentDirectory(), file)).ToList(), tempDirectory);++ await CopyCoverImagesToBackupDirectory(tempDirectory);+try{ZipFile.CreateFromDirectory(tempDirectory, zipPath);@@ -100,6 +106,31 @@ namespace API.Services.Tasks_logger.LogInformation("Database backup completed");}+ private async Task CopyCoverImagesToBackupDirectory(string tempDirectory)+ {+ var outputTempDir = Path.Join(tempDirectory, "covers");+ DirectoryService.ExistOrCreate(outputTempDir);++ try+ {+ var seriesImages = await _unitOfWork.SeriesRepository.GetLockedCoverImagesAsync();+ _directoryService.CopyFilesToDirectory(+ seriesImages.Select(s => Path.Join(DirectoryService.CoverImageDirectory, s)), outputTempDir);++ var collectionTags = await _unitOfWork.CollectionTagRepository.GetAllCoverImagesAsync();+ _directoryService.CopyFilesToDirectory(+ collectionTags.Select(s => Path.Join(DirectoryService.CoverImageDirectory, s)), outputTempDir);++ var chapterImages = await _unitOfWork.ChapterRepository.GetCoverImagesForLockedChaptersAsync();+ _directoryService.CopyFilesToDirectory(+ chapterImages.Select(s => Path.Join(DirectoryService.CoverImageDirectory, s)), outputTempDir);+ }+ catch (IOException e)+ {+ // Swallow exception. This can be a duplicate cover being copied as chapter and volumes can share same file.+ }+ }+/// <summary>/// Removes Database backups older than 30 days. If all backups are older than 30 days, the latest is kept./// </summary>diff --git a/API/Services/Tasks/CleanupService.cs b/API/Services/Tasks/CleanupService.csindex ae04d46bd..c1edf2e6b 100644--- a/API/Services/Tasks/CleanupService.cs+++ b/API/Services/Tasks/CleanupService.cs@@ -1,7 +1,11 @@using System.IO;+using System.Linq;+using System.Threading.Tasks;+using API.Interfaces;using API.Interfaces.Services;using Hangfire;using Microsoft.Extensions.Logging;+using NetVips;namespace API.Services.Tasks{@@ -13,27 +17,79 @@ namespace API.Services.Tasksprivate readonly ICacheService _cacheService;private readonly ILogger<CleanupService> _logger;private readonly IBackupService _backupService;+ private readonly IUnitOfWork _unitOfWork;+ private readonly IDirectoryService _directoryService;- public CleanupService(ICacheService cacheService, ILogger<CleanupService> logger, IBackupService backupService)+ public CleanupService(ICacheService cacheService, ILogger<CleanupService> logger,+ IBackupService backupService, IUnitOfWork unitOfWork, IDirectoryService directoryService){_cacheService = cacheService;_logger = logger;_backupService = backupService;+ _unitOfWork = unitOfWork;+ _directoryService = directoryService;+ }++ public void CleanupCacheDirectory()+ {+ _logger.LogInformation("Cleaning cache directory");+ _cacheService.Cleanup();}/// <summary>- /// Cleans up Temp, cache, and old database backups+ /// Cleans up Temp, cache, deleted cover images, and old database backups/// </summary>[AutomaticRetry(Attempts = 3, LogEvents = false, OnAttemptsExceeded = AttemptsExceededAction.Fail)]- public void Cleanup()+ public async Task Cleanup(){+ _logger.LogInformation("Starting Cleanup");_logger.LogInformation("Cleaning temp directory");var tempDirectory = Path.Join(Directory.GetCurrentDirectory(), "temp");DirectoryService.ClearDirectory(tempDirectory);- _logger.LogInformation("Cleaning cache directory");- _cacheService.Cleanup();+ CleanupCacheDirectory();_logger.LogInformation("Cleaning old database backups");_backupService.CleanupBackups();+ _logger.LogInformation("Cleaning deleted cover images");+ await DeleteSeriesCoverImages();+ await DeleteChapterCoverImages();+ await DeleteTagCoverImages();+ _logger.LogInformation("Cleanup finished");+ }++ private async Task DeleteSeriesCoverImages()+ {+ var images = await _unitOfWork.SeriesRepository.GetAllCoverImagesAsync();+ var files = _directoryService.GetFiles(DirectoryService.CoverImageDirectory, ImageService.SeriesCoverImageRegex);+ foreach (var file in files)+ {+ if (images.Contains(Path.GetFileName(file))) continue;+ File.Delete(file);++ }+ }++ private async Task DeleteChapterCoverImages()+ {+ var images = await _unitOfWork.ChapterRepository.GetAllCoverImagesAsync();+ var files = _directoryService.GetFiles(DirectoryService.CoverImageDirectory, ImageService.ChapterCoverImageRegex);+ foreach (var file in files)+ {+ if (images.Contains(Path.GetFileName(file))) continue;+ File.Delete(file);++ }+ }++ private async Task DeleteTagCoverImages()+ {+ var images = await _unitOfWork.CollectionTagRepository.GetAllCoverImagesAsync();+ var files = _directoryService.GetFiles(DirectoryService.CoverImageDirectory, ImageService.CollectionTagCoverImageRegex);+ foreach (var file in files)+ {+ if (images.Contains(Path.GetFileName(file))) continue;+ File.Delete(file);++ }}}}diff --git a/API/Services/Tasks/Scanner/ParseScannedFiles.cs b/API/Services/Tasks/Scanner/ParseScannedFiles.csindex 9bb13021f..afcfc1c13 100644--- a/API/Services/Tasks/Scanner/ParseScannedFiles.cs+++ b/API/Services/Tasks/Scanner/ParseScannedFiles.cs@@ -48,7 +48,7 @@ namespace API.Services.Tasks.Scannerpublic static IList<ParserInfo> GetInfosByName(Dictionary<ParsedSeries, List<ParserInfo>> parsedSeries, Series series){var existingKey = parsedSeries.Keys.FirstOrDefault(ps =>- ps.Format == series.Format && ps.NormalizedName == Parser.Parser.Normalize(series.OriginalName));+ ps.Format == series.Format && ps.NormalizedName.Equals(Parser.Parser.Normalize(series.OriginalName)));return existingKey != null ? parsedSeries[existingKey] : new List<ParserInfo>();}diff --git a/API/Services/Tasks/ScannerService.cs b/API/Services/Tasks/ScannerService.csindex 6f494de2b..6ef453ac3 100644--- a/API/Services/Tasks/ScannerService.cs+++ b/API/Services/Tasks/ScannerService.cs@@ -277,6 +277,9 @@ namespace API.Services.Tasks_logger.LogError(ex, "There was an exception updating volumes for {SeriesName}", series.Name);}});++ // Last step, remove any series that have no pages+ library.Series = library.Series.Where(s => s.Pages > 0).ToList();}public IEnumerable<Series> FindSeriesNotOnDisk(ICollection<Series> existingSeries, Dictionary<ParsedSeries, List<ParserInfo>> parsedSeries)diff --git a/API/Startup.cs b/API/Startup.csindex cfdae2d9d..ddbf25105 100644--- a/API/Startup.cs+++ b/API/Startup.cs@@ -132,7 +132,7 @@ namespace APInew Microsoft.Net.Http.Headers.CacheControlHeaderValue(){Public = false,- MaxAge = TimeSpan.FromSeconds(10)+ MaxAge = TimeSpan.FromSeconds(10),};context.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.Vary] =new[] { "Accept-Encoding" };diff --git a/UI/Web/src/app/cards/card-item/card-item.component.html b/UI/Web/src/app/cards/card-item/card-item.component.htmlindex 478764aaf..d2ef9853b 100644--- a/UI/Web/src/app/cards/card-item/card-item.component.html+++ b/UI/Web/src/app/cards/card-item/card-item.component.html@@ -15,7 +15,7 @@</span></div><div class="error-banner" *ngIf="total === 0 && !supressArchiveWarning">- Cannot Read Archive+ Cannot Read</div>