mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-31 12:14:44 -04:00
* Added some documentation. Removed Require Admin Role from Search Tags. Added Summary to be updated on UpdateTag. * Added Swagger xml doc generation to beef up the documentation. Started adding xml comments to the APIs. This is a needed, slow task for upcoming Plugins system. * Implemented the ability to upload a custom series image to override the existing cover image. Refactored some code out to use ImageService and added more documentation * When a page cache fails, delete cache directory so user can try to reload. * Implemented the ability to lock a series cover image such that after user uploads something, it wont get refreshed by Kavita. * Implemented the ability to reset cover image for series by unlocking * Kick off a series refresh after a cover is unlocked. * Ability to press enter to load a url * Ability to reset selection * Cleaned up cover chooser such that reset is nicer, errors inform user to use file upload, series edit modal now doesn't use scrollable body. Mobile tweaks. CoverImageLocked is now sent to the UI. * More css changes to look better * When no bookmarks, don't show both markups * Fixed issues where images wouldn't refresh after cover image was changed. * Implemented the ability to change the cover images for collection tags. * Added property and API for chapter cover image update * Added UI code to prepare for updating cover image for chapters. need to rearrange components * Moved a ton of code around to separate card related screens into their own module. * Implemented the ability to update a chapter/volume cover image * Refactored action for volume to say edit to reflect modal action * Fixed issue where after editing chapter cover image, the underlying card wouldn't update * Fixed an issue where we were passing volumeId to the reset chapter lock. Changed some logic in volume cover image generation. * Automatically apply when you hit reset cover image
105 lines
2.8 KiB
C#
105 lines
2.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using API.Comparators;
|
|
using API.Entities;
|
|
using API.Interfaces.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
using NetVips;
|
|
|
|
namespace API.Services
|
|
{
|
|
|
|
public class ImageService : IImageService
|
|
{
|
|
private readonly ILogger<ImageService> _logger;
|
|
private readonly IDirectoryService _directoryService;
|
|
private readonly NaturalSortComparer _naturalSortComparer;
|
|
|
|
public ImageService(ILogger<ImageService> logger, IDirectoryService directoryService)
|
|
{
|
|
_logger = logger;
|
|
_directoryService = directoryService;
|
|
_naturalSortComparer = new NaturalSortComparer();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Finds the first image in the directory of the first file. Does not check for "cover/folder".ext files to override.
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
public string GetCoverFile(MangaFile file)
|
|
{
|
|
var directory = Path.GetDirectoryName(file.FilePath);
|
|
if (string.IsNullOrEmpty(directory))
|
|
{
|
|
_logger.LogError("Could not find Directory for {File}", file.FilePath);
|
|
return null;
|
|
}
|
|
|
|
var firstImage = _directoryService.GetFilesWithExtension(directory, Parser.Parser.ImageFileExtensions)
|
|
.OrderBy(f => f, _naturalSortComparer).FirstOrDefault();
|
|
|
|
return firstImage;
|
|
}
|
|
|
|
public byte[] GetCoverImage(string path, bool createThumbnail = false)
|
|
{
|
|
if (string.IsNullOrEmpty(path)) return Array.Empty<byte>();
|
|
|
|
try
|
|
{
|
|
if (createThumbnail)
|
|
{
|
|
return CreateThumbnail(path);
|
|
}
|
|
|
|
using var img = Image.NewFromFile(path);
|
|
using var stream = new MemoryStream();
|
|
img.JpegsaveStream(stream);
|
|
return stream.ToArray();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "[GetCoverImage] There was an error and prevented thumbnail generation on {ImageFile}. Defaulting to no cover image", path);
|
|
}
|
|
|
|
return Array.Empty<byte>();
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public byte[] CreateThumbnail(string path)
|
|
{
|
|
try
|
|
{
|
|
using var thumbnail = Image.Thumbnail(path, MetadataService.ThumbnailWidth);
|
|
return thumbnail.WriteToBuffer(".jpg");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "Error creating thumbnail from url");
|
|
}
|
|
|
|
return Array.Empty<byte>();
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public byte[] CreateThumbnailFromBase64(string encodedImage)
|
|
{
|
|
try
|
|
{
|
|
using var thumbnail = Image.ThumbnailBuffer(Convert.FromBase64String(encodedImage), MetadataService.ThumbnailWidth);
|
|
return thumbnail.WriteToBuffer(".jpg");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "Error creating thumbnail from url");
|
|
}
|
|
|
|
return Array.Empty<byte>();
|
|
}
|
|
}
|
|
}
|