| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Threading; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Jellyfin.Data.Enums; |
| | 7 | | using Jellyfin.Database.Implementations.Enums; |
| | 8 | | using MediaBrowser.Controller.Drawing; |
| | 9 | | using MediaBrowser.Controller.Dto; |
| | 10 | | using MediaBrowser.Controller.Entities; |
| | 11 | | using MediaBrowser.Controller.Library; |
| | 12 | | using MediaBrowser.Controller.Persistence; |
| | 13 | | using MediaBrowser.Model.Entities; |
| | 14 | | using Microsoft.Extensions.Logging; |
| | 15 | |
|
| | 16 | | namespace Emby.Server.Implementations.Library; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// The splashscreen post scan task. |
| | 20 | | /// </summary> |
| | 21 | | public class SplashscreenPostScanTask : ILibraryPostScanTask |
| | 22 | | { |
| | 23 | | private readonly IItemRepository _itemRepository; |
| | 24 | | private readonly IImageEncoder _imageEncoder; |
| | 25 | | private readonly ILogger<SplashscreenPostScanTask> _logger; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Initializes a new instance of the <see cref="SplashscreenPostScanTask"/> class. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="itemRepository">Instance of the <see cref="IItemRepository"/> interface.</param> |
| | 31 | | /// <param name="imageEncoder">Instance of the <see cref="IImageEncoder"/> interface.</param> |
| | 32 | | /// <param name="logger">Instance of the <see cref="ILogger{SplashscreenPostScanTask}"/> interface.</param> |
| | 33 | | public SplashscreenPostScanTask( |
| | 34 | | IItemRepository itemRepository, |
| | 35 | | IImageEncoder imageEncoder, |
| | 36 | | ILogger<SplashscreenPostScanTask> logger) |
| | 37 | | { |
| 21 | 38 | | _itemRepository = itemRepository; |
| 21 | 39 | | _imageEncoder = imageEncoder; |
| 21 | 40 | | _logger = logger; |
| 21 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <inheritdoc /> |
| | 44 | | public Task Run(IProgress<double> progress, CancellationToken cancellationToken) |
| | 45 | | { |
| 10 | 46 | | var posters = GetItemsWithImageType(ImageType.Primary) |
| 10 | 47 | | .Select(x => x.GetImages(ImageType.Primary).FirstOrDefault()?.Path) |
| 10 | 48 | | .Where(path => !string.IsNullOrEmpty(path)) |
| 10 | 49 | | .Select(path => path!) |
| 10 | 50 | | .ToList(); |
| 10 | 51 | | var backdrops = GetItemsWithImageType(ImageType.Thumb) |
| 10 | 52 | | .Select(x => x.GetImages(ImageType.Thumb).FirstOrDefault()?.Path) |
| 10 | 53 | | .Where(path => !string.IsNullOrEmpty(path)) |
| 10 | 54 | | .Select(path => path!) |
| 10 | 55 | | .ToList(); |
| 10 | 56 | | if (backdrops.Count == 0) |
| | 57 | | { |
| | 58 | | // Thumb images fit better because they include the title in the image but are not provided with TMDb. |
| | 59 | | // Using backdrops as a fallback to generate an image at all |
| 10 | 60 | | _logger.LogDebug("No thumb images found. Using backdrops to generate splashscreen"); |
| 10 | 61 | | backdrops = GetItemsWithImageType(ImageType.Backdrop) |
| 10 | 62 | | .Select(x => x.GetImages(ImageType.Backdrop).FirstOrDefault()?.Path) |
| 10 | 63 | | .Where(path => !string.IsNullOrEmpty(path)) |
| 10 | 64 | | .Select(path => path!) |
| 10 | 65 | | .ToList(); |
| | 66 | | } |
| | 67 | |
|
| 10 | 68 | | _imageEncoder.CreateSplashscreen(posters, backdrops); |
| 10 | 69 | | return Task.CompletedTask; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | private IReadOnlyList<BaseItem> GetItemsWithImageType(ImageType imageType) |
| | 73 | | { |
| | 74 | | // TODO make included libraries configurable |
| 30 | 75 | | return _itemRepository.GetItemList(new InternalItemsQuery |
| 30 | 76 | | { |
| 30 | 77 | | CollapseBoxSetItems = false, |
| 30 | 78 | | Recursive = true, |
| 30 | 79 | | DtoOptions = new DtoOptions(false), |
| 30 | 80 | | ImageTypes = [imageType], |
| 30 | 81 | | Limit = 30, |
| 30 | 82 | | // TODO max parental rating configurable |
| 30 | 83 | | MaxParentalRating = new(10, null), |
| 30 | 84 | | OrderBy = |
| 30 | 85 | | [ |
| 30 | 86 | | (ItemSortBy.Random, SortOrder.Ascending) |
| 30 | 87 | | ], |
| 30 | 88 | | IncludeItemTypes = [BaseItemKind.Movie, BaseItemKind.Series] |
| 30 | 89 | | }); |
| | 90 | | } |
| | 91 | | } |