mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-07-09 03:04:19 -04:00
Hooked up the API layer to be able to extract images from PDF again for Tachiyomi explicitly (#1686)
This commit is contained in:
parent
d2f5651cfa
commit
018c0ab15c
@ -87,16 +87,17 @@ public class ReaderController : BaseApiController
|
|||||||
/// Returns an image for a given chapter. Will perform bounding checks
|
/// Returns an image for a given chapter. Will perform bounding checks
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>This will cache the chapter images for reading</remarks>
|
/// <remarks>This will cache the chapter images for reading</remarks>
|
||||||
/// <param name="chapterId"></param>
|
/// <param name="chapterId">Chapter Id</param>
|
||||||
/// <param name="page"></param>
|
/// <param name="page">Page in question</param>
|
||||||
|
/// <param name="extractPdf">Should Kavita extract pdf into images. Defaults to false.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("image")]
|
[HttpGet("image")]
|
||||||
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour)]
|
[ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour)]
|
||||||
[AllowAnonymous]
|
[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;
|
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");
|
if (chapter == null) return BadRequest("There was an issue finding image file for reading");
|
||||||
|
|
||||||
try
|
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.
|
/// Returns various information about a Chapter. Side effect: This will cache the chapter images for reading.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="chapterId"></param>
|
/// <param name="chapterId"></param>
|
||||||
|
/// <param name="extractPdf">Should Kavita extract pdf into images. Defaults to false.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("chapter-info")]
|
[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
|
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");
|
if (chapter == null) return BadRequest("Could not find Chapter");
|
||||||
|
|
||||||
var dto = await _unitOfWork.ChapterRepository.GetChapterInfoDtoAsync(chapterId);
|
var dto = await _unitOfWork.ChapterRepository.GetChapterInfoDtoAsync(chapterId);
|
||||||
|
@ -49,9 +49,9 @@ public interface IBookService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Extracts a PDF file's pages as images to an target directory
|
/// Extracts a PDF file's pages as images to an target directory
|
||||||
/// </summary>
|
/// </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="fileFilePath"></param>
|
||||||
/// <param name="targetDirectory">Where the files will be extracted to. If doesn't exist, will be created.</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);
|
void ExtractPdfImages(string fileFilePath, string targetDirectory);
|
||||||
|
|
||||||
Task<string> ScopePage(HtmlDocument doc, EpubBookRef book, string apiBase, HtmlNode body, Dictionary<string, int> mappings, int page);
|
Task<string> ScopePage(HtmlDocument doc, EpubBookRef book, string apiBase, HtmlNode body, Dictionary<string, int> mappings, int page);
|
||||||
|
@ -20,8 +20,9 @@ public interface ICacheService
|
|||||||
/// cache operations (except cleanup).
|
/// cache operations (except cleanup).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="chapterId"></param>
|
/// <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>
|
/// <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>
|
/// <summary>
|
||||||
/// Clears cache directory of all volumes. This can be invoked from deleting a library or a series.
|
/// Clears cache directory of all volumes. This can be invoked from deleting a library or a series.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -31,7 +32,7 @@ public interface ICacheService
|
|||||||
string GetCachedPagePath(Chapter chapter, int page);
|
string GetCachedPagePath(Chapter chapter, int page);
|
||||||
string GetCachedBookmarkPagePath(int seriesId, int page);
|
string GetCachedBookmarkPagePath(int seriesId, int page);
|
||||||
string GetCachedFile(Chapter chapter);
|
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);
|
Task<int> CacheBookmarkForSeries(int userId, int seriesId);
|
||||||
void CleanupBookmarkCache(int seriesId);
|
void CleanupBookmarkCache(int seriesId);
|
||||||
}
|
}
|
||||||
@ -95,7 +96,7 @@ public class CacheService : ICacheService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="chapterId"></param>
|
/// <param name="chapterId"></param>
|
||||||
/// <returns>This will always return the Chapter for the chapterId</returns>
|
/// <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);
|
_directoryService.ExistOrCreate(_directoryService.CacheDirectory);
|
||||||
var chapter = await _unitOfWork.ChapterRepository.GetChapterAsync(chapterId);
|
var chapter = await _unitOfWork.ChapterRepository.GetChapterAsync(chapterId);
|
||||||
@ -103,7 +104,7 @@ public class CacheService : ICacheService
|
|||||||
|
|
||||||
if (_directoryService.Exists(extractPath)) return chapter;
|
if (_directoryService.Exists(extractPath)) return chapter;
|
||||||
var files = chapter?.Files.ToList();
|
var files = chapter?.Files.ToList();
|
||||||
ExtractChapterFiles(extractPath, files);
|
ExtractChapterFiles(extractPath, files, extractPdfToImages);
|
||||||
|
|
||||||
return chapter;
|
return chapter;
|
||||||
}
|
}
|
||||||
@ -114,8 +115,9 @@ public class CacheService : ICacheService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="extractPath"></param>
|
/// <param name="extractPath"></param>
|
||||||
/// <param name="files"></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>
|
/// <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 removeNonImages = true;
|
||||||
var fileCount = files.Count;
|
var fileCount = files.Count;
|
||||||
@ -143,12 +145,17 @@ public class CacheService : ICacheService
|
|||||||
case MangaFormat.Epub:
|
case MangaFormat.Epub:
|
||||||
case MangaFormat.Pdf:
|
case MangaFormat.Pdf:
|
||||||
{
|
{
|
||||||
removeNonImages = false;
|
|
||||||
if (!_directoryService.FileSystem.File.Exists(files[0].FilePath))
|
if (!_directoryService.FileSystem.File.Exists(files[0].FilePath))
|
||||||
{
|
{
|
||||||
_logger.LogError("{File} does not exist on disk", 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");
|
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.ExistOrCreate(extractPath);
|
||||||
_directoryService.CopyFileToDirectory(files[0].FilePath, extractPath);
|
_directoryService.CopyFileToDirectory(files[0].FilePath, extractPath);
|
||||||
|
@ -199,6 +199,8 @@ public class ReadingItemService : IReadingItemService
|
|||||||
_imageService.ExtractImages(fileFilePath, targetDirectory, imageCount);
|
_imageService.ExtractImages(fileFilePath, targetDirectory, imageCount);
|
||||||
break;
|
break;
|
||||||
case MangaFormat.Pdf:
|
case MangaFormat.Pdf:
|
||||||
|
_bookService.ExtractPdfImages(fileFilePath, targetDirectory);
|
||||||
|
break;
|
||||||
case MangaFormat.Unknown:
|
case MangaFormat.Unknown:
|
||||||
case MangaFormat.Epub:
|
case MangaFormat.Epub:
|
||||||
break;
|
break;
|
||||||
|
24
openapi.json
24
openapi.json
@ -7,7 +7,7 @@
|
|||||||
"name": "GPL-3.0",
|
"name": "GPL-3.0",
|
||||||
"url": "https://github.com/Kareadita/Kavita/blob/develop/LICENSE"
|
"url": "https://github.com/Kareadita/Kavita/blob/develop/LICENSE"
|
||||||
},
|
},
|
||||||
"version": "0.6.1.7"
|
"version": "0.6.1.8"
|
||||||
},
|
},
|
||||||
"servers": [
|
"servers": [
|
||||||
{
|
{
|
||||||
@ -3411,7 +3411,7 @@
|
|||||||
{
|
{
|
||||||
"name": "chapterId",
|
"name": "chapterId",
|
||||||
"in": "query",
|
"in": "query",
|
||||||
"description": "",
|
"description": "Chapter Id",
|
||||||
"schema": {
|
"schema": {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"format": "int32"
|
"format": "int32"
|
||||||
@ -3420,11 +3420,20 @@
|
|||||||
{
|
{
|
||||||
"name": "page",
|
"name": "page",
|
||||||
"in": "query",
|
"in": "query",
|
||||||
"description": "",
|
"description": "Page in question",
|
||||||
"schema": {
|
"schema": {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"format": "int32"
|
"format": "int32"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "extractPdf",
|
||||||
|
"in": "query",
|
||||||
|
"description": "Should Kavita extract pdf into images. Defaults to false.",
|
||||||
|
"schema": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
@ -3491,6 +3500,15 @@
|
|||||||
"type": "integer",
|
"type": "integer",
|
||||||
"format": "int32"
|
"format": "int32"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "extractPdf",
|
||||||
|
"in": "query",
|
||||||
|
"description": "Should Kavita extract pdf into images. Defaults to false.",
|
||||||
|
"schema": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user