Kavita/API/Services/ReadingItemService.cs
Joseph Milazzo 3ab3a10ae7
New PDF Reader (#1324)
* Refactored all the code that opens the reader to use a unified function. Added new library and setup basic pdf reader route.

* Progress saving is implemented. Targeting ES6 now.

* Customized the toolbar to remove things we don't want, made the download button download with correct filename. Adjusted zoom setting to work well on first load regardless of device.

* Stream the pdf file to the UI rather than handling the download ourselves.

* Started implementing a custom toolbar.

* Fixed up the jump bar calculations

* Fixed filtering being broken

* Pushing up for Robbie to cleanup the toolbar layout

* Added an additional button. Working on logic while robbie takes styling

* Tried to fix the code for robbie

* Tweaks for fonts

* Added button for book mode, but doesn't seem to work after renderer is built

* Removed book mode

* Removed the old image caching code for pdfs as it's not needed with new reader

* Removed the interfaces to extract images from pdf.

* Fixed original pagination area not scaling correctly

* Integrated series remove events to library detail

* Cleaned up the getter naming convention

* Cleaned up some of the manga reader code to reduce cluter and improve re-use

* Implemented Japanese parser support for volume and chapters.

* Fixed a bug where resetting scroll in manga reader wasn't working

* Fixed a bug where word count grew on each scan.

* Removed unused variable

* Ensure we calculate word count on files with their own cache timestamp

* Adjusted size of reel headers

* Put some code in for moving on original image with keyboard, but it's not in use.

* Cleaned up the css for the pdf reader

* Cleaned up the code

* Tweaked the list item so we show scrollbar now when fully read
2022-06-15 14:43:32 -07:00

140 lines
4.9 KiB
C#

using System;
using API.Data.Metadata;
using API.Entities.Enums;
using API.Parser;
namespace API.Services;
public interface IReadingItemService
{
ComicInfo GetComicInfo(string filePath);
int GetNumberOfPages(string filePath, 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);
}
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)
{
_archiveService = archiveService;
_bookService = bookService;
_imageService = imageService;
_directoryService = directoryService;
_defaultParser = new DefaultParser(directoryService);
}
/// <summary>
/// Gets the ComicInfo for the file if it exists. Null otherwise.
/// </summary>
/// <param name="filePath">Fully qualified path of file</param>
/// <returns></returns>
public ComicInfo? GetComicInfo(string filePath)
{
if (Parser.Parser.IsEpub(filePath))
{
return _bookService.GetComicInfo(filePath);
}
if (Parser.Parser.IsComicInfoExtension(filePath))
{
return _archiveService.GetComicInfo(filePath);
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="filePath"></param>
/// <param name="format"></param>
/// <returns></returns>
public int GetNumberOfPages(string filePath, MangaFormat format)
{
switch (format)
{
case MangaFormat.Archive:
{
return _archiveService.GetNumberOfPagesFromArchive(filePath);
}
case MangaFormat.Pdf:
case MangaFormat.Epub:
{
return _bookService.GetNumberOfPages(filePath);
}
case MangaFormat.Image:
{
return 1;
}
case MangaFormat.Unknown:
default:
return 0;
}
}
public string GetCoverImage(string filePath, string fileName, MangaFormat format)
{
if (string.IsNullOrEmpty(filePath) || string.IsNullOrEmpty(fileName))
{
return string.Empty;
}
return format switch
{
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
};
}
/// <summary>
/// Extracts the reading item to the target directory using the appropriate method
/// </summary>
/// <param name="fileFilePath">File to extract</param>
/// <param name="targetDirectory">Where to extract to. Will be created if does not exist</param>
/// <param name="format">Format of the File</param>
/// <param name="imageCount">If the file is of type image, pass number of files needed. If > 0, will copy the whole directory.</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public void Extract(string fileFilePath, string targetDirectory, MangaFormat format, int imageCount = 1)
{
switch (format)
{
case MangaFormat.Archive:
_archiveService.ExtractArchive(fileFilePath, targetDirectory);
break;
case MangaFormat.Image:
_imageService.ExtractImages(fileFilePath, targetDirectory, imageCount);
break;
case MangaFormat.Pdf:
case MangaFormat.Unknown:
case MangaFormat.Epub:
break;
default:
throw new ArgumentOutOfRangeException(nameof(format), format, null);
}
}
/// <summary>
/// Parses information out of a file. If file is a book (epub), it will use book metadata regardless of LibraryType
/// </summary>
/// <param name="path"></param>
/// <param name="rootPath"></param>
/// <param name="type"></param>
/// <returns></returns>
public ParserInfo Parse(string path, string rootPath, LibraryType type)
{
return Parser.Parser.IsEpub(path) ? _bookService.ParseInfo(path) : _defaultParser.Parse(path, rootPath, type);
}
}