mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-31 20:24:27 -04:00
* Updated a typo in manage tasks of Reoccuring -> Recurring * Fixed a bug in MinimumNumberFromRange where a regex wasn't properly constructed which could skew results. * Fixed a bug where Volume numbers that were a float wouldn't render correctly in the manga reader menu. * Added the ability to double click on the image to bookmark it. Optimized the bookmark and unbookmark flows to remove 2 DB calls and reworked some flow of calls to speed it up. Fixed some logic where when using double (manga) flow, both of the images wouldn't show the bookmark effect, despite both of them being saved. Likewise, fixed a bug where both images weren't updating UI state, so switching from double (manga) to single, the second image wouldn't show as bookmarked without a refresh. * Double click works perfectly for bookmarking * Collection cover image chooser will now prompt with all series covers by default. Reset button is now moved up to the first slot if applicable. * When a Completed series is fully read by a user, a nightly task will now remove that series from their Want to Read list. * Added ability to trigger Want to Read cleanup from Tasks page. * Moved the brightness readout to the label line and fixed a bootstrap migration bug where small buttons weren't actually small. * Implemented ability to filter against release year (min or max or both). * Fixed a log message that wasn't properly formatted when scan finished an no files changes. * Cleaned up some code and merged some methods * Implemented sort by Release year metadata filter. * Fixed the code that finds ComicInfo.xml inside archives to only check the root and check explicitly for casing, so it must be ComicInfo.xml. * Dependency updates * Refactored some strings into consts and used TriggerJob rather than just enqueuing * Fixed the prefetcher which wasn't properly loading in the correct order as it was designed. * Cleaned up all traces of CircularArray from MangaReader * Removed a debug code * Fixed a bug with webtoon reader in fullscreen mode where continuous reader wouldn't trigger * When cleaning up series from users' want to read lists, include both completed and cancelled. * Fixed a bug where small images wouldn't have the pagination area extend to the bottom on manga reader * Added a new method for hashing during prod builds and ensure we always use aot * Fixed a bug where the save button wouldn't enable when color change occured. * Cleaned up some issues in one of contributor's PR.
201 lines
6.9 KiB
C#
201 lines
6.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using SixLabors.ImageSharp;
|
|
using Image = NetVips.Image;
|
|
|
|
namespace API.Services;
|
|
|
|
public interface IImageService
|
|
{
|
|
void ExtractImages(string fileFilePath, string targetDirectory, int fileCount = 1);
|
|
string GetCoverImage(string path, string fileName, string outputDirectory);
|
|
|
|
/// <summary>
|
|
/// Creates a Thumbnail version of a base64 image
|
|
/// </summary>
|
|
/// <param name="encodedImage">base64 encoded image</param>
|
|
/// <param name="fileName"></param>
|
|
/// <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 outputDirectory);
|
|
/// <summary>
|
|
/// Converts the passed image to webP and outputs it in the same directory
|
|
/// </summary>
|
|
/// <param name="filePath">Full path to the image to convert</param>
|
|
/// <param name="outputPath">Where to output the file</param>
|
|
/// <returns>File of written webp image</returns>
|
|
Task<string> ConvertToWebP(string filePath, string outputPath);
|
|
|
|
Task<bool> IsImage(string filePath);
|
|
}
|
|
|
|
public class ImageService : IImageService
|
|
{
|
|
private readonly ILogger<ImageService> _logger;
|
|
private readonly IDirectoryService _directoryService;
|
|
public const string ChapterCoverImageRegex = @"v\d+_c\d+";
|
|
public const string SeriesCoverImageRegex = @"series\d+";
|
|
public const string CollectionTagCoverImageRegex = @"tag\d+";
|
|
public const string ReadingListCoverImageRegex = @"readinglist\d+";
|
|
|
|
|
|
/// <summary>
|
|
/// Width of the Thumbnail generation
|
|
/// </summary>
|
|
private const int ThumbnailWidth = 320;
|
|
|
|
public ImageService(ILogger<ImageService> logger, IDirectoryService directoryService)
|
|
{
|
|
_logger = logger;
|
|
_directoryService = directoryService;
|
|
}
|
|
|
|
public void ExtractImages(string fileFilePath, string targetDirectory, int fileCount = 1)
|
|
{
|
|
_directoryService.ExistOrCreate(targetDirectory);
|
|
if (fileCount == 1)
|
|
{
|
|
_directoryService.CopyFileToDirectory(fileFilePath, targetDirectory);
|
|
}
|
|
else
|
|
{
|
|
_directoryService.CopyDirectoryToDirectory(Path.GetDirectoryName(fileFilePath), targetDirectory,
|
|
Tasks.Scanner.Parser.Parser.ImageFileExtensions);
|
|
}
|
|
}
|
|
|
|
public string GetCoverImage(string path, string fileName, string outputDirectory)
|
|
{
|
|
if (string.IsNullOrEmpty(path)) return string.Empty;
|
|
|
|
try
|
|
{
|
|
using var thumbnail = Image.Thumbnail(path, ThumbnailWidth);
|
|
var filename = fileName + ".png";
|
|
thumbnail.WriteToFile(_directoryService.FileSystem.Path.Join(outputDirectory, filename));
|
|
return filename;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "[GetCoverImage] There was an error and prevented thumbnail generation on {ImageFile}. Defaulting to no cover image", path);
|
|
}
|
|
|
|
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.
|
|
/// </summary>
|
|
/// <param name="stream">Stream to write to disk. Ensure this is rewinded.</param>
|
|
/// <param name="fileName">filename to save as without extension</param>
|
|
/// <param name="outputDirectory">Where to output the file, defaults to covers directory</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, string outputDirectory)
|
|
{
|
|
using var thumbnail = Image.ThumbnailStream(stream, ThumbnailWidth);
|
|
var filename = fileName + ".png";
|
|
_directoryService.ExistOrCreate(outputDirectory);
|
|
try
|
|
{
|
|
_directoryService.FileSystem.File.Delete(_directoryService.FileSystem.Path.Join(outputDirectory, filename));
|
|
} catch (Exception) {/* Swallow exception */}
|
|
thumbnail.WriteToFile(_directoryService.FileSystem.Path.Join(outputDirectory, filename));
|
|
return filename;
|
|
}
|
|
|
|
public async Task<string> ConvertToWebP(string filePath, string outputPath)
|
|
{
|
|
var file = _directoryService.FileSystem.FileInfo.FromFileName(filePath);
|
|
var fileName = file.Name.Replace(file.Extension, string.Empty);
|
|
var outputFile = Path.Join(outputPath, fileName + ".webp");
|
|
|
|
|
|
using var sourceImage = await SixLabors.ImageSharp.Image.LoadAsync(filePath);
|
|
await sourceImage.SaveAsWebpAsync(outputFile);
|
|
return outputFile;
|
|
}
|
|
|
|
public async Task<bool> IsImage(string filePath)
|
|
{
|
|
try
|
|
{
|
|
var info = await SixLabors.ImageSharp.Image.IdentifyAsync(filePath);
|
|
if (info == null) return false;
|
|
|
|
return true;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
/* Swallow Exception */
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public string CreateThumbnailFromBase64(string encodedImage, string fileName)
|
|
{
|
|
try
|
|
{
|
|
using var thumbnail = Image.ThumbnailBuffer(Convert.FromBase64String(encodedImage), ThumbnailWidth);
|
|
var filename = fileName + ".png";
|
|
thumbnail.WriteToFile(_directoryService.FileSystem.Path.Join(_directoryService.CoverImageDirectory, fileName + ".png"));
|
|
return filename;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "Error creating thumbnail from url");
|
|
}
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the name format for a chapter cover image
|
|
/// </summary>
|
|
/// <param name="chapterId"></param>
|
|
/// <param name="volumeId"></param>
|
|
/// <returns></returns>
|
|
public static string GetChapterFormat(int chapterId, int volumeId)
|
|
{
|
|
return $"v{volumeId}_c{chapterId}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the name format for a series cover image
|
|
/// </summary>
|
|
/// <param name="seriesId"></param>
|
|
/// <returns></returns>
|
|
public static string GetSeriesFormat(int seriesId)
|
|
{
|
|
return $"series{seriesId}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the name format for a collection tag cover image
|
|
/// </summary>
|
|
/// <param name="tagId"></param>
|
|
/// <returns></returns>
|
|
public static string GetCollectionTagFormat(int tagId)
|
|
{
|
|
return $"tag{tagId}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the name format for a reading list cover image
|
|
/// </summary>
|
|
/// <param name="readingListId"></param>
|
|
/// <returns></returns>
|
|
public static string GetReadingListFormat(int readingListId)
|
|
{
|
|
return $"readinglist{readingListId}";
|
|
}
|
|
|
|
|
|
}
|