Misc Fixes and Changes (#927)

* Cleaned up a ton of warnings/suggestions from the IDE.

* Fixed a bug when clearing the filters some presets could be undone.

* Renamed a class in the OPDS spec

* Simplified logic for when Fit To Screen rendering logic occurs. It now works always rather than only on cover images.

* Give some additional info to the user on what the differences between Library Types are

* Don't scan .qpkg folders (QNAP devices)

* Refactored some code to enable ability to test CoverImage Test. This is a broken test, test.zip is waiting on an issue in NetVips.

* Fixed an issue where Extra might get flagged as special too early, if in a word like Extraordinary

* Cleaned up the regex for the extra issue to be more flexible
This commit is contained in:
Joseph Milazzo 2022-01-12 15:00:00 -08:00 committed by GitHub
parent 6afc17e93e
commit fb71d54fe6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 162 additions and 361 deletions

View file

@ -22,7 +22,7 @@ namespace API.Services
{
void ExtractArchive(string archivePath, string extractPath);
int GetNumberOfPagesFromArchive(string archivePath);
string GetCoverImage(string archivePath, string fileName);
string GetCoverImage(string archivePath, string fileName, string outputDirectory);
bool IsValidArchive(string archivePath);
ComicInfo GetComicInfo(string archivePath);
ArchiveLibrary CanOpen(string archivePath);
@ -186,7 +186,7 @@ namespace API.Services
/// <param name="archivePath"></param>
/// <param name="fileName">File name to use based on context of entity.</param>
/// <returns></returns>
public string GetCoverImage(string archivePath, string fileName)
public string GetCoverImage(string archivePath, string fileName, string outputDirectory)
{
if (archivePath == null || !IsValidArchive(archivePath)) return string.Empty;
try
@ -203,7 +203,7 @@ namespace API.Services
var entry = archive.Entries.Single(e => e.FullName == entryName);
using var stream = entry.Open();
return CreateThumbnail(archivePath + " - " + entry.FullName, stream, fileName);
return CreateThumbnail(archivePath + " - " + entry.FullName, stream, fileName, outputDirectory);
}
case ArchiveLibrary.SharpCompress:
{
@ -215,7 +215,7 @@ namespace API.Services
using var stream = entry.OpenEntryStream();
return CreateThumbnail(archivePath + " - " + entry.Key, stream, fileName);
return CreateThumbnail(archivePath + " - " + entry.Key, stream, fileName, outputDirectory);
}
case ArchiveLibrary.NotSupported:
_logger.LogWarning("[GetCoverImage] This archive cannot be read: {ArchivePath}. Defaulting to no cover image", archivePath);
@ -279,14 +279,15 @@ namespace API.Services
return Tuple.Create(fileBytes, zipPath);
}
private string CreateThumbnail(string entryName, Stream stream, string fileName)
private string CreateThumbnail(string entryName, Stream stream, string fileName, string outputDirectory)
{
try
{
return _imageService.WriteCoverThumbnail(stream, fileName);
return _imageService.WriteCoverThumbnail(stream, fileName, outputDirectory);
}
catch (Exception ex)
{
// NOTE: I can just let this bubble up
_logger.LogWarning(ex, "[GetCoverImage] There was an error and prevented thumbnail generation on {EntryName}. Defaulting to no cover image", entryName);
}
@ -337,28 +338,24 @@ namespace API.Services
public static void CleanComicInfo(ComicInfo info)
{
if (info != null)
{
info.Writer = Parser.Parser.CleanAuthor(info.Writer);
info.Colorist = Parser.Parser.CleanAuthor(info.Colorist);
info.Editor = Parser.Parser.CleanAuthor(info.Editor);
info.Inker = Parser.Parser.CleanAuthor(info.Inker);
info.Letterer = Parser.Parser.CleanAuthor(info.Letterer);
info.Penciller = Parser.Parser.CleanAuthor(info.Penciller);
info.Publisher = Parser.Parser.CleanAuthor(info.Publisher);
info.Characters = Parser.Parser.CleanAuthor(info.Characters);
if (info == null) return;
if (!string.IsNullOrEmpty(info.Web))
info.Writer = Parser.Parser.CleanAuthor(info.Writer);
info.Colorist = Parser.Parser.CleanAuthor(info.Colorist);
info.Editor = Parser.Parser.CleanAuthor(info.Editor);
info.Inker = Parser.Parser.CleanAuthor(info.Inker);
info.Letterer = Parser.Parser.CleanAuthor(info.Letterer);
info.Penciller = Parser.Parser.CleanAuthor(info.Penciller);
info.Publisher = Parser.Parser.CleanAuthor(info.Publisher);
info.Characters = Parser.Parser.CleanAuthor(info.Characters);
if (!string.IsNullOrEmpty(info.Web))
{
// ComicVine stores the Issue number in Number field and does not use Volume.
if (!info.Web.Contains("https://comicvine.gamespot.com/")) return;
if (info.Volume.Equals("1"))
{
// TODO: Validate this works through testing
// ComicVine stores the Issue number in Number field and does not use Volume.
if (info.Web.Contains("https://comicvine.gamespot.com/"))
{
if (info.Volume.Equals("1"))
{
info.Volume = Parser.Parser.DefaultVolume;
}
}
info.Volume = Parser.Parser.DefaultVolume;
}
}
}
@ -451,7 +448,7 @@ namespace API.Services
private void ExtractArchiveEntries(ZipArchive archive, string extractPath)
{
// TODO: In cases where we try to extract, but there are InvalidPathChars, we need to inform the user
// TODO: In cases where we try to extract, but there are InvalidPathChars, we need to inform the user (throw exception, let middleware inform user)
var needsFlattening = ArchiveNeedsFlattening(archive);
if (!archive.HasFiles() && !needsFlattening) return;

View file

@ -27,7 +27,7 @@ namespace API.Services
public interface IBookService
{
int GetNumberOfPages(string filePath);
string GetCoverImage(string fileFilePath, string fileName);
string GetCoverImage(string fileFilePath, string fileName, string outputDirectory);
Task<Dictionary<string, int>> CreateKeyToPageMappingAsync(EpubBookRef book);
/// <summary>
@ -299,7 +299,6 @@ namespace API.Services
var classes = htmlNode.Attributes["class"].Value + " " + bodyClasses;
body.Attributes.Add("class", $"{classes}");
// I actually need the body tag itself for the classes, so i will create a div and put the body stuff there.
//return Ok($"<div class=\"{body.Attributes["class"].Value}\">{body.InnerHtml}</div>");
return $"<div class=\"{body.Attributes["class"].Value}\">{body.InnerHtml}</div>";
}
@ -369,7 +368,6 @@ namespace API.Services
var info = new ComicInfo()
{
// TODO: Summary is in html, we need to turn it into string
Summary = epubBook.Schema.Package.Metadata.Description,
Writer = string.Join(",", epubBook.Schema.Package.Metadata.Creators.Select(c => Parser.Parser.CleanAuthor(c.Creator))),
Publisher = string.Join(",", epubBook.Schema.Package.Metadata.Publishers),
@ -634,13 +632,13 @@ namespace API.Services
/// <param name="fileFilePath"></param>
/// <param name="fileName">Name of the new file.</param>
/// <returns></returns>
public string GetCoverImage(string fileFilePath, string fileName)
public string GetCoverImage(string fileFilePath, string fileName, string outputDirectory)
{
if (!IsValidFile(fileFilePath)) return string.Empty;
if (Parser.Parser.IsPdf(fileFilePath))
{
return GetPdfCoverImage(fileFilePath, fileName);
return GetPdfCoverImage(fileFilePath, fileName, outputDirectory);
}
using var epubBook = EpubReader.OpenBook(fileFilePath);
@ -656,7 +654,7 @@ namespace API.Services
if (coverImageContent == null) return string.Empty;
using var stream = coverImageContent.GetContentStream();
return _imageService.WriteCoverThumbnail(stream, fileName);
return _imageService.WriteCoverThumbnail(stream, fileName, outputDirectory);
}
catch (Exception ex)
{
@ -667,7 +665,7 @@ namespace API.Services
}
private string GetPdfCoverImage(string fileFilePath, string fileName)
private string GetPdfCoverImage(string fileFilePath, string fileName, string outputDirectory)
{
try
{
@ -677,7 +675,7 @@ namespace API.Services
using var stream = StreamManager.GetStream("BookService.GetPdfPage");
GetPdfPage(docReader, 0, stream);
return _imageService.WriteCoverThumbnail(stream, fileName);
return _imageService.WriteCoverThumbnail(stream, fileName, outputDirectory);
}
catch (Exception ex)

View file

@ -171,7 +171,10 @@ namespace API.Services
var path = GetCachePath(chapter.Id);
var files = _directoryService.GetFilesWithExtension(path, Parser.Parser.ImageFileExtensions);
using var nc = new NaturalSortComparer();
files = files.ToList().OrderBy(Path.GetFileNameWithoutExtension, nc).ToArray();
files = files
.AsEnumerable()
.OrderBy(Path.GetFileNameWithoutExtension, nc)
.ToArray();
if (files.Length == 0)

View file

@ -68,7 +68,7 @@ namespace API.Services
private readonly ILogger<DirectoryService> _logger;
private static readonly Regex ExcludeDirectories = new Regex(
@"@eaDir|\.DS_Store",
@"@eaDir|\.DS_Store|\.qpkg",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static readonly string BackupDirectory = Path.Join(Directory.GetCurrentDirectory(), "config", "backups");
@ -139,8 +139,7 @@ namespace API.Services
while (FileSystem.Path.GetDirectoryName(path) != Path.GetDirectoryName(root))
{
//var folder = new DirectoryInfo(path).Name;
var folder = FileSystem.DirectoryInfo.FromDirectoryName(path).Name;
var folder = FileSystem.DirectoryInfo.FromDirectoryName(path).Name;
paths.Add(folder);
path = path.Substring(0, path.LastIndexOf(separator));
}
@ -169,7 +168,6 @@ namespace API.Services
/// <returns></returns>
public IEnumerable<string> GetFiles(string path, string fileNameRegex = "", SearchOption searchOption = SearchOption.TopDirectoryOnly)
{
// TODO: Refactor this and GetFilesWithCertainExtensions to use same implementation
if (!FileSystem.Directory.Exists(path)) return ImmutableList<string>.Empty;
if (fileNameRegex != string.Empty)
@ -279,13 +277,12 @@ namespace API.Services
public string[] GetFilesWithExtension(string path, string searchPatternExpression = "")
{
// TODO: Use GitFiles instead
if (searchPatternExpression != string.Empty)
{
return GetFilesWithCertainExtensions(path, searchPatternExpression).ToArray();
}
if (searchPatternExpression != string.Empty)
{
return GetFilesWithCertainExtensions(path, searchPatternExpression).ToArray();
}
return !FileSystem.Directory.Exists(path) ? Array.Empty<string>() : FileSystem.Directory.GetFiles(path);
return !FileSystem.Directory.Exists(path) ? Array.Empty<string>() : FileSystem.Directory.GetFiles(path);
}
/// <summary>
@ -468,11 +465,9 @@ namespace API.Services
/// <exception cref="ArgumentException"></exception>
public int TraverseTreeParallelForEach(string root, Action<string> action, string searchPattern, ILogger logger)
{
//Count of files traversed and timer for diagnostic output
//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.
//var procCount = Environment.ProcessorCount;
// Data structure to hold names of subfolders to be examined for files.
var dirs = new Stack<string>();
@ -505,8 +500,7 @@ namespace API.Services
}
try {
// TODO: Replace this with GetFiles - It's the same code
files = GetFilesWithCertainExtensions(currentDir, searchPattern)
files = GetFilesWithCertainExtensions(currentDir, searchPattern)
.ToArray();
}
catch (UnauthorizedAccessException e) {
@ -526,22 +520,7 @@ namespace API.Services
// 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) => {
// Interlocked.Add(ref fileCount, c);
// });
// }
foreach (var file in files) {
foreach (var file in files) {
action(file);
fileCount++;
}

View file

@ -1,8 +1,5 @@
using System;
using System.IO;
using System.Linq;
using API.Comparators;
using API.Entities;
using Microsoft.Extensions.Logging;
using NetVips;
@ -10,16 +7,9 @@ namespace API.Services;
public interface IImageService
{
void ExtractImages(string fileFilePath, string targetDirectory, int fileCount);
string GetCoverImage(string path, string fileName);
string GetCoverFile(MangaFile file);
void ExtractImages(string fileFilePath, string targetDirectory, int fileCount = 1);
string GetCoverImage(string path, string fileName, string outputDirectory);
/// <summary>
/// Creates a Thumbnail version of an image
/// </summary>
/// <param name="path">Path to the image file</param>
/// <returns>File name with extension of the file. This will always write to <see cref="DirectoryService.CoverImageDirectory"/></returns>
//string CreateThumbnail(string path, string fileName);
/// <summary>
/// Creates a Thumbnail version of a base64 image
/// </summary>
@ -27,7 +17,7 @@ public interface IImageService
/// <returns>File name with extension of the file. This will always write to <see cref="DirectoryService.CoverImageDirectory"/></returns>
string CreateThumbnailFromBase64(string encodedImage, string fileName);
string WriteCoverThumbnail(Stream stream, string fileName);
string WriteCoverThumbnail(Stream stream, string fileName, string outputDirectory);
}
public class ImageService : IImageService
@ -50,7 +40,7 @@ public class ImageService : IImageService
_directoryService = directoryService;
}
public void ExtractImages(string fileFilePath, string targetDirectory, int fileCount = 1)
public void ExtractImages(string fileFilePath, string targetDirectory, int fileCount)
{
_directoryService.ExistOrCreate(targetDirectory);
if (fileCount == 1)
@ -64,37 +54,15 @@ public class ImageService : IImageService
}
}
/// <summary>
/// Finds the first image in the directory of the first file. Does not check for "cover/folder".ext files to override.
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public string GetCoverFile(MangaFile file)
{
var directory = Path.GetDirectoryName(file.FilePath);
if (string.IsNullOrEmpty(directory))
{
_logger.LogError("Could not find Directory for {File}", file.FilePath);
return null;
}
using var nc = new NaturalSortComparer();
var firstImage = _directoryService.GetFilesWithExtension(directory, Parser.Parser.ImageFileExtensions)
.OrderBy(Path.GetFileNameWithoutExtension, nc).FirstOrDefault();
return firstImage;
}
public string GetCoverImage(string path, string fileName)
public string GetCoverImage(string path, string fileName, string outputDirectory)
{
if (string.IsNullOrEmpty(path)) return string.Empty;
try
{
//return CreateThumbnail(path, fileName);
using var thumbnail = Image.Thumbnail(path, ThumbnailWidth);
var filename = fileName + ".png";
thumbnail.WriteToFile(_directoryService.FileSystem.Path.Join(_directoryService.CoverImageDirectory, filename));
thumbnail.WriteToFile(_directoryService.FileSystem.Path.Join(outputDirectory, filename));
return filename;
}
catch (Exception ex)
@ -105,24 +73,6 @@ public class ImageService : IImageService
return string.Empty;
}
/// <inheritdoc />
// public string CreateThumbnail(string path, string fileName)
// {
// try
// {
// using var thumbnail = Image.Thumbnail(path, ThumbnailWidth);
// var filename = fileName + ".png";
// thumbnail.WriteToFile(_directoryService.FileSystem.Path.Join(_directoryService.CoverImageDirectory, filename));
// return filename;
// }
// catch (Exception e)
// {
// _logger.LogError(e, "Error creating thumbnail from url");
// }
//
// 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.
@ -130,12 +80,16 @@ public class ImageService : IImageService
/// <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 string WriteCoverThumbnail(Stream stream, string fileName)
public string WriteCoverThumbnail(Stream stream, string fileName, string outputDirectory)
{
using var thumbnail = Image.ThumbnailStream(stream, ThumbnailWidth);
var filename = fileName + ".png";
_directoryService.ExistOrCreate(_directoryService.CoverImageDirectory);
thumbnail.WriteToFile(_directoryService.FileSystem.Path.Join(_directoryService.CoverImageDirectory, filename));
_directoryService.ExistOrCreate(outputDirectory);
try
{
_directoryService.FileSystem.File.Delete(_directoryService.FileSystem.Path.Join(outputDirectory, filename));
} catch (Exception ex) {/* Swallow exception */}
thumbnail.WriteToFile(_directoryService.FileSystem.Path.Join(outputDirectory, filename));
return filename;
}

View file

@ -193,7 +193,6 @@ public class MetadataService : IMetadataService
/// <param name="forceUpdate">Force updating cover image even if underlying file has not been modified or chapter already has a cover image</param>
public async Task RefreshMetadata(int libraryId, bool forceUpdate = false)
{
// TODO: Think about splitting the comicinfo stuff into a separate task
var library = await _unitOfWork.LibraryRepository.GetLibraryForIdAsync(libraryId, LibraryIncludes.None);
_logger.LogInformation("[MetadataService] Beginning metadata refresh of {LibraryName}", library.Name);

View file

@ -20,7 +20,6 @@ public interface IReaderService
Task<int> CapPageToChapter(int chapterId, int page);
Task<int> GetNextChapterIdAsync(int seriesId, int volumeId, int currentChapterId, int userId);
Task<int> GetPrevChapterIdAsync(int seriesId, int volumeId, int currentChapterId, int userId);
//Task<string> BookmarkFile();
}
public class ReaderService : IReaderService
@ -310,16 +309,6 @@ public class ReaderService : IReaderService
return -1;
}
// public async Task<string> BookmarkFile()
// {
// var chapter = await _cacheService.Ensure(bookmarkDto.ChapterId);
// if (chapter == null) return string.Empty;
// var path = _cacheService.GetCachedPagePath(chapter, bookmarkDto.Page);
// var fileInfo = new FileInfo(path);
//
// return _directoryService.CopyFileToDirectory(path, Path.Join(_directoryService.BookmarkDirectory,
// $"{user.Id}", $"{bookmarkDto.SeriesId}"));
// }
private static int GetNextChapterId(IEnumerable<ChapterDto> chapters, string currentChapterNumber)
{

View file

@ -9,7 +9,7 @@ public interface IReadingItemService
{
ComicInfo GetComicInfo(string filePath);
int GetNumberOfPages(string filePath, MangaFormat format);
string GetCoverImage(string fileFilePath, string fileName, MangaFormat format);
string GetCoverImage(string filePath, string fileName, MangaFormat format);
void Extract(string fileFilePath, string targetDirectory, MangaFormat format, int imageCount = 1);
ParserInfo Parse(string path, string rootPath, LibraryType type);
}
@ -19,6 +19,7 @@ public class ReadingItemService : IReadingItemService
private readonly IArchiveService _archiveService;
private readonly IBookService _bookService;
private readonly IImageService _imageService;
private readonly IDirectoryService _directoryService;
private readonly DefaultParser _defaultParser;
public ReadingItemService(IArchiveService archiveService, IBookService bookService, IImageService imageService, IDirectoryService directoryService)
@ -26,6 +27,7 @@ public class ReadingItemService : IReadingItemService
_archiveService = archiveService;
_bookService = bookService;
_imageService = imageService;
_directoryService = directoryService;
_defaultParser = new DefaultParser(directoryService);
}
@ -85,12 +87,13 @@ public class ReadingItemService : IReadingItemService
{
return string.Empty;
}
return format switch
{
MangaFormat.Epub => _bookService.GetCoverImage(filePath, fileName),
MangaFormat.Archive => _archiveService.GetCoverImage(filePath, fileName),
MangaFormat.Image => _imageService.GetCoverImage(filePath, fileName),
MangaFormat.Pdf => _bookService.GetCoverImage(filePath, fileName),
MangaFormat.Epub => _bookService.GetCoverImage(filePath, fileName, _directoryService.CoverImageDirectory),
MangaFormat.Archive => _archiveService.GetCoverImage(filePath, fileName, _directoryService.CoverImageDirectory),
MangaFormat.Image => _imageService.GetCoverImage(filePath, fileName, _directoryService.CoverImageDirectory),
MangaFormat.Pdf => _bookService.GetCoverImage(filePath, fileName, _directoryService.CoverImageDirectory),
_ => string.Empty
};
}