File Dimension API (#1690)
* Implemented an api for getting file dimensions for a given chapter. This is for CDisplayEx integration. This might be usable in Double Renderer. * Added the cached filename for new API
This commit is contained in:
parent
0961cac65a
commit
e6b18457f2
12 changed files with 122 additions and 49 deletions
|
|
@ -824,7 +824,7 @@ public class OpdsController : BaseApiController
|
|||
|
||||
try
|
||||
{
|
||||
var path = _cacheService.GetCachedPagePath(chapter, pageNumber);
|
||||
var path = _cacheService.GetCachedPagePath(chapter.Id, pageNumber);
|
||||
if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) return BadRequest($"No such image for page {pageNumber}");
|
||||
|
||||
var content = await _directoryService.ReadFileAsync(path);
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ public class ReaderController : BaseApiController
|
|||
|
||||
try
|
||||
{
|
||||
var path = _cacheService.GetCachedPagePath(chapter, page);
|
||||
var path = _cacheService.GetCachedPagePath(chapter.Id, page);
|
||||
if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) return BadRequest($"No such image for page {page}. Try refreshing to allow re-cache.");
|
||||
var format = Path.GetExtension(path).Replace(".", "");
|
||||
|
||||
|
|
@ -152,6 +152,23 @@ public class ReaderController : BaseApiController
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the file dimensions for all pages in a chapter. If the underlying chapter is PDF, use extractPDF to unpack as images.
|
||||
/// </summary>
|
||||
/// <remarks>This has a side effect of caching the images.
|
||||
/// This will only be populated on archive filetypes and not in bookmark mode</remarks>
|
||||
/// <param name="chapterId"></param>
|
||||
/// <param name="extractPdf"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("file-dimensions")]
|
||||
public async Task<ActionResult<IEnumerable<FileDimensionDto>>> GetFileDimensions(int chapterId, bool extractPdf = false)
|
||||
{
|
||||
if (chapterId <= 0) return null;
|
||||
var chapter = await _cacheService.Ensure(chapterId, extractPdf);
|
||||
if (chapter == null) return BadRequest("Could not find Chapter");
|
||||
return Ok(_cacheService.GetCachedFileDimensions(chapterId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns various information about a Chapter. Side effect: This will cache the chapter images for reading.
|
||||
/// </summary>
|
||||
|
|
@ -183,7 +200,7 @@ public class ReaderController : BaseApiController
|
|||
Pages = dto.Pages,
|
||||
ChapterTitle = dto.ChapterTitle ?? string.Empty,
|
||||
Subtitle = string.Empty,
|
||||
Title = dto.SeriesName
|
||||
Title = dto.SeriesName,
|
||||
};
|
||||
|
||||
if (info.ChapterTitle is {Length: > 0}) {
|
||||
|
|
@ -195,14 +212,14 @@ public class ReaderController : BaseApiController
|
|||
info.Subtitle = info.FileName;
|
||||
} else if (!info.IsSpecial && info.VolumeNumber.Equals(Services.Tasks.Scanner.Parser.Parser.DefaultVolume))
|
||||
{
|
||||
info.Subtitle = _readerService.FormatChapterName(info.LibraryType, true, true) + info.ChapterNumber;
|
||||
info.Subtitle = ReaderService.FormatChapterName(info.LibraryType, true, true) + info.ChapterNumber;
|
||||
}
|
||||
else
|
||||
{
|
||||
info.Subtitle = "Volume " + info.VolumeNumber;
|
||||
if (!info.ChapterNumber.Equals(Services.Tasks.Scanner.Parser.Parser.DefaultChapter))
|
||||
{
|
||||
info.Subtitle += " " + _readerService.FormatChapterName(info.LibraryType, true, true) +
|
||||
info.Subtitle += " " + ReaderService.FormatChapterName(info.LibraryType, true, true) +
|
||||
info.ChapterNumber;
|
||||
}
|
||||
}
|
||||
|
|
@ -673,7 +690,7 @@ public class ReaderController : BaseApiController
|
|||
if (chapter == null) return BadRequest("Could not find cached image. Reload and try again.");
|
||||
|
||||
bookmarkDto.Page = _readerService.CapPageToChapter(chapter, bookmarkDto.Page);
|
||||
var path = _cacheService.GetCachedPagePath(chapter, bookmarkDto.Page);
|
||||
var path = _cacheService.GetCachedPagePath(chapter.Id, bookmarkDto.Page);
|
||||
|
||||
if (!await _bookmarkService.BookmarkPage(user, bookmarkDto, path)) return BadRequest("Could not save bookmark");
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using API.Entities.Enums;
|
||||
using System.Collections.Generic;
|
||||
using API.Entities.Enums;
|
||||
|
||||
namespace API.DTOs.Reader;
|
||||
|
||||
|
|
|
|||
13
API/DTOs/Reader/FileDimensionDto.cs
Normal file
13
API/DTOs/Reader/FileDimensionDto.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
namespace API.DTOs.Reader;
|
||||
|
||||
public class FileDimensionDto
|
||||
{
|
||||
public int Width { get; set; }
|
||||
public int Height { get; set; }
|
||||
public int PageNumber { get; set; }
|
||||
/// <summary>
|
||||
/// The filename of the cached file. If this was nested in a subfolder, the foldername will be appended with _
|
||||
/// </summary>
|
||||
/// <example>chapter01_page01.png</example>
|
||||
public string FileName { get; set; } = default!;
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ using System.Text.RegularExpressions;
|
|||
using API.DTOs.ReadingLists;
|
||||
using API.Entities;
|
||||
using API.Entities.Enums;
|
||||
using API.Services;
|
||||
|
||||
namespace API.Helpers;
|
||||
|
||||
|
|
@ -40,35 +41,9 @@ public static class ReadingListHelper
|
|||
}
|
||||
|
||||
if (title == string.Empty) {
|
||||
title = FormatChapterName(item.LibraryType, true, true) + chapterNum;
|
||||
title = ReaderService.FormatChapterName(item.LibraryType, true, true) + chapterNum;
|
||||
}
|
||||
return title;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Formats a Chapter name based on the library it's in
|
||||
/// </summary>
|
||||
/// <param name="libraryType"></param>
|
||||
/// <param name="includeHash">For comics only, includes a # which is used for numbering on cards</param>
|
||||
/// <param name="includeSpace">Add a space at the end of the string. if includeHash and includeSpace are true, only hash will be at the end.</param>
|
||||
/// <returns></returns>
|
||||
private static string FormatChapterName(LibraryType libraryType, bool includeHash = false,
|
||||
bool includeSpace = false)
|
||||
{
|
||||
switch (libraryType)
|
||||
{
|
||||
case LibraryType.Manga:
|
||||
return "Chapter" + (includeSpace ? " " : string.Empty);
|
||||
case LibraryType.Comic:
|
||||
if (includeHash) {
|
||||
return "Issue #";
|
||||
}
|
||||
return "Issue" + (includeSpace ? " " : string.Empty);
|
||||
case LibraryType.Book:
|
||||
return "Book" + (includeSpace ? " " : string.Empty);
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(libraryType), libraryType, null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,11 +5,13 @@ using System.Linq;
|
|||
using System.Threading.Tasks;
|
||||
using API.Comparators;
|
||||
using API.Data;
|
||||
using API.DTOs.Reader;
|
||||
using API.Entities;
|
||||
using API.Entities.Enums;
|
||||
using API.Extensions;
|
||||
using Kavita.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NetVips;
|
||||
|
||||
namespace API.Services;
|
||||
|
||||
|
|
@ -29,7 +31,8 @@ public interface ICacheService
|
|||
/// <param name="chapterIds">Volumes that belong to that library. Assume the library might have been deleted before this invocation.</param>
|
||||
void CleanupChapters(IEnumerable<int> chapterIds);
|
||||
void CleanupBookmarks(IEnumerable<int> seriesIds);
|
||||
string GetCachedPagePath(Chapter chapter, int page);
|
||||
string GetCachedPagePath(int chapterId, int page);
|
||||
IEnumerable<FileDimensionDto> GetCachedFileDimensions(int chapterId);
|
||||
string GetCachedBookmarkPagePath(int seriesId, int page);
|
||||
string GetCachedFile(Chapter chapter);
|
||||
public void ExtractChapterFiles(string extractPath, IReadOnlyList<MangaFile> files, bool extractPdfImages = false);
|
||||
|
|
@ -55,6 +58,35 @@ public class CacheService : ICacheService
|
|||
_bookmarkService = bookmarkService;
|
||||
}
|
||||
|
||||
public IEnumerable<FileDimensionDto> GetCachedFileDimensions(int chapterId)
|
||||
{
|
||||
var path = GetCachePath(chapterId);
|
||||
var files = _directoryService.GetFilesWithExtension(path, Tasks.Scanner.Parser.Parser.ImageFileExtensions)
|
||||
.OrderByNatural(Path.GetFileNameWithoutExtension)
|
||||
.ToArray();
|
||||
|
||||
if (files.Length == 0)
|
||||
{
|
||||
return ArraySegment<FileDimensionDto>.Empty;
|
||||
}
|
||||
|
||||
var dimensions = new List<FileDimensionDto>();
|
||||
for (var i = 0; i < files.Length; i++)
|
||||
{
|
||||
var file = files[i];
|
||||
using var image = Image.NewFromStream(File.OpenRead(file), access: Enums.Access.SequentialUnbuffered);
|
||||
dimensions.Add(new FileDimensionDto()
|
||||
{
|
||||
PageNumber = i,
|
||||
Height = image.Height,
|
||||
Width = image.Width,
|
||||
FileName = file
|
||||
});
|
||||
}
|
||||
|
||||
return dimensions;
|
||||
}
|
||||
|
||||
public string GetCachedBookmarkPagePath(int seriesId, int page)
|
||||
{
|
||||
// Calculate what chapter the page belongs to
|
||||
|
|
@ -208,13 +240,13 @@ public class CacheService : ICacheService
|
|||
/// <summary>
|
||||
/// Returns the absolute path of a cached page.
|
||||
/// </summary>
|
||||
/// <param name="chapter">Chapter entity with Files populated.</param>
|
||||
/// <param name="chapterId">Chapter id with Files populated.</param>
|
||||
/// <param name="page">Page number to look for</param>
|
||||
/// <returns>Page filepath or empty if no files found.</returns>
|
||||
public string GetCachedPagePath(Chapter chapter, int page)
|
||||
public string GetCachedPagePath(int chapterId, int page)
|
||||
{
|
||||
// Calculate what chapter the page belongs to
|
||||
var path = GetCachePath(chapter.Id);
|
||||
var path = GetCachePath(chapterId);
|
||||
// TODO: We can optimize this by extracting and renaming, so we don't need to scan for the files and can do a direct access
|
||||
var files = _directoryService.GetFilesWithExtension(path, Tasks.Scanner.Parser.Parser.ImageFileExtensions)
|
||||
.OrderByNatural(Path.GetFileNameWithoutExtension)
|
||||
|
|
|
|||
|
|
@ -32,7 +32,6 @@ public interface IReaderService
|
|||
Task MarkChaptersUntilAsRead(AppUser user, int seriesId, float chapterNumber);
|
||||
Task MarkVolumesUntilAsRead(AppUser user, int seriesId, int volumeNumber);
|
||||
HourEstimateRangeDto GetTimeEstimate(long wordCount, int pageCount, bool isEpub);
|
||||
string FormatChapterName(LibraryType libraryType, bool includeHash = false, bool includeSpace = false);
|
||||
}
|
||||
|
||||
public class ReaderService : IReaderService
|
||||
|
|
@ -612,7 +611,7 @@ public class ReaderService : IReaderService
|
|||
/// <param name="includeHash">For comics only, includes a # which is used for numbering on cards</param>
|
||||
/// <param name="includeSpace">Add a space at the end of the string. if includeHash and includeSpace are true, only hash will be at the end.</param>
|
||||
/// <returns></returns>
|
||||
public string FormatChapterName(LibraryType libraryType, bool includeHash = false, bool includeSpace = false)
|
||||
public static string FormatChapterName(LibraryType libraryType, bool includeHash = false, bool includeSpace = false)
|
||||
{
|
||||
switch(libraryType)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue