From e026ba84c5e0d0a761cb1cae403e6f601e6fa2e0 Mon Sep 17 00:00:00 2001 From: David Ullmer Date: Tue, 17 Aug 2021 18:12:45 +0200 Subject: Add Splashscreen API endpoint to ImageController --- Jellyfin.Api/Controllers/ImageController.cs | 230 ++++++++++++++++++++-------- 1 file changed, 165 insertions(+), 65 deletions(-) (limited to 'Jellyfin.Api/Controllers/ImageController.cs') diff --git a/Jellyfin.Api/Controllers/ImageController.cs b/Jellyfin.Api/Controllers/ImageController.cs index 86933074d..0755b6c44 100644 --- a/Jellyfin.Api/Controllers/ImageController.cs +++ b/Jellyfin.Api/Controllers/ImageController.cs @@ -11,12 +11,14 @@ using System.Threading.Tasks; using Jellyfin.Api.Attributes; using Jellyfin.Api.Constants; using Jellyfin.Api.Helpers; +using MediaBrowser.Common.Configuration; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Drawing; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Branding; using MediaBrowser.Model.Drawing; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; @@ -44,6 +46,8 @@ namespace Jellyfin.Api.Controllers private readonly IAuthorizationContext _authContext; private readonly ILogger _logger; private readonly IServerConfigurationManager _serverConfigurationManager; + private readonly IApplicationPaths _appPaths; + private readonly IImageGenerator _imageGenerator; /// /// Initializes a new instance of the class. @@ -56,6 +60,8 @@ namespace Jellyfin.Api.Controllers /// Instance of the interface. /// Instance of the interface. /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. public ImageController( IUserManager userManager, ILibraryManager libraryManager, @@ -64,7 +70,9 @@ namespace Jellyfin.Api.Controllers IFileSystem fileSystem, IAuthorizationContext authContext, ILogger logger, - IServerConfigurationManager serverConfigurationManager) + IServerConfigurationManager serverConfigurationManager, + IApplicationPaths appPaths, + IImageGenerator imageGenerator) { _userManager = userManager; _libraryManager = libraryManager; @@ -74,6 +82,8 @@ namespace Jellyfin.Api.Controllers _authContext = authContext; _logger = logger; _serverConfigurationManager = serverConfigurationManager; + _appPaths = appPaths; + _imageGenerator = imageGenerator; } /// @@ -1692,6 +1702,130 @@ namespace Jellyfin.Api.Controllers .ConfigureAwait(false); } + /// + /// Generates or gets the splashscreen. + /// + /// Optional. Supply the cache tag from the item object to receive strong caching headers. + /// Determines the output format of the image - original,gif,jpg,png. + /// The maximum image width to return. + /// The maximum image height to return. + /// The fixed image width to return. + /// The fixed image height to return. + /// Optional. Quality setting, from 0-100. Defaults to 90 and should suffice in most cases. + /// Width of box to fill. + /// Height of box to fill. + /// Optional. Blur image. + /// Optional. Apply a background color for transparent images. + /// Optional. Apply a foreground layer on top of the image. + /// Darken the generated image. + /// Splashscreen returned successfully. + /// The splashscreen. + [HttpGet("Branding/Splashscreen")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesImageFile] + public async Task GetSplashscreen( + [FromQuery] string? tag, + [FromQuery] ImageFormat? format, + [FromQuery] int? maxWidth, + [FromQuery] int? maxHeight, + [FromQuery] int? width, + [FromQuery] int? height, + [FromQuery] int? quality, + [FromQuery] int? fillWidth, + [FromQuery] int? fillHeight, + [FromQuery] int? blur, + [FromQuery] string? backgroundColor, + [FromQuery] string? foregroundLayer, + [FromQuery] bool? darken = false) + { + string splashscreenPath; + var brandingOptions = _serverConfigurationManager.GetConfiguration("branding"); + if (!string.IsNullOrWhiteSpace(brandingOptions.SplashscreenLocation)) + { + splashscreenPath = brandingOptions.SplashscreenLocation!; + } + else + { + var filename = darken!.Value ? "splashscreen-darken.webp" : "splashscreen.webp"; + splashscreenPath = Path.Combine(_appPaths.DataPath, filename); + + if (!System.IO.File.Exists(splashscreenPath) && _imageGenerator.GetSupportedImages().Contains(GeneratedImages.Splashscreen)) + { + _imageGenerator.GenerateSplashscreen(new SplashscreenOptions(splashscreenPath, darken.Value)); + } + } + + var outputFormats = GetOutputFormats(format); + + TimeSpan? cacheDuration = null; + if (!string.IsNullOrEmpty(tag)) + { + cacheDuration = TimeSpan.FromDays(365); + } + + var options = new ImageProcessingOptions + { + Image = new ItemImageInfo + { + Path = splashscreenPath, + Height = 1080, + Width = 1920 + }, + Height = height, + MaxHeight = maxHeight, + MaxWidth = maxWidth, + FillHeight = fillHeight, + FillWidth = fillWidth, + Quality = quality ?? 100, + Width = width, + Blur = blur, + BackgroundColor = backgroundColor, + ForegroundLayer = foregroundLayer, + SupportedOutputFormats = outputFormats + }; + return await GetImageResult( + options, + cacheDuration, + new Dictionary(), + Request.Method.Equals(HttpMethods.Head, StringComparison.OrdinalIgnoreCase)); + } + + /// + /// Uploads a custom splashscreen. + /// + /// A indicating success. + /// Error reading the image format. + [HttpPost("Branding/Splashscreen")] + [Authorize(Policy = Policies.RequiresElevation)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [AcceptsImageFile] + public async Task UploadCustomSplashscreen() + { + await using var memoryStream = await GetMemoryStream(Request.Body).ConfigureAwait(false); + + // Handle image/png; charset=utf-8 + var mimeType = Request.ContentType.Split(';').FirstOrDefault(); + + if (mimeType == null) + { + throw new ArgumentException("Error reading mimetype from uploaded image!"); + } + + var filePath = Path.Combine(_appPaths.DataPath, "splashscreen-upload" + MimeTypes.ToExtension(mimeType)); + var brandingOptions = _serverConfigurationManager.GetConfiguration("branding"); + brandingOptions.SplashscreenLocation = filePath; + _serverConfigurationManager.SaveConfiguration("branding", brandingOptions); + + // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 . + await using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous)) + { + await memoryStream.CopyToAsync(fs, CancellationToken.None).ConfigureAwait(false); + } + + return NoContent(); + } + private static async Task GetMemoryStream(Stream inputStream) { using var reader = new StreamReader(inputStream); @@ -1823,25 +1957,35 @@ namespace Jellyfin.Api.Controllers { "realTimeInfo.dlna.org", "DLNA.ORG_TLAG=*" } }; + if (!imageInfo.IsLocalFile && item != null) + { + imageInfo = await _libraryManager.ConvertImageToLocal(item, imageInfo, imageIndex ?? 0).ConfigureAwait(false); + } + + var options = new ImageProcessingOptions + { + Height = height, + ImageIndex = imageIndex ?? 0, + Image = imageInfo, + Item = item, + ItemId = itemId, + MaxHeight = maxHeight, + MaxWidth = maxWidth, + FillHeight = fillHeight, + FillWidth = fillWidth, + Quality = quality ?? 100, + Width = width, + AddPlayedIndicator = addPlayedIndicator ?? false, + PercentPlayed = percentPlayed ?? 0, + UnplayedCount = unplayedCount, + Blur = blur, + BackgroundColor = backgroundColor, + ForegroundLayer = foregroundLayer, + SupportedOutputFormats = outputFormats + }; + return await GetImageResult( - item, - itemId, - imageIndex, - width, - height, - maxWidth, - maxHeight, - fillWidth, - fillHeight, - quality, - addPlayedIndicator, - percentPlayed, - unplayedCount, - blur, - backgroundColor, - foregroundLayer, - imageInfo, - outputFormats, + options, cacheDuration, responseHeaders, isHeadRequest).ConfigureAwait(false); @@ -1922,56 +2066,12 @@ namespace Jellyfin.Api.Controllers } private async Task GetImageResult( - BaseItem? item, - Guid itemId, - int? index, - int? width, - int? height, - int? maxWidth, - int? maxHeight, - int? fillWidth, - int? fillHeight, - int? quality, - bool? addPlayedIndicator, - double? percentPlayed, - int? unplayedCount, - int? blur, - string? backgroundColor, - string? foregroundLayer, - ItemImageInfo imageInfo, - IReadOnlyCollection supportedFormats, + ImageProcessingOptions imageProcessingOptions, TimeSpan? cacheDuration, IDictionary headers, bool isHeadRequest) { - if (!imageInfo.IsLocalFile && item != null) - { - imageInfo = await _libraryManager.ConvertImageToLocal(item, imageInfo, index ?? 0).ConfigureAwait(false); - } - - var options = new ImageProcessingOptions - { - Height = height, - ImageIndex = index ?? 0, - Image = imageInfo, - Item = item, - ItemId = itemId, - MaxHeight = maxHeight, - MaxWidth = maxWidth, - FillHeight = fillHeight, - FillWidth = fillWidth, - Quality = quality ?? 100, - Width = width, - AddPlayedIndicator = addPlayedIndicator ?? false, - PercentPlayed = percentPlayed ?? 0, - UnplayedCount = unplayedCount, - Blur = blur, - BackgroundColor = backgroundColor, - ForegroundLayer = foregroundLayer, - SupportedOutputFormats = supportedFormats - }; - - var (imagePath, imageContentType, dateImageModified) = await _imageProcessor.ProcessImage(options).ConfigureAwait(false); + var (imagePath, imageContentType, dateImageModified) = await _imageProcessor.ProcessImage(imageProcessingOptions).ConfigureAwait(false); var disableCaching = Request.Headers[HeaderNames.CacheControl].Contains("no-cache"); var parsingSuccessful = DateTime.TryParse(Request.Headers[HeaderNames.IfModifiedSince], out var ifModifiedSinceHeader); -- cgit v1.2.3 From 68db3be0e754d223a290c9276345bc9551eab887 Mon Sep 17 00:00:00 2001 From: David Ullmer Date: Tue, 17 Aug 2021 18:34:54 +0200 Subject: Remove darkening filter from Splashscreen Using the foregroundLayer parameter has the same effect --- Jellyfin.Api/Controllers/ImageController.cs | 9 +++---- Jellyfin.Drawing.Skia/DefaultImageGenerator.cs | 4 +-- Jellyfin.Drawing.Skia/SplashscreenBuilder.cs | 18 +++----------- MediaBrowser.Controller/Drawing/IImageGenerator.cs | 4 +-- .../Drawing/SplashscreenOptions.cs | 29 ---------------------- 5 files changed, 10 insertions(+), 54 deletions(-) delete mode 100644 MediaBrowser.Controller/Drawing/SplashscreenOptions.cs (limited to 'Jellyfin.Api/Controllers/ImageController.cs') diff --git a/Jellyfin.Api/Controllers/ImageController.cs b/Jellyfin.Api/Controllers/ImageController.cs index 0755b6c44..d7aac9b9c 100644 --- a/Jellyfin.Api/Controllers/ImageController.cs +++ b/Jellyfin.Api/Controllers/ImageController.cs @@ -1717,7 +1717,6 @@ namespace Jellyfin.Api.Controllers /// Optional. Blur image. /// Optional. Apply a background color for transparent images. /// Optional. Apply a foreground layer on top of the image. - /// Darken the generated image. /// Splashscreen returned successfully. /// The splashscreen. [HttpGet("Branding/Splashscreen")] @@ -1735,8 +1734,7 @@ namespace Jellyfin.Api.Controllers [FromQuery] int? fillHeight, [FromQuery] int? blur, [FromQuery] string? backgroundColor, - [FromQuery] string? foregroundLayer, - [FromQuery] bool? darken = false) + [FromQuery] string? foregroundLayer) { string splashscreenPath; var brandingOptions = _serverConfigurationManager.GetConfiguration("branding"); @@ -1746,12 +1744,11 @@ namespace Jellyfin.Api.Controllers } else { - var filename = darken!.Value ? "splashscreen-darken.webp" : "splashscreen.webp"; - splashscreenPath = Path.Combine(_appPaths.DataPath, filename); + splashscreenPath = Path.Combine(_appPaths.DataPath, "splashscreen.webp"); if (!System.IO.File.Exists(splashscreenPath) && _imageGenerator.GetSupportedImages().Contains(GeneratedImages.Splashscreen)) { - _imageGenerator.GenerateSplashscreen(new SplashscreenOptions(splashscreenPath, darken.Value)); + _imageGenerator.GenerateSplashscreen(splashscreenPath); } } diff --git a/Jellyfin.Drawing.Skia/DefaultImageGenerator.cs b/Jellyfin.Drawing.Skia/DefaultImageGenerator.cs index 780d0b060..4d029d906 100644 --- a/Jellyfin.Drawing.Skia/DefaultImageGenerator.cs +++ b/Jellyfin.Drawing.Skia/DefaultImageGenerator.cs @@ -44,7 +44,7 @@ namespace Jellyfin.Drawing.Skia } /// - public void GenerateSplashscreen(SplashscreenOptions generationOptions) + public void GenerateSplashscreen(string outputPath) { var posters = GetItemsWithImageType(ImageType.Primary).Select(x => x.GetImages(ImageType.Primary).First().Path).ToList(); var landscape = GetItemsWithImageType(ImageType.Thumb).Select(x => x.GetImages(ImageType.Thumb).First().Path).ToList(); @@ -57,7 +57,7 @@ namespace Jellyfin.Drawing.Skia } var splashBuilder = new SplashscreenBuilder((SkiaEncoder)_imageEncoder); - splashBuilder.GenerateSplash(posters, landscape, generationOptions.OutputPath, generationOptions.ApplyFilter); + splashBuilder.GenerateSplash(posters, landscape, outputPath); } private IReadOnlyList GetItemsWithImageType(ImageType imageType) diff --git a/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs b/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs index 7cb10bfee..205651554 100644 --- a/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs +++ b/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs @@ -36,10 +36,9 @@ namespace Jellyfin.Drawing.Skia /// The poster paths. /// The landscape paths. /// The output path. - /// Whether to apply the darkening filter. - public void GenerateSplash(IReadOnlyList posters, IReadOnlyList backdrop, string outputPath, bool applyFilter) + public void GenerateSplash(IReadOnlyList posters, IReadOnlyList backdrop, string outputPath) { - var wall = GenerateCollage(posters, backdrop, applyFilter); + var wall = GenerateCollage(posters, backdrop); var transformed = Transform3D(wall); using var outputStream = new SKFileWStream(outputPath); @@ -52,9 +51,8 @@ namespace Jellyfin.Drawing.Skia /// /// The poster paths. /// The landscape paths. - /// Whether to apply the darkening filter. /// The created collage as a bitmap. - private SKBitmap GenerateCollage(IReadOnlyList posters, IReadOnlyList backdrop, bool applyFilter) + private SKBitmap GenerateCollage(IReadOnlyList posters, IReadOnlyList backdrop) { _random = new Random(); @@ -119,16 +117,6 @@ namespace Jellyfin.Drawing.Skia } } - if (applyFilter) - { - var paintColor = new SKPaint - { - Color = SKColors.Black.WithAlpha(0x50), - Style = SKPaintStyle.Fill - }; - canvas.DrawRect(0, 0, WallWidth, WallHeight, paintColor); - } - return bitmap; } diff --git a/MediaBrowser.Controller/Drawing/IImageGenerator.cs b/MediaBrowser.Controller/Drawing/IImageGenerator.cs index 21699c3f0..4f944ce54 100644 --- a/MediaBrowser.Controller/Drawing/IImageGenerator.cs +++ b/MediaBrowser.Controller/Drawing/IImageGenerator.cs @@ -11,7 +11,7 @@ namespace MediaBrowser.Controller.Drawing /// /// Generates a splashscreen. /// - /// The options used to generate the splashscreen. - void GenerateSplashscreen(SplashscreenOptions generationOptions); + /// The path where the splashscreen should be saved. + void GenerateSplashscreen(string outputPath); } } diff --git a/MediaBrowser.Controller/Drawing/SplashscreenOptions.cs b/MediaBrowser.Controller/Drawing/SplashscreenOptions.cs deleted file mode 100644 index ba268b8eb..000000000 --- a/MediaBrowser.Controller/Drawing/SplashscreenOptions.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace MediaBrowser.Controller.Drawing -{ - /// - /// Options used to generate the splashscreen. - /// - public class SplashscreenOptions - { - /// - /// Initializes a new instance of the class. - /// - /// The output path. - /// Optional. Apply a darkening filter. - public SplashscreenOptions(string outputPath, bool applyFilter = false) - { - OutputPath = outputPath; - ApplyFilter = applyFilter; - } - - /// - /// Gets or sets the output path. - /// - public string OutputPath { get; set; } - - /// - /// Gets or sets a value indicating whether to apply a darkening filter at the end. - /// - public bool ApplyFilter { get; set; } - } -} -- cgit v1.2.3 From c70452b4a4046b82e26a2e73b1d67e8f82fe0e32 Mon Sep 17 00:00:00 2001 From: David Ullmer Date: Tue, 17 Aug 2021 19:18:24 +0200 Subject: Add missing response code documentation --- Jellyfin.Api/Controllers/ImageController.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'Jellyfin.Api/Controllers/ImageController.cs') diff --git a/Jellyfin.Api/Controllers/ImageController.cs b/Jellyfin.Api/Controllers/ImageController.cs index d7aac9b9c..3b2fc98e7 100644 --- a/Jellyfin.Api/Controllers/ImageController.cs +++ b/Jellyfin.Api/Controllers/ImageController.cs @@ -1764,9 +1764,7 @@ namespace Jellyfin.Api.Controllers { Image = new ItemImageInfo { - Path = splashscreenPath, - Height = 1080, - Width = 1920 + Path = splashscreenPath }, Height = height, MaxHeight = maxHeight, @@ -1791,11 +1789,15 @@ namespace Jellyfin.Api.Controllers /// Uploads a custom splashscreen. /// /// A indicating success. + /// Sucessfully uploaded new splashscreen. + /// Error reading MimeType from uploaded image. + /// User does not have permission to upload splashscreen.. /// Error reading the image format. [HttpPost("Branding/Splashscreen")] [Authorize(Policy = Policies.RequiresElevation)] [ProducesResponseType(StatusCodes.Status204NoContent)] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] [AcceptsImageFile] public async Task UploadCustomSplashscreen() { @@ -1806,7 +1808,7 @@ namespace Jellyfin.Api.Controllers if (mimeType == null) { - throw new ArgumentException("Error reading mimetype from uploaded image!"); + return BadRequest("Error reading mimetype from uploaded image"); } var filePath = Path.Combine(_appPaths.DataPath, "splashscreen-upload" + MimeTypes.ToExtension(mimeType)); -- cgit v1.2.3 From 9e0958d8224ef3fa51893fd5a38cc57104f32422 Mon Sep 17 00:00:00 2001 From: David Ullmer Date: Wed, 18 Aug 2021 14:22:01 +0200 Subject: Apply suggestions from code review --- Jellyfin.Api/Controllers/ImageController.cs | 7 +++---- Jellyfin.Drawing.Skia/SplashscreenBuilder.cs | 6 ++---- MediaBrowser.Model/Branding/BrandingOptions.cs | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) (limited to 'Jellyfin.Api/Controllers/ImageController.cs') diff --git a/Jellyfin.Api/Controllers/ImageController.cs b/Jellyfin.Api/Controllers/ImageController.cs index 3b2fc98e7..24059cddd 100644 --- a/Jellyfin.Api/Controllers/ImageController.cs +++ b/Jellyfin.Api/Controllers/ImageController.cs @@ -1803,15 +1803,14 @@ namespace Jellyfin.Api.Controllers { await using var memoryStream = await GetMemoryStream(Request.Body).ConfigureAwait(false); - // Handle image/png; charset=utf-8 - var mimeType = Request.ContentType.Split(';').FirstOrDefault(); + var mimeType = MediaTypeHeaderValue.Parse(Request.ContentType).MediaType; - if (mimeType == null) + if (!mimeType.HasValue) { return BadRequest("Error reading mimetype from uploaded image"); } - var filePath = Path.Combine(_appPaths.DataPath, "splashscreen-upload" + MimeTypes.ToExtension(mimeType)); + var filePath = Path.Combine(_appPaths.DataPath, "splashscreen-upload" + MimeTypes.ToExtension(mimeType.Value)); var brandingOptions = _serverConfigurationManager.GetConfiguration("branding"); brandingOptions.SplashscreenLocation = filePath; _serverConfigurationManager.SaveConfiguration("branding", brandingOptions); diff --git a/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs b/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs index 205651554..9f801c320 100644 --- a/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs +++ b/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs @@ -19,8 +19,6 @@ namespace Jellyfin.Drawing.Skia private readonly SkiaEncoder _skiaEncoder; - private Random? _random; - /// /// Initializes a new instance of the class. /// @@ -54,7 +52,7 @@ namespace Jellyfin.Drawing.Skia /// The created collage as a bitmap. private SKBitmap GenerateCollage(IReadOnlyList posters, IReadOnlyList backdrop) { - _random = new Random(); + var random = new Random(); var posterIndex = 0; var backdropIndex = 0; @@ -67,7 +65,7 @@ namespace Jellyfin.Drawing.Skia for (int i = 0; i < Rows; i++) { - int imageCounter = _random.Next(0, 5); + int imageCounter = random.Next(0, 5); int currentWidthPos = i * 75; int currentHeight = i * (posterHeight + Spacing); diff --git a/MediaBrowser.Model/Branding/BrandingOptions.cs b/MediaBrowser.Model/Branding/BrandingOptions.cs index 18a177e2d..56e5a8715 100644 --- a/MediaBrowser.Model/Branding/BrandingOptions.cs +++ b/MediaBrowser.Model/Branding/BrandingOptions.cs @@ -33,6 +33,6 @@ namespace MediaBrowser.Model.Branding /// Gets the splashscreen url. /// [XmlIgnore] - public string SplashscreenUrl => "/Branding/Splashscreen"; + public string? SplashscreenUrl => "/Branding/Splashscreen"; } } -- cgit v1.2.3 From 360fd70fc74325008b031c9a1155b9b76724866d Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Tue, 4 Jan 2022 08:37:57 -0700 Subject: Clean up --- .../Tasks/RefreshMediaLibraryTask.cs | 2 +- Jellyfin.Api/Controllers/ImageController.cs | 32 +++--- Jellyfin.Drawing.Skia/DefaultImageGenerator.cs | 113 ++++++++++----------- Jellyfin.Server/CoreAppHost.cs | 2 +- .../Drawing/GeneratedImageType.cs | 12 +++ MediaBrowser.Controller/Drawing/GeneratedImages.cs | 13 --- MediaBrowser.Controller/Drawing/IImageGenerator.cs | 32 +++--- MediaBrowser.Model/Branding/BrandingOptions.cs | 58 +++++------ 8 files changed, 133 insertions(+), 131 deletions(-) create mode 100644 MediaBrowser.Controller/Drawing/GeneratedImageType.cs delete mode 100644 MediaBrowser.Controller/Drawing/GeneratedImages.cs (limited to 'Jellyfin.Api/Controllers/ImageController.cs') diff --git a/Emby.Server.Implementations/ScheduledTasks/Tasks/RefreshMediaLibraryTask.cs b/Emby.Server.Implementations/ScheduledTasks/Tasks/RefreshMediaLibraryTask.cs index 64393669b..c12fc2113 100644 --- a/Emby.Server.Implementations/ScheduledTasks/Tasks/RefreshMediaLibraryTask.cs +++ b/Emby.Server.Implementations/ScheduledTasks/Tasks/RefreshMediaLibraryTask.cs @@ -83,7 +83,7 @@ namespace Emby.Server.Implementations.ScheduledTasks.Tasks progress.Report(0); - _imageGenerator.GenerateSplashscreen(Path.Combine(_applicationPaths.DataPath, "splashscreen.webp")); + _imageGenerator.Generate(GeneratedImageType.Splashscreen, Path.Combine(_applicationPaths.DataPath, "splashscreen.webp")); return ((LibraryManager)_libraryManager).ValidateMediaLibraryInternal(progress, cancellationToken); } diff --git a/Jellyfin.Api/Controllers/ImageController.cs b/Jellyfin.Api/Controllers/ImageController.cs index 24059cddd..6d34ca770 100644 --- a/Jellyfin.Api/Controllers/ImageController.cs +++ b/Jellyfin.Api/Controllers/ImageController.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using System.Globalization; @@ -1705,18 +1706,18 @@ namespace Jellyfin.Api.Controllers /// /// Generates or gets the splashscreen. /// - /// Optional. Supply the cache tag from the item object to receive strong caching headers. + /// Supply the cache tag from the item object to receive strong caching headers. /// Determines the output format of the image - original,gif,jpg,png. /// The maximum image width to return. /// The maximum image height to return. /// The fixed image width to return. /// The fixed image height to return. - /// Optional. Quality setting, from 0-100. Defaults to 90 and should suffice in most cases. /// Width of box to fill. /// Height of box to fill. - /// Optional. Blur image. - /// Optional. Apply a background color for transparent images. - /// Optional. Apply a foreground layer on top of the image. + /// Blur image. + /// Apply a background color for transparent images. + /// Apply a foreground layer on top of the image. + /// Quality setting, from 0-100. /// Splashscreen returned successfully. /// The splashscreen. [HttpGet("Branding/Splashscreen")] @@ -1729,12 +1730,12 @@ namespace Jellyfin.Api.Controllers [FromQuery] int? maxHeight, [FromQuery] int? width, [FromQuery] int? height, - [FromQuery] int? quality, [FromQuery] int? fillWidth, [FromQuery] int? fillHeight, [FromQuery] int? blur, [FromQuery] string? backgroundColor, - [FromQuery] string? foregroundLayer) + [FromQuery] string? foregroundLayer, + [FromQuery, Range(0, 100)] int quality = 90) { string splashscreenPath; var brandingOptions = _serverConfigurationManager.GetConfiguration("branding"); @@ -1746,9 +1747,9 @@ namespace Jellyfin.Api.Controllers { splashscreenPath = Path.Combine(_appPaths.DataPath, "splashscreen.webp"); - if (!System.IO.File.Exists(splashscreenPath) && _imageGenerator.GetSupportedImages().Contains(GeneratedImages.Splashscreen)) + if (!System.IO.File.Exists(splashscreenPath) && _imageGenerator.GetSupportedImages().Contains(GeneratedImageType.Splashscreen)) { - _imageGenerator.GenerateSplashscreen(splashscreenPath); + _imageGenerator.Generate(GeneratedImageType.Splashscreen, splashscreenPath); } } @@ -1771,18 +1772,20 @@ namespace Jellyfin.Api.Controllers MaxWidth = maxWidth, FillHeight = fillHeight, FillWidth = fillWidth, - Quality = quality ?? 100, + Quality = quality, Width = width, Blur = blur, BackgroundColor = backgroundColor, ForegroundLayer = foregroundLayer, SupportedOutputFormats = outputFormats }; + return await GetImageResult( - options, - cacheDuration, - new Dictionary(), - Request.Method.Equals(HttpMethods.Head, StringComparison.OrdinalIgnoreCase)); + options, + cacheDuration, + ImmutableDictionary.Empty, + Request.Method.Equals(HttpMethods.Head, StringComparison.OrdinalIgnoreCase)) + .ConfigureAwait(false); } /// @@ -1815,7 +1818,6 @@ namespace Jellyfin.Api.Controllers brandingOptions.SplashscreenLocation = filePath; _serverConfigurationManager.SaveConfiguration("branding", brandingOptions); - // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 . await using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous)) { await memoryStream.CopyToAsync(fs, CancellationToken.None).ConfigureAwait(false); diff --git a/Jellyfin.Drawing.Skia/DefaultImageGenerator.cs b/Jellyfin.Drawing.Skia/DefaultImageGenerator.cs index 4d029d906..e102b8f49 100644 --- a/Jellyfin.Drawing.Skia/DefaultImageGenerator.cs +++ b/Jellyfin.Drawing.Skia/DefaultImageGenerator.cs @@ -10,74 +10,73 @@ using MediaBrowser.Model.Entities; using MediaBrowser.Model.Querying; using Microsoft.Extensions.Logging; -namespace Jellyfin.Drawing.Skia +namespace Jellyfin.Drawing.Skia; + +/// +/// The default image generator. +/// +public class DefaultImageGenerator : IImageGenerator { + private readonly IImageEncoder _imageEncoder; + private readonly IItemRepository _itemRepository; + private readonly ILogger _logger; + /// - /// The default image generator. + /// Initializes a new instance of the class. /// - public class DefaultImageGenerator : IImageGenerator + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. + public DefaultImageGenerator( + IImageEncoder imageEncoder, + IItemRepository itemRepository, + ILogger logger) { - private readonly IImageEncoder _imageEncoder; - private readonly IItemRepository _itemRepository; - private readonly ILogger _logger; + _imageEncoder = imageEncoder; + _itemRepository = itemRepository; + _logger = logger; + } - /// - /// Initializes a new instance of the class. - /// - /// Instance of the interface. - /// Instance of the interface. - /// Instance of the interface. - public DefaultImageGenerator( - IImageEncoder imageEncoder, - IItemRepository itemRepository, - ILogger logger) - { - _imageEncoder = imageEncoder; - _itemRepository = itemRepository; - _logger = logger; - } + /// + public IReadOnlyList GetSupportedImages() + { + return new[] { GeneratedImageType.Splashscreen }; + } - /// - public GeneratedImages[] GetSupportedImages() + /// + public void Generate(GeneratedImageType imageTypeType, string outputPath) + { + var posters = GetItemsWithImageType(ImageType.Primary).Select(x => x.GetImages(ImageType.Primary).First().Path).ToList(); + var landscape = GetItemsWithImageType(ImageType.Thumb).Select(x => x.GetImages(ImageType.Thumb).First().Path).ToList(); + if (landscape.Count == 0) { - return new[] { GeneratedImages.Splashscreen }; + // Thumb images fit better because they include the title in the image but are not provided with TMDb. + // Using backdrops as a fallback to generate an image at all + _logger.LogDebug("No thumb images found. Using backdrops to generate splashscreen"); + landscape = GetItemsWithImageType(ImageType.Backdrop).Select(x => x.GetImages(ImageType.Backdrop).First().Path).ToList(); } - /// - public void GenerateSplashscreen(string outputPath) - { - var posters = GetItemsWithImageType(ImageType.Primary).Select(x => x.GetImages(ImageType.Primary).First().Path).ToList(); - var landscape = GetItemsWithImageType(ImageType.Thumb).Select(x => x.GetImages(ImageType.Thumb).First().Path).ToList(); - if (landscape.Count == 0) - { - // Thumb images fit better because they include the title in the image but are not provided with TMDb. - // Using backdrops as a fallback to generate an image at all - _logger.LogDebug("No thumb images found. Using backdrops to generate splashscreen."); - landscape = GetItemsWithImageType(ImageType.Backdrop).Select(x => x.GetImages(ImageType.Backdrop).First().Path).ToList(); - } - - var splashBuilder = new SplashscreenBuilder((SkiaEncoder)_imageEncoder); - splashBuilder.GenerateSplash(posters, landscape, outputPath); - } + var splashBuilder = new SplashscreenBuilder((SkiaEncoder)_imageEncoder); + splashBuilder.GenerateSplash(posters, landscape, outputPath); + } - private IReadOnlyList GetItemsWithImageType(ImageType imageType) + private IReadOnlyList GetItemsWithImageType(ImageType imageType) + { + // todo make included libraries configurable + return _itemRepository.GetItemList(new InternalItemsQuery { - // todo make included libraries configurable - return _itemRepository.GetItemList(new InternalItemsQuery + CollapseBoxSetItems = false, + Recursive = true, + DtoOptions = new DtoOptions(false), + ImageTypes = new[] { imageType }, + Limit = 30, + // todo max parental rating configurable + MaxParentalRating = 10, + OrderBy = new ValueTuple[] { - CollapseBoxSetItems = false, - Recursive = true, - DtoOptions = new DtoOptions(false), - ImageTypes = new ImageType[] { imageType }, - Limit = 30, - // todo max parental rating configurable - MaxParentalRating = 10, - OrderBy = new ValueTuple[] - { - new ValueTuple(ItemSortBy.Random, SortOrder.Ascending) - }, - IncludeItemTypes = new string[] { "Movie", "Series" } - }); - } + new(ItemSortBy.Random, SortOrder.Ascending) + }, + IncludeItemTypes = new[] { BaseItemKind.Movie, BaseItemKind.Series } + }); } } diff --git a/Jellyfin.Server/CoreAppHost.cs b/Jellyfin.Server/CoreAppHost.cs index ac7ab2dee..b2f9a518a 100644 --- a/Jellyfin.Server/CoreAppHost.cs +++ b/Jellyfin.Server/CoreAppHost.cs @@ -86,7 +86,7 @@ namespace Jellyfin.Server serviceCollection.AddSingleton(); // TODO search plugins - ServiceCollection.AddSingleton(); + serviceCollection.AddSingleton(); // TODO search the assemblies instead of adding them manually? serviceCollection.AddSingleton(); diff --git a/MediaBrowser.Controller/Drawing/GeneratedImageType.cs b/MediaBrowser.Controller/Drawing/GeneratedImageType.cs new file mode 100644 index 000000000..a8db86d4f --- /dev/null +++ b/MediaBrowser.Controller/Drawing/GeneratedImageType.cs @@ -0,0 +1,12 @@ +namespace MediaBrowser.Controller.Drawing; + +/// +/// Which generated image type the supports. +/// +public enum GeneratedImageType +{ + /// + /// The splashscreen. + /// + Splashscreen = 0 +} diff --git a/MediaBrowser.Controller/Drawing/GeneratedImages.cs b/MediaBrowser.Controller/Drawing/GeneratedImages.cs deleted file mode 100644 index 47b60979b..000000000 --- a/MediaBrowser.Controller/Drawing/GeneratedImages.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace MediaBrowser.Controller.Drawing -{ - /// - /// Which generated images an supports. - /// - public enum GeneratedImages - { - /// - /// The splashscreen. - /// - Splashscreen - } -} diff --git a/MediaBrowser.Controller/Drawing/IImageGenerator.cs b/MediaBrowser.Controller/Drawing/IImageGenerator.cs index f7e7f83b2..773db02cb 100644 --- a/MediaBrowser.Controller/Drawing/IImageGenerator.cs +++ b/MediaBrowser.Controller/Drawing/IImageGenerator.cs @@ -1,20 +1,22 @@ -namespace MediaBrowser.Controller.Drawing +using System.Collections.Generic; + +namespace MediaBrowser.Controller.Drawing; + +/// +/// Interface for an image generator. +/// +public interface IImageGenerator { /// - /// Interface for an image generator. + /// Gets the supported generated images of the image generator. /// - public interface IImageGenerator - { - /// - /// Gets the supported generated images of the image generator. - /// - /// The supported images. - GeneratedImages[] GetSupportedImages(); + /// The supported generated image types. + IReadOnlyList GetSupportedImages(); - /// - /// Generates a splashscreen. - /// - /// The path where the splashscreen should be saved. - void GenerateSplashscreen(string outputPath); - } + /// + /// Generates a splashscreen. + /// + /// The image to generate. + /// The path where the splashscreen should be saved. + void Generate(GeneratedImageType imageTypeType, string outputPath); } diff --git a/MediaBrowser.Model/Branding/BrandingOptions.cs b/MediaBrowser.Model/Branding/BrandingOptions.cs index 56e5a8715..01db70885 100644 --- a/MediaBrowser.Model/Branding/BrandingOptions.cs +++ b/MediaBrowser.Model/Branding/BrandingOptions.cs @@ -1,38 +1,38 @@ using System.Text.Json.Serialization; using System.Xml.Serialization; -#pragma warning disable CS1591 +namespace MediaBrowser.Model.Branding; -namespace MediaBrowser.Model.Branding +/// +/// The branding options. +/// +public class BrandingOptions { - public class BrandingOptions - { - /// - /// Gets or sets the login disclaimer. - /// - /// The login disclaimer. - public string? LoginDisclaimer { get; set; } + /// + /// Gets or sets the login disclaimer. + /// + /// The login disclaimer. + public string? LoginDisclaimer { get; set; } - /// - /// Gets or sets the custom CSS. - /// - /// The custom CSS. - public string? CustomCss { get; set; } + /// + /// Gets or sets the custom CSS. + /// + /// The custom CSS. + public string? CustomCss { get; set; } - /// - /// Gets or sets the splashscreen location on disk. - /// - /// - /// Not served via the API. - /// Only used to save the custom uploaded user splashscreen in the configuration file. - /// - [JsonIgnore] - public string? SplashscreenLocation { get; set; } + /// + /// Gets or sets the splashscreen location on disk. + /// + /// + /// Not served via the API. + /// Only used to save the custom uploaded user splashscreen in the configuration file. + /// + [JsonIgnore] + public string? SplashscreenLocation { get; set; } - /// - /// Gets the splashscreen url. - /// - [XmlIgnore] - public string? SplashscreenUrl => "/Branding/Splashscreen"; - } + /// + /// Gets the splashscreen url. + /// + [XmlIgnore] + public string SplashscreenUrl => "/Branding/Splashscreen"; } -- cgit v1.2.3 From ecb73168b34e3d58dff186b6d90fb4bdd192e24a Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Mon, 10 Jan 2022 08:25:46 -0700 Subject: Suggestions from review --- Emby.Drawing/NullImageEncoder.cs | 6 ++ .../Library/SplashscreenPostScanTask.cs | 79 +++++++++++++++++++++ .../Tasks/RefreshMediaLibraryTask.cs | 17 +---- Jellyfin.Api/Controllers/ImageController.cs | 21 +++--- Jellyfin.Drawing.Skia/DefaultImageGenerator.cs | 82 ---------------------- Jellyfin.Drawing.Skia/SkiaEncoder.cs | 8 +++ Jellyfin.Drawing.Skia/SplashscreenBuilder.cs | 14 ++-- Jellyfin.Server/CoreAppHost.cs | 3 - MediaBrowser.Controller/Drawing/IImageEncoder.cs | 7 ++ MediaBrowser.Controller/Drawing/IImageGenerator.cs | 22 ------ 10 files changed, 119 insertions(+), 140 deletions(-) create mode 100644 Emby.Server.Implementations/Library/SplashscreenPostScanTask.cs delete mode 100644 Jellyfin.Drawing.Skia/DefaultImageGenerator.cs delete mode 100644 MediaBrowser.Controller/Drawing/IImageGenerator.cs (limited to 'Jellyfin.Api/Controllers/ImageController.cs') diff --git a/Emby.Drawing/NullImageEncoder.cs b/Emby.Drawing/NullImageEncoder.cs index 1c05aa916..d0a26b713 100644 --- a/Emby.Drawing/NullImageEncoder.cs +++ b/Emby.Drawing/NullImageEncoder.cs @@ -43,6 +43,12 @@ namespace Emby.Drawing throw new NotImplementedException(); } + /// + public void CreateSplashscreen(IReadOnlyList posters, IReadOnlyList backdrops) + { + throw new NotImplementedException(); + } + /// public string GetImageBlurHash(int xComp, int yComp, string path) { diff --git a/Emby.Server.Implementations/Library/SplashscreenPostScanTask.cs b/Emby.Server.Implementations/Library/SplashscreenPostScanTask.cs new file mode 100644 index 000000000..65445d3af --- /dev/null +++ b/Emby.Server.Implementations/Library/SplashscreenPostScanTask.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Jellyfin.Data.Enums; +using MediaBrowser.Controller.Drawing; +using MediaBrowser.Controller.Dto; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Persistence; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Querying; +using Microsoft.Extensions.Logging; + +namespace Emby.Server.Implementations.Library; + +/// +/// The splashscreen post scan task. +/// +public class SplashscreenPostScanTask : ILibraryPostScanTask +{ + private readonly IItemRepository _itemRepository; + private readonly IImageEncoder _imageEncoder; + private readonly ILogger _logger; + + /// + /// Initializes a new instance of the class. + /// + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. + public SplashscreenPostScanTask( + IItemRepository itemRepository, + IImageEncoder imageEncoder, + ILogger logger) + { + _itemRepository = itemRepository; + _imageEncoder = imageEncoder; + _logger = logger; + } + + /// + public Task Run(IProgress progress, CancellationToken cancellationToken) + { + var posters = GetItemsWithImageType(ImageType.Primary).Select(x => x.GetImages(ImageType.Primary).First().Path).ToList(); + var backdrops = GetItemsWithImageType(ImageType.Thumb).Select(x => x.GetImages(ImageType.Thumb).First().Path).ToList(); + if (backdrops.Count == 0) + { + // Thumb images fit better because they include the title in the image but are not provided with TMDb. + // Using backdrops as a fallback to generate an image at all + _logger.LogDebug("No thumb images found. Using backdrops to generate splashscreen"); + backdrops = GetItemsWithImageType(ImageType.Backdrop).Select(x => x.GetImages(ImageType.Backdrop).First().Path).ToList(); + } + + _imageEncoder.CreateSplashscreen(posters, backdrops); + return Task.CompletedTask; + } + + private IReadOnlyList GetItemsWithImageType(ImageType imageType) + { + // TODO make included libraries configurable + return _itemRepository.GetItemList(new InternalItemsQuery + { + CollapseBoxSetItems = false, + Recursive = true, + DtoOptions = new DtoOptions(false), + ImageTypes = new[] { imageType }, + Limit = 30, + // TODO max parental rating configurable + MaxParentalRating = 10, + OrderBy = new ValueTuple[] + { + new(ItemSortBy.Random, SortOrder.Ascending) + }, + IncludeItemTypes = new[] { BaseItemKind.Movie, BaseItemKind.Series } + }); + } +} diff --git a/Emby.Server.Implementations/ScheduledTasks/Tasks/RefreshMediaLibraryTask.cs b/Emby.Server.Implementations/ScheduledTasks/Tasks/RefreshMediaLibraryTask.cs index c12fc2113..7c27ae384 100644 --- a/Emby.Server.Implementations/ScheduledTasks/Tasks/RefreshMediaLibraryTask.cs +++ b/Emby.Server.Implementations/ScheduledTasks/Tasks/RefreshMediaLibraryTask.cs @@ -2,12 +2,9 @@ using System; using System.Collections.Generic; -using System.IO; using System.Threading; using System.Threading.Tasks; using Emby.Server.Implementations.Library; -using MediaBrowser.Common.Configuration; -using MediaBrowser.Controller.Drawing; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Globalization; using MediaBrowser.Model.Tasks; @@ -24,26 +21,16 @@ namespace Emby.Server.Implementations.ScheduledTasks.Tasks /// private readonly ILibraryManager _libraryManager; private readonly ILocalizationManager _localization; - private readonly IImageGenerator _imageGenerator; - private readonly IApplicationPaths _applicationPaths; /// /// Initializes a new instance of the class. /// /// Instance of the interface. /// Instance of the interface. - /// Instance of the interface. - /// Instance of the interface. - public RefreshMediaLibraryTask( - ILibraryManager libraryManager, - ILocalizationManager localization, - IImageGenerator imageGenerator, - IApplicationPaths applicationPaths) + public RefreshMediaLibraryTask(ILibraryManager libraryManager, ILocalizationManager localization) { _libraryManager = libraryManager; _localization = localization; - _imageGenerator = imageGenerator; - _applicationPaths = applicationPaths; } /// @@ -83,8 +70,6 @@ namespace Emby.Server.Implementations.ScheduledTasks.Tasks progress.Report(0); - _imageGenerator.Generate(GeneratedImageType.Splashscreen, Path.Combine(_applicationPaths.DataPath, "splashscreen.webp")); - return ((LibraryManager)_libraryManager).ValidateMediaLibraryInternal(progress, cancellationToken); } } diff --git a/Jellyfin.Api/Controllers/ImageController.cs b/Jellyfin.Api/Controllers/ImageController.cs index 6d34ca770..b44a21d03 100644 --- a/Jellyfin.Api/Controllers/ImageController.cs +++ b/Jellyfin.Api/Controllers/ImageController.cs @@ -48,7 +48,7 @@ namespace Jellyfin.Api.Controllers private readonly ILogger _logger; private readonly IServerConfigurationManager _serverConfigurationManager; private readonly IApplicationPaths _appPaths; - private readonly IImageGenerator _imageGenerator; + private readonly IImageEncoder _imageEncoder; /// /// Initializes a new instance of the class. @@ -62,7 +62,7 @@ namespace Jellyfin.Api.Controllers /// Instance of the interface. /// Instance of the interface. /// Instance of the interface. - /// Instance of the interface. + /// Instance of the interface. public ImageController( IUserManager userManager, ILibraryManager libraryManager, @@ -73,7 +73,7 @@ namespace Jellyfin.Api.Controllers ILogger logger, IServerConfigurationManager serverConfigurationManager, IApplicationPaths appPaths, - IImageGenerator imageGenerator) + IImageEncoder imageEncoder) { _userManager = userManager; _libraryManager = libraryManager; @@ -84,7 +84,7 @@ namespace Jellyfin.Api.Controllers _logger = logger; _serverConfigurationManager = serverConfigurationManager; _appPaths = appPaths; - _imageGenerator = imageGenerator; + _imageEncoder = imageEncoder; } /// @@ -1737,19 +1737,20 @@ namespace Jellyfin.Api.Controllers [FromQuery] string? foregroundLayer, [FromQuery, Range(0, 100)] int quality = 90) { - string splashscreenPath; var brandingOptions = _serverConfigurationManager.GetConfiguration("branding"); - if (!string.IsNullOrWhiteSpace(brandingOptions.SplashscreenLocation)) + string splashscreenPath; + + if (!string.IsNullOrWhiteSpace(brandingOptions.SplashscreenLocation) + && System.IO.File.Exists(brandingOptions.SplashscreenLocation)) { - splashscreenPath = brandingOptions.SplashscreenLocation!; + splashscreenPath = brandingOptions.SplashscreenLocation; } else { splashscreenPath = Path.Combine(_appPaths.DataPath, "splashscreen.webp"); - - if (!System.IO.File.Exists(splashscreenPath) && _imageGenerator.GetSupportedImages().Contains(GeneratedImageType.Splashscreen)) + if (!System.IO.File.Exists(splashscreenPath)) { - _imageGenerator.Generate(GeneratedImageType.Splashscreen, splashscreenPath); + return NotFound(); } } diff --git a/Jellyfin.Drawing.Skia/DefaultImageGenerator.cs b/Jellyfin.Drawing.Skia/DefaultImageGenerator.cs deleted file mode 100644 index e102b8f49..000000000 --- a/Jellyfin.Drawing.Skia/DefaultImageGenerator.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Jellyfin.Data.Enums; -using MediaBrowser.Controller.Drawing; -using MediaBrowser.Controller.Dto; -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Persistence; -using MediaBrowser.Model.Entities; -using MediaBrowser.Model.Querying; -using Microsoft.Extensions.Logging; - -namespace Jellyfin.Drawing.Skia; - -/// -/// The default image generator. -/// -public class DefaultImageGenerator : IImageGenerator -{ - private readonly IImageEncoder _imageEncoder; - private readonly IItemRepository _itemRepository; - private readonly ILogger _logger; - - /// - /// Initializes a new instance of the class. - /// - /// Instance of the interface. - /// Instance of the interface. - /// Instance of the interface. - public DefaultImageGenerator( - IImageEncoder imageEncoder, - IItemRepository itemRepository, - ILogger logger) - { - _imageEncoder = imageEncoder; - _itemRepository = itemRepository; - _logger = logger; - } - - /// - public IReadOnlyList GetSupportedImages() - { - return new[] { GeneratedImageType.Splashscreen }; - } - - /// - public void Generate(GeneratedImageType imageTypeType, string outputPath) - { - var posters = GetItemsWithImageType(ImageType.Primary).Select(x => x.GetImages(ImageType.Primary).First().Path).ToList(); - var landscape = GetItemsWithImageType(ImageType.Thumb).Select(x => x.GetImages(ImageType.Thumb).First().Path).ToList(); - if (landscape.Count == 0) - { - // Thumb images fit better because they include the title in the image but are not provided with TMDb. - // Using backdrops as a fallback to generate an image at all - _logger.LogDebug("No thumb images found. Using backdrops to generate splashscreen"); - landscape = GetItemsWithImageType(ImageType.Backdrop).Select(x => x.GetImages(ImageType.Backdrop).First().Path).ToList(); - } - - var splashBuilder = new SplashscreenBuilder((SkiaEncoder)_imageEncoder); - splashBuilder.GenerateSplash(posters, landscape, outputPath); - } - - private IReadOnlyList GetItemsWithImageType(ImageType imageType) - { - // todo make included libraries configurable - return _itemRepository.GetItemList(new InternalItemsQuery - { - CollapseBoxSetItems = false, - Recursive = true, - DtoOptions = new DtoOptions(false), - ImageTypes = new[] { imageType }, - Limit = 30, - // todo max parental rating configurable - MaxParentalRating = 10, - OrderBy = new ValueTuple[] - { - new(ItemSortBy.Random, SortOrder.Ascending) - }, - IncludeItemTypes = new[] { BaseItemKind.Movie, BaseItemKind.Series } - }); - } -} diff --git a/Jellyfin.Drawing.Skia/SkiaEncoder.cs b/Jellyfin.Drawing.Skia/SkiaEncoder.cs index 6d0a5ac2b..c40103f82 100644 --- a/Jellyfin.Drawing.Skia/SkiaEncoder.cs +++ b/Jellyfin.Drawing.Skia/SkiaEncoder.cs @@ -492,6 +492,14 @@ namespace Jellyfin.Drawing.Skia } } + /// + public void CreateSplashscreen(IReadOnlyList posters, IReadOnlyList backdrops) + { + var splashBuilder = new SplashscreenBuilder(this); + var outputPath = Path.Combine(_appPaths.DataPath, "splashscreen.webp"); + splashBuilder.GenerateSplash(posters, backdrops, outputPath); + } + private void DrawIndicator(SKCanvas canvas, int imageWidth, int imageHeight, ImageProcessingOptions options) { try diff --git a/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs b/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs index 9f801c320..132c35e67 100644 --- a/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs +++ b/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs @@ -32,12 +32,12 @@ namespace Jellyfin.Drawing.Skia /// Generate a splashscreen. /// /// The poster paths. - /// The landscape paths. + /// The landscape paths. /// The output path. - public void GenerateSplash(IReadOnlyList posters, IReadOnlyList backdrop, string outputPath) + public void GenerateSplash(IReadOnlyList posters, IReadOnlyList backdrops, string outputPath) { - var wall = GenerateCollage(posters, backdrop); - var transformed = Transform3D(wall); + using var wall = GenerateCollage(posters, backdrops); + using var transformed = Transform3D(wall); using var outputStream = new SKFileWStream(outputPath); using var pixmap = new SKPixmap(new SKImageInfo(FinalWidth, FinalHeight), transformed.GetPixels()); @@ -48,9 +48,9 @@ namespace Jellyfin.Drawing.Skia /// Generates a collage of posters and landscape pictures. /// /// The poster paths. - /// The landscape paths. + /// The landscape paths. /// The created collage as a bitmap. - private SKBitmap GenerateCollage(IReadOnlyList posters, IReadOnlyList backdrop) + private SKBitmap GenerateCollage(IReadOnlyList posters, IReadOnlyList backdrops) { var random = new Random(); @@ -82,7 +82,7 @@ namespace Jellyfin.Drawing.Skia posterIndex = newPosterIndex; break; default: - currentImage = SkiaHelper.GetNextValidImage(_skiaEncoder, backdrop, backdropIndex, out int newBackdropIndex); + currentImage = SkiaHelper.GetNextValidImage(_skiaEncoder, backdrops, backdropIndex, out int newBackdropIndex); backdropIndex = newBackdropIndex; break; } diff --git a/Jellyfin.Server/CoreAppHost.cs b/Jellyfin.Server/CoreAppHost.cs index b2f9a518a..67e50b92d 100644 --- a/Jellyfin.Server/CoreAppHost.cs +++ b/Jellyfin.Server/CoreAppHost.cs @@ -85,9 +85,6 @@ namespace Jellyfin.Server serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); - // TODO search plugins - serviceCollection.AddSingleton(); - // TODO search the assemblies instead of adding them manually? serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); diff --git a/MediaBrowser.Controller/Drawing/IImageEncoder.cs b/MediaBrowser.Controller/Drawing/IImageEncoder.cs index 4e67cfee4..e5c8ebfaf 100644 --- a/MediaBrowser.Controller/Drawing/IImageEncoder.cs +++ b/MediaBrowser.Controller/Drawing/IImageEncoder.cs @@ -74,5 +74,12 @@ namespace MediaBrowser.Controller.Drawing /// The options to use when creating the collage. /// Optional. void CreateImageCollage(ImageCollageOptions options, string? libraryName); + + /// + /// Creates a new splashscreen image. + /// + /// The list of poster paths. + /// The list of backdrop paths. + void CreateSplashscreen(IReadOnlyList posters, IReadOnlyList backdrops); } } diff --git a/MediaBrowser.Controller/Drawing/IImageGenerator.cs b/MediaBrowser.Controller/Drawing/IImageGenerator.cs deleted file mode 100644 index 773db02cb..000000000 --- a/MediaBrowser.Controller/Drawing/IImageGenerator.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Collections.Generic; - -namespace MediaBrowser.Controller.Drawing; - -/// -/// Interface for an image generator. -/// -public interface IImageGenerator -{ - /// - /// Gets the supported generated images of the image generator. - /// - /// The supported generated image types. - IReadOnlyList GetSupportedImages(); - - /// - /// Generates a splashscreen. - /// - /// The image to generate. - /// The path where the splashscreen should be saved. - void Generate(GeneratedImageType imageTypeType, string outputPath); -} -- cgit v1.2.3 From 0d335082c8cf541637f4bcae1dc0399f649d24ce Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Mon, 10 Jan 2022 10:59:32 -0700 Subject: suggestions from review --- Emby.Server.Implementations/Library/SplashscreenPostScanTask.cs | 4 ++-- Jellyfin.Api/Controllers/ImageController.cs | 2 +- Jellyfin.Drawing.Skia/SplashscreenBuilder.cs | 4 +--- MediaBrowser.Model/Branding/BrandingOptions.cs | 6 ------ 4 files changed, 4 insertions(+), 12 deletions(-) (limited to 'Jellyfin.Api/Controllers/ImageController.cs') diff --git a/Emby.Server.Implementations/Library/SplashscreenPostScanTask.cs b/Emby.Server.Implementations/Library/SplashscreenPostScanTask.cs index aed5711fa..320685b1f 100644 --- a/Emby.Server.Implementations/Library/SplashscreenPostScanTask.cs +++ b/Emby.Server.Implementations/Library/SplashscreenPostScanTask.cs @@ -69,9 +69,9 @@ public class SplashscreenPostScanTask : ILibraryPostScanTask Limit = 30, // TODO max parental rating configurable MaxParentalRating = 10, - OrderBy = new ValueTuple[] + OrderBy = new[] { - new(ItemSortBy.Random, SortOrder.Ascending) + (ItemSortBy.Random, SortOrder.Ascending) }, IncludeItemTypes = new[] { BaseItemKind.Movie, BaseItemKind.Series } }); diff --git a/Jellyfin.Api/Controllers/ImageController.cs b/Jellyfin.Api/Controllers/ImageController.cs index b44a21d03..7cc526d21 100644 --- a/Jellyfin.Api/Controllers/ImageController.cs +++ b/Jellyfin.Api/Controllers/ImageController.cs @@ -1793,7 +1793,7 @@ namespace Jellyfin.Api.Controllers /// Uploads a custom splashscreen. /// /// A indicating success. - /// Sucessfully uploaded new splashscreen. + /// Successfully uploaded new splashscreen. /// Error reading MimeType from uploaded image. /// User does not have permission to upload splashscreen.. /// Error reading the image format. diff --git a/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs b/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs index 132c35e67..e5fa6c2bd 100644 --- a/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs +++ b/Jellyfin.Drawing.Skia/SplashscreenBuilder.cs @@ -52,8 +52,6 @@ namespace Jellyfin.Drawing.Skia /// The created collage as a bitmap. private SKBitmap GenerateCollage(IReadOnlyList posters, IReadOnlyList backdrops) { - var random = new Random(); - var posterIndex = 0; var backdropIndex = 0; @@ -65,7 +63,7 @@ namespace Jellyfin.Drawing.Skia for (int i = 0; i < Rows; i++) { - int imageCounter = random.Next(0, 5); + int imageCounter = Random.Shared.Next(0, 5); int currentWidthPos = i * 75; int currentHeight = i * (posterHeight + Spacing); diff --git a/MediaBrowser.Model/Branding/BrandingOptions.cs b/MediaBrowser.Model/Branding/BrandingOptions.cs index 01db70885..cc42c1718 100644 --- a/MediaBrowser.Model/Branding/BrandingOptions.cs +++ b/MediaBrowser.Model/Branding/BrandingOptions.cs @@ -29,10 +29,4 @@ public class BrandingOptions /// [JsonIgnore] public string? SplashscreenLocation { get; set; } - - /// - /// Gets the splashscreen url. - /// - [XmlIgnore] - public string SplashscreenUrl => "/Branding/Splashscreen"; } -- cgit v1.2.3 From 8a36fe7ed51d1a1da7c629e10ff9eebf81cf9785 Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Mon, 10 Jan 2022 17:01:17 -0700 Subject: Use png for storage --- Jellyfin.Api/Controllers/ImageController.cs | 2 +- Jellyfin.Drawing.Skia/SkiaEncoder.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'Jellyfin.Api/Controllers/ImageController.cs') diff --git a/Jellyfin.Api/Controllers/ImageController.cs b/Jellyfin.Api/Controllers/ImageController.cs index 7cc526d21..5997ac549 100644 --- a/Jellyfin.Api/Controllers/ImageController.cs +++ b/Jellyfin.Api/Controllers/ImageController.cs @@ -1747,7 +1747,7 @@ namespace Jellyfin.Api.Controllers } else { - splashscreenPath = Path.Combine(_appPaths.DataPath, "splashscreen.webp"); + splashscreenPath = Path.Combine(_appPaths.DataPath, "splashscreen.png"); if (!System.IO.File.Exists(splashscreenPath)) { return NotFound(); diff --git a/Jellyfin.Drawing.Skia/SkiaEncoder.cs b/Jellyfin.Drawing.Skia/SkiaEncoder.cs index c40103f82..1fa8e570d 100644 --- a/Jellyfin.Drawing.Skia/SkiaEncoder.cs +++ b/Jellyfin.Drawing.Skia/SkiaEncoder.cs @@ -496,7 +496,7 @@ namespace Jellyfin.Drawing.Skia public void CreateSplashscreen(IReadOnlyList posters, IReadOnlyList backdrops) { var splashBuilder = new SplashscreenBuilder(this); - var outputPath = Path.Combine(_appPaths.DataPath, "splashscreen.webp"); + var outputPath = Path.Combine(_appPaths.DataPath, "splashscreen.png"); splashBuilder.GenerateSplash(posters, backdrops, outputPath); } -- cgit v1.2.3