SplashscreenController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Jellyfin.Api.Attributes;
  6. using Jellyfin.Data.Enums;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Controller.Drawing;
  9. using MediaBrowser.Controller.Dto;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Persistence;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.Net;
  14. using MediaBrowser.Model.Querying;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Mvc;
  17. using Microsoft.Extensions.Logging;
  18. namespace Jellyfin.Api.Controllers
  19. {
  20. /// <summary>
  21. /// Splashscreen controller.
  22. /// </summary>
  23. [Route("Splashscreen")]
  24. public class SplashscreenController : BaseJellyfinApiController
  25. {
  26. private readonly IImageEncoder _imageEncoder;
  27. private readonly IItemRepository _itemRepository;
  28. private readonly IApplicationPaths _appPaths;
  29. private readonly ILogger _logger;
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="SplashscreenController"/> class.
  32. /// </summary>
  33. /// <param name="imageEncoder">Instance of the <see cref="IImageEncoder"/> interface.</param>
  34. /// <param name="itemRepository">Instance of the <see cref="IItemRepository"/> interface.</param>
  35. /// <param name="applicationPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
  36. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  37. public SplashscreenController(
  38. IImageEncoder imageEncoder,
  39. IItemRepository itemRepository,
  40. IApplicationPaths applicationPaths,
  41. ILogger<SplashscreenController> logger)
  42. {
  43. _imageEncoder = imageEncoder;
  44. _itemRepository = itemRepository;
  45. _appPaths = applicationPaths;
  46. _logger = logger;
  47. }
  48. /// <summary>
  49. /// Generates or gets the splashscreen.
  50. /// </summary>
  51. /// <param name="darken">Darken the generated image.</param>
  52. /// <param name="width">The image width.</param>
  53. /// <param name="height">The image height.</param>
  54. /// <param name="regenerate">Whether to regenerate the image, regardless if one already exists.</param>
  55. /// <returns>The splashscreen.</returns>
  56. [HttpGet]
  57. [ProducesResponseType(StatusCodes.Status200OK)]
  58. [ProducesResponseType(StatusCodes.Status500InternalServerError)]
  59. [ProducesImageFile]
  60. public ActionResult GetSplashscreen(
  61. [FromQuery] bool? darken = false,
  62. [FromQuery] int? width = 1920,
  63. [FromQuery] int? height = 1080,
  64. [FromQuery] bool? regenerate = false)
  65. {
  66. var outputPath = Path.Combine(_appPaths.DataPath, $"splashscreen-{width}x{height}-{darken}.jpg");
  67. if (!System.IO.File.Exists(outputPath) || (regenerate ?? false))
  68. {
  69. var posters = GetItemsWithImageType(ImageType.Primary).Select(x => x.GetImages(ImageType.Primary).First().Path).ToList();
  70. var landscape = GetItemsWithImageType(ImageType.Thumb).Select(x => x.GetImages(ImageType.Thumb).First().Path).ToList();
  71. if (landscape.Count == 0)
  72. {
  73. _logger.LogDebug("No thumb images found. Using backdrops to generate splashscreen.");
  74. landscape = GetItemsWithImageType(ImageType.Backdrop).Select(x => x.GetImages(ImageType.Backdrop).First().Path).ToList();
  75. }
  76. _imageEncoder.CreateSplashscreen(new SplashscreenOptions(posters, landscape, outputPath, width!.Value, height!.Value, darken!.Value));
  77. }
  78. return PhysicalFile(outputPath, MimeTypes.GetMimeType(outputPath));
  79. }
  80. private IReadOnlyList<BaseItem> GetItemsWithImageType(ImageType imageType)
  81. {
  82. return _itemRepository.GetItemList(new InternalItemsQuery
  83. {
  84. CollapseBoxSetItems = false,
  85. Recursive = true,
  86. DtoOptions = new DtoOptions(false),
  87. ImageTypes = new ImageType[] { imageType },
  88. Limit = 8,
  89. OrderBy = new ValueTuple<string, SortOrder>[]
  90. {
  91. new ValueTuple<string, SortOrder>(ItemSortBy.Random, SortOrder.Ascending)
  92. },
  93. IncludeItemTypes = new string[] { "Movie", "Series" }
  94. });
  95. }
  96. }
  97. }