EPUB Support (#178)
* Added book filetype detection and reorganized tests due to size of file * Added ability to get basic Parse Info from Book and Pages. * We can now scan books and get them in a library with cover images. * Take the first image in the epub if the cover isn't set. * Implemented the ability to unzip the ebup to cache. Implemented a test api to load html files. * Just some test code to figure out how to approach this. * Fixed some merge conflicts * Removed some dead code from merge * Snapshot: I can now load everything properly into the UI by rewriting the urls before I send them back. I don't notice any lag from this method. It can be optimized further. * Implemented a way to load the content in the browser not via an iframe. * Added a note * Anchor mappings is complete. New anchors are updated so references now resolve to javascript:void() for UI to take care of internally loading and the appropriate page is mapped to it. Anchors that are external have target="_blank" added so they don't force you out of the app and styles are of course inlined. * Oops i need this * Table of contents api implemented (rough) and some small enhancements to codebase for books. * GetBookPageResources now only loads files from within the book. Nested chapter list support and images now use html parsing instead of string parsing. * Fonts now are remapped to load from endpoint. * book-resources now uses a key, ensuring the file is in proper format for lookup. Changed chapter list based on structure with one HEADER and nested chapters. * Properly handle svg resource requests and when there are part anchors that are clickable, make sure we handle them in the UI by adding a kavita-page handler. * Add Chapter group page even if one isn't set by using first page (without part) from nestedChildren. * Added extra debug code for issue #163. * Added new user preferences for books and updated the css so we scope it to our reading section. * Cleaned up style code * Implemented ability to save book preferences and some cleanup on existing apis. * Added an api for checking if a user has read something in a library type before. * Forgot to make sure the has reading progress is against a user lol. * Remove cacheservice code for books, sine we use an in-memory method * Handle svg images as well * Enhanced cover image extraction to check for a "cover" image if the cover image wasn't set in OPF before falling back to the first image. * Fixed an issue with special books not properly generating metadata due to not having filename set. * Cleanup, removed warmup task code from statup/program and changed taskscheduler to schedule tasks on startup only (or if tasks are changed from UI). * Code cleanup * Code cleanup * So much code. Lots of refactors to try to test scanner service. Moved a lot of the queries into Extensions to allow to easier test, even though it's hacky. Support @font-face src:url swaps with ' and ". Source summary information from epubs. * Well...baseURL needs to come from BE and not from UI lol. * Adjusted migrations so default values match Entity * Removed comment * I think I finally fixed #163! The issue was that when i checked if it had a parserInfo, i wasn't considering that the chapter range might have a - in it (0-6) and so when the code to check if range could parse out a number failed, it treated it like a special and checked range against info's filename. * Some bugfixes * Lots of testing, extracting code to make it easier to test. This code is buggy, but fixed a bug where 1) If we changed the normalization code, we would remove the whole db during a scan and 2) We weren't actually removing series properly. Other than that, code is being extracted to remove duplication and centralize logic. * More code cleanup and test cleanup to ensure scan loop is working as expected and matches expectaions from tests. * Cleaned up the code and made it so if I change normalization, which I do in this branch, it wont break existing DBs. * Some comic parser changes for partial chapter support. * Added some code for directory service and scanner service along with python code to generate test files (not used yet). Fixed up all the tests. * Code smells
This commit is contained in:
parent
2b99c8abfa
commit
a01613f80f
103 changed files with 5017 additions and 2480 deletions
|
@ -21,11 +21,12 @@ namespace API.Services
|
|||
/// <summary>
|
||||
/// Responsible for manipulating Archive files. Used by <see cref="CacheService"/> and <see cref="ScannerService"/>
|
||||
/// </summary>
|
||||
// ReSharper disable once ClassWithVirtualMembersNeverInherited.Global
|
||||
public class ArchiveService : IArchiveService
|
||||
{
|
||||
private readonly ILogger<ArchiveService> _logger;
|
||||
private const int ThumbnailWidth = 320; // 153w x 230h
|
||||
private static readonly RecyclableMemoryStreamManager _streamManager = new();
|
||||
private static readonly RecyclableMemoryStreamManager StreamManager = new();
|
||||
private readonly NaturalSortComparer _comparer;
|
||||
|
||||
public ArchiveService(ILogger<ArchiveService> logger)
|
||||
|
@ -41,7 +42,7 @@ namespace API.Services
|
|||
/// <returns></returns>
|
||||
public virtual ArchiveLibrary CanOpen(string archivePath)
|
||||
{
|
||||
if (!File.Exists(archivePath) || !Parser.Parser.IsArchive(archivePath)) return ArchiveLibrary.NotSupported;
|
||||
if (!(File.Exists(archivePath) && Parser.Parser.IsArchive(archivePath) || Parser.Parser.IsEpub(archivePath))) return ArchiveLibrary.NotSupported;
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -172,7 +173,7 @@ namespace API.Services
|
|||
var entryName = FindFolderEntry(entryNames) ?? FirstFileEntry(entryNames);
|
||||
var entry = archive.Entries.Single(e => e.Key == entryName);
|
||||
|
||||
using var ms = _streamManager.GetStream();
|
||||
using var ms = StreamManager.GetStream();
|
||||
entry.WriteTo(ms);
|
||||
ms.Position = 0;
|
||||
|
||||
|
@ -197,7 +198,7 @@ namespace API.Services
|
|||
private static byte[] ConvertEntryToByteArray(ZipArchiveEntry entry)
|
||||
{
|
||||
using var stream = entry.Open();
|
||||
using var ms = _streamManager.GetStream();
|
||||
using var ms = StreamManager.GetStream();
|
||||
stream.CopyTo(ms);
|
||||
return ms.ToArray();
|
||||
}
|
||||
|
@ -248,7 +249,7 @@ namespace API.Services
|
|||
return false;
|
||||
}
|
||||
|
||||
if (Parser.Parser.IsArchive(archivePath)) return true;
|
||||
if (Parser.Parser.IsArchive(archivePath) || Parser.Parser.IsEpub(archivePath)) return true;
|
||||
|
||||
_logger.LogError("Archive {ArchivePath} is not a valid archive", archivePath);
|
||||
return false;
|
||||
|
@ -261,7 +262,7 @@ namespace API.Services
|
|||
{
|
||||
if (Path.GetFileNameWithoutExtension(entry.Key).ToLower().EndsWith("comicinfo") && !Parser.Parser.HasBlacklistedFolderInPath(entry.Key) && Parser.Parser.IsXml(entry.Key))
|
||||
{
|
||||
using var ms = _streamManager.GetStream();
|
||||
using var ms = StreamManager.GetStream();
|
||||
entry.WriteTo(ms);
|
||||
ms.Position = 0;
|
||||
|
||||
|
@ -398,10 +399,10 @@ namespace API.Services
|
|||
break;
|
||||
}
|
||||
case ArchiveLibrary.NotSupported:
|
||||
_logger.LogError("[GetNumberOfPagesFromArchive] This archive cannot be read: {ArchivePath}. Defaulting to 0 pages", archivePath);
|
||||
_logger.LogError("[ExtractArchive] This archive cannot be read: {ArchivePath}. Defaulting to 0 pages", archivePath);
|
||||
return;
|
||||
default:
|
||||
_logger.LogError("[GetNumberOfPagesFromArchive] There was an exception when reading archive stream: {ArchivePath}. Defaulting to 0 pages", archivePath);
|
||||
_logger.LogError("[ExtractArchive] There was an exception when reading archive stream: {ArchivePath}. Defaulting to 0 pages", archivePath);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
257
API/Services/BookService.cs
Normal file
257
API/Services/BookService.cs
Normal file
|
@ -0,0 +1,257 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using API.Entities.Enums;
|
||||
using API.Entities.Interfaces;
|
||||
using API.Interfaces;
|
||||
using API.Parser;
|
||||
using ExCSS;
|
||||
using HtmlAgilityPack;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NetVips;
|
||||
using VersOne.Epub;
|
||||
using VersOne.Epub.Schema;
|
||||
|
||||
namespace API.Services
|
||||
{
|
||||
public class BookService : IBookService
|
||||
{
|
||||
private readonly ILogger<BookService> _logger;
|
||||
|
||||
private const int ThumbnailWidth = 320; // 153w x 230h
|
||||
private readonly StylesheetParser _cssParser = new ();
|
||||
|
||||
public BookService(ILogger<BookService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private static bool HasClickableHrefPart(HtmlNode anchor)
|
||||
{
|
||||
return anchor.GetAttributeValue("href", string.Empty).Contains("#")
|
||||
&& anchor.GetAttributeValue("tabindex", string.Empty) != "-1"
|
||||
&& anchor.GetAttributeValue("role", string.Empty) != "presentation";
|
||||
}
|
||||
|
||||
public static string GetContentType(EpubContentType type)
|
||||
{
|
||||
string contentType;
|
||||
switch (type)
|
||||
{
|
||||
case EpubContentType.IMAGE_GIF:
|
||||
contentType = "image/gif";
|
||||
break;
|
||||
case EpubContentType.IMAGE_PNG:
|
||||
contentType = "image/png";
|
||||
break;
|
||||
case EpubContentType.IMAGE_JPEG:
|
||||
contentType = "image/jpeg";
|
||||
break;
|
||||
case EpubContentType.FONT_OPENTYPE:
|
||||
contentType = "font/otf";
|
||||
break;
|
||||
case EpubContentType.FONT_TRUETYPE:
|
||||
contentType = "font/ttf";
|
||||
break;
|
||||
case EpubContentType.IMAGE_SVG:
|
||||
contentType = "image/svg+xml";
|
||||
break;
|
||||
default:
|
||||
contentType = "application/octet-stream";
|
||||
break;
|
||||
}
|
||||
|
||||
return contentType;
|
||||
}
|
||||
|
||||
public static void UpdateLinks(HtmlNode anchor, Dictionary<string, int> mappings, int currentPage)
|
||||
{
|
||||
if (anchor.Name != "a") return;
|
||||
var hrefParts = BookService.CleanContentKeys(anchor.GetAttributeValue("href", string.Empty))
|
||||
.Split("#");
|
||||
var mappingKey = hrefParts[0];
|
||||
if (!mappings.ContainsKey(mappingKey))
|
||||
{
|
||||
if (HasClickableHrefPart(anchor))
|
||||
{
|
||||
var part = hrefParts.Length > 1
|
||||
? hrefParts[1]
|
||||
: anchor.GetAttributeValue("href", string.Empty);
|
||||
anchor.Attributes.Add("kavita-page", $"{currentPage}");
|
||||
anchor.Attributes.Add("kavita-part", part);
|
||||
anchor.Attributes.Remove("href");
|
||||
anchor.Attributes.Add("href", "javascript:void(0)");
|
||||
}
|
||||
else
|
||||
{
|
||||
anchor.Attributes.Add("target", "_blank");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var mappedPage = mappings[mappingKey];
|
||||
anchor.Attributes.Add("kavita-page", $"{mappedPage}");
|
||||
if (hrefParts.Length > 1)
|
||||
{
|
||||
anchor.Attributes.Add("kavita-part",
|
||||
hrefParts[1]);
|
||||
}
|
||||
|
||||
anchor.Attributes.Remove("href");
|
||||
anchor.Attributes.Add("href", "javascript:void(0)");
|
||||
}
|
||||
|
||||
public async Task<string> ScopeStyles(string stylesheetHtml, string apiBase)
|
||||
{
|
||||
var styleContent = RemoveWhiteSpaceFromStylesheets(stylesheetHtml);
|
||||
styleContent =
|
||||
Parser.Parser.FontSrcUrlRegex.Replace(styleContent, "$1" + apiBase + "$2" + "$3");
|
||||
|
||||
styleContent = styleContent.Replace("body", ".reading-section");
|
||||
|
||||
var stylesheet = await _cssParser.ParseAsync(styleContent);
|
||||
foreach (var styleRule in stylesheet.StyleRules)
|
||||
{
|
||||
if (styleRule.Selector.Text == ".reading-section") continue;
|
||||
if (styleRule.Selector.Text.Contains(","))
|
||||
{
|
||||
styleRule.Text = styleRule.Text.Replace(styleRule.SelectorText,
|
||||
string.Join(", ",
|
||||
styleRule.Selector.Text.Split(",").Select(s => ".reading-section " + s)));
|
||||
continue;
|
||||
}
|
||||
styleRule.Text = ".reading-section " + styleRule.Text;
|
||||
}
|
||||
return RemoveWhiteSpaceFromStylesheets(stylesheet.ToCss());
|
||||
}
|
||||
|
||||
public string GetSummaryInfo(string filePath)
|
||||
{
|
||||
if (!IsValidFile(filePath)) return string.Empty;
|
||||
|
||||
var epubBook = EpubReader.OpenBook(filePath);
|
||||
return epubBook.Schema.Package.Metadata.Description;
|
||||
}
|
||||
|
||||
private bool IsValidFile(string filePath)
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
_logger.LogError("Book {EpubFile} could not be found", filePath);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Parser.Parser.IsBook(filePath)) return true;
|
||||
|
||||
_logger.LogError("Book {EpubFile} is not a valid EPUB", filePath);
|
||||
return false;
|
||||
}
|
||||
|
||||
public int GetNumberOfPages(string filePath)
|
||||
{
|
||||
if (!IsValidFile(filePath) || !Parser.Parser.IsEpub(filePath)) return 0;
|
||||
|
||||
try
|
||||
{
|
||||
var epubBook = EpubReader.OpenBook(filePath);
|
||||
return epubBook.Content.Html.Count;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "There was an exception getting number of pages, defaulting to 0");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static string CleanContentKeys(string key)
|
||||
{
|
||||
return key.Replace("../", string.Empty);
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, int>> CreateKeyToPageMappingAsync(EpubBookRef book)
|
||||
{
|
||||
var dict = new Dictionary<string, int>();
|
||||
var pageCount = 0;
|
||||
foreach (var contentFileRef in await book.GetReadingOrderAsync())
|
||||
{
|
||||
if (contentFileRef.ContentType != EpubContentType.XHTML_1_1) continue;
|
||||
dict.Add(contentFileRef.FileName, pageCount);
|
||||
pageCount += 1;
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
public static ParserInfo ParseInfo(string filePath)
|
||||
{
|
||||
var epubBook = EpubReader.OpenBook(filePath);
|
||||
|
||||
return new ParserInfo()
|
||||
{
|
||||
Chapters = "0",
|
||||
Edition = "",
|
||||
Format = MangaFormat.Book,
|
||||
Filename = Path.GetFileName(filePath),
|
||||
Title = epubBook.Title,
|
||||
FullFilePath = filePath,
|
||||
IsSpecial = false,
|
||||
Series = epubBook.Title,
|
||||
Volumes = "0"
|
||||
};
|
||||
}
|
||||
|
||||
public byte[] GetCoverImage(string fileFilePath, bool createThumbnail = true)
|
||||
{
|
||||
if (!IsValidFile(fileFilePath)) return Array.Empty<byte>();
|
||||
|
||||
var epubBook = EpubReader.OpenBook(fileFilePath);
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
// Try to get the cover image from OPF file, if not set, try to parse it from all the files, then result to the first one.
|
||||
var coverImageContent = epubBook.Content.Cover
|
||||
?? epubBook.Content.Images.Values.FirstOrDefault(file => Parser.Parser.IsCoverImage(file.FileName))
|
||||
?? epubBook.Content.Images.Values.First();
|
||||
|
||||
if (coverImageContent == null) return Array.Empty<byte>();
|
||||
|
||||
if (createThumbnail)
|
||||
{
|
||||
using var stream = new MemoryStream(coverImageContent.ReadContent());
|
||||
|
||||
using var thumbnail = Image.ThumbnailStream(stream, ThumbnailWidth);
|
||||
return thumbnail.WriteToBuffer(".jpg");
|
||||
}
|
||||
|
||||
return coverImageContent.ReadContent();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "There was a critical error and prevented thumbnail generation on {BookFile}. Defaulting to no cover image", fileFilePath);
|
||||
}
|
||||
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
|
||||
private static string RemoveWhiteSpaceFromStylesheets(string body)
|
||||
{
|
||||
body = Regex.Replace(body, @"[a-zA-Z]+#", "#");
|
||||
body = Regex.Replace(body, @"[\n\r]+\s*", string.Empty);
|
||||
body = Regex.Replace(body, @"\s+", " ");
|
||||
body = Regex.Replace(body, @"\s?([:,;{}])\s?", "$1");
|
||||
body = body.Replace(";}", "}");
|
||||
body = Regex.Replace(body, @"([\s:]0)(px|pt|%|em)", "$1");
|
||||
|
||||
// Remove comments from CSS
|
||||
body = Regex.Replace(body, @"/\*[\d\D]*?\*/", string.Empty);
|
||||
|
||||
return body;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ using System.Linq;
|
|||
using System.Threading.Tasks;
|
||||
using API.Comparators;
|
||||
using API.Entities;
|
||||
using API.Entities.Enums;
|
||||
using API.Extensions;
|
||||
using API.Interfaces;
|
||||
using API.Interfaces.Services;
|
||||
|
@ -20,7 +21,8 @@ namespace API.Services
|
|||
private readonly NumericComparer _numericComparer;
|
||||
public static readonly string CacheDirectory = Path.GetFullPath(Path.Join(Directory.GetCurrentDirectory(), "cache/"));
|
||||
|
||||
public CacheService(ILogger<CacheService> logger, IUnitOfWork unitOfWork, IArchiveService archiveService, IDirectoryService directoryService)
|
||||
public CacheService(ILogger<CacheService> logger, IUnitOfWork unitOfWork, IArchiveService archiveService,
|
||||
IDirectoryService directoryService)
|
||||
{
|
||||
_logger = logger;
|
||||
_unitOfWork = unitOfWork;
|
||||
|
@ -31,7 +33,6 @@ namespace API.Services
|
|||
|
||||
public void EnsureCacheDirectory()
|
||||
{
|
||||
_logger.LogDebug("Checking if valid Cache directory: {CacheDirectory}", CacheDirectory);
|
||||
if (!DirectoryService.ExistOrCreate(CacheDirectory))
|
||||
{
|
||||
_logger.LogError("Cache directory {CacheDirectory} is not accessible or does not exist. Creating...", CacheDirectory);
|
||||
|
@ -53,7 +54,12 @@ namespace API.Services
|
|||
{
|
||||
extraPath = file.Id + "";
|
||||
}
|
||||
_archiveService.ExtractArchive(file.FilePath, Path.Join(extractPath, extraPath));
|
||||
|
||||
if (file.Format == MangaFormat.Archive)
|
||||
{
|
||||
_archiveService.ExtractArchive(file.FilePath, Path.Join(extractPath, extraPath));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (fileCount > 1)
|
||||
|
@ -123,6 +129,11 @@ namespace API.Services
|
|||
var path = GetCachePath(chapter.Id);
|
||||
var files = _directoryService.GetFilesWithExtension(path, Parser.Parser.ImageFileExtensions);
|
||||
Array.Sort(files, _numericComparer);
|
||||
|
||||
if (files.Length == 0)
|
||||
{
|
||||
return (files.ElementAt(0), mangaFile);
|
||||
}
|
||||
|
||||
// Since array is 0 based, we need to keep that in account (only affects last image)
|
||||
if (page == files.Length)
|
||||
|
|
|
@ -58,8 +58,7 @@ namespace API.Services
|
|||
{
|
||||
rootPath = rootPath.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
||||
}
|
||||
// NOTE: I Could use Path.GetRelativePath and split on separator character instead.
|
||||
|
||||
|
||||
var path = fullPath.EndsWith(separator) ? fullPath.Substring(0, fullPath.Length - 1) : fullPath;
|
||||
var root = rootPath.EndsWith(separator) ? rootPath.Substring(0, rootPath.Length - 1) : rootPath;
|
||||
var paths = new List<string>();
|
||||
|
@ -215,9 +214,9 @@ namespace API.Services
|
|||
/// <param name="action">Action to apply on file path</param>
|
||||
/// <param name="searchPattern">Regex pattern to search against</param>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
public static int TraverseTreeParallelForEach(string root, Action<string> action, string searchPattern)
|
||||
{
|
||||
//Count of files traversed and timer for diagnostic output
|
||||
public static int TraverseTreeParallelForEach(string root, Action<string> action, string searchPattern, ILogger logger)
|
||||
{
|
||||
//Count of files traversed and timer for diagnostic output
|
||||
var fileCount = 0;
|
||||
|
||||
// Determine whether to parallelize file processing on each folder based on processor count.
|
||||
|
@ -242,11 +241,13 @@ namespace API.Services
|
|||
// Thrown if we do not have discovery permission on the directory.
|
||||
catch (UnauthorizedAccessException e) {
|
||||
Console.WriteLine(e.Message);
|
||||
logger.LogError(e, "Unauthorized access on {Directory}", currentDir);
|
||||
continue;
|
||||
}
|
||||
// Thrown if another process has deleted the directory after we retrieved its name.
|
||||
catch (DirectoryNotFoundException e) {
|
||||
Console.WriteLine(e.Message);
|
||||
logger.LogError(e, "Directory not found on {Directory}", currentDir);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -268,24 +269,27 @@ namespace API.Services
|
|||
}
|
||||
|
||||
// Execute in parallel if there are enough files in the directory.
|
||||
// Otherwise, execute sequentially.Files are opened and processed
|
||||
// Otherwise, execute sequentially. Files are opened and processed
|
||||
// synchronously but this could be modified to perform async I/O.
|
||||
try {
|
||||
if (files.Length < procCount) {
|
||||
foreach (var file in files) {
|
||||
action(file);
|
||||
fileCount++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
Parallel.ForEach(files, () => 0, (file, _, localCount) =>
|
||||
{ action(file);
|
||||
return ++localCount;
|
||||
},
|
||||
(c) => {
|
||||
// ReSharper disable once AccessToModifiedClosure
|
||||
Interlocked.Add(ref fileCount, c);
|
||||
});
|
||||
// if (files.Length < procCount) {
|
||||
// foreach (var file in files) {
|
||||
// action(file);
|
||||
// fileCount++;
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// Parallel.ForEach(files, () => 0, (file, _, localCount) =>
|
||||
// { action(file);
|
||||
// return ++localCount;
|
||||
// },
|
||||
// (c) => {
|
||||
// Interlocked.Add(ref fileCount, c);
|
||||
// });
|
||||
// }
|
||||
foreach (var file in files) {
|
||||
action(file);
|
||||
fileCount++;
|
||||
}
|
||||
}
|
||||
catch (AggregateException ae) {
|
||||
|
|
|
@ -5,6 +5,8 @@ using System.IO;
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using API.Entities;
|
||||
using API.Entities.Enums;
|
||||
using API.Entities.Interfaces;
|
||||
using API.Extensions;
|
||||
using API.Interfaces;
|
||||
using API.Interfaces.Services;
|
||||
|
@ -17,12 +19,14 @@ namespace API.Services
|
|||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ILogger<MetadataService> _logger;
|
||||
private readonly IArchiveService _archiveService;
|
||||
private readonly IBookService _bookService;
|
||||
|
||||
public MetadataService(IUnitOfWork unitOfWork, ILogger<MetadataService> logger, IArchiveService archiveService)
|
||||
public MetadataService(IUnitOfWork unitOfWork, ILogger<MetadataService> logger, IArchiveService archiveService, IBookService bookService)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_logger = logger;
|
||||
_archiveService = archiveService;
|
||||
_bookService = bookService;
|
||||
}
|
||||
|
||||
private static bool ShouldFindCoverImage(byte[] coverImage, bool forceUpdate = false)
|
||||
|
@ -30,13 +34,25 @@ namespace API.Services
|
|||
return forceUpdate || coverImage == null || !coverImage.Any();
|
||||
}
|
||||
|
||||
private byte[] GetCoverImage(MangaFile file, bool createThumbnail = true)
|
||||
{
|
||||
if (file.Format == MangaFormat.Book)
|
||||
{
|
||||
return _bookService.GetCoverImage(file.FilePath, createThumbnail);
|
||||
}
|
||||
else
|
||||
{
|
||||
return _archiveService.GetCoverImage(file.FilePath, createThumbnail);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateMetadata(Chapter chapter, bool forceUpdate)
|
||||
{
|
||||
var firstFile = chapter.Files.OrderBy(x => x.Chapter).FirstOrDefault();
|
||||
if (ShouldFindCoverImage(chapter.CoverImage, forceUpdate) && firstFile != null && !new FileInfo(firstFile.FilePath).IsLastWriteLessThan(firstFile.LastModified))
|
||||
{
|
||||
chapter.Files ??= new List<MangaFile>();
|
||||
chapter.CoverImage = _archiveService.GetCoverImage(firstFile.FilePath, true);
|
||||
chapter.CoverImage = GetCoverImage(firstFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -55,7 +71,7 @@ namespace API.Services
|
|||
var firstFile = firstChapter?.Files.OrderBy(x => x.Chapter).FirstOrDefault();
|
||||
if (firstFile != null && !new FileInfo(firstFile.FilePath).IsLastWriteLessThan(firstFile.LastModified))
|
||||
{
|
||||
volume.CoverImage = _archiveService.GetCoverImage(firstFile.FilePath, true);
|
||||
volume.CoverImage = GetCoverImage(firstFile);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -72,7 +88,7 @@ namespace API.Services
|
|||
if (ShouldFindCoverImage(series.CoverImage, forceUpdate))
|
||||
{
|
||||
series.Volumes ??= new List<Volume>();
|
||||
var firstCover = series.Volumes.OrderBy(x => x.Number).FirstOrDefault(x => x.Number != 0);
|
||||
var firstCover = series.Volumes.GetCoverImage(series.Library.Type);
|
||||
byte[] coverImage = null;
|
||||
if (firstCover == null && series.Volumes.Any())
|
||||
{
|
||||
|
@ -92,24 +108,33 @@ namespace API.Services
|
|||
series.CoverImage = firstCover?.CoverImage ?? coverImage;
|
||||
}
|
||||
|
||||
UpdateSeriesSummary(series, forceUpdate);
|
||||
}
|
||||
|
||||
private void UpdateSeriesSummary(Series series, bool forceUpdate)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(series.Summary) && !forceUpdate) return;
|
||||
|
||||
var firstVolume = series.Volumes.FirstOrDefault(v => v.Chapters.Any() && v.Number == 1);
|
||||
var firstChapter = firstVolume?.Chapters.FirstOrDefault(c => c.Files.Any());
|
||||
|
||||
var isBook = series.Library.Type == LibraryType.Book;
|
||||
var firstVolume = series.Volumes.FirstWithChapters(isBook);
|
||||
var firstChapter = firstVolume?.Chapters.GetFirstChapterWithFiles();
|
||||
|
||||
// NOTE: This suffers from code changes not taking effect due to stale data
|
||||
var firstFile = firstChapter?.Files.FirstOrDefault();
|
||||
if (firstFile != null && !new FileInfo(firstFile.FilePath).DoesLastWriteMatch(firstFile.LastModified))
|
||||
if (firstFile != null &&
|
||||
(forceUpdate || !firstFile.HasFileBeenModified()))
|
||||
{
|
||||
series.Summary = _archiveService.GetSummaryInfo(firstFile.FilePath);
|
||||
series.Summary = isBook ? _bookService.GetSummaryInfo(firstFile.FilePath) : _archiveService.GetSummaryInfo(firstFile.FilePath);
|
||||
|
||||
firstFile.LastModified = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void RefreshMetadata(int libraryId, bool forceUpdate = false)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
var library = Task.Run(() => _unitOfWork.LibraryRepository.GetFullLibraryForIdAsync(libraryId)).Result;
|
||||
var library = Task.Run(() => _unitOfWork.LibraryRepository.GetFullLibraryForIdAsync(libraryId)).GetAwaiter().GetResult();
|
||||
|
||||
// TODO: See if we can break this up into multiple threads that process 20 series at a time then save so we can reduce amount of memory used
|
||||
_logger.LogInformation("Beginning metadata refresh of {LibraryName}", library.Name);
|
||||
|
|
|
@ -20,7 +20,7 @@ namespace API.Services
|
|||
private readonly ICleanupService _cleanupService;
|
||||
|
||||
public static BackgroundJobServer Client => new BackgroundJobServer();
|
||||
|
||||
|
||||
|
||||
public TaskScheduler(ICacheService cacheService, ILogger<TaskScheduler> logger, IScannerService scannerService,
|
||||
IUnitOfWork unitOfWork, IMetadataService metadataService, IBackupService backupService, ICleanupService cleanupService)
|
||||
|
@ -32,20 +32,19 @@ namespace API.Services
|
|||
_metadataService = metadataService;
|
||||
_backupService = backupService;
|
||||
_cleanupService = cleanupService;
|
||||
|
||||
ScheduleTasks();
|
||||
}
|
||||
|
||||
public void ScheduleTasks()
|
||||
{
|
||||
_logger.LogInformation("Scheduling reoccurring tasks");
|
||||
|
||||
string setting = Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.TaskScan)).GetAwaiter().GetResult().Value;
|
||||
var setting = Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.TaskScan)).GetAwaiter().GetResult().Value;
|
||||
if (setting != null)
|
||||
{
|
||||
_logger.LogDebug("Scheduling Scan Library Task for {Setting}", setting);
|
||||
var scanLibrarySetting = setting;
|
||||
_logger.LogDebug("Scheduling Scan Library Task for {Setting}", scanLibrarySetting);
|
||||
RecurringJob.AddOrUpdate("scan-libraries", () => _scannerService.ScanLibraries(),
|
||||
() => CronConverter.ConvertToCronNotation(setting));
|
||||
() => CronConverter.ConvertToCronNotation(scanLibrarySetting));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -69,7 +68,7 @@ namespace API.Services
|
|||
public void ScanLibrary(int libraryId, bool forceUpdate = false)
|
||||
{
|
||||
_logger.LogInformation("Enqueuing library scan for: {LibraryId}", libraryId);
|
||||
BackgroundJob.Enqueue(() => _scannerService.ScanLibrary(libraryId, forceUpdate));
|
||||
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());
|
||||
}
|
||||
|
|
|
@ -6,8 +6,10 @@ using System.IO;
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using API.Comparators;
|
||||
using API.Data;
|
||||
using API.Entities;
|
||||
using API.Entities.Enums;
|
||||
using API.Entities.Interfaces;
|
||||
using API.Extensions;
|
||||
using API.Interfaces;
|
||||
using API.Interfaces.Services;
|
||||
|
@ -23,17 +25,19 @@ namespace API.Services.Tasks
|
|||
private readonly ILogger<ScannerService> _logger;
|
||||
private readonly IArchiveService _archiveService;
|
||||
private readonly IMetadataService _metadataService;
|
||||
private readonly IBookService _bookService;
|
||||
private ConcurrentDictionary<string, List<ParserInfo>> _scannedSeries;
|
||||
private readonly NaturalSortComparer _naturalSort;
|
||||
|
||||
public ScannerService(IUnitOfWork unitOfWork, ILogger<ScannerService> logger, IArchiveService archiveService,
|
||||
IMetadataService metadataService)
|
||||
IMetadataService metadataService, IBookService bookService)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_logger = logger;
|
||||
_archiveService = archiveService;
|
||||
_metadataService = metadataService;
|
||||
_naturalSort = new NaturalSortComparer(true);
|
||||
_bookService = bookService;
|
||||
_naturalSort = new NaturalSortComparer();
|
||||
}
|
||||
|
||||
|
||||
|
@ -43,13 +47,14 @@ namespace API.Services.Tasks
|
|||
var libraries = Task.Run(() => _unitOfWork.LibraryRepository.GetLibrariesAsync()).Result.ToList();
|
||||
foreach (var lib in libraries)
|
||||
{
|
||||
// BUG?: I think we need to keep _scannedSeries within the ScanLibrary instance since this is multithreaded.
|
||||
ScanLibrary(lib.Id, false);
|
||||
}
|
||||
}
|
||||
|
||||
private bool ShouldSkipFolderScan(FolderPath folder, ref int skippedFolders)
|
||||
{
|
||||
// NOTE: This solution isn't the best, but it has potential. We need to handle a few other cases so it works great.
|
||||
// NOTE: The only way to skip folders is if Directory hasn't been modified, we aren't doing a forcedUpdate and version hasn't changed between scans.
|
||||
return false;
|
||||
|
||||
// if (!_forceUpdate && Directory.GetLastWriteTime(folder.Path) < folder.LastScanned)
|
||||
|
@ -66,6 +71,7 @@ namespace API.Services.Tasks
|
|||
public void ScanLibrary(int libraryId, bool forceUpdate)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
_scannedSeries = new ConcurrentDictionary<string, List<ParserInfo>>();
|
||||
Library library;
|
||||
try
|
||||
{
|
||||
|
@ -79,260 +85,281 @@ namespace API.Services.Tasks
|
|||
}
|
||||
|
||||
|
||||
_logger.LogInformation("Beginning scan on {LibraryName}. Forcing metadata update: {ForceUpdate}", library.Name, forceUpdate);
|
||||
|
||||
_scannedSeries = new ConcurrentDictionary<string, List<ParserInfo>>();
|
||||
|
||||
var totalFiles = 0;
|
||||
var skippedFolders = 0;
|
||||
foreach (var folderPath in library.Folders)
|
||||
{
|
||||
if (ShouldSkipFolderScan(folderPath, ref skippedFolders)) continue;
|
||||
|
||||
try {
|
||||
totalFiles += DirectoryService.TraverseTreeParallelForEach(folderPath.Path, (f) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
ProcessFile(f, folderPath.Path, library.Type);
|
||||
}
|
||||
catch (FileNotFoundException exception)
|
||||
{
|
||||
_logger.LogError(exception, "The file {Filename} could not be found", f);
|
||||
}
|
||||
}, Parser.Parser.ArchiveFileExtensions);
|
||||
}
|
||||
catch (ArgumentException ex) {
|
||||
_logger.LogError(ex, "The directory '{FolderPath}' does not exist", folderPath.Path);
|
||||
}
|
||||
|
||||
folderPath.LastScanned = DateTime.Now;
|
||||
}
|
||||
|
||||
var scanElapsedTime = sw.ElapsedMilliseconds;
|
||||
_logger.LogInformation("Folders Scanned {TotalFiles} files in {ElapsedScanTime} milliseconds", totalFiles, scanElapsedTime);
|
||||
sw.Restart();
|
||||
if (skippedFolders == library.Folders.Count)
|
||||
{
|
||||
_logger.LogInformation("All Folders were skipped due to no modifications to the directories");
|
||||
_unitOfWork.LibraryRepository.Update(library);
|
||||
_scannedSeries = null;
|
||||
_logger.LogInformation("Processed {TotalFiles} files in {ElapsedScanTime} milliseconds for {LibraryName}", totalFiles, sw.ElapsedMilliseconds, library.Name);
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove any series where there were no parsed infos
|
||||
var filtered = _scannedSeries.Where(kvp => kvp.Value.Count != 0);
|
||||
var series = filtered.ToDictionary(v => v.Key, v => v.Value);
|
||||
|
||||
var series = ScanLibrariesForSeries(forceUpdate, library, sw, out var totalFiles, out var scanElapsedTime);
|
||||
UpdateLibrary(library, series);
|
||||
|
||||
_unitOfWork.LibraryRepository.Update(library);
|
||||
|
||||
if (Task.Run(() => _unitOfWork.Complete()).Result)
|
||||
{
|
||||
_logger.LogInformation("Scan completed on {LibraryName}. Parsed {ParsedSeriesCount} series in {ElapsedScanTime} ms", library.Name, series.Keys.Count, sw.ElapsedMilliseconds);
|
||||
_logger.LogInformation("Processed {TotalFiles} files and {ParsedSeriesCount} series in {ElapsedScanTime} milliseconds for {LibraryName}", totalFiles, series.Keys.Count, sw.ElapsedMilliseconds + scanElapsedTime, library.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError("There was a critical error that resulted in a failed scan. Please check logs and rescan");
|
||||
_logger.LogCritical("There was a critical error that resulted in a failed scan. Please check logs and rescan");
|
||||
}
|
||||
_scannedSeries = null;
|
||||
|
||||
_logger.LogInformation("Processed {TotalFiles} files in {ElapsedScanTime} milliseconds for {LibraryName}", totalFiles, sw.ElapsedMilliseconds + scanElapsedTime, library.Name);
|
||||
|
||||
// Cleanup any user progress that doesn't exist
|
||||
var cleanedUp = Task.Run(() => _unitOfWork.AppUserProgressRepository.CleanupAbandonedChapters()).Result;
|
||||
_logger.LogInformation("Removed {Count} abandoned progress rows", cleanedUp);
|
||||
|
||||
CleanupUserProgress();
|
||||
|
||||
BackgroundJob.Enqueue(() => _metadataService.RefreshMetadata(libraryId, forceUpdate));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Remove any user progress rows that no longer exist since scan library ran and deleted series/volumes/chapters
|
||||
/// </summary>
|
||||
private void CleanupUserProgress()
|
||||
{
|
||||
var cleanedUp = Task.Run(() => _unitOfWork.AppUserProgressRepository.CleanupAbandonedChapters()).Result;
|
||||
_logger.LogInformation("Removed {Count} abandoned progress rows", cleanedUp);
|
||||
}
|
||||
|
||||
private Dictionary<string, List<ParserInfo>> ScanLibrariesForSeries(bool forceUpdate, Library library, Stopwatch sw, out int totalFiles,
|
||||
out long scanElapsedTime)
|
||||
{
|
||||
_logger.LogInformation("Beginning scan on {LibraryName}. Forcing metadata update: {ForceUpdate}", library.Name,
|
||||
forceUpdate);
|
||||
totalFiles = 0;
|
||||
var skippedFolders = 0;
|
||||
foreach (var folderPath in library.Folders)
|
||||
{
|
||||
if (ShouldSkipFolderScan(folderPath, ref skippedFolders)) continue;
|
||||
|
||||
// NOTE: we can refactor this to allow all filetypes and handle everything in the ProcessFile to allow mixed library types.
|
||||
var searchPattern = Parser.Parser.ArchiveFileExtensions;
|
||||
if (library.Type == LibraryType.Book)
|
||||
{
|
||||
searchPattern = Parser.Parser.BookFileExtensions;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
totalFiles += DirectoryService.TraverseTreeParallelForEach(folderPath.Path, (f) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
ProcessFile(f, folderPath.Path, library.Type);
|
||||
}
|
||||
catch (FileNotFoundException exception)
|
||||
{
|
||||
_logger.LogError(exception, "The file {Filename} could not be found", f);
|
||||
}
|
||||
}, searchPattern, _logger);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
_logger.LogError(ex, "The directory '{FolderPath}' does not exist", folderPath.Path);
|
||||
}
|
||||
|
||||
folderPath.LastScanned = DateTime.Now;
|
||||
}
|
||||
|
||||
scanElapsedTime = sw.ElapsedMilliseconds;
|
||||
_logger.LogInformation("Folders Scanned {TotalFiles} files in {ElapsedScanTime} milliseconds", totalFiles,
|
||||
scanElapsedTime);
|
||||
sw.Restart();
|
||||
if (skippedFolders == library.Folders.Count)
|
||||
{
|
||||
_logger.LogInformation("All Folders were skipped due to no modifications to the directories");
|
||||
_unitOfWork.LibraryRepository.Update(library);
|
||||
_scannedSeries = null;
|
||||
_logger.LogInformation("Processed {TotalFiles} files in {ElapsedScanTime} milliseconds for {LibraryName}",
|
||||
totalFiles, sw.ElapsedMilliseconds, library.Name);
|
||||
return new Dictionary<string, List<ParserInfo>>();
|
||||
}
|
||||
|
||||
return SeriesWithInfos(_scannedSeries);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns any series where there were parsed infos
|
||||
/// </summary>
|
||||
/// <param name="scannedSeries"></param>
|
||||
/// <returns></returns>
|
||||
private static Dictionary<string, List<ParserInfo>> SeriesWithInfos(IDictionary<string, List<ParserInfo>> scannedSeries)
|
||||
{
|
||||
var filtered = scannedSeries.Where(kvp => kvp.Value.Count > 0);
|
||||
var series = filtered.ToDictionary(v => v.Key, v => v.Value);
|
||||
return series;
|
||||
}
|
||||
|
||||
|
||||
private void UpdateLibrary(Library library, Dictionary<string, List<ParserInfo>> parsedSeries)
|
||||
{
|
||||
if (parsedSeries == null) throw new ArgumentNullException(nameof(parsedSeries));
|
||||
|
||||
|
||||
// First, remove any series that are not in parsedSeries list
|
||||
var missingSeries = FindSeriesNotOnDisk(library.Series, parsedSeries);
|
||||
var removeCount = RemoveMissingSeries(library.Series, missingSeries);
|
||||
_logger.LogInformation("Removed {RemoveMissingSeries} series that are no longer on disk", removeCount);
|
||||
var missingSeries = FindSeriesNotOnDisk(library.Series, parsedSeries).ToList();
|
||||
library.Series = RemoveMissingSeries(library.Series, missingSeries, out var removeCount);
|
||||
if (removeCount > 0)
|
||||
{
|
||||
_logger.LogInformation("Removed {RemoveMissingSeries} series that are no longer on disk:", removeCount);
|
||||
foreach (var s in missingSeries)
|
||||
{
|
||||
_logger.LogDebug("Removed {SeriesName}", s.Name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Add new series that have parsedInfos
|
||||
foreach (var (key, infos) in parsedSeries)
|
||||
{
|
||||
var existingSeries = library.Series.SingleOrDefault(s => s.NormalizedName == Parser.Parser.Normalize(key));
|
||||
// Key is normalized already
|
||||
var existingSeries = library.Series.SingleOrDefault(s => s.NormalizedName == key || Parser.Parser.Normalize(s.OriginalName) == key);
|
||||
if (existingSeries == null)
|
||||
{
|
||||
var name = infos.Count > 0 ? infos[0].Series : key;
|
||||
existingSeries = new Series()
|
||||
{
|
||||
Name = name,
|
||||
OriginalName = name,
|
||||
LocalizedName = name,
|
||||
NormalizedName = Parser.Parser.Normalize(key),
|
||||
SortName = key,
|
||||
Summary = "",
|
||||
Volumes = new List<Volume>()
|
||||
};
|
||||
existingSeries = DbFactory.Series(infos[0].Series);
|
||||
library.Series.Add(existingSeries);
|
||||
}
|
||||
existingSeries.NormalizedName = Parser.Parser.Normalize(key);
|
||||
existingSeries.LocalizedName ??= key;
|
||||
}
|
||||
|
||||
existingSeries.NormalizedName = Parser.Parser.Normalize(existingSeries.Name);
|
||||
existingSeries.OriginalName ??= infos[0].Series;
|
||||
}
|
||||
|
||||
// Now, we only have to deal with series that exist on disk. Let's recalculate the volumes for each series
|
||||
var librarySeries = library.Series.ToList();
|
||||
Parallel.ForEach(librarySeries, (series) =>
|
||||
{
|
||||
_logger.LogInformation("Processing series {SeriesName}", series.Name);
|
||||
UpdateVolumes(series, parsedSeries[Parser.Parser.Normalize(series.OriginalName)].ToArray());
|
||||
series.Pages = series.Volumes.Sum(v => v.Pages);
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Processing series {SeriesName}", series.OriginalName);
|
||||
UpdateVolumes(series, parsedSeries[Parser.Parser.Normalize(series.OriginalName)].ToArray());
|
||||
series.Pages = series.Volumes.Sum(v => v.Pages);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "There was an exception updating volumes for {SeriesName}", series.Name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public IEnumerable<Series> FindSeriesNotOnDisk(ICollection<Series> existingSeries, Dictionary<string, List<ParserInfo>> parsedSeries)
|
||||
{
|
||||
var foundSeries = parsedSeries.Select(s => s.Key).ToList();
|
||||
var missingSeries = existingSeries.Where(es => !es.NameInList(foundSeries)
|
||||
|| !es.NameInList(parsedSeries.Keys));
|
||||
return missingSeries;
|
||||
return existingSeries.Where(es => !es.NameInList(foundSeries));
|
||||
}
|
||||
|
||||
public int RemoveMissingSeries(ICollection<Series> existingSeries, IEnumerable<Series> missingSeries)
|
||||
/// <summary>
|
||||
/// Removes all instances of missingSeries' Series from existingSeries Collection. Existing series is updated by
|
||||
/// reference and the removed element count is returned.
|
||||
/// </summary>
|
||||
/// <param name="existingSeries">Existing Series in DB</param>
|
||||
/// <param name="missingSeries">Series not found on disk or can't be parsed</param>
|
||||
/// <param name="removeCount"></param>
|
||||
/// <returns>the updated existingSeries</returns>
|
||||
public static ICollection<Series> RemoveMissingSeries(ICollection<Series> existingSeries, IEnumerable<Series> missingSeries, out int removeCount)
|
||||
{
|
||||
|
||||
var removeCount = existingSeries.Count;
|
||||
var existingCount = existingSeries.Count;
|
||||
var missingList = missingSeries.ToList();
|
||||
existingSeries = existingSeries.Except(missingList).ToList();
|
||||
// if (existingSeries == null || existingSeries.Count == 0) return 0;
|
||||
// foreach (var existing in missingSeries)
|
||||
// {
|
||||
// existingSeries.Remove(existing);
|
||||
// removeCount += 1;
|
||||
// }
|
||||
removeCount -= existingSeries.Count;
|
||||
|
||||
existingSeries = existingSeries.Where(
|
||||
s => !missingList.Exists(
|
||||
m => m.NormalizedName.Equals(s.NormalizedName))).ToList();
|
||||
|
||||
return removeCount;
|
||||
removeCount = existingCount - existingSeries.Count;
|
||||
|
||||
return existingSeries;
|
||||
}
|
||||
|
||||
private void UpdateVolumes(Series series, ParserInfo[] parsedInfos)
|
||||
{
|
||||
var startingVolumeCount = series.Volumes.Count;
|
||||
// Add new volumes and update chapters per volume
|
||||
var distinctVolumes = parsedInfos.Select(p => p.Volumes).Distinct().ToList();
|
||||
_logger.LogDebug("Updating {DistinctVolumes} volumes", distinctVolumes.Count);
|
||||
var distinctVolumes = parsedInfos.DistinctVolumes();
|
||||
_logger.LogDebug("Updating {DistinctVolumes} volumes on {SeriesName}", distinctVolumes.Count, series.Name);
|
||||
foreach (var volumeNumber in distinctVolumes)
|
||||
{
|
||||
var infos = parsedInfos.Where(p => p.Volumes == volumeNumber).ToArray();
|
||||
|
||||
var volume = series.Volumes.SingleOrDefault(s => s.Name == volumeNumber);
|
||||
if (volume == null)
|
||||
{
|
||||
volume = new Volume()
|
||||
{
|
||||
Name = volumeNumber,
|
||||
Number = (int) Parser.Parser.MinimumNumberFromRange(volumeNumber),
|
||||
IsSpecial = false,
|
||||
Chapters = new List<Chapter>()
|
||||
};
|
||||
volume = DbFactory.Volume(volumeNumber);
|
||||
series.Volumes.Add(volume);
|
||||
}
|
||||
|
||||
// NOTE: I don't think we need this as chapters now handle specials
|
||||
//volume.IsSpecial = volume.Number == 0 && infos.All(p => p.Chapters == "0" || p.IsSpecial);
|
||||
// NOTE: Instead of creating and adding? Why Not Merge a new volume into an existing, so no matter what, new properties,etc get propagated?
|
||||
|
||||
_logger.LogDebug("Parsing {SeriesName} - Volume {VolumeNumber}", series.Name, volume.Name);
|
||||
|
||||
var infos = parsedInfos.Where(p => p.Volumes == volumeNumber).ToArray();
|
||||
UpdateChapters(volume, infos);
|
||||
volume.Pages = volume.Chapters.Sum(c => c.Pages);
|
||||
}
|
||||
|
||||
// BUG: This is causing volumes to be removed when they shouldn't
|
||||
// Remove existing volumes that aren't in parsedInfos and volumes that have no chapters
|
||||
var existingVolumeLength = series.Volumes.Count;
|
||||
// var existingVols = series.Volumes;
|
||||
// foreach (var v in existingVols)
|
||||
// {
|
||||
// // NOTE: I think checking if Chapter count is 0 is enough, we don't need parsedInfos
|
||||
// if (parsedInfos.All(p => p.Volumes != v.Name)) // || v.Chapters.Count == 0 (this wont work yet because we don't take care of chapters correctly vs parsedInfos)
|
||||
// {
|
||||
// _logger.LogDebug("Removed {Series} - {Volume} as there were no chapters", series.Name, v.Name);
|
||||
// series.Volumes.Remove(v);
|
||||
// }
|
||||
// }
|
||||
series.Volumes = series.Volumes.Where(v => parsedInfos.Any(p => p.Volumes == v.Name)).ToList();
|
||||
if (existingVolumeLength != series.Volumes.Count)
|
||||
|
||||
// Remove existing volumes that aren't in parsedInfos
|
||||
var nonDeletedVolumes = series.Volumes.Where(v => parsedInfos.Select(p => p.Volumes).Contains(v.Name)).ToList();
|
||||
if (series.Volumes.Count != nonDeletedVolumes.Count)
|
||||
{
|
||||
_logger.LogDebug("Removed {Count} volumes from {SeriesName} where parsed infos were not mapping with volume name", (existingVolumeLength - series.Volumes.Count), series.Name);
|
||||
_logger.LogDebug("Removed {Count} volumes from {SeriesName} where parsed infos were not mapping with volume name",
|
||||
(series.Volumes.Count - nonDeletedVolumes.Count), series.Name);
|
||||
var deletedVolumes = series.Volumes.Except(nonDeletedVolumes);
|
||||
foreach (var volume in deletedVolumes)
|
||||
{
|
||||
var file = volume.Chapters.FirstOrDefault()?.Files.FirstOrDefault()?.FilePath ?? "no files";
|
||||
if (!new FileInfo(file).Exists)
|
||||
{
|
||||
_logger.LogError("Volume cleanup code was trying to remove a volume with a file still existing on disk. File: {File}", file);
|
||||
}
|
||||
_logger.LogDebug("Removed {SeriesName} - Volume {Volume}: {File}", series.Name, volume.Name, file);
|
||||
}
|
||||
|
||||
series.Volumes = nonDeletedVolumes;
|
||||
}
|
||||
|
||||
_logger.LogDebug("Updated {SeriesName} volumes from {StartingVolumeCount} to {VolumeCount}",
|
||||
series.Name, startingVolumeCount, series.Volumes.Count);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="volume"></param>
|
||||
/// <param name="parsedInfos"></param>
|
||||
private void UpdateChapters(Volume volume, ParserInfo[] parsedInfos)
|
||||
{
|
||||
var startingChapters = volume.Chapters.Count;
|
||||
|
||||
// Add new chapters
|
||||
foreach (var info in parsedInfos)
|
||||
{
|
||||
var specialTreatment = (info.IsSpecial || (info.Volumes == "0" && info.Chapters == "0"));
|
||||
// Specials go into their own chapters with Range being their filename and IsSpecial = True. Non-Specials with Vol and Chap as 0
|
||||
// also are treated like specials for UI grouping.
|
||||
// NOTE: If there are duplicate files that parse out to be the same but a different series name (but parses to same normalized name ie History's strongest
|
||||
// vs Historys strongest), this code will break and the duplicate will be skipped.
|
||||
Chapter chapter = null;
|
||||
Chapter chapter;
|
||||
try
|
||||
{
|
||||
// TODO: Extract to FindExistingChapter()
|
||||
chapter = specialTreatment
|
||||
? volume.Chapters.SingleOrDefault(c => c.Range == info.Filename
|
||||
|| (c.Files.Select(f => f.FilePath)
|
||||
.Contains(info.FullFilePath)))
|
||||
: volume.Chapters.SingleOrDefault(c => c.Range == info.Chapters);
|
||||
chapter = volume.Chapters.GetChapterByRange(info);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "{FileName} mapped as '{Series} - Vol {Volume} Ch {Chapter}' is a duplicate, skipping", info.FullFilePath, info.Series, info.Volumes, info.Chapters);
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (chapter == null)
|
||||
{
|
||||
_logger.LogDebug("Adding new chapter, {Series} - Vol {Volume} Ch {Chapter} - Needs Special Treatment? {NeedsSpecialTreatment}", info.Series, info.Volumes, info.Chapters, specialTreatment);
|
||||
chapter = new Chapter()
|
||||
{
|
||||
Number = Parser.Parser.MinimumNumberFromRange(info.Chapters) + string.Empty,
|
||||
Range = specialTreatment ? info.Filename : info.Chapters,
|
||||
Files = new List<MangaFile>(),
|
||||
IsSpecial = specialTreatment
|
||||
};
|
||||
volume.Chapters.Add(chapter);
|
||||
_logger.LogDebug(
|
||||
"Adding new chapter, {Series} - Vol {Volume} Ch {Chapter}", info.Series, info.Volumes, info.Chapters);
|
||||
volume.Chapters.Add(DbFactory.Chapter(info));
|
||||
}
|
||||
|
||||
chapter.Files ??= new List<MangaFile>();
|
||||
chapter.IsSpecial = specialTreatment;
|
||||
else
|
||||
{
|
||||
chapter.UpdateFrom(info);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Add files
|
||||
foreach (var info in parsedInfos)
|
||||
{
|
||||
var specialTreatment = (info.IsSpecial || (info.Volumes == "0" && info.Chapters == "0"));
|
||||
var specialTreatment = info.IsSpecialInfo();
|
||||
Chapter chapter = null;
|
||||
try
|
||||
{
|
||||
chapter = volume.Chapters.SingleOrDefault(c => c.Range == info.Chapters || (specialTreatment && c.Range == info.Filename));
|
||||
chapter = volume.Chapters.GetChapterByRange(info);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "There was an exception parsing chapter. Skipping {SeriesName} Vol {VolumeNumber} Chapter {ChapterNumber} - Special treatment: {NeedsSpecialTreatment}", info.Series, volume.Name, info.Chapters, specialTreatment);
|
||||
continue;
|
||||
}
|
||||
if (chapter == null) continue;
|
||||
AddOrUpdateFileForChapter(chapter, info);
|
||||
chapter.Number = Parser.Parser.MinimumNumberFromRange(info.Chapters) + string.Empty;
|
||||
chapter.Range = specialTreatment ? info.Filename : info.Chapters;
|
||||
chapter.Pages = chapter.Files.Sum(f => f.Pages);
|
||||
}
|
||||
|
||||
|
||||
|
@ -340,11 +367,7 @@ namespace API.Services.Tasks
|
|||
var existingChapters = volume.Chapters.ToList();
|
||||
foreach (var existingChapter in existingChapters)
|
||||
{
|
||||
var specialTreatment = (existingChapter.IsSpecial || (existingChapter.Number == "0" && !int.TryParse(existingChapter.Range, out int i)));
|
||||
var hasInfo = specialTreatment ? parsedInfos.Any(v => v.Filename == existingChapter.Range)
|
||||
: parsedInfos.Any(v => v.Chapters == existingChapter.Range);
|
||||
|
||||
if (!hasInfo || existingChapter.Files.Count == 0)
|
||||
if (existingChapter.Files.Count == 0 || !parsedInfos.HasInfo(existingChapter))
|
||||
{
|
||||
_logger.LogDebug("Removed chapter {Chapter} for Volume {VolumeNumber} on {SeriesName}", existingChapter.Range, volume.Name, parsedInfos[0].Series);
|
||||
volume.Chapters.Remove(existingChapter);
|
||||
|
@ -355,13 +378,9 @@ namespace API.Services.Tasks
|
|||
existingChapter.Files = existingChapter.Files
|
||||
.Where(f => parsedInfos.Any(p => p.FullFilePath == f.FilePath))
|
||||
.OrderBy(f => f.FilePath, _naturalSort).ToList();
|
||||
existingChapter.Pages = existingChapter.Files.Sum(f => f.Pages);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
_logger.LogDebug("Updated chapters from {StartingChaptersCount} to {ChapterCount}",
|
||||
startingChapters, volume.Chapters.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -393,7 +412,8 @@ namespace API.Services.Tasks
|
|||
_logger.LogDebug("Checking if we can merge {NormalizedSeries}", normalizedSeries);
|
||||
var existingName = collectedSeries.SingleOrDefault(p => Parser.Parser.Normalize(p.Key) == normalizedSeries)
|
||||
.Key;
|
||||
if (!string.IsNullOrEmpty(existingName) && info.Series != existingName)
|
||||
// BUG: We are comparing info.Series against a normalized string. They should never match. (This can cause series to not delete or parse correctly after a rename)
|
||||
if (!string.IsNullOrEmpty(existingName)) // && info.Series != existingName
|
||||
{
|
||||
_logger.LogDebug("Found duplicate parsed infos, merged {Original} into {Merged}", info.Series, existingName);
|
||||
return existingName;
|
||||
|
@ -411,25 +431,61 @@ namespace API.Services.Tasks
|
|||
/// <param name="type">Library type to determine parsing to perform</param>
|
||||
private void ProcessFile(string path, string rootPath, LibraryType type)
|
||||
{
|
||||
var info = Parser.Parser.Parse(path, rootPath, type);
|
||||
ParserInfo info;
|
||||
|
||||
if (type == LibraryType.Book && Parser.Parser.IsEpub(path))
|
||||
{
|
||||
info = BookService.ParseInfo(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
info = Parser.Parser.Parse(path, rootPath, type);
|
||||
}
|
||||
|
||||
if (info == null)
|
||||
{
|
||||
_logger.LogWarning("[Scanner] Could not parse series from {Path}", path);
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == LibraryType.Book && Parser.Parser.IsEpub(path) && Parser.Parser.ParseVolume(info.Series) != "0")
|
||||
{
|
||||
info = Parser.Parser.Parse(path, rootPath, type);
|
||||
var info2 = BookService.ParseInfo(path);
|
||||
info.Merge(info2);
|
||||
}
|
||||
|
||||
TrackSeries(info);
|
||||
}
|
||||
|
||||
private MangaFile CreateMangaFile(ParserInfo info)
|
||||
{
|
||||
return new MangaFile()
|
||||
switch (info.Format)
|
||||
{
|
||||
FilePath = info.FullFilePath,
|
||||
Format = info.Format,
|
||||
Pages = _archiveService.GetNumberOfPagesFromArchive(info.FullFilePath)
|
||||
};
|
||||
case MangaFormat.Archive:
|
||||
{
|
||||
return new MangaFile()
|
||||
{
|
||||
FilePath = info.FullFilePath,
|
||||
Format = info.Format,
|
||||
Pages = _archiveService.GetNumberOfPagesFromArchive(info.FullFilePath)
|
||||
};
|
||||
}
|
||||
case MangaFormat.Book:
|
||||
{
|
||||
return new MangaFile()
|
||||
{
|
||||
FilePath = info.FullFilePath,
|
||||
Format = info.Format,
|
||||
Pages = _bookService.GetNumberOfPages(info.FullFilePath)
|
||||
};
|
||||
}
|
||||
default:
|
||||
_logger.LogWarning("[Scanner] Ignoring {Filename}. Non-archives are not supported", info.Filename);
|
||||
break;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void AddOrUpdateFileForChapter(Chapter chapter, ParserInfo info)
|
||||
|
@ -439,22 +495,21 @@ namespace API.Services.Tasks
|
|||
if (existingFile != null)
|
||||
{
|
||||
existingFile.Format = info.Format;
|
||||
if (!new FileInfo(existingFile.FilePath).DoesLastWriteMatch(existingFile.LastModified))
|
||||
if (!existingFile.HasFileBeenModified() && existingFile.Pages > 0)
|
||||
{
|
||||
existingFile.Pages = _archiveService.GetNumberOfPagesFromArchive(info.FullFilePath);
|
||||
existingFile.Pages = existingFile.Format == MangaFormat.Book
|
||||
? _bookService.GetNumberOfPages(info.FullFilePath)
|
||||
: _archiveService.GetNumberOfPagesFromArchive(info.FullFilePath);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (info.Format == MangaFormat.Archive)
|
||||
var file = CreateMangaFile(info);
|
||||
if (file != null)
|
||||
{
|
||||
chapter.Files.Add(CreateMangaFile(info));
|
||||
chapter.Files.Add(file);
|
||||
existingFile = chapter.Files.Last();
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("Ignoring {Filename}. Non-archives are not supported", info.Filename);
|
||||
}
|
||||
}
|
||||
|
||||
if (existingFile != null)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue