jellyfin/Emby.Server.Implementations/Dto/DtoService.cs

1428 lines
50 KiB
C#
Raw Normal View History

#nullable disable
2019-11-01 18:38:54 +01:00
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
2020-05-20 19:07:53 +02:00
using Jellyfin.Data.Entities;
2020-05-13 04:10:35 +02:00
using Jellyfin.Data.Enums;
2021-12-20 13:31:07 +01:00
using Jellyfin.Extensions;
using MediaBrowser.Common;
2014-05-17 23:23:48 +02:00
using MediaBrowser.Controller.Channels;
2013-09-18 20:49:06 +02:00
using MediaBrowser.Controller.Drawing;
2013-09-04 19:02:19 +02:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Playlists;
2014-02-21 19:48:15 +01:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Trickplay;
using MediaBrowser.Model.Dto;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using Microsoft.Extensions.Logging;
2020-05-20 19:07:53 +02:00
using Book = MediaBrowser.Controller.Entities.Book;
using Episode = MediaBrowser.Controller.Entities.TV.Episode;
using Movie = MediaBrowser.Controller.Entities.Movies.Movie;
using MusicAlbum = MediaBrowser.Controller.Entities.Audio.MusicAlbum;
using Person = MediaBrowser.Controller.Entities.Person;
using Photo = MediaBrowser.Controller.Entities.Photo;
using Season = MediaBrowser.Controller.Entities.TV.Season;
using Series = MediaBrowser.Controller.Entities.TV.Series;
2013-02-21 02:33:05 +01:00
namespace Emby.Server.Implementations.Dto
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
public class DtoService : IDtoService
2013-02-21 02:33:05 +01:00
{
2020-06-06 02:15:56 +02:00
private readonly ILogger<DtoService> _logger;
private readonly ILibraryManager _libraryManager;
2013-10-02 18:08:58 +02:00
private readonly IUserDataManager _userDataRepository;
2013-06-18 21:16:27 +02:00
private readonly IItemRepository _itemRepo;
2013-09-18 20:49:06 +02:00
private readonly IImageProcessor _imageProcessor;
2014-02-21 19:48:15 +01:00
private readonly IProviderManager _providerManager;
2024-02-09 19:46:28 +01:00
private readonly IRecordingsManager _recordingsManager;
2013-10-02 18:08:58 +02:00
2014-10-24 06:54:35 +02:00
private readonly IApplicationHost _appHost;
private readonly IMediaSourceManager _mediaSourceManager;
2020-04-04 20:56:50 +02:00
private readonly Lazy<ILiveTvManager> _livetvManagerFactory;
private readonly ITrickplayManager _trickplayManager;
public DtoService(
ILogger<DtoService> logger,
ILibraryManager libraryManager,
IUserDataManager userDataRepository,
IItemRepository itemRepo,
IImageProcessor imageProcessor,
IProviderManager providerManager,
2024-02-09 19:46:28 +01:00
IRecordingsManager recordingsManager,
IApplicationHost appHost,
IMediaSourceManager mediaSourceManager,
Lazy<ILiveTvManager> livetvManagerFactory,
ITrickplayManager trickplayManager)
{
_logger = logger;
_libraryManager = libraryManager;
_userDataRepository = userDataRepository;
2013-06-18 21:16:27 +02:00
_itemRepo = itemRepo;
2013-09-18 20:49:06 +02:00
_imageProcessor = imageProcessor;
2014-02-21 19:48:15 +01:00
_providerManager = providerManager;
2024-02-09 19:46:28 +01:00
_recordingsManager = recordingsManager;
2014-10-24 06:54:35 +02:00
_appHost = appHost;
2015-03-07 23:43:53 +01:00
_mediaSourceManager = mediaSourceManager;
2020-04-04 20:56:50 +02:00
_livetvManagerFactory = livetvManagerFactory;
_trickplayManager = trickplayManager;
}
2021-09-03 18:46:34 +02:00
private ILiveTvManager LivetvManager => _livetvManagerFactory.Value;
/// <inheritdoc />
2020-05-20 19:07:53 +02:00
public IReadOnlyList<BaseItemDto> GetBaseItemDtos(IReadOnlyList<BaseItem> items, DtoOptions options, User user = null, BaseItem owner = null)
2015-01-24 20:03:55 +01:00
{
var accessibleItems = user is null ? items : items.Where(x => x.IsVisible(user)).ToList();
var returnItems = new BaseItemDto[accessibleItems.Count];
2023-03-01 00:44:57 +01:00
List<(BaseItem, BaseItemDto)> programTuples = null;
List<(BaseItemDto, LiveTvChannel)> channelTuples = null;
2015-01-24 20:03:55 +01:00
for (int index = 0; index < accessibleItems.Count; index++)
2015-01-24 20:03:55 +01:00
{
var item = accessibleItems[index];
var dto = GetBaseItemDtoInternal(item, options, user, owner);
2015-01-24 20:03:55 +01:00
if (item is LiveTvChannel tvChannel)
2016-03-22 07:49:36 +01:00
{
2023-03-01 00:44:57 +01:00
(channelTuples ??= new()).Add((dto, tvChannel));
2016-03-22 07:49:36 +01:00
}
else if (item is LiveTvProgram)
2016-03-02 19:42:39 +01:00
{
2023-03-01 00:44:57 +01:00
(programTuples ??= new()).Add((item, dto));
2016-03-02 19:42:39 +01:00
}
if (item is IItemByName byName)
2015-01-24 20:03:55 +01:00
{
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.ItemCounts))
2015-03-26 05:44:24 +01:00
{
2016-05-11 04:21:28 +02:00
var libraryItems = byName.GetTaggedItems(new InternalItemsQuery(user)
{
2017-05-21 09:25:49 +02:00
Recursive = true,
DtoOptions = new DtoOptions(false)
{
EnableImages = false
}
2016-05-11 04:21:28 +02:00
});
2015-01-24 20:03:55 +01:00
2021-12-15 18:25:36 +01:00
SetItemByNameInfo(item, dto, libraryItems);
2015-03-26 05:44:24 +01:00
}
2015-01-24 20:03:55 +01:00
}
2017-08-19 21:43:35 +02:00
returnItems[index] = dto;
2015-01-24 20:03:55 +01:00
}
2023-03-01 00:44:57 +01:00
if (programTuples is not null)
2016-03-02 19:42:39 +01:00
{
LivetvManager.AddInfoToProgramDto(programTuples, options.Fields, user).GetAwaiter().GetResult();
2016-03-02 19:42:39 +01:00
}
2023-03-01 00:44:57 +01:00
if (channelTuples is not null)
2016-03-22 07:49:36 +01:00
{
LivetvManager.AddChannelInfo(channelTuples, options, user);
2016-03-22 07:49:36 +01:00
}
2017-08-19 21:43:35 +02:00
return returnItems;
2015-01-24 20:03:55 +01:00
}
2020-05-20 19:07:53 +02:00
public BaseItemDto GetBaseItemDto(BaseItem item, DtoOptions options, User user = null, BaseItem owner = null)
2014-11-30 20:01:33 +01:00
{
var dto = GetBaseItemDtoInternal(item, options, user, owner);
if (item is LiveTvChannel tvChannel)
2016-03-22 07:49:36 +01:00
{
2021-11-09 22:29:33 +01:00
LivetvManager.AddChannelInfo(new[] { (dto, tvChannel) }, options, user);
2016-03-22 07:49:36 +01:00
}
else if (item is LiveTvProgram)
2016-03-02 19:42:39 +01:00
{
2021-11-09 22:29:33 +01:00
LivetvManager.AddInfoToProgramDto(new[] { (item, dto) }, options.Fields, user).GetAwaiter().GetResult();
2016-03-02 19:42:39 +01:00
}
if (item is IItemByName itemByName
&& options.ContainsField(ItemFields.ItemCounts))
2014-06-28 21:35:30 +02:00
{
SetItemByNameInfo(
item,
dto,
GetTaggedItems(
itemByName,
user,
new DtoOptions(false)
{
EnableImages = false
2021-12-15 18:25:36 +01:00
}));
2014-06-28 21:35:30 +02:00
}
2014-07-28 00:01:29 +02:00
return dto;
2014-06-28 21:35:30 +02:00
}
2020-05-20 19:07:53 +02:00
private static IList<BaseItem> GetTaggedItems(IItemByName byName, User user, DtoOptions options)
2015-07-08 18:10:34 +02:00
{
return byName.GetTaggedItems(
new InternalItemsQuery(user)
{
Recursive = true,
DtoOptions = options
});
2015-01-24 20:03:55 +01:00
}
2020-05-20 19:07:53 +02:00
private BaseItemDto GetBaseItemDtoInternal(BaseItem item, DtoOptions options, User user = null, BaseItem owner = null)
2013-02-21 02:33:05 +01:00
{
2014-10-24 06:54:35 +02:00
var dto = new BaseItemDto
{
ServerId = _appHost.SystemId
};
2013-02-21 02:33:05 +01:00
2016-03-19 20:32:37 +01:00
if (item.SourceType == SourceType.Channel)
{
dto.SourceType = item.SourceType.ToString();
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.People))
2013-04-03 04:59:27 +02:00
{
AttachPeople(dto, item, user);
2013-04-03 04:59:27 +02:00
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.PrimaryImageAspectRatio))
2013-04-03 04:59:27 +02:00
{
try
{
2016-01-16 06:01:57 +01:00
AttachPrimaryImageAspectRatio(dto, item);
2013-04-03 04:59:27 +02:00
}
catch (Exception ex)
{
// Have to use a catch-all unfortunately because some .net image methods throw plain Exceptions
2020-08-31 22:20:19 +02:00
_logger.LogError(ex, "Error generating PrimaryImageAspectRatio for {ItemName}", item.Name);
2013-04-03 04:59:27 +02:00
}
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.DisplayPreferencesId))
{
dto.DisplayPreferencesId = item.DisplayPreferencesId.ToString("N", CultureInfo.InvariantCulture);
}
2022-12-05 15:01:13 +01:00
if (user is not null)
2013-06-17 22:35:43 +02:00
{
2017-05-26 08:48:54 +02:00
AttachUserSpecificInfo(dto, item, user, options);
2013-06-17 22:35:43 +02:00
}
2019-09-20 12:42:08 +02:00
if (item is IHasMediaSources
&& options.ContainsField(ItemFields.MediaSources))
{
dto.MediaSources = _mediaSourceManager.GetStaticMediaSources(item, true, user).ToArray();
2017-08-23 19:27:53 +02:00
2019-09-20 12:42:08 +02:00
NormalizeMediaSourceContainers(dto);
}
2015-04-04 04:16:42 +02:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.Studios))
{
AttachStudios(dto, item);
}
2014-11-30 20:01:33 +01:00
AttachBasicFields(dto, item, owner, options);
2013-02-21 02:33:05 +01:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.CanDelete))
2015-02-06 06:39:07 +01:00
{
2022-12-05 15:00:20 +01:00
dto.CanDelete = user is null
2015-02-06 06:39:07 +01:00
? item.CanDelete()
: item.CanDelete(user);
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.CanDownload))
2015-02-06 06:39:07 +01:00
{
2022-12-05 15:00:20 +01:00
dto.CanDownload = user is null
2015-02-06 06:39:07 +01:00
? item.CanDownload()
: item.CanDownload(user);
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.Etag))
2015-04-15 17:41:42 +02:00
{
dto.Etag = item.GetEtag(user);
}
2015-04-14 05:45:17 +02:00
2024-02-09 19:46:28 +01:00
var activeRecording = _recordingsManager.GetActiveRecordingInfo(item.Path);
2022-12-05 15:01:13 +01:00
if (activeRecording is not null)
2015-05-31 20:22:51 +02:00
{
2021-02-11 01:09:23 +01:00
dto.Type = BaseItemKind.Recording;
2018-09-12 19:26:21 +02:00
dto.CanDownload = false;
dto.RunTimeTicks = null;
2017-08-30 20:06:54 +02:00
2018-09-12 19:26:21 +02:00
if (!string.IsNullOrEmpty(dto.SeriesName))
{
dto.EpisodeTitle = dto.Name;
dto.Name = dto.SeriesName;
2017-08-23 21:45:52 +02:00
}
2020-06-15 23:43:52 +02:00
2024-02-09 19:46:28 +01:00
LivetvManager.AddInfoToRecordingDto(item, dto, activeRecording, user);
2015-05-31 20:22:51 +02:00
}
if (item is Audio audio)
{
dto.HasLyrics = audio.GetMediaStreams().Any(s => s.Type == MediaStreamType.Lyric);
}
2013-02-21 02:33:05 +01:00
return dto;
}
private static void NormalizeMediaSourceContainers(BaseItemDto dto)
2017-08-23 19:27:53 +02:00
{
foreach (var mediaSource in dto.MediaSources)
{
var container = mediaSource.Container;
2018-09-12 19:26:21 +02:00
if (string.IsNullOrEmpty(container))
2017-08-23 19:27:53 +02:00
{
continue;
}
2020-06-15 23:43:52 +02:00
var containers = container.Split(',');
2017-08-23 19:27:53 +02:00
if (containers.Length < 2)
{
continue;
}
var path = mediaSource.Path;
string fileExtensionContainer = null;
2018-09-12 19:26:21 +02:00
if (!string.IsNullOrEmpty(path))
2017-08-23 19:27:53 +02:00
{
path = Path.GetExtension(path);
2018-09-12 19:26:21 +02:00
if (!string.IsNullOrEmpty(path))
2017-08-23 19:27:53 +02:00
{
path = Path.GetExtension(path);
2018-09-12 19:26:21 +02:00
if (!string.IsNullOrEmpty(path))
2017-08-23 19:27:53 +02:00
{
path = path.TrimStart('.');
}
2021-12-20 13:31:07 +01:00
if (!string.IsNullOrEmpty(path) && containers.Contains(path, StringComparison.OrdinalIgnoreCase))
2017-08-23 19:27:53 +02:00
{
fileExtensionContainer = path;
}
}
}
mediaSource.Container = fileExtensionContainer ?? containers[0];
}
}
2020-05-20 19:07:53 +02:00
public BaseItemDto GetItemByNameDto(BaseItem item, DtoOptions options, List<BaseItem> taggedItems, User user = null)
{
var dto = GetBaseItemDtoInternal(item, options, user);
2014-06-28 21:35:30 +02:00
2022-12-05 15:01:13 +01:00
if (taggedItems is not null && options.ContainsField(ItemFields.ItemCounts))
2015-03-26 05:44:24 +01:00
{
2021-12-15 18:25:36 +01:00
SetItemByNameInfo(item, dto, taggedItems);
2015-03-26 05:44:24 +01:00
}
2014-06-28 21:35:30 +02:00
return dto;
}
2021-12-15 18:25:36 +01:00
private static void SetItemByNameInfo(BaseItem item, BaseItemDto dto, IList<BaseItem> taggedItems)
{
2016-08-13 22:54:29 +02:00
if (item is MusicArtist)
{
dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum);
dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo);
dto.SongCount = taggedItems.Count(i => i is Audio);
}
2016-08-13 22:54:29 +02:00
else if (item is MusicGenre)
{
dto.ArtistCount = taggedItems.Count(i => i is MusicArtist);
dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum);
dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo);
dto.SongCount = taggedItems.Count(i => i is Audio);
}
else
{
// This populates them all and covers Genre, Person, Studio, Year
2016-08-13 22:54:29 +02:00
dto.ArtistCount = taggedItems.Count(i => i is MusicArtist);
dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum);
dto.EpisodeCount = taggedItems.Count(i => i is Episode);
dto.MovieCount = taggedItems.Count(i => i is Movie);
2016-03-19 22:55:42 +01:00
dto.TrailerCount = taggedItems.Count(i => i is Trailer);
dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo);
dto.SeriesCount = taggedItems.Count(i => i is Series);
dto.ProgramCount = taggedItems.Count(i => i is LiveTvProgram);
dto.SongCount = taggedItems.Count(i => i is Audio);
}
2013-12-02 22:46:22 +01:00
dto.ChildCount = taggedItems.Count;
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Attaches the user specific info.
/// </summary>
2020-05-20 19:07:53 +02:00
private void AttachUserSpecificInfo(BaseItemDto dto, BaseItem item, User user, DtoOptions options)
2013-02-21 02:33:05 +01:00
{
if (item.IsFolder)
{
var folder = (Folder)item;
2013-07-25 21:17:44 +02:00
2018-09-12 19:26:21 +02:00
if (options.EnableUserData)
2016-08-17 21:28:43 +02:00
{
2018-09-12 19:26:21 +02:00
dto.UserData = _userDataRepository.GetUserDataDto(item, dto, user, options);
2016-08-17 21:28:43 +02:00
}
2016-06-15 20:56:37 +02:00
2016-08-16 19:08:37 +02:00
if (!dto.ChildCount.HasValue && item.SourceType == SourceType.Library)
2016-06-15 20:56:37 +02:00
{
2016-12-13 08:36:30 +01:00
// For these types we can try to optimize and assume these values will be equal
2018-09-12 19:26:21 +02:00
if (item is MusicAlbum || item is Season || item is Playlist)
2016-12-13 08:36:30 +01:00
{
dto.ChildCount = dto.RecursiveItemCount;
var folderChildCount = folder.LinkedChildren.Length;
// The default is an empty array, so we can't reliably use the count when it's empty
if (folderChildCount > 0)
{
dto.ChildCount ??= folderChildCount;
}
2016-12-13 08:36:30 +01:00
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.ChildCount))
2016-12-13 08:36:30 +01:00
{
2020-05-13 04:10:35 +02:00
dto.ChildCount ??= GetChildCount(folder, user);
2016-12-13 08:36:30 +01:00
}
2013-02-21 02:33:05 +01:00
}
2013-05-08 03:17:41 +02:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.CumulativeRunTimeTicks))
2016-05-09 05:13:38 +02:00
{
2016-05-10 20:13:26 +02:00
dto.CumulativeRunTimeTicks = item.RunTimeTicks;
2016-05-09 05:13:38 +02:00
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.DateLastMediaAdded))
2016-05-09 05:13:38 +02:00
{
dto.DateLastMediaAdded = folder.DateLastMediaAdded;
}
}
else
2013-07-16 19:18:32 +02:00
{
2018-09-12 19:26:21 +02:00
if (options.EnableUserData)
2016-08-17 21:28:43 +02:00
{
2017-05-26 08:48:54 +02:00
dto.UserData = _userDataRepository.GetUserDataDto(item, user);
2016-08-17 21:28:43 +02:00
}
2013-05-08 03:17:41 +02:00
}
2014-02-21 06:35:56 +01:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.PlayAccess))
{
dto.PlayAccess = item.GetPlayAccess(user);
}
2013-02-21 02:33:05 +01:00
}
2020-05-20 19:07:53 +02:00
private static int GetChildCount(Folder folder, User user)
{
2016-06-18 19:26:42 +02:00
// Right now this is too slow to calculate for top level folders on a per-user basis
// Just return something so that apps that are expecting a value won't think the folders are empty
if (folder is ICollectionFolder || folder is UserView)
{
return Random.Shared.Next(1, 10);
2016-06-18 19:26:42 +02:00
}
2016-06-16 15:24:12 +02:00
return folder.GetChildCount(user);
}
private static void SetBookProperties(BaseItemDto dto, Book item)
2013-09-04 19:02:19 +02:00
{
dto.SeriesName = item.SeriesName;
}
2020-06-15 23:43:52 +02:00
private static void SetPhotoProperties(BaseItemDto dto, Photo item)
{
dto.CameraMake = item.CameraMake;
dto.CameraModel = item.CameraModel;
dto.Software = item.Software;
dto.ExposureTime = item.ExposureTime;
dto.FocalLength = item.FocalLength;
dto.ImageOrientation = item.Orientation;
dto.Aperture = item.Aperture;
dto.ShutterSpeed = item.ShutterSpeed;
2014-08-30 16:26:29 +02:00
dto.Latitude = item.Latitude;
dto.Longitude = item.Longitude;
dto.Altitude = item.Altitude;
dto.IsoSpeedRating = item.IsoSpeedRating;
2014-10-12 17:18:26 +02:00
2016-10-11 08:46:59 +02:00
var album = item.AlbumEntity;
2014-08-30 16:26:29 +02:00
2022-12-05 15:01:13 +01:00
if (album is not null)
2014-08-30 16:26:29 +02:00
{
2014-09-03 04:30:24 +02:00
dto.Album = album.Name;
2018-09-12 19:26:21 +02:00
dto.AlbumId = album.Id;
2014-08-30 16:26:29 +02:00
}
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
private void SetMusicVideoProperties(BaseItemDto dto, MusicVideo item)
{
if (!string.IsNullOrEmpty(item.Album))
{
2017-05-21 09:25:49 +02:00
var parentAlbumIds = _libraryManager.GetItemIds(new InternalItemsQuery
2016-05-11 16:36:28 +02:00
{
2021-12-12 03:31:30 +01:00
IncludeItemTypes = new[] { BaseItemKind.MusicAlbum },
2017-05-21 09:25:49 +02:00
Name = item.Album,
Limit = 1
});
2017-05-21 09:25:49 +02:00
if (parentAlbumIds.Count > 0)
{
2018-09-12 19:26:21 +02:00
dto.AlbumId = parentAlbumIds[0];
}
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
dto.Album = item.Album;
}
2013-02-21 02:33:05 +01:00
private string[] GetImageTags(BaseItem item, List<ItemImageInfo> images)
2013-09-04 19:02:19 +02:00
{
2016-07-05 07:40:18 +02:00
return images
.Select(p => GetImageCacheTag(item, p))
2022-12-05 15:01:13 +01:00
.Where(i => i is not null)
.ToArray();
2014-02-07 21:30:41 +01:00
}
2014-05-08 22:09:53 +02:00
private string GetImageCacheTag(BaseItem item, ItemImageInfo image)
2013-09-04 19:02:19 +02:00
{
try
2013-02-21 02:33:05 +01:00
{
2014-02-07 21:30:41 +01:00
return _imageProcessor.GetImageCacheTag(item, image);
}
2014-04-13 19:27:13 +02:00
catch (Exception ex)
2013-02-21 02:33:05 +01:00
{
2021-11-09 13:14:31 +01:00
_logger.LogError(ex, "Error getting {ImageType} image info for {Path}", image.Type, image.Path);
2013-09-04 19:02:19 +02:00
return null;
2013-02-21 02:33:05 +01:00
}
}
/// <summary>
/// Attaches People DTO's to a DTOBaseItem.
2013-09-04 19:02:19 +02:00
/// </summary>
/// <param name="dto">The dto.</param>
/// <param name="item">The item.</param>
/// <param name="user">The requesting user.</param>
private void AttachPeople(BaseItemDto dto, BaseItem item, User user = null)
2013-09-04 19:02:19 +02:00
{
// Ordering by person type to ensure actors and artists are at the front.
// This is taking advantage of the fact that they both begin with A
// This should be improved in the future
2015-06-21 05:35:22 +02:00
var people = _libraryManager.GetPeople(item).OrderBy(i => i.SortOrder ?? int.MaxValue)
2014-06-30 15:28:38 +02:00
.ThenBy(i =>
{
if (i.IsType(PersonKind.Actor))
2014-06-30 15:28:38 +02:00
{
return 0;
}
2020-06-15 23:43:52 +02:00
if (i.IsType(PersonKind.GuestStar))
2014-06-30 15:28:38 +02:00
{
return 1;
}
2020-06-15 23:43:52 +02:00
if (i.IsType(PersonKind.Director))
2014-06-30 15:28:38 +02:00
{
return 2;
}
2020-06-15 23:43:52 +02:00
if (i.IsType(PersonKind.Writer))
2014-06-30 15:28:38 +02:00
{
return 3;
}
2020-06-15 23:43:52 +02:00
if (i.IsType(PersonKind.Producer))
2014-06-30 15:28:38 +02:00
{
return 4;
}
2020-06-15 23:43:52 +02:00
if (i.IsType(PersonKind.Composer))
2014-06-30 15:28:38 +02:00
{
return 4;
}
return 10;
})
.ToList();
2013-02-21 02:33:05 +01:00
2014-11-21 15:28:30 +01:00
var list = new List<BaseItemPerson>();
2013-02-21 02:33:05 +01:00
var dictionary = people.Select(p => p.Name)
2013-09-04 19:02:19 +02:00
.Distinct(StringComparer.OrdinalIgnoreCase).Select(c =>
{
try
{
return _libraryManager.GetPerson(c);
}
2014-08-25 05:54:45 +02:00
catch (Exception ex)
2013-09-04 19:02:19 +02:00
{
2018-12-20 13:39:58 +01:00
_logger.LogError(ex, "Error getting person {Name}", c);
return null;
}
2022-12-05 15:01:13 +01:00
}).Where(i => i is not null)
2023-04-06 19:30:17 +02:00
.Where(i => user is null || i.IsVisible(user))
2022-12-19 15:21:42 +01:00
.DistinctBy(x => x.Name, StringComparer.OrdinalIgnoreCase)
2013-09-04 19:02:19 +02:00
.ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < people.Count; i++)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
var person = people[i];
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
var baseItemPerson = new BaseItemPerson
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
Name = person.Name,
Role = person.Role,
Type = person.Type
};
2013-02-21 02:33:05 +01:00
if (dictionary.TryGetValue(person.Name, out Person entity))
2013-09-04 19:02:19 +02:00
{
baseItemPerson.PrimaryImageTag = GetTagAndFillBlurhash(dto, entity, ImageType.Primary);
2022-01-22 15:46:12 +01:00
baseItemPerson.Id = entity.Id;
2022-12-05 15:01:13 +01:00
if (dto.ImageBlurHashes is not null)
2021-01-01 20:35:03 +01:00
{
2021-01-05 15:00:48 +01:00
// Only add BlurHash for the person's image.
baseItemPerson.ImageBlurHashes = new Dictionary<ImageType, Dictionary<string, string>>();
foreach (var (imageType, blurHash) in dto.ImageBlurHashes)
2021-01-01 20:35:03 +01:00
{
2022-12-05 15:01:13 +01:00
if (blurHash is not null)
2021-01-01 20:35:03 +01:00
{
2021-01-05 15:00:48 +01:00
baseItemPerson.ImageBlurHashes[imageType] = new Dictionary<string, string>();
foreach (var (imageId, blurHashValue) in blurHash)
{
if (string.Equals(baseItemPerson.PrimaryImageTag, imageId, StringComparison.OrdinalIgnoreCase))
{
baseItemPerson.ImageBlurHashes[imageType][imageId] = blurHashValue;
}
}
2021-01-01 20:35:03 +01:00
}
}
}
2014-11-21 15:28:30 +01:00
list.Add(baseItemPerson);
2013-02-21 02:33:05 +01:00
}
}
2014-11-21 15:28:30 +01:00
2018-12-28 16:48:26 +01:00
dto.People = list.ToArray();
2013-09-04 19:02:19 +02:00
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
/// <summary>
/// Attaches the studios.
/// </summary>
/// <param name="dto">The dto.</param>
/// <param name="item">The item.</param>
private void AttachStudios(BaseItemDto dto, BaseItem item)
2013-09-04 19:02:19 +02:00
{
2017-05-18 23:05:47 +02:00
dto.Studios = item.Studios
2018-09-12 19:26:21 +02:00
.Where(i => !string.IsNullOrEmpty(i))
.Select(i => new NameGuidPair
{
2017-05-18 23:05:47 +02:00
Name = i,
2018-09-12 19:26:21 +02:00
Id = _libraryManager.GetStudioId(i)
2017-05-18 23:05:47 +02:00
})
.ToArray();
}
2013-07-08 21:31:45 +02:00
2017-05-18 23:05:47 +02:00
private void AttachGenreItems(BaseItemDto dto, BaseItem item)
{
dto.GenreItems = item.Genres
2018-09-12 19:26:21 +02:00
.Where(i => !string.IsNullOrEmpty(i))
.Select(i => new NameGuidPair
2013-07-08 21:31:45 +02:00
{
2017-05-18 23:05:47 +02:00
Name = i,
2017-06-18 09:11:55 +02:00
Id = GetGenreId(i, item)
2017-05-18 23:05:47 +02:00
})
.ToArray();
}
2013-09-04 19:02:19 +02:00
2018-09-12 19:26:21 +02:00
private Guid GetGenreId(string name, BaseItem owner)
2017-05-18 23:05:47 +02:00
{
if (owner is IHasMusicGenres)
{
2018-09-12 19:26:21 +02:00
return _libraryManager.GetMusicGenreId(name);
2017-05-18 23:05:47 +02:00
}
2013-07-08 21:31:45 +02:00
2018-09-12 19:26:21 +02:00
return _libraryManager.GetGenreId(name);
2014-10-12 17:18:26 +02:00
}
private string GetTagAndFillBlurhash(BaseItemDto dto, BaseItem item, ImageType imageType, int imageIndex = 0)
{
var image = item.GetImageInfo(imageType, imageIndex);
2022-12-05 15:01:13 +01:00
if (image is not null)
{
return GetTagAndFillBlurhash(dto, item, image);
}
return null;
}
private string GetTagAndFillBlurhash(BaseItemDto dto, BaseItem item, ItemImageInfo image)
{
var tag = GetImageCacheTag(item, image);
if (!string.IsNullOrEmpty(image.BlurHash))
{
dto.ImageBlurHashes ??= new Dictionary<ImageType, Dictionary<string, string>>();
if (!dto.ImageBlurHashes.TryGetValue(image.Type, out var value))
{
value = new Dictionary<string, string>();
dto.ImageBlurHashes[image.Type] = value;
}
value[tag] = image.BlurHash;
}
return tag;
}
private string[] GetTagsAndFillBlurhashes(BaseItemDto dto, BaseItem item, ImageType imageType, int limit)
{
return GetTagsAndFillBlurhashes(dto, item, imageType, item.GetImages(imageType).Take(limit).ToList());
}
private string[] GetTagsAndFillBlurhashes(BaseItemDto dto, BaseItem item, ImageType imageType, List<ItemImageInfo> images)
{
var tags = GetImageTags(item, images);
var hashes = new Dictionary<string, string>();
for (int i = 0; i < images.Count; i++)
{
var img = images[i];
if (!string.IsNullOrEmpty(img.BlurHash))
{
var tag = tags[i];
hashes[tag] = img.BlurHash;
}
}
if (hashes.Count > 0)
{
dto.ImageBlurHashes ??= new Dictionary<ImageType, Dictionary<string, string>>();
dto.ImageBlurHashes[imageType] = hashes;
}
return tags;
}
2013-09-04 19:02:19 +02:00
/// <summary>
/// Sets simple property values on a DTOBaseItem.
2013-09-04 19:02:19 +02:00
/// </summary>
/// <param name="dto">The dto.</param>
/// <param name="item">The item.</param>
/// <param name="owner">The owner.</param>
2014-11-30 20:01:33 +01:00
/// <param name="options">The options.</param>
private void AttachBasicFields(BaseItemDto dto, BaseItem item, BaseItem owner, DtoOptions options)
2013-09-04 19:02:19 +02:00
{
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.DateCreated))
2013-05-24 06:02:42 +02:00
{
2013-09-04 19:02:19 +02:00
dto.DateCreated = item.DateCreated;
2013-05-24 06:02:42 +02:00
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.Settings))
2013-09-04 19:02:19 +02:00
{
dto.LockedFields = item.LockedFields;
2014-03-22 17:16:43 +01:00
dto.LockData = item.IsLocked;
2014-03-24 18:54:45 +01:00
dto.ForcedSortName = item.ForcedSortName;
2013-07-16 18:03:28 +02:00
}
2013-02-21 02:33:05 +01:00
dto.Container = item.Container;
2013-09-04 19:02:19 +02:00
dto.EndDate = item.EndDate;
2013-04-25 00:34:38 +02:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.ExternalUrls))
2014-02-21 19:48:15 +01:00
{
dto.ExternalUrls = _providerManager.GetExternalUrls(item).ToArray();
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.Tags))
2013-09-04 19:02:19 +02:00
{
2016-06-02 19:43:29 +02:00
dto.Tags = item.Tags;
2013-09-04 19:02:19 +02:00
}
2013-07-12 21:56:40 +02:00
2022-02-17 08:15:26 +01:00
if (item is IHasAspectRatio hasAspectRatio)
{
dto.AspectRatio = hasAspectRatio.AspectRatio;
}
2013-08-31 01:55:17 +02:00
dto.ImageBlurHashes = new Dictionary<ImageType, Dictionary<string, string>>();
2014-11-30 20:01:33 +01:00
var backdropLimit = options.GetImageLimit(ImageType.Backdrop);
if (backdropLimit > 0)
{
dto.BackdropImageTags = GetTagsAndFillBlurhashes(dto, item, ImageType.Backdrop, backdropLimit);
2014-11-30 20:01:33 +01:00
}
2013-09-18 04:43:34 +02:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.Genres))
2013-08-31 01:55:17 +02:00
{
2013-09-04 19:02:19 +02:00
dto.Genres = item.Genres;
2017-05-18 23:05:47 +02:00
AttachGenreItems(dto, item);
}
2016-08-18 07:56:10 +02:00
if (options.EnableImages)
{
2016-08-18 07:56:10 +02:00
dto.ImageTags = new Dictionary<ImageType, string>();
2014-11-30 20:01:33 +01:00
2016-08-18 07:56:10 +02:00
// Prevent implicitly captured closure
var currentItem = item;
2020-05-19 13:56:52 +02:00
foreach (var image in currentItem.ImageInfos.Where(i => !currentItem.AllowsMultipleImages(i.Type)))
2016-08-18 07:56:10 +02:00
{
if (options.GetImageLimit(image.Type) > 0)
2014-11-30 20:01:33 +01:00
{
var tag = GetTagAndFillBlurhash(dto, item, image);
2016-08-18 07:56:10 +02:00
2022-12-05 15:01:13 +01:00
if (tag is not null)
2016-08-18 07:56:10 +02:00
{
dto.ImageTags[image.Type] = tag;
}
2014-11-30 20:01:33 +01:00
}
}
}
2018-09-12 19:26:21 +02:00
dto.Id = item.Id;
2013-09-04 19:02:19 +02:00
dto.IndexNumber = item.IndexNumber;
2016-07-05 07:40:18 +02:00
dto.ParentIndexNumber = item.ParentIndexNumber;
2016-08-17 21:28:43 +02:00
if (item.IsFolder)
{
dto.IsFolder = true;
}
else if (item is IHasMediaSources)
{
dto.IsFolder = false;
}
2013-09-04 19:02:19 +02:00
dto.MediaType = item.MediaType;
2021-08-29 00:32:50 +02:00
if (item is not LiveTvProgram)
{
dto.LocationType = item.LocationType;
}
2015-10-28 20:40:38 +01:00
dto.Audio = item.Audio;
2013-09-11 19:54:59 +02:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.Settings))
2016-10-08 07:57:38 +02:00
{
dto.PreferredMetadataCountryCode = item.PreferredMetadataCountryCode;
dto.PreferredMetadataLanguage = item.PreferredMetadataLanguage;
}
2016-07-04 22:11:30 +02:00
dto.CriticRating = item.CriticRating;
2013-11-06 17:06:16 +01:00
if (item is IHasDisplayOrder hasDisplayOrder)
{
dto.DisplayOrder = hasDisplayOrder.DisplayOrder;
}
if (item is IHasCollectionType hasCollectionType)
2014-06-05 04:32:40 +02:00
{
2018-09-12 19:26:21 +02:00
dto.CollectionType = hasCollectionType.CollectionType;
2014-06-05 04:32:40 +02:00
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.RemoteTrailers))
2013-02-21 02:33:05 +01:00
{
2018-09-12 19:26:21 +02:00
dto.RemoteTrailers = item.RemoteTrailers;
2013-09-04 19:02:19 +02:00
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
dto.Name = item.Name;
dto.OfficialRating = item.OfficialRating;
2013-03-09 06:15:51 +01:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.Overview))
2013-09-04 19:02:19 +02:00
{
dto.Overview = item.Overview;
2013-02-21 02:33:05 +01:00
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.OriginalTitle))
2016-04-20 07:21:40 +02:00
{
dto.OriginalTitle = item.OriginalTitle;
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.ParentId))
2013-07-25 21:17:44 +02:00
{
2018-09-12 19:26:21 +02:00
dto.ParentId = item.DisplayParentId;
2013-07-25 21:17:44 +02:00
}
2013-02-21 02:33:05 +01:00
2016-07-05 07:40:18 +02:00
AddInheritedImages(dto, item, options, owner);
2013-10-24 19:49:24 +02:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.Path))
2013-09-04 19:02:19 +02:00
{
2017-02-20 21:50:58 +01:00
dto.Path = GetMappedPath(item, owner);
2013-02-21 02:33:05 +01:00
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.EnableMediaSourceDisplay))
{
dto.EnableMediaSourceDisplay = item.EnableMediaSourceDisplay;
}
2013-09-04 19:02:19 +02:00
dto.PremiereDate = item.PremiereDate;
dto.ProductionYear = item.ProductionYear;
2013-05-04 05:13:28 +02:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.ProviderIds))
2013-09-04 19:02:19 +02:00
{
dto.ProviderIds = item.ProviderIds;
}
2013-05-04 05:13:28 +02:00
2013-09-04 19:02:19 +02:00
dto.RunTimeTicks = item.RunTimeTicks;
2013-05-04 05:13:28 +02:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.SortName))
2013-09-04 19:02:19 +02:00
{
dto.SortName = item.SortName;
}
2013-05-04 05:13:28 +02:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.CustomRating))
2013-09-04 19:02:19 +02:00
{
dto.CustomRating = item.CustomRating;
}
2013-05-04 05:13:28 +02:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.Taglines))
2013-09-04 19:02:19 +02:00
{
2018-09-12 19:26:21 +02:00
if (!string.IsNullOrEmpty(item.Tagline))
{
dto.Taglines = new string[] { item.Tagline };
}
dto.Taglines ??= Array.Empty<string>();
2013-09-04 19:02:19 +02:00
}
2013-05-04 05:13:28 +02:00
2021-02-11 01:09:23 +01:00
dto.Type = item.GetBaseItemKind();
if ((item.CommunityRating ?? 0) > 0)
{
dto.CommunityRating = item.CommunityRating;
}
2014-11-30 20:01:33 +01:00
2022-02-17 08:15:26 +01:00
if (item is ISupportsPlaceHolders supportsPlaceHolders && supportsPlaceHolders.IsPlaceHolder)
{
dto.IsPlaceHolder = supportsPlaceHolders.IsPlaceHolder;
}
2014-03-22 17:16:43 +01:00
dto.LUFS = item.LUFS;
2013-09-04 19:02:19 +02:00
// Add audio info
2022-02-17 08:15:26 +01:00
if (item is Audio audio)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.Album = audio.Album;
dto.ExtraType = audio.ExtraType;
2013-02-21 02:33:05 +01:00
2015-05-04 16:35:38 +02:00
var albumParent = audio.AlbumEntity;
2013-02-21 02:33:05 +01:00
2022-12-05 15:01:13 +01:00
if (albumParent is not null)
2013-09-04 19:02:19 +02:00
{
2018-09-12 19:26:21 +02:00
dto.AlbumId = albumParent.Id;
dto.AlbumPrimaryImageTag = GetTagAndFillBlurhash(dto, albumParent, ImageType.Primary);
2013-02-21 02:33:05 +01:00
}
2014-03-21 04:31:40 +01:00
2020-06-14 11:11:11 +02:00
// if (options.ContainsField(ItemFields.MediaSourceCount))
2021-08-29 00:32:50 +02:00
// {
2015-01-24 20:03:55 +01:00
// Songs always have one
2021-08-29 00:32:50 +02:00
// }
2013-02-21 02:33:05 +01:00
}
2019-08-29 22:28:33 +02:00
if (item is IHasArtist hasArtist)
2015-03-13 18:25:28 +01:00
{
dto.Artists = hasArtist.Artists;
2020-06-14 11:11:11 +02:00
// var artistItems = _libraryManager.GetArtists(new InternalItemsQuery
2021-08-29 00:32:50 +02:00
// {
2016-12-13 08:36:30 +01:00
// EnableTotalRecordCount = false,
// ItemIds = new[] { item.Id.ToString("N", CultureInfo.InvariantCulture) }
2021-08-29 00:32:50 +02:00
// });
2016-12-13 08:36:30 +01:00
2020-06-14 11:11:11 +02:00
// dto.ArtistItems = artistItems.Items
2016-12-13 08:36:30 +01:00
// .Select(i =>
// {
// var artist = i.Item1;
// return new NameIdPair
// {
// Name = artist.Name,
// Id = artist.Id.ToString("N", CultureInfo.InvariantCulture)
2016-12-13 08:36:30 +01:00
// };
// })
// .ToList();
2016-09-06 07:02:05 +02:00
// Include artists that are not in the database yet, e.g., just added via metadata editor
2020-06-14 11:11:11 +02:00
// var foundArtists = artistItems.Items.Select(i => i.Item1.Name).ToList();
2017-08-11 08:29:49 +02:00
dto.ArtistItems = hasArtist.Artists
2021-08-29 00:32:50 +02:00
// .Except(foundArtists, new DistinctNameComparer())
2016-09-06 07:02:05 +02:00
.Select(i =>
{
2016-10-06 20:55:01 +02:00
// This should not be necessary but we're seeing some cases of it
2018-09-12 19:26:21 +02:00
if (string.IsNullOrEmpty(i))
2016-10-06 20:55:01 +02:00
{
return null;
}
2017-05-22 06:54:02 +02:00
var artist = _libraryManager.GetArtist(i, new DtoOptions(false)
{
EnableImages = false
});
2022-12-05 15:01:13 +01:00
if (artist is not null)
2016-09-06 07:02:05 +02:00
{
2018-09-12 19:26:21 +02:00
return new NameGuidPair
2016-09-06 07:02:05 +02:00
{
Name = artist.Name,
2018-09-12 19:26:21 +02:00
Id = artist.Id
2016-09-06 07:02:05 +02:00
};
}
return null;
2022-12-05 15:01:13 +01:00
}).Where(i => i is not null).ToArray();
2015-03-13 18:25:28 +01:00
}
2022-02-17 08:15:26 +01:00
if (item is IHasAlbumArtist hasAlbumArtist)
{
2014-06-23 18:05:19 +02:00
dto.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault();
2015-03-13 16:54:20 +01:00
2020-06-14 11:11:11 +02:00
// var artistItems = _libraryManager.GetAlbumArtists(new InternalItemsQuery
2021-08-29 00:32:50 +02:00
// {
2016-12-13 08:36:30 +01:00
// EnableTotalRecordCount = false,
// ItemIds = new[] { item.Id.ToString("N", CultureInfo.InvariantCulture) }
2021-08-29 00:32:50 +02:00
// });
2016-12-13 08:36:30 +01:00
2020-06-14 11:11:11 +02:00
// dto.AlbumArtists = artistItems.Items
2016-12-13 08:36:30 +01:00
// .Select(i =>
// {
// var artist = i.Item1;
// return new NameIdPair
// {
// Name = artist.Name,
// Id = artist.Id.ToString("N", CultureInfo.InvariantCulture)
2016-12-13 08:36:30 +01:00
// };
// })
// .ToList();
2017-08-10 20:01:31 +02:00
dto.AlbumArtists = hasAlbumArtist.AlbumArtists
2021-08-29 00:32:50 +02:00
// .Except(foundArtists, new DistinctNameComparer())
2015-03-13 16:54:20 +01:00
.Select(i =>
{
2016-12-13 08:36:30 +01:00
// This should not be necessary but we're seeing some cases of it
2018-09-12 19:26:21 +02:00
if (string.IsNullOrEmpty(i))
2015-03-13 16:54:20 +01:00
{
2016-12-13 08:36:30 +01:00
return null;
}
2017-05-22 06:54:02 +02:00
var artist = _libraryManager.GetArtist(i, new DtoOptions(false)
{
EnableImages = false
});
2022-12-05 15:01:13 +01:00
if (artist is not null)
2016-12-13 08:36:30 +01:00
{
2018-09-12 19:26:21 +02:00
return new NameGuidPair
2016-12-13 08:36:30 +01:00
{
Name = artist.Name,
2018-09-12 19:26:21 +02:00
Id = artist.Id
2016-12-13 08:36:30 +01:00
};
}
return null;
2022-12-05 15:01:13 +01:00
}).Where(i => i is not null).ToArray();
}
2013-09-04 19:02:19 +02:00
// Add video info
2021-08-29 00:32:50 +02:00
if (item is Video video)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.VideoType = video.VideoType;
dto.Video3DFormat = video.Video3DFormat;
dto.IsoType = video.IsoType;
2013-02-21 02:33:05 +01:00
2015-10-24 17:33:22 +02:00
if (video.HasSubtitles)
{
dto.HasSubtitles = video.HasSubtitles;
}
2017-08-10 20:01:31 +02:00
if (video.AdditionalParts.Length != 0)
2014-11-30 20:01:33 +01:00
{
2017-08-10 20:01:31 +02:00
dto.PartCount = video.AdditionalParts.Length + 1;
2014-11-30 20:01:33 +01:00
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.MediaSourceCount))
2014-11-30 20:01:33 +01:00
{
2016-05-01 22:56:06 +02:00
var mediaSourceCount = video.MediaSourceCount;
if (mediaSourceCount != 1)
2014-12-01 13:43:34 +01:00
{
2016-05-01 22:56:06 +02:00
dto.MediaSourceCount = mediaSourceCount;
2014-12-01 13:43:34 +01:00
}
2014-11-30 20:01:33 +01:00
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.Chapters))
2014-03-20 16:55:22 +01:00
{
2018-09-12 19:26:21 +02:00
dto.Chapters = _itemRepo.GetChapters(item);
2014-03-20 16:55:22 +01:00
}
2016-03-19 05:22:33 +01:00
2023-02-22 09:08:35 +01:00
if (options.ContainsField(ItemFields.Trickplay))
{
dto.Trickplay = _trickplayManager.GetTrickplayManifest(item).GetAwaiter().GetResult();
2023-02-22 09:08:35 +01:00
}
dto.ExtraType = video.ExtraType;
2013-02-21 02:33:05 +01:00
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.MediaStreams))
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
// Add VideoInfo
2021-08-29 00:32:50 +02:00
if (item is IHasMediaSources)
2013-09-04 19:02:19 +02:00
{
2017-08-19 21:43:35 +02:00
MediaStream[] mediaStreams;
2014-03-21 04:31:40 +01:00
2022-12-05 15:01:13 +01:00
if (dto.MediaSources is not null && dto.MediaSources.Length > 0)
2013-12-06 04:39:44 +01:00
{
2018-09-12 19:26:21 +02:00
if (item.SourceType == SourceType.Channel)
{
mediaStreams = dto.MediaSources[0].MediaStreams.ToArray();
}
else
{
string id = item.Id.ToString("N", CultureInfo.InvariantCulture);
mediaStreams = dto.MediaSources.Where(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase))
2018-09-12 19:26:21 +02:00
.SelectMany(i => i.MediaStreams)
.ToArray();
}
2014-03-21 04:31:40 +01:00
}
else
{
mediaStreams = _mediaSourceManager.GetStaticMediaSources(item, true)[0].MediaStreams.ToArray();
2014-03-21 04:31:40 +01:00
}
2013-12-06 04:39:44 +01:00
2014-03-21 04:31:40 +01:00
dto.MediaStreams = mediaStreams;
2013-09-04 19:02:19 +02:00
}
}
2018-09-12 19:26:21 +02:00
BaseItem[] allExtras = null;
if (options.ContainsField(ItemFields.SpecialFeatureCount))
{
2020-03-28 13:40:56 +01:00
allExtras = item.GetExtras().ToArray();
2018-09-12 19:26:21 +02:00
dto.SpecialFeatureCount = allExtras.Count(i => i.ExtraType.HasValue && BaseItem.DisplayExtraTypes.Contains(i.ExtraType.Value));
}
if (options.ContainsField(ItemFields.LocalTrailerCount))
{
if (item is IHasTrailers hasTrailers)
{
dto.LocalTrailerCount = hasTrailers.LocalTrailers.Count;
2022-01-14 20:54:29 +01:00
}
else
{
dto.LocalTrailerCount = (allExtras ?? item.GetExtras()).Count(i => i.ExtraType == ExtraType.Trailer);
}
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
// Add EpisodeInfo
if (item is Episode episode)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.IndexNumberEnd = episode.IndexNumberEnd;
2016-05-23 00:37:50 +02:00
dto.SeriesName = episode.SeriesName;
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.SpecialEpisodeNumbers))
2014-12-19 05:20:07 +01:00
{
dto.AirsAfterSeasonNumber = episode.AirsAfterSeasonNumber;
dto.AirsBeforeEpisodeNumber = episode.AirsBeforeEpisodeNumber;
dto.AirsBeforeSeasonNumber = episode.AirsBeforeSeasonNumber;
}
2016-07-04 22:11:30 +02:00
dto.SeasonName = episode.SeasonName;
2018-09-12 19:26:21 +02:00
dto.SeasonId = episode.SeasonId;
dto.SeriesId = episode.SeriesId;
2016-07-05 08:01:31 +02:00
2016-07-05 07:40:18 +02:00
Series episodeSeries = null;
2016-05-23 00:37:50 +02:00
2019-10-05 18:24:04 +02:00
// this block will add the series poster for episodes without a poster
// TODO maybe remove the if statement entirely
2020-06-14 11:11:11 +02:00
// if (options.ContainsField(ItemFields.SeriesPrimaryImage))
2016-07-05 07:40:18 +02:00
{
2021-08-29 00:32:50 +02:00
episodeSeries ??= episode.Series;
2022-12-05 15:01:13 +01:00
if (episodeSeries is not null)
2014-12-01 13:43:34 +01:00
{
dto.SeriesPrimaryImageTag = GetTagAndFillBlurhash(dto, episodeSeries, ImageType.Primary);
2022-12-05 15:00:20 +01:00
if (dto.ImageTags is null || !dto.ImageTags.ContainsKey(ImageType.Primary))
{
AttachPrimaryImageAspectRatio(dto, episodeSeries);
}
2014-12-01 13:43:34 +01:00
}
2016-07-05 07:40:18 +02:00
}
2014-12-01 13:43:34 +01:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.SeriesStudio))
2016-07-05 07:40:18 +02:00
{
2021-08-29 00:32:50 +02:00
episodeSeries ??= episode.Series;
2022-12-05 15:01:13 +01:00
if (episodeSeries is not null)
2014-12-01 13:43:34 +01:00
{
2016-05-23 00:37:50 +02:00
dto.SeriesStudio = episodeSeries.Studios.FirstOrDefault();
2014-12-01 13:43:34 +01:00
}
}
}
2013-02-21 02:33:05 +01:00
2016-05-23 00:37:50 +02:00
// Add SeriesInfo
if (item is Series series)
2016-05-23 00:37:50 +02:00
{
2017-08-13 22:15:07 +02:00
dto.AirDays = series.AirDays;
dto.AirTime = series.AirTime;
2021-08-29 00:32:50 +02:00
dto.Status = series.Status?.ToString();
2016-05-23 00:37:50 +02:00
}
2013-09-04 19:02:19 +02:00
// Add SeasonInfo
if (item is Season season)
2013-02-21 02:33:05 +01:00
{
2016-07-04 22:11:30 +02:00
dto.SeriesName = season.SeriesName;
2018-09-12 19:26:21 +02:00
dto.SeriesId = season.SeriesId;
2016-07-05 08:01:31 +02:00
series = null;
2016-07-04 22:11:30 +02:00
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.SeriesStudio))
2016-07-05 08:01:31 +02:00
{
2021-08-29 00:32:50 +02:00
series ??= season.Series;
2022-12-05 15:01:13 +01:00
if (series is not null)
2016-07-04 22:11:30 +02:00
{
dto.SeriesStudio = series.Studios.FirstOrDefault();
}
2016-07-05 08:01:31 +02:00
}
2013-11-09 19:44:09 +01:00
2019-10-05 18:24:04 +02:00
// this block will add the series poster for seasons without a poster
// TODO maybe remove the if statement entirely
2020-06-14 11:11:11 +02:00
// if (options.ContainsField(ItemFields.SeriesPrimaryImage))
2016-07-05 08:01:31 +02:00
{
2021-08-29 00:32:50 +02:00
series ??= season.Series;
2022-12-05 15:01:13 +01:00
if (series is not null)
2014-12-01 13:43:34 +01:00
{
dto.SeriesPrimaryImageTag = GetTagAndFillBlurhash(dto, series, ImageType.Primary);
2022-12-05 15:00:20 +01:00
if (dto.ImageTags is null || !dto.ImageTags.ContainsKey(ImageType.Primary))
{
AttachPrimaryImageAspectRatio(dto, series);
}
2014-12-01 13:43:34 +01:00
}
}
2013-02-21 02:33:05 +01:00
}
if (item is MusicVideo musicVideo)
2013-09-04 19:02:19 +02:00
{
SetMusicVideoProperties(dto, musicVideo);
2013-02-21 02:33:05 +01:00
}
if (item is Book book)
2013-09-04 19:02:19 +02:00
{
SetBookProperties(dto, book);
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.ProductionLocations))
2016-10-08 20:51:07 +02:00
{
if (item.ProductionLocations.Length > 0 || item is Movie)
{
dto.ProductionLocations = item.ProductionLocations;
}
2016-10-08 20:51:07 +02:00
}
2018-09-12 19:26:21 +02:00
if (options.ContainsField(ItemFields.Width))
{
var width = item.Width;
if (width > 0)
{
dto.Width = width;
}
}
if (options.ContainsField(ItemFields.Height))
{
var height = item.Height;
if (height > 0)
{
dto.Height = height;
}
}
if (options.ContainsField(ItemFields.IsHD))
{
// Compatibility
if (item.IsHD)
{
dto.IsHD = true;
}
}
if (item is Photo photo)
{
SetPhotoProperties(dto, photo);
}
2015-08-17 06:08:33 +02:00
dto.ChannelId = item.ChannelId;
2015-10-29 14:28:05 +01:00
2018-09-12 19:26:21 +02:00
if (item.SourceType == SourceType.Channel)
2014-05-17 23:23:48 +02:00
{
2016-03-19 06:04:38 +01:00
var channel = _libraryManager.GetItemById(item.ChannelId);
2022-12-05 15:01:13 +01:00
if (channel is not null)
2016-03-19 06:04:38 +01:00
{
dto.ChannelName = channel.Name;
}
2014-05-17 23:23:48 +02:00
}
}
2017-06-29 21:10:58 +02:00
private BaseItem GetImageDisplayParent(BaseItem currentItem, BaseItem originalItem)
2016-12-17 09:27:41 +01:00
{
if (currentItem is MusicAlbum musicAlbum)
2016-12-17 09:27:41 +01:00
{
2017-05-22 06:54:02 +02:00
var artist = musicAlbum.GetMusicArtist(new DtoOptions(false));
2022-12-05 15:01:13 +01:00
if (artist is not null)
2016-12-17 09:27:41 +01:00
{
return artist;
}
}
2017-01-15 22:29:00 +01:00
2018-09-12 19:26:21 +02:00
var parent = currentItem.DisplayParent ?? currentItem.GetOwner() ?? currentItem.GetParent();
2017-06-29 21:10:58 +02:00
2022-12-05 15:00:20 +01:00
if (parent is null && originalItem is not UserRootFolder && originalItem is not UserView && originalItem is not AggregateFolder && originalItem is not ICollectionFolder && originalItem is not Channel)
2017-06-29 21:10:58 +02:00
{
parent = _libraryManager.GetCollectionFolders(originalItem).FirstOrDefault();
}
return parent;
2016-12-17 09:27:41 +01:00
}
2016-07-05 07:40:18 +02:00
private void AddInheritedImages(BaseItemDto dto, BaseItem item, DtoOptions options, BaseItem owner)
{
2016-11-21 09:54:53 +01:00
if (!item.SupportsInheritedParentImages)
{
return;
}
2016-07-05 07:40:18 +02:00
var logoLimit = options.GetImageLimit(ImageType.Logo);
var artLimit = options.GetImageLimit(ImageType.Art);
var thumbLimit = options.GetImageLimit(ImageType.Thumb);
var backdropLimit = options.GetImageLimit(ImageType.Backdrop);
2016-11-21 09:54:53 +01:00
// For now. Emby apps are not using this
artLimit = 0;
2016-07-05 07:40:18 +02:00
if (logoLimit == 0 && artLimit == 0 && thumbLimit == 0 && backdropLimit == 0)
{
return;
}
BaseItem parent = null;
var isFirst = true;
2017-08-10 22:06:36 +02:00
var imageTags = dto.ImageTags;
2022-12-05 15:01:13 +01:00
while ((!(imageTags is not null && imageTags.ContainsKey(ImageType.Logo)) && logoLimit > 0)
|| (!(imageTags is not null && imageTags.ContainsKey(ImageType.Art)) && artLimit > 0)
|| (!(imageTags is not null && imageTags.ContainsKey(ImageType.Thumb)) && thumbLimit > 0)
2021-09-03 18:46:34 +02:00
|| parent is Series)
2016-07-05 07:40:18 +02:00
{
2021-09-03 18:46:34 +02:00
parent ??= isFirst ? GetImageDisplayParent(item, item) ?? owner : parent;
2022-12-05 15:00:20 +01:00
if (parent is null)
2016-07-08 05:22:02 +02:00
{
break;
}
var allImages = parent.ImageInfos;
2022-12-05 15:01:13 +01:00
if (logoLimit > 0 && !(imageTags is not null && imageTags.ContainsKey(ImageType.Logo)) && dto.ParentLogoItemId is null)
2016-07-05 07:40:18 +02:00
{
2016-07-08 05:22:02 +02:00
var image = allImages.FirstOrDefault(i => i.Type == ImageType.Logo);
2016-07-05 07:40:18 +02:00
2022-12-05 15:01:13 +01:00
if (image is not null)
2016-07-05 07:40:18 +02:00
{
2022-01-22 22:22:55 +01:00
dto.ParentLogoItemId = parent.Id;
dto.ParentLogoImageTag = GetTagAndFillBlurhash(dto, parent, image);
2016-07-05 07:40:18 +02:00
}
}
2020-06-15 23:43:52 +02:00
2022-12-05 15:01:13 +01:00
if (artLimit > 0 && !(imageTags is not null && imageTags.ContainsKey(ImageType.Art)) && dto.ParentArtItemId is null)
2016-07-05 07:40:18 +02:00
{
2016-07-08 05:22:02 +02:00
var image = allImages.FirstOrDefault(i => i.Type == ImageType.Art);
2016-07-05 07:40:18 +02:00
2022-12-05 15:01:13 +01:00
if (image is not null)
2016-07-05 07:40:18 +02:00
{
2022-01-22 22:22:55 +01:00
dto.ParentArtItemId = parent.Id;
dto.ParentArtImageTag = GetTagAndFillBlurhash(dto, parent, image);
2016-07-05 07:40:18 +02:00
}
}
2020-06-15 23:43:52 +02:00
2022-12-05 15:01:13 +01:00
if (thumbLimit > 0 && !(imageTags is not null && imageTags.ContainsKey(ImageType.Thumb)) && (dto.ParentThumbItemId is null || parent is Series) && parent is not ICollectionFolder && parent is not UserView)
2016-07-05 07:40:18 +02:00
{
2016-07-08 05:22:02 +02:00
var image = allImages.FirstOrDefault(i => i.Type == ImageType.Thumb);
2016-07-05 07:40:18 +02:00
2022-12-05 15:01:13 +01:00
if (image is not null)
2016-07-05 07:40:18 +02:00
{
2022-01-22 22:22:55 +01:00
dto.ParentThumbItemId = parent.Id;
dto.ParentThumbImageTag = GetTagAndFillBlurhash(dto, parent, image);
2016-07-05 07:40:18 +02:00
}
}
2020-06-15 23:43:52 +02:00
2022-12-05 15:01:13 +01:00
if (backdropLimit > 0 && !((dto.BackdropImageTags is not null && dto.BackdropImageTags.Length > 0) || (dto.ParentBackdropImageTags is not null && dto.ParentBackdropImageTags.Length > 0)))
2016-07-05 07:40:18 +02:00
{
2016-07-08 05:22:02 +02:00
var images = allImages.Where(i => i.Type == ImageType.Backdrop).Take(backdropLimit).ToList();
2016-07-05 07:40:18 +02:00
if (images.Count > 0)
{
2022-01-22 22:22:55 +01:00
dto.ParentBackdropItemId = parent.Id;
dto.ParentBackdropImageTags = GetTagsAndFillBlurhashes(dto, parent, ImageType.Backdrop, images);
2016-07-05 07:40:18 +02:00
}
}
isFirst = false;
2016-11-21 09:54:53 +01:00
if (!parent.SupportsInheritedParentImages)
{
break;
}
2017-06-29 21:10:58 +02:00
parent = GetImageDisplayParent(parent, item);
2016-07-05 07:40:18 +02:00
}
}
2017-02-20 21:50:58 +01:00
private string GetMappedPath(BaseItem item, BaseItem ownerItem)
2014-03-20 16:55:22 +01:00
{
2014-03-21 04:31:40 +01:00
var path = item.Path;
2014-03-20 16:55:22 +01:00
2018-09-12 19:26:21 +02:00
if (item.IsFileProtocol)
2014-03-20 16:55:22 +01:00
{
2017-02-20 21:50:58 +01:00
path = _libraryManager.GetPathAfterNetworkSubstitution(path, ownerItem ?? item);
2014-03-20 16:55:22 +01:00
}
return path;
}
2013-02-21 02:33:05 +01:00
/// <summary>
2013-09-04 19:02:19 +02:00
/// Attaches the primary image aspect ratio.
2013-02-21 02:33:05 +01:00
/// </summary>
2013-09-04 19:02:19 +02:00
/// <param name="dto">The dto.</param>
2013-02-21 02:33:05 +01:00
/// <param name="item">The item.</param>
2017-10-22 08:22:43 +02:00
public void AttachPrimaryImageAspectRatio(IItemDto dto, BaseItem item)
2016-01-16 06:01:57 +01:00
{
dto.PrimaryImageAspectRatio = GetPrimaryImageAspectRatio(item);
}
2017-10-22 08:22:43 +02:00
public double? GetPrimaryImageAspectRatio(BaseItem item)
2013-02-21 02:33:05 +01:00
{
2014-02-07 21:30:41 +01:00
var imageInfo = item.GetImageInfo(ImageType.Primary, 0);
2013-05-05 06:49:49 +02:00
2022-12-05 15:00:20 +01:00
if (imageInfo is null)
2013-09-04 19:02:19 +02:00
{
2016-01-16 06:01:57 +01:00
return null;
2013-09-04 19:02:19 +02:00
}
if (!imageInfo.IsLocalFile)
{
return item.GetDefaultPrimaryImageAspectRatio();
2013-09-04 19:02:19 +02:00
}
try
{
2021-11-10 23:29:41 +01:00
var size = _imageProcessor.GetImageDimensions(item, imageInfo);
2021-11-10 23:46:56 +01:00
var width = size.Width;
var height = size.Height;
if (width > 0 && height > 0)
2017-05-15 23:14:51 +02:00
{
2021-11-10 23:46:56 +01:00
return (double)width / height;
2017-05-15 23:14:51 +02:00
}
2013-09-04 19:02:19 +02:00
}
catch (Exception ex)
2013-09-04 19:02:19 +02:00
{
2021-11-10 23:29:41 +01:00
_logger.LogError(ex, "Failed to determine primary image aspect ratio for {ImagePath}", imageInfo.Path);
2015-01-30 22:17:19 +01:00
}
2016-01-21 09:23:02 +01:00
2021-11-10 23:29:41 +01:00
return item.GetDefaultPrimaryImageAspectRatio();
2013-05-05 06:49:49 +02:00
}
2013-02-21 02:33:05 +01:00
}
}