Jellyfin.Drawing minor improvements
Reduce duplicate/dead code
This commit is contained in:
parent
d5e86188a1
commit
c707baed83
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Jellyfin.Data.Entities;
|
using Jellyfin.Data.Entities;
|
||||||
using MediaBrowser.Controller.Entities;
|
using MediaBrowser.Controller.Entities;
|
||||||
|
@ -70,14 +69,6 @@ namespace MediaBrowser.Controller.Drawing
|
||||||
|
|
||||||
string? GetImageCacheTag(User user);
|
string? GetImageCacheTag(User user);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Processes the image.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="options">The options.</param>
|
|
||||||
/// <param name="toStream">To stream.</param>
|
|
||||||
/// <returns>Task.</returns>
|
|
||||||
Task ProcessImage(ImageProcessingOptions options, Stream toStream);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Processes the image.
|
/// Processes the image.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -97,7 +88,5 @@ namespace MediaBrowser.Controller.Drawing
|
||||||
/// <param name="options">The options.</param>
|
/// <param name="options">The options.</param>
|
||||||
/// <param name="libraryName">The library name to draw onto the collage.</param>
|
/// <param name="libraryName">The library name to draw onto the collage.</param>
|
||||||
void CreateImageCollage(ImageCollageOptions options, string? libraryName);
|
void CreateImageCollage(ImageCollageOptions options, string? libraryName);
|
||||||
|
|
||||||
bool SupportsTransparency(string path);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,7 +119,8 @@ namespace MediaBrowser.Controller.Drawing
|
||||||
private bool IsFormatSupported(string originalImagePath)
|
private bool IsFormatSupported(string originalImagePath)
|
||||||
{
|
{
|
||||||
var ext = Path.GetExtension(originalImagePath);
|
var ext = Path.GetExtension(originalImagePath);
|
||||||
return SupportedOutputFormats.Any(outputFormat => string.Equals(ext, "." + outputFormat, StringComparison.OrdinalIgnoreCase));
|
ext = ext.Replace(".jpeg", ".jpg", StringComparison.OrdinalIgnoreCase);
|
||||||
|
return SupportedOutputFormats.Any(outputFormat => string.Equals(ext, outputFormat.GetExtension(), StringComparison.OrdinalIgnoreCase));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -650,15 +650,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
{
|
{
|
||||||
ArgumentException.ThrowIfNullOrEmpty(inputPath);
|
ArgumentException.ThrowIfNullOrEmpty(inputPath);
|
||||||
|
|
||||||
var outputExtension = targetFormat switch
|
var outputExtension = targetFormat?.GetExtension() ?? ".jpg";
|
||||||
{
|
|
||||||
ImageFormat.Bmp => ".bmp",
|
|
||||||
ImageFormat.Gif => ".gif",
|
|
||||||
ImageFormat.Jpg => ".jpg",
|
|
||||||
ImageFormat.Png => ".png",
|
|
||||||
ImageFormat.Webp => ".webp",
|
|
||||||
_ => ".jpg"
|
|
||||||
};
|
|
||||||
|
|
||||||
var tempExtractPath = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid() + outputExtension);
|
var tempExtractPath = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid() + outputExtension);
|
||||||
Directory.CreateDirectory(Path.GetDirectoryName(tempExtractPath));
|
Directory.CreateDirectory(Path.GetDirectoryName(tempExtractPath));
|
||||||
|
|
|
@ -24,4 +24,21 @@ public static class ImageFormatExtensions
|
||||||
ImageFormat.Webp => "image/webp",
|
ImageFormat.Webp => "image/webp",
|
||||||
_ => throw new InvalidEnumArgumentException(nameof(format), (int)format, typeof(ImageFormat))
|
_ => throw new InvalidEnumArgumentException(nameof(format), (int)format, typeof(ImageFormat))
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the correct extension for this <see cref="ImageFormat" />.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="format">This <see cref="ImageFormat" />.</param>
|
||||||
|
/// <exception cref="InvalidEnumArgumentException">The <paramref name="format"/> is an invalid enumeration value.</exception>
|
||||||
|
/// <returns>The correct extension for this <see cref="ImageFormat" />.</returns>
|
||||||
|
public static string GetExtension(this ImageFormat format)
|
||||||
|
=> format switch
|
||||||
|
{
|
||||||
|
ImageFormat.Bmp => ".bmp",
|
||||||
|
ImageFormat.Gif => ".gif",
|
||||||
|
ImageFormat.Jpg => ".jpg",
|
||||||
|
ImageFormat.Png => ".png",
|
||||||
|
ImageFormat.Webp => ".webp",
|
||||||
|
_ => throw new InvalidEnumArgumentException(nameof(format), (int)format, typeof(ImageFormat))
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -204,16 +204,10 @@ namespace MediaBrowser.Providers.MediaInfo
|
||||||
? Path.GetExtension(attachmentStream.FileName)
|
? Path.GetExtension(attachmentStream.FileName)
|
||||||
: MimeTypes.ToExtension(attachmentStream.MimeType);
|
: MimeTypes.ToExtension(attachmentStream.MimeType);
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(extension))
|
|
||||||
{
|
|
||||||
extension = ".jpg";
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageFormat format = extension switch
|
ImageFormat format = extension switch
|
||||||
{
|
{
|
||||||
".bmp" => ImageFormat.Bmp,
|
".bmp" => ImageFormat.Bmp,
|
||||||
".gif" => ImageFormat.Gif,
|
".gif" => ImageFormat.Gif,
|
||||||
".jpg" => ImageFormat.Jpg,
|
|
||||||
".png" => ImageFormat.Png,
|
".png" => ImageFormat.Png,
|
||||||
".webp" => ImageFormat.Webp,
|
".webp" => ImageFormat.Webp,
|
||||||
_ => ImageFormat.Jpg
|
_ => ImageFormat.Jpg
|
||||||
|
|
|
@ -200,20 +200,10 @@ public class SkiaEncoder : IImageEncoder
|
||||||
{
|
{
|
||||||
if (!orientation.HasValue)
|
if (!orientation.HasValue)
|
||||||
{
|
{
|
||||||
return SKEncodedOrigin.TopLeft;
|
return SKEncodedOrigin.Default;
|
||||||
}
|
}
|
||||||
|
|
||||||
return orientation.Value switch
|
return (SKEncodedOrigin)orientation.Value;
|
||||||
{
|
|
||||||
ImageOrientation.TopRight => SKEncodedOrigin.TopRight,
|
|
||||||
ImageOrientation.RightTop => SKEncodedOrigin.RightTop,
|
|
||||||
ImageOrientation.RightBottom => SKEncodedOrigin.RightBottom,
|
|
||||||
ImageOrientation.LeftTop => SKEncodedOrigin.LeftTop,
|
|
||||||
ImageOrientation.LeftBottom => SKEncodedOrigin.LeftBottom,
|
|
||||||
ImageOrientation.BottomRight => SKEncodedOrigin.BottomRight,
|
|
||||||
ImageOrientation.BottomLeft => SKEncodedOrigin.BottomLeft,
|
|
||||||
_ => SKEncodedOrigin.TopLeft
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -107,22 +107,10 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool SupportsImageCollageCreation => _imageEncoder.SupportsImageCollageCreation;
|
public bool SupportsImageCollageCreation => _imageEncoder.SupportsImageCollageCreation;
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task ProcessImage(ImageProcessingOptions options, Stream toStream)
|
|
||||||
{
|
|
||||||
var file = await ProcessImage(options).ConfigureAwait(false);
|
|
||||||
using var fileStream = AsyncFile.OpenRead(file.Path);
|
|
||||||
await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IReadOnlyCollection<ImageFormat> GetSupportedImageOutputFormats()
|
public IReadOnlyCollection<ImageFormat> GetSupportedImageOutputFormats()
|
||||||
=> _imageEncoder.SupportedOutputFormats;
|
=> _imageEncoder.SupportedOutputFormats;
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool SupportsTransparency(string path)
|
|
||||||
=> _transparentImageTypes.Contains(Path.GetExtension(path));
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task<(string Path, string? MimeType, DateTime DateModified)> ProcessImage(ImageProcessingOptions options)
|
public async Task<(string Path, string? MimeType, DateTime DateModified)> ProcessImage(ImageProcessingOptions options)
|
||||||
{
|
{
|
||||||
|
@ -224,7 +212,7 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (cacheFilePath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(cacheFilePath));
|
return (cacheFilePath, outputFormat.GetMimeType(), _fileSystem.GetLastWriteTimeUtc(cacheFilePath));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -262,17 +250,6 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||||
return ImageFormat.Jpg;
|
return ImageFormat.Jpg;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetMimeType(ImageFormat format, string path)
|
|
||||||
=> format switch
|
|
||||||
{
|
|
||||||
ImageFormat.Bmp => MimeTypes.GetMimeType("i.bmp"),
|
|
||||||
ImageFormat.Gif => MimeTypes.GetMimeType("i.gif"),
|
|
||||||
ImageFormat.Jpg => MimeTypes.GetMimeType("i.jpg"),
|
|
||||||
ImageFormat.Png => MimeTypes.GetMimeType("i.png"),
|
|
||||||
ImageFormat.Webp => MimeTypes.GetMimeType("i.webp"),
|
|
||||||
_ => MimeTypes.GetMimeType(path)
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the cache file path based on a set of parameters.
|
/// Gets the cache file path based on a set of parameters.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -374,7 +351,7 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||||
filename.Append(",v=");
|
filename.Append(",v=");
|
||||||
filename.Append(Version);
|
filename.Append(Version);
|
||||||
|
|
||||||
return GetCachePath(ResizedImageCachePath, filename.ToString(), "." + format.ToString().ToLowerInvariant());
|
return GetCachePath(ResizedImageCachePath, filename.ToString(), format.GetExtension());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
@ -471,35 +448,6 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||||
return Task.FromResult((originalImagePath, dateModified));
|
return Task.FromResult((originalImagePath, dateModified));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO _mediaEncoder.ConvertImage is not implemented
|
|
||||||
// if (!_imageEncoder.SupportedInputFormats.Contains(inputFormat))
|
|
||||||
// {
|
|
||||||
// try
|
|
||||||
// {
|
|
||||||
// string filename = (originalImagePath + dateModified.Ticks.ToString(CultureInfo.InvariantCulture)).GetMD5().ToString("N", CultureInfo.InvariantCulture);
|
|
||||||
//
|
|
||||||
// string cacheExtension = _mediaEncoder.SupportsEncoder("libwebp") ? ".webp" : ".png";
|
|
||||||
// var outputPath = Path.Combine(_appPaths.ImageCachePath, "converted-images", filename + cacheExtension);
|
|
||||||
//
|
|
||||||
// var file = _fileSystem.GetFileInfo(outputPath);
|
|
||||||
// if (!file.Exists)
|
|
||||||
// {
|
|
||||||
// await _mediaEncoder.ConvertImage(originalImagePath, outputPath).ConfigureAwait(false);
|
|
||||||
// dateModified = _fileSystem.GetLastWriteTimeUtc(outputPath);
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// dateModified = file.LastWriteTimeUtc;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// originalImagePath = outputPath;
|
|
||||||
// }
|
|
||||||
// catch (Exception ex)
|
|
||||||
// {
|
|
||||||
// _logger.LogError(ex, "Image conversion failed for {Path}", originalImagePath);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
return Task.FromResult((originalImagePath, dateModified));
|
return Task.FromResult((originalImagePath, dateModified));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,4 +30,17 @@ public static class ImageFormatExtensionsTests
|
||||||
[InlineData((ImageFormat)5)]
|
[InlineData((ImageFormat)5)]
|
||||||
public static void GetMimeType_Valid_ThrowsInvalidEnumArgumentException(ImageFormat format)
|
public static void GetMimeType_Valid_ThrowsInvalidEnumArgumentException(ImageFormat format)
|
||||||
=> Assert.Throws<InvalidEnumArgumentException>(() => format.GetMimeType());
|
=> Assert.Throws<InvalidEnumArgumentException>(() => format.GetMimeType());
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(GetAllImageFormats))]
|
||||||
|
public static void GetExtension_Valid_Valid(ImageFormat format)
|
||||||
|
=> Assert.Null(Record.Exception(() => format.GetExtension()));
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData((ImageFormat)int.MinValue)]
|
||||||
|
[InlineData((ImageFormat)int.MaxValue)]
|
||||||
|
[InlineData((ImageFormat)(-1))]
|
||||||
|
[InlineData((ImageFormat)5)]
|
||||||
|
public static void GetExtension_Valid_ThrowsInvalidEnumArgumentException(ImageFormat format)
|
||||||
|
=> Assert.Throws<InvalidEnumArgumentException>(() => format.GetExtension());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user