Hooked up the API layer to be able to extract images from PDF again for Tachiyomi explicitly (#1686)

This commit is contained in:
Joe Milazzo 2022-12-10 08:00:36 -06:00 committed by GitHub
parent d2f5651cfa
commit 018c0ab15c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 16 deletions

View File

@ -87,16 +87,17 @@ public class ReaderController : BaseApiController
/// Returns an image for a given chapter. Will perform bounding checks
/// </summary>
/// <remarks>This will cache the chapter images for reading</remarks>
/// <param name="chapterId"></param>
/// <param name="page"></param>
/// <param name="chapterId">Chapter Id</param>
/// <param name="page">Page in question</param>
/// <param name="extractPdf">Should Kavita extract pdf into images. Defaults to false.</param>
/// <returns></returns>
[HttpGet("image")]
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour)]
[AllowAnonymous]
public async Task<ActionResult> GetImage(int chapterId, int page)
public async Task<ActionResult> GetImage(int chapterId, int page, bool extractPdf = false)
{
if (page < 0) page = 0;
var chapter = await _cacheService.Ensure(chapterId);
var chapter = await _cacheService.Ensure(chapterId, extractPdf);
if (chapter == null) return BadRequest("There was an issue finding image file for reading");
try
@ -155,12 +156,13 @@ public class ReaderController : BaseApiController
/// Returns various information about a Chapter. Side effect: This will cache the chapter images for reading.
/// </summary>
/// <param name="chapterId"></param>
/// <param name="extractPdf">Should Kavita extract pdf into images. Defaults to false.</param>
/// <returns></returns>
[HttpGet("chapter-info")]
public async Task<ActionResult<ChapterInfoDto>> GetChapterInfo(int chapterId)
public async Task<ActionResult<ChapterInfoDto>> GetChapterInfo(int chapterId, bool extractPdf = false)
{
if (chapterId <= 0) return null; // This can happen occasionally from UI, we should just ignore
var chapter = await _cacheService.Ensure(chapterId);
var chapter = await _cacheService.Ensure(chapterId, extractPdf);
if (chapter == null) return BadRequest("Could not find Chapter");
var dto = await _unitOfWork.ChapterRepository.GetChapterInfoDtoAsync(chapterId);

View File

@ -49,9 +49,9 @@ public interface IBookService
/// <summary>
/// Extracts a PDF file's pages as images to an target directory
/// </summary>
/// <remarks>This method relies on Docnet which has explict patches from Kavita for ARM support. This should only be used with Tachiyomi</remarks>
/// <param name="fileFilePath"></param>
/// <param name="targetDirectory">Where the files will be extracted to. If doesn't exist, will be created.</param>
[Obsolete("This method of reading is no longer supported. Please use native pdf reader")]
void ExtractPdfImages(string fileFilePath, string targetDirectory);
Task<string> ScopePage(HtmlDocument doc, EpubBookRef book, string apiBase, HtmlNode body, Dictionary<string, int> mappings, int page);

View File

@ -20,8 +20,9 @@ public interface ICacheService
/// cache operations (except cleanup).
/// </summary>
/// <param name="chapterId"></param>
/// <param name="extractPdfToImages">Extracts a PDF into images for a different reading experience</param>
/// <returns>Chapter for the passed chapterId. Side-effect from ensuring cache.</returns>
Task<Chapter> Ensure(int chapterId);
Task<Chapter> Ensure(int chapterId, bool extractPdfToImages = false);
/// <summary>
/// Clears cache directory of all volumes. This can be invoked from deleting a library or a series.
/// </summary>
@ -31,7 +32,7 @@ public interface ICacheService
string GetCachedPagePath(Chapter chapter, int page);
string GetCachedBookmarkPagePath(int seriesId, int page);
string GetCachedFile(Chapter chapter);
public void ExtractChapterFiles(string extractPath, IReadOnlyList<MangaFile> files);
public void ExtractChapterFiles(string extractPath, IReadOnlyList<MangaFile> files, bool extractPdfImages = false);
Task<int> CacheBookmarkForSeries(int userId, int seriesId);
void CleanupBookmarkCache(int seriesId);
}
@ -95,7 +96,7 @@ public class CacheService : ICacheService
/// </summary>
/// <param name="chapterId"></param>
/// <returns>This will always return the Chapter for the chapterId</returns>
public async Task<Chapter> Ensure(int chapterId)
public async Task<Chapter> Ensure(int chapterId, bool extractPdfToImages = false)
{
_directoryService.ExistOrCreate(_directoryService.CacheDirectory);
var chapter = await _unitOfWork.ChapterRepository.GetChapterAsync(chapterId);
@ -103,7 +104,7 @@ public class CacheService : ICacheService
if (_directoryService.Exists(extractPath)) return chapter;
var files = chapter?.Files.ToList();
ExtractChapterFiles(extractPath, files);
ExtractChapterFiles(extractPath, files, extractPdfToImages);
return chapter;
}
@ -114,8 +115,9 @@ public class CacheService : ICacheService
/// </summary>
/// <param name="extractPath"></param>
/// <param name="files"></param>
/// <param name="extractPdfImages">Defaults to false, if true, will extract the images from the PDF renderer and not move the pdf file</param>
/// <returns></returns>
public void ExtractChapterFiles(string extractPath, IReadOnlyList<MangaFile> files)
public void ExtractChapterFiles(string extractPath, IReadOnlyList<MangaFile> files, bool extractPdfImages = false)
{
var removeNonImages = true;
var fileCount = files.Count;
@ -143,12 +145,17 @@ public class CacheService : ICacheService
case MangaFormat.Epub:
case MangaFormat.Pdf:
{
removeNonImages = false;
if (!_directoryService.FileSystem.File.Exists(files[0].FilePath))
{
_logger.LogError("{File} does not exist on disk", files[0].FilePath);
throw new KavitaException($"{files[0].FilePath} does not exist on disk");
}
if (extractPdfImages)
{
_readingItemService.Extract(file.FilePath, Path.Join(extractPath, extraPath), file.Format);
break;
}
removeNonImages = false;
_directoryService.ExistOrCreate(extractPath);
_directoryService.CopyFileToDirectory(files[0].FilePath, extractPath);

View File

@ -199,6 +199,8 @@ public class ReadingItemService : IReadingItemService
_imageService.ExtractImages(fileFilePath, targetDirectory, imageCount);
break;
case MangaFormat.Pdf:
_bookService.ExtractPdfImages(fileFilePath, targetDirectory);
break;
case MangaFormat.Unknown:
case MangaFormat.Epub:
break;

View File

@ -7,7 +7,7 @@
"name": "GPL-3.0",
"url": "https://github.com/Kareadita/Kavita/blob/develop/LICENSE"
},
"version": "0.6.1.7"
"version": "0.6.1.8"
},
"servers": [
{
@ -3411,7 +3411,7 @@
{
"name": "chapterId",
"in": "query",
"description": "",
"description": "Chapter Id",
"schema": {
"type": "integer",
"format": "int32"
@ -3420,11 +3420,20 @@
{
"name": "page",
"in": "query",
"description": "",
"description": "Page in question",
"schema": {
"type": "integer",
"format": "int32"
}
},
{
"name": "extractPdf",
"in": "query",
"description": "Should Kavita extract pdf into images. Defaults to false.",
"schema": {
"type": "boolean",
"default": false
}
}
],
"responses": {
@ -3491,6 +3500,15 @@
"type": "integer",
"format": "int32"
}
},
{
"name": "extractPdf",
"in": "query",
"description": "Should Kavita extract pdf into images. Defaults to false.",
"schema": {
"type": "boolean",
"default": false
}
}
],
"responses": {