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

1428 lines
49 KiB
C#
Raw Normal View History

2019-11-01 10:38:54 -07:00
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Common;
2014-05-17 14:23:48 -07:00
using MediaBrowser.Controller.Channels;
2013-09-18 11:49:06 -07:00
using MediaBrowser.Controller.Drawing;
2013-09-04 10:02:19 -07:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
2013-02-20 18:33:05 -07:00
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Playlists;
2014-02-21 11:48:15 -07:00
using MediaBrowser.Controller.Providers;
2013-02-20 18:33:05 -07:00
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto;
2013-02-20 18:33:05 -07:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using Microsoft.Extensions.Logging;
2013-02-20 18:33:05 -07:00
namespace Emby.Server.Implementations.Dto
2013-02-20 18:33:05 -07:00
{
2013-09-04 10:02:19 -07:00
public class DtoService : IDtoService
2013-02-20 18:33:05 -07:00
{
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
2013-10-02 09:08:58 -07:00
private readonly IUserDataManager _userDataRepository;
2013-06-18 12:16:27 -07:00
private readonly IItemRepository _itemRepo;
2013-09-18 11:49:06 -07:00
private readonly IImageProcessor _imageProcessor;
2014-02-21 11:48:15 -07:00
private readonly IProviderManager _providerManager;
2013-10-02 09:08:58 -07:00
2014-10-23 21:54:35 -07:00
private readonly IApplicationHost _appHost;
2015-03-07 15:43:53 -07:00
private readonly Func<IMediaSourceManager> _mediaSourceManager;
2015-05-31 11:22:51 -07:00
private readonly Func<ILiveTvManager> _livetvManager;
public DtoService(
ILoggerFactory loggerFactory,
ILibraryManager libraryManager,
IUserDataManager userDataRepository,
IItemRepository itemRepo,
IImageProcessor imageProcessor,
IProviderManager providerManager,
IApplicationHost appHost,
Func<IMediaSourceManager> mediaSourceManager,
Func<ILiveTvManager> livetvManager)
{
_logger = loggerFactory.CreateLogger(nameof(DtoService));
_libraryManager = libraryManager;
_userDataRepository = userDataRepository;
2013-06-18 12:16:27 -07:00
_itemRepo = itemRepo;
2013-09-18 11:49:06 -07:00
_imageProcessor = imageProcessor;
2014-02-21 11:48:15 -07:00
_providerManager = providerManager;
2014-10-23 21:54:35 -07:00
_appHost = appHost;
2015-03-07 15:43:53 -07:00
_mediaSourceManager = mediaSourceManager;
2015-05-31 11:22:51 -07:00
_livetvManager = livetvManager;
}
2013-02-20 18:33:05 -07:00
/// <summary>
2013-05-15 09:56:38 -07:00
/// Converts a BaseItem to a DTOBaseItem
2013-02-20 18:33:05 -07:00
/// </summary>
/// <param name="item">The item.</param>
/// <param name="fields">The fields.</param>
2013-05-15 09:56:38 -07:00
/// <param name="user">The user.</param>
/// <param name="owner">The owner.</param>
2013-02-20 18:33:05 -07:00
/// <returns>Task{DtoBaseItem}.</returns>
2019-01-13 13:37:13 -07:00
/// <exception cref="ArgumentNullException">item</exception>
2017-08-19 12:43:35 -07:00
public BaseItemDto GetBaseItemDto(BaseItem item, ItemFields[] fields, User user = null, BaseItem owner = null)
2014-06-28 12:35:30 -07:00
{
2014-11-30 12:01:33 -07:00
var options = new DtoOptions
{
2014-12-20 22:57:06 -07:00
Fields = fields
2014-11-30 12:01:33 -07:00
};
2015-01-24 12:03:55 -07:00
2014-11-30 12:01:33 -07:00
return GetBaseItemDto(item, options, user, owner);
}
/// <inheritdoc />
public IReadOnlyList<BaseItemDto> GetBaseItemDtos(IReadOnlyList<BaseItem> items, DtoOptions options, User user = null, BaseItem owner = null)
2015-01-24 12:03:55 -07:00
{
var returnItems = new BaseItemDto[items.Count];
var programTuples = new List<(BaseItem, BaseItemDto)>();
var channelTuples = new List<(BaseItemDto, LiveTvChannel)>();
2015-01-24 12:03:55 -07:00
for (int index = 0; index < items.Count; index++)
2015-01-24 12:03:55 -07:00
{
var item = items[index];
var dto = GetBaseItemDtoInternal(item, options, user, owner);
2015-01-24 12:03:55 -07:00
if (item is LiveTvChannel tvChannel)
2016-03-21 23:49:36 -07:00
{
channelTuples.Add((dto, tvChannel));
2016-03-21 23:49:36 -07:00
}
else if (item is LiveTvProgram)
2016-03-02 11:42:39 -07:00
{
programTuples.Add((item, dto));
2016-03-02 11:42:39 -07:00
}
if (item is IItemByName byName)
2015-01-24 12:03:55 -07:00
{
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.ItemCounts))
2015-03-25 21:44:24 -07:00
{
2016-05-10 19:21:28 -07:00
var libraryItems = byName.GetTaggedItems(new InternalItemsQuery(user)
{
2017-05-21 00:25:49 -07:00
Recursive = true,
DtoOptions = new DtoOptions(false)
{
EnableImages = false
}
2016-05-10 19:21:28 -07:00
});
2015-01-24 12:03:55 -07:00
2018-09-12 10:26:21 -07:00
SetItemByNameInfo(item, dto, libraryItems, user);
2015-03-25 21:44:24 -07:00
}
2015-01-24 12:03:55 -07:00
}
2017-08-19 12:43:35 -07:00
returnItems[index] = dto;
2015-01-24 12:03:55 -07:00
}
2016-03-02 11:42:39 -07:00
if (programTuples.Count > 0)
{
_livetvManager().AddInfoToProgramDto(programTuples, options.Fields, user).GetAwaiter().GetResult();
2016-03-02 11:42:39 -07:00
}
2016-03-21 23:49:36 -07:00
if (channelTuples.Count > 0)
{
2017-08-27 17:33:05 -07:00
_livetvManager().AddChannelInfo(channelTuples, options, user);
2016-03-21 23:49:36 -07:00
}
2017-08-19 12:43:35 -07:00
return returnItems;
2015-01-24 12:03:55 -07:00
}
2014-11-30 12:01:33 -07:00
public BaseItemDto GetBaseItemDto(BaseItem item, DtoOptions options, User user = null, BaseItem owner = null)
{
var dto = GetBaseItemDtoInternal(item, options, user, owner);
if (item is LiveTvChannel tvChannel)
2016-03-21 23:49:36 -07:00
{
var list = new List<(BaseItemDto, LiveTvChannel)>(1) { (dto, tvChannel) };
2017-08-27 17:33:05 -07:00
_livetvManager().AddChannelInfo(list, options, user);
2016-03-21 23:49:36 -07:00
}
else if (item is LiveTvProgram)
2016-03-02 11:42:39 -07:00
{
var list = new List<(BaseItem, BaseItemDto)>(1) { (item, dto) };
2016-03-02 11:42:39 -07:00
var task = _livetvManager().AddInfoToProgramDto(list, options.Fields, user);
Task.WaitAll(task);
}
if (item is IItemByName itemByName
&& options.ContainsField(ItemFields.ItemCounts))
2014-06-28 12:35:30 -07:00
{
SetItemByNameInfo(
item,
dto,
GetTaggedItems(
itemByName,
user,
new DtoOptions(false)
{
EnableImages = false
}),
user);
2014-06-28 12:35:30 -07:00
}
2014-07-27 15:01:29 -07:00
return dto;
2014-06-28 12:35:30 -07:00
}
private static IList<BaseItem> GetTaggedItems(IItemByName byName, User user, DtoOptions options)
2015-07-08 09:10:34 -07:00
{
return byName.GetTaggedItems(
new InternalItemsQuery(user)
{
Recursive = true,
DtoOptions = options
});
2015-01-24 12:03:55 -07:00
}
private BaseItemDto GetBaseItemDtoInternal(BaseItem item, DtoOptions options, User user = null, BaseItem owner = null)
2013-02-20 18:33:05 -07:00
{
2014-10-23 21:54:35 -07:00
var dto = new BaseItemDto
{
ServerId = _appHost.SystemId
};
2013-02-20 18:33:05 -07:00
2016-03-19 12:32:37 -07:00
if (item.SourceType == SourceType.Channel)
{
dto.SourceType = item.SourceType.ToString();
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.People))
2013-04-02 19:59:27 -07:00
{
AttachPeople(dto, item);
2013-04-02 19:59:27 -07:00
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.PrimaryImageAspectRatio))
2013-04-02 19:59:27 -07:00
{
try
{
2016-01-15 22:01:57 -07:00
AttachPrimaryImageAspectRatio(dto, item);
2013-04-02 19:59:27 -07:00
}
catch (Exception ex)
{
// Have to use a catch-all unfortunately because some .net image methods throw plain Exceptions
2018-12-20 05:11:26 -07:00
_logger.LogError(ex, "Error generating PrimaryImageAspectRatio for {itemName}", item.Name);
2013-04-02 19:59:27 -07:00
}
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.DisplayPreferencesId))
{
dto.DisplayPreferencesId = item.DisplayPreferencesId.ToString("N", CultureInfo.InvariantCulture);
}
2013-06-17 13:35:43 -07:00
if (user != null)
{
2017-05-25 23:48:54 -07:00
AttachUserSpecificInfo(dto, item, user, options);
2013-06-17 13:35:43 -07:00
}
2019-09-20 03:42:08 -07:00
if (item is IHasMediaSources
&& options.ContainsField(ItemFields.MediaSources))
{
2019-09-20 03:42:08 -07:00
dto.MediaSources = _mediaSourceManager().GetStaticMediaSources(item, true, user).ToArray();
2017-08-23 10:27:53 -07:00
2019-09-20 03:42:08 -07:00
NormalizeMediaSourceContainers(dto);
}
2015-04-03 19:16:42 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.Studios))
{
AttachStudios(dto, item);
}
2014-11-30 12:01:33 -07:00
AttachBasicFields(dto, item, owner, options);
2013-02-20 18:33:05 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.CanDelete))
2015-02-05 22:39:07 -07:00
{
dto.CanDelete = user == null
? item.CanDelete()
: item.CanDelete(user);
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.CanDownload))
2015-02-05 22:39:07 -07:00
{
dto.CanDownload = user == null
? item.CanDownload()
: item.CanDownload(user);
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.Etag))
2015-04-15 08:41:42 -07:00
{
dto.Etag = item.GetEtag(user);
}
2015-04-13 20:45:17 -07:00
2017-08-23 10:08:17 -07:00
var liveTvManager = _livetvManager();
2018-09-12 10:26:21 -07:00
var activeRecording = liveTvManager.GetActiveRecordingInfo(item.Path);
if (activeRecording != null)
2015-05-31 11:22:51 -07:00
{
2018-09-12 10:26:21 -07:00
dto.Type = "Recording";
dto.CanDownload = false;
dto.RunTimeTicks = null;
2017-08-30 11:06:54 -07:00
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrEmpty(dto.SeriesName))
{
dto.EpisodeTitle = dto.Name;
dto.Name = dto.SeriesName;
2017-08-23 12:45:52 -07:00
}
2018-09-12 10:26:21 -07:00
liveTvManager.AddInfoToRecordingDto(item, dto, activeRecording, user);
2015-05-31 11:22:51 -07:00
}
2013-02-20 18:33:05 -07:00
return dto;
}
private static void NormalizeMediaSourceContainers(BaseItemDto dto)
2017-08-23 10:27:53 -07:00
{
foreach (var mediaSource in dto.MediaSources)
{
var container = mediaSource.Container;
2018-09-12 10:26:21 -07:00
if (string.IsNullOrEmpty(container))
2017-08-23 10:27:53 -07:00
{
continue;
}
var containers = container.Split(new[] { ',' });
if (containers.Length < 2)
{
continue;
}
var path = mediaSource.Path;
string fileExtensionContainer = null;
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrEmpty(path))
2017-08-23 10:27:53 -07:00
{
path = Path.GetExtension(path);
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrEmpty(path))
2017-08-23 10:27:53 -07:00
{
path = Path.GetExtension(path);
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrEmpty(path))
2017-08-23 10:27:53 -07:00
{
path = path.TrimStart('.');
}
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrEmpty(path) && containers.Contains(path, StringComparer.OrdinalIgnoreCase))
2017-08-23 10:27:53 -07:00
{
fileExtensionContainer = path;
}
}
}
mediaSource.Container = fileExtensionContainer ?? containers[0];
}
}
2018-09-12 10:26:21 -07:00
public BaseItemDto GetItemByNameDto(BaseItem item, DtoOptions options, List<BaseItem> taggedItems, User user = null)
{
var dto = GetBaseItemDtoInternal(item, options, user);
2014-06-28 12:35:30 -07:00
2018-09-12 10:26:21 -07:00
if (taggedItems != null && options.ContainsField(ItemFields.ItemCounts))
2015-03-25 21:44:24 -07:00
{
SetItemByNameInfo(item, dto, taggedItems, user);
}
2014-06-28 12:35:30 -07:00
return dto;
}
private static void SetItemByNameInfo(BaseItem item, BaseItemDto dto, IList<BaseItem> taggedItems, User user = null)
{
2016-08-13 13:54:29 -07: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 13:54:29 -07: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 13:54:29 -07: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 14:55:42 -07: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 14:46:22 -07:00
dto.ChildCount = taggedItems.Count;
}
2013-02-20 18:33:05 -07:00
/// <summary>
/// Attaches the user specific info.
/// </summary>
2018-09-12 10:26:21 -07:00
private void AttachUserSpecificInfo(BaseItemDto dto, BaseItem item, User user, DtoOptions options)
2013-02-20 18:33:05 -07:00
{
if (item.IsFolder)
{
var folder = (Folder)item;
2013-07-25 12:17:44 -07:00
2018-09-12 10:26:21 -07:00
if (options.EnableUserData)
2016-08-17 12:28:43 -07:00
{
2018-09-12 10:26:21 -07:00
dto.UserData = _userDataRepository.GetUserDataDto(item, dto, user, options);
2016-08-17 12:28:43 -07:00
}
2016-06-15 11:56:37 -07:00
2016-08-16 10:08:37 -07:00
if (!dto.ChildCount.HasValue && item.SourceType == SourceType.Library)
2016-06-15 11:56:37 -07:00
{
2016-12-13 00:36:30 -07:00
// For these types we can try to optimize and assume these values will be equal
2018-09-12 10:26:21 -07:00
if (item is MusicAlbum || item is Season || item is Playlist)
2016-12-13 00:36:30 -07:00
{
dto.ChildCount = dto.RecursiveItemCount;
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.ChildCount))
2016-12-13 00:36:30 -07:00
{
dto.ChildCount = dto.ChildCount ?? GetChildCount(folder, user);
}
2013-02-20 18:33:05 -07:00
}
2013-05-07 18:17:41 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.CumulativeRunTimeTicks))
2016-05-08 20:13:38 -07:00
{
2016-05-10 11:13:26 -07:00
dto.CumulativeRunTimeTicks = item.RunTimeTicks;
2016-05-08 20:13:38 -07:00
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.DateLastMediaAdded))
2016-05-08 20:13:38 -07:00
{
dto.DateLastMediaAdded = folder.DateLastMediaAdded;
}
}
2013-05-07 18:17:41 -07:00
else
2013-07-16 10:18:32 -07:00
{
2018-09-12 10:26:21 -07:00
if (options.EnableUserData)
2016-08-17 12:28:43 -07:00
{
2017-05-25 23:48:54 -07:00
dto.UserData = _userDataRepository.GetUserDataDto(item, user);
2016-08-17 12:28:43 -07:00
}
2013-05-07 18:17:41 -07:00
}
2014-02-20 22:35:56 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.PlayAccess))
{
dto.PlayAccess = item.GetPlayAccess(user);
}
2014-12-26 22:08:39 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.BasicSyncInfo))
2016-08-02 23:38:19 -07:00
{
2017-04-25 11:23:20 -07:00
var userCanSync = user != null && user.Policy.EnableContentDownloading;
2018-09-12 10:26:21 -07:00
if (userCanSync && item.SupportsExternalTransfer)
2016-08-17 12:28:43 -07:00
{
dto.SupportsSync = true;
}
2016-08-02 23:38:19 -07:00
}
2013-02-20 18:33:05 -07:00
}
private static int GetChildCount(Folder folder, User user)
{
2016-06-18 10:26:42 -07: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 new Random().Next(1, 10);
}
2016-06-16 06:24:12 -07:00
return folder.GetChildCount(user);
}
2013-09-04 10:02:19 -07:00
/// <summary>
/// Gets client-side Id of a server-side BaseItem
/// </summary>
/// <param name="item">The item.</param>
/// <returns>System.String.</returns>
2019-01-13 13:37:13 -07:00
/// <exception cref="ArgumentNullException">item</exception>
2013-09-04 10:02:19 -07:00
public string GetDtoId(BaseItem item)
{
return item.Id.ToString("N", CultureInfo.InvariantCulture);
2013-09-04 10:02:19 -07:00
}
2013-02-20 18:33:05 -07:00
private static void SetBookProperties(BaseItemDto dto, Book item)
2013-09-04 10:02:19 -07:00
{
dto.SeriesName = item.SeriesName;
}
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 07:26:29 -07:00
dto.Latitude = item.Latitude;
dto.Longitude = item.Longitude;
dto.Altitude = item.Altitude;
dto.IsoSpeedRating = item.IsoSpeedRating;
2014-10-12 08:18:26 -07:00
2016-10-10 23:46:59 -07:00
var album = item.AlbumEntity;
2014-08-30 07:26:29 -07:00
if (album != null)
{
2014-09-02 19:30:24 -07:00
dto.Album = album.Name;
2018-09-12 10:26:21 -07:00
dto.AlbumId = album.Id;
2014-08-30 07:26:29 -07:00
}
}
2013-02-20 18:33:05 -07:00
2013-09-04 10:02:19 -07:00
private void SetMusicVideoProperties(BaseItemDto dto, MusicVideo item)
{
if (!string.IsNullOrEmpty(item.Album))
{
2017-05-21 00:25:49 -07:00
var parentAlbumIds = _libraryManager.GetItemIds(new InternalItemsQuery
2016-05-11 07:36:28 -07:00
{
IncludeItemTypes = new[] { typeof(MusicAlbum).Name },
2017-05-21 00:25:49 -07:00
Name = item.Album,
Limit = 1
2016-05-11 07:36:28 -07:00
2017-05-21 00:25:49 -07:00
});
2017-05-21 00:25:49 -07:00
if (parentAlbumIds.Count > 0)
{
2018-09-12 10:26:21 -07:00
dto.AlbumId = parentAlbumIds[0];
}
2013-02-20 18:33:05 -07:00
}
2013-09-04 10:02:19 -07:00
dto.Album = item.Album;
}
2013-02-20 18:33:05 -07:00
private string[] GetImageTags(BaseItem item, List<ItemImageInfo> images)
2013-09-04 10:02:19 -07:00
{
2016-07-04 22:40:18 -07:00
return images
.Select(p => GetImageCacheTag(item, p))
.Where(i => i != null)
.ToArray();
2014-02-07 13:30:41 -07:00
}
2014-05-08 13:09:53 -07:00
private string GetImageCacheTag(BaseItem item, ImageType type)
2014-02-07 13:30:41 -07:00
{
try
{
return _imageProcessor.GetImageCacheTag(item, type);
}
2014-04-13 10:27:13 -07:00
catch (Exception ex)
2014-02-07 13:30:41 -07:00
{
2018-12-20 05:11:26 -07:00
_logger.LogError(ex, "Error getting {type} image info", type);
2014-02-07 13:30:41 -07:00
return null;
}
}
2014-05-08 13:09:53 -07:00
private string GetImageCacheTag(BaseItem item, ItemImageInfo image)
2013-09-04 10:02:19 -07:00
{
try
2013-02-20 18:33:05 -07:00
{
2014-02-07 13:30:41 -07:00
return _imageProcessor.GetImageCacheTag(item, image);
}
2014-04-13 10:27:13 -07:00
catch (Exception ex)
2013-02-20 18:33:05 -07:00
{
2018-12-20 05:11:26 -07:00
_logger.LogError(ex, "Error getting {imageType} image info for {path}", image.Type, image.Path);
2013-09-04 10:02:19 -07:00
return null;
2013-02-20 18:33:05 -07:00
}
}
/// <summary>
2013-09-04 10:02:19 -07:00
/// Attaches People DTO's to a DTOBaseItem
/// </summary>
/// <param name="dto">The dto.</param>
/// <param name="item">The item.</param>
/// <returns>Task.</returns>
private void AttachPeople(BaseItemDto dto, BaseItem item)
2013-09-04 10:02:19 -07: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-20 20:35:22 -07:00
var people = _libraryManager.GetPeople(item).OrderBy(i => i.SortOrder ?? int.MaxValue)
2014-06-30 06:28:38 -07:00
.ThenBy(i =>
{
if (i.IsType(PersonType.Actor))
{
return 0;
}
if (i.IsType(PersonType.GuestStar))
{
return 1;
}
if (i.IsType(PersonType.Director))
{
return 2;
}
if (i.IsType(PersonType.Writer))
{
return 3;
}
if (i.IsType(PersonType.Producer))
{
return 4;
}
if (i.IsType(PersonType.Composer))
{
return 4;
}
return 10;
})
.ToList();
2013-02-20 18:33:05 -07:00
2014-11-21 07:28:30 -07:00
var list = new List<BaseItemPerson>();
2013-02-20 18:33:05 -07:00
var dictionary = people.Select(p => p.Name)
2013-09-04 10:02:19 -07:00
.Distinct(StringComparer.OrdinalIgnoreCase).Select(c =>
{
try
{
return _libraryManager.GetPerson(c);
}
2014-08-24 20:54:45 -07:00
catch (Exception ex)
2013-09-04 10:02:19 -07:00
{
2018-12-20 05:39:58 -07:00
_logger.LogError(ex, "Error getting person {Name}", c);
return null;
}
}).Where(i => i != null)
2019-02-02 04:27:06 -07:00
.GroupBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.Select(x => x.First())
2013-09-04 10:02:19 -07:00
.ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < people.Count; i++)
2013-02-20 18:33:05 -07:00
{
2013-09-04 10:02:19 -07:00
var person = people[i];
2013-02-20 18:33:05 -07:00
2013-09-04 10:02:19 -07:00
var baseItemPerson = new BaseItemPerson
2013-02-20 18:33:05 -07:00
{
2013-09-04 10:02:19 -07:00
Name = person.Name,
Role = person.Role,
Type = person.Type
};
2013-02-20 18:33:05 -07:00
if (dictionary.TryGetValue(person.Name, out Person entity))
2013-09-04 10:02:19 -07:00
{
2014-02-07 13:30:41 -07:00
baseItemPerson.PrimaryImageTag = GetImageCacheTag(entity, ImageType.Primary);
baseItemPerson.Id = entity.Id.ToString("N", CultureInfo.InvariantCulture);
2014-11-21 07:28:30 -07:00
list.Add(baseItemPerson);
2013-02-20 18:33:05 -07:00
}
}
2014-11-21 07:28:30 -07:00
2018-12-28 08:48:26 -07:00
dto.People = list.ToArray();
2013-09-04 10:02:19 -07:00
}
2013-02-20 18:33:05 -07:00
2013-09-04 10:02:19 -07:00
/// <summary>
/// Attaches the studios.
/// </summary>
/// <param name="dto">The dto.</param>
/// <param name="item">The item.</param>
/// <returns>Task.</returns>
private void AttachStudios(BaseItemDto dto, BaseItem item)
2013-09-04 10:02:19 -07:00
{
2017-05-18 14:05:47 -07:00
dto.Studios = item.Studios
2018-09-12 10:26:21 -07:00
.Where(i => !string.IsNullOrEmpty(i))
.Select(i => new NameGuidPair
{
2017-05-18 14:05:47 -07:00
Name = i,
2018-09-12 10:26:21 -07:00
Id = _libraryManager.GetStudioId(i)
2017-05-18 14:05:47 -07:00
})
.ToArray();
}
2013-07-08 12:31:45 -07:00
2017-05-18 14:05:47 -07:00
private void AttachGenreItems(BaseItemDto dto, BaseItem item)
{
dto.GenreItems = item.Genres
2018-09-12 10:26:21 -07:00
.Where(i => !string.IsNullOrEmpty(i))
.Select(i => new NameGuidPair
2013-07-08 12:31:45 -07:00
{
2017-05-18 14:05:47 -07:00
Name = i,
2017-06-18 00:11:55 -07:00
Id = GetGenreId(i, item)
2017-05-18 14:05:47 -07:00
})
.ToArray();
}
2013-09-04 10:02:19 -07:00
2018-09-12 10:26:21 -07:00
private Guid GetGenreId(string name, BaseItem owner)
2017-05-18 14:05:47 -07:00
{
if (owner is IHasMusicGenres)
{
2018-09-12 10:26:21 -07:00
return _libraryManager.GetMusicGenreId(name);
2017-05-18 14:05:47 -07:00
}
2013-07-08 12:31:45 -07:00
2018-09-12 10:26:21 -07:00
return _libraryManager.GetGenreId(name);
2014-10-12 08:18:26 -07:00
}
2013-09-04 10:02:19 -07:00
/// <summary>
/// Sets simple property values on a DTOBaseItem
/// </summary>
/// <param name="dto">The dto.</param>
/// <param name="item">The item.</param>
/// <param name="owner">The owner.</param>
2014-11-30 12:01:33 -07:00
/// <param name="options">The options.</param>
private void AttachBasicFields(BaseItemDto dto, BaseItem item, BaseItem owner, DtoOptions options)
2013-09-04 10:02:19 -07:00
{
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.DateCreated))
2013-05-23 21:02:42 -07:00
{
2013-09-04 10:02:19 -07:00
dto.DateCreated = item.DateCreated;
2013-05-23 21:02:42 -07:00
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.Settings))
2013-09-04 10:02:19 -07:00
{
dto.LockedFields = item.LockedFields;
2014-03-22 09:16:43 -07:00
dto.LockData = item.IsLocked;
2014-03-24 10:54:45 -07:00
dto.ForcedSortName = item.ForcedSortName;
2013-07-16 09:03:28 -07:00
}
2016-07-17 09:59:50 -07:00
dto.Container = item.Container;
2013-02-20 18:33:05 -07:00
2013-09-04 10:02:19 -07:00
dto.EndDate = item.EndDate;
2013-04-24 15:34:38 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.ExternalUrls))
2014-02-21 11:48:15 -07:00
{
dto.ExternalUrls = _providerManager.GetExternalUrls(item).ToArray();
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.Tags))
2013-09-04 10:02:19 -07:00
{
2016-06-02 10:43:29 -07:00
dto.Tags = item.Tags;
2013-09-04 10:02:19 -07:00
}
2013-07-12 12:56:40 -07:00
var hasAspectRatio = item as IHasAspectRatio;
if (hasAspectRatio != null)
{
dto.AspectRatio = hasAspectRatio.AspectRatio;
}
2013-08-30 16:55:17 -07:00
2014-11-30 12:01:33 -07:00
var backdropLimit = options.GetImageLimit(ImageType.Backdrop);
if (backdropLimit > 0)
{
2016-07-04 22:40:18 -07:00
dto.BackdropImageTags = GetImageTags(item, item.GetImages(ImageType.Backdrop).Take(backdropLimit).ToList());
2014-11-30 12:01:33 -07:00
}
2013-09-17 19:43:34 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.ScreenshotImageTags))
2013-09-17 19:43:34 -07:00
{
2014-11-30 12:01:33 -07:00
var screenshotLimit = options.GetImageLimit(ImageType.Screenshot);
if (screenshotLimit > 0)
{
2016-07-04 23:05:43 -07:00
dto.ScreenshotImageTags = GetImageTags(item, item.GetImages(ImageType.Screenshot).Take(screenshotLimit).ToList());
2014-11-30 12:01:33 -07:00
}
2013-09-17 19:43:34 -07:00
}
2013-09-04 10:02:19 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.Genres))
2013-08-30 16:55:17 -07:00
{
2013-09-04 10:02:19 -07:00
dto.Genres = item.Genres;
2017-05-18 14:05:47 -07:00
AttachGenreItems(dto, item);
}
2016-08-17 22:56:10 -07:00
if (options.EnableImages)
{
2016-08-17 22:56:10 -07:00
dto.ImageTags = new Dictionary<ImageType, string>();
2014-11-30 12:01:33 -07:00
2016-08-17 22:56:10 -07:00
// Prevent implicitly captured closure
var currentItem = item;
foreach (var image in currentItem.ImageInfos.Where(i => !currentItem.AllowsMultipleImages(i.Type))
.ToList())
{
if (options.GetImageLimit(image.Type) > 0)
2014-11-30 12:01:33 -07:00
{
2016-08-17 22:56:10 -07:00
var tag = GetImageCacheTag(item, image);
if (tag != null)
{
dto.ImageTags[image.Type] = tag;
}
2014-11-30 12:01:33 -07:00
}
}
}
2018-09-12 10:26:21 -07:00
dto.Id = item.Id;
2013-09-04 10:02:19 -07:00
dto.IndexNumber = item.IndexNumber;
2016-07-04 22:40:18 -07:00
dto.ParentIndexNumber = item.ParentIndexNumber;
2016-08-17 12:28:43 -07:00
if (item.IsFolder)
{
dto.IsFolder = true;
}
else if (item is IHasMediaSources)
{
dto.IsFolder = false;
}
2013-09-04 10:02:19 -07:00
dto.MediaType = item.MediaType;
if (!(item is LiveTvProgram))
{
dto.LocationType = item.LocationType;
}
2015-10-28 12:40:38 -07:00
dto.Audio = item.Audio;
2013-09-11 10:54:59 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.Settings))
2016-10-07 22:57:38 -07:00
{
dto.PreferredMetadataCountryCode = item.PreferredMetadataCountryCode;
dto.PreferredMetadataLanguage = item.PreferredMetadataLanguage;
}
2016-07-04 13:11:30 -07:00
dto.CriticRating = item.CriticRating;
2013-11-06 09:06:16 -07:00
if (item is IHasDisplayOrder hasDisplayOrder)
{
dto.DisplayOrder = hasDisplayOrder.DisplayOrder;
}
if (item is IHasCollectionType hasCollectionType)
2014-06-04 19:32:40 -07:00
{
2018-09-12 10:26:21 -07:00
dto.CollectionType = hasCollectionType.CollectionType;
2014-06-04 19:32:40 -07:00
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.RemoteTrailers))
2013-02-20 18:33:05 -07:00
{
2018-09-12 10:26:21 -07:00
dto.RemoteTrailers = item.RemoteTrailers;
2013-09-04 10:02:19 -07:00
}
2013-02-20 18:33:05 -07:00
2013-09-04 10:02:19 -07:00
dto.Name = item.Name;
dto.OfficialRating = item.OfficialRating;
2013-03-08 22:15:51 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.Overview))
2013-09-04 10:02:19 -07:00
{
dto.Overview = item.Overview;
2013-02-20 18:33:05 -07:00
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.OriginalTitle))
2016-04-19 22:21:40 -07:00
{
dto.OriginalTitle = item.OriginalTitle;
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.ParentId))
2013-07-25 12:17:44 -07:00
{
2018-09-12 10:26:21 -07:00
dto.ParentId = item.DisplayParentId;
2013-07-25 12:17:44 -07:00
}
2013-02-20 18:33:05 -07:00
2016-07-04 22:40:18 -07:00
AddInheritedImages(dto, item, options, owner);
2013-10-24 10:49:24 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.Path))
2013-09-04 10:02:19 -07:00
{
2017-02-20 13:50:58 -07:00
dto.Path = GetMappedPath(item, owner);
2013-02-20 18:33:05 -07:00
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.EnableMediaSourceDisplay))
{
dto.EnableMediaSourceDisplay = item.EnableMediaSourceDisplay;
}
2013-09-04 10:02:19 -07:00
dto.PremiereDate = item.PremiereDate;
dto.ProductionYear = item.ProductionYear;
2013-05-03 20:13:28 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.ProviderIds))
2013-09-04 10:02:19 -07:00
{
dto.ProviderIds = item.ProviderIds;
}
2013-05-03 20:13:28 -07:00
2013-09-04 10:02:19 -07:00
dto.RunTimeTicks = item.RunTimeTicks;
2013-05-03 20:13:28 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.SortName))
2013-09-04 10:02:19 -07:00
{
dto.SortName = item.SortName;
}
2013-05-03 20:13:28 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.CustomRating))
2013-09-04 10:02:19 -07:00
{
dto.CustomRating = item.CustomRating;
}
2013-05-03 20:13:28 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.Taglines))
2013-09-04 10:02:19 -07:00
{
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrEmpty(item.Tagline))
{
dto.Taglines = new string[] { item.Tagline };
}
if (dto.Taglines == null)
{
2018-09-12 10:26:21 -07:00
dto.Taglines = Array.Empty<string>();
}
2013-09-04 10:02:19 -07:00
}
2013-05-03 20:13:28 -07:00
2013-11-21 13:48:26 -07:00
dto.Type = item.GetClientTypeName();
if ((item.CommunityRating ?? 0) > 0)
{
dto.CommunityRating = item.CommunityRating;
}
2014-11-30 12:01:33 -07:00
var supportsPlaceHolders = item as ISupportsPlaceHolders;
2017-06-12 00:11:54 -07:00
if (supportsPlaceHolders != null && supportsPlaceHolders.IsPlaceHolder)
{
dto.IsPlaceHolder = supportsPlaceHolders.IsPlaceHolder;
}
2014-03-22 09:16:43 -07:00
2013-09-04 10:02:19 -07:00
// Add audio info
var audio = item as Audio;
if (audio != null)
2013-02-20 18:33:05 -07:00
{
2013-09-04 10:02:19 -07:00
dto.Album = audio.Album;
2017-06-11 13:40:25 -07:00
if (audio.ExtraType.HasValue)
{
dto.ExtraType = audio.ExtraType.Value.ToString();
}
2013-02-20 18:33:05 -07:00
2015-05-04 07:35:38 -07:00
var albumParent = audio.AlbumEntity;
2013-02-20 18:33:05 -07:00
2013-09-04 10:02:19 -07:00
if (albumParent != null)
{
2018-09-12 10:26:21 -07:00
dto.AlbumId = albumParent.Id;
2013-02-20 18:33:05 -07:00
2014-02-07 13:30:41 -07:00
dto.AlbumPrimaryImageTag = GetImageCacheTag(albumParent, ImageType.Primary);
2013-02-20 18:33:05 -07:00
}
2014-03-20 20:31:40 -07:00
2018-09-12 10:26:21 -07:00
//if (options.ContainsField(ItemFields.MediaSourceCount))
2014-12-01 05:43:34 -07:00
//{
2015-01-24 12:03:55 -07:00
// Songs always have one
2014-12-01 05:43:34 -07:00
//}
2013-02-20 18:33:05 -07:00
}
2019-08-29 13:28:33 -07:00
if (item is IHasArtist hasArtist)
2015-03-13 10:25:28 -07:00
{
dto.Artists = hasArtist.Artists;
2016-12-13 00:36:30 -07:00
//var artistItems = _libraryManager.GetArtists(new InternalItemsQuery
//{
// EnableTotalRecordCount = false,
// ItemIds = new[] { item.Id.ToString("N", CultureInfo.InvariantCulture) }
2016-12-13 00:36:30 -07:00
//});
//dto.ArtistItems = artistItems.Items
// .Select(i =>
// {
// var artist = i.Item1;
// return new NameIdPair
// {
// Name = artist.Name,
// Id = artist.Id.ToString("N", CultureInfo.InvariantCulture)
2016-12-13 00:36:30 -07:00
// };
// })
// .ToList();
2016-09-05 22:02:05 -07:00
// Include artists that are not in the database yet, e.g., just added via metadata editor
2016-12-13 00:36:30 -07:00
//var foundArtists = artistItems.Items.Select(i => i.Item1.Name).ToList();
2017-08-10 23:29:49 -07:00
dto.ArtistItems = hasArtist.Artists
2016-12-13 00:36:30 -07:00
//.Except(foundArtists, new DistinctNameComparer())
2016-09-05 22:02:05 -07:00
.Select(i =>
{
2016-10-06 11:55:01 -07:00
// This should not be necessary but we're seeing some cases of it
2018-09-12 10:26:21 -07:00
if (string.IsNullOrEmpty(i))
2016-10-06 11:55:01 -07:00
{
return null;
}
2017-05-21 21:54:02 -07:00
var artist = _libraryManager.GetArtist(i, new DtoOptions(false)
{
EnableImages = false
});
2016-09-05 22:02:05 -07:00
if (artist != null)
{
2018-09-12 10:26:21 -07:00
return new NameGuidPair
2016-09-05 22:02:05 -07:00
{
Name = artist.Name,
2018-09-12 10:26:21 -07:00
Id = artist.Id
2016-09-05 22:02:05 -07:00
};
}
return null;
2017-08-10 23:29:49 -07:00
}).Where(i => i != null).ToArray();
2015-03-13 10:25:28 -07:00
}
2015-03-13 10:25:28 -07:00
var hasAlbumArtist = item as IHasAlbumArtist;
if (hasAlbumArtist != null)
{
2014-06-23 09:05:19 -07:00
dto.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault();
2015-03-13 08:54:20 -07:00
2016-12-13 00:36:30 -07:00
//var artistItems = _libraryManager.GetAlbumArtists(new InternalItemsQuery
//{
// EnableTotalRecordCount = false,
// ItemIds = new[] { item.Id.ToString("N", CultureInfo.InvariantCulture) }
2016-12-13 00:36:30 -07:00
//});
//dto.AlbumArtists = artistItems.Items
// .Select(i =>
// {
// var artist = i.Item1;
// return new NameIdPair
// {
// Name = artist.Name,
// Id = artist.Id.ToString("N", CultureInfo.InvariantCulture)
2016-12-13 00:36:30 -07:00
// };
// })
// .ToList();
2017-08-10 11:01:31 -07:00
dto.AlbumArtists = hasAlbumArtist.AlbumArtists
2016-12-13 00:36:30 -07:00
//.Except(foundArtists, new DistinctNameComparer())
2015-03-13 08:54:20 -07:00
.Select(i =>
{
2016-12-13 00:36:30 -07:00
// This should not be necessary but we're seeing some cases of it
2018-09-12 10:26:21 -07:00
if (string.IsNullOrEmpty(i))
2015-03-13 08:54:20 -07:00
{
2016-12-13 00:36:30 -07:00
return null;
}
2017-05-21 21:54:02 -07:00
var artist = _libraryManager.GetArtist(i, new DtoOptions(false)
{
EnableImages = false
});
2016-12-13 00:36:30 -07:00
if (artist != null)
{
2018-09-12 10:26:21 -07:00
return new NameGuidPair
2016-12-13 00:36:30 -07:00
{
Name = artist.Name,
2018-09-12 10:26:21 -07:00
Id = artist.Id
2016-12-13 00:36:30 -07:00
};
}
return null;
2017-08-10 11:01:31 -07:00
}).Where(i => i != null).ToArray();
}
2013-09-04 10:02:19 -07:00
// Add video info
var video = item as Video;
if (video != null)
2013-02-20 18:33:05 -07:00
{
2013-09-04 10:02:19 -07:00
dto.VideoType = video.VideoType;
dto.Video3DFormat = video.Video3DFormat;
dto.IsoType = video.IsoType;
2013-02-20 18:33:05 -07:00
2015-10-24 08:33:22 -07:00
if (video.HasSubtitles)
{
dto.HasSubtitles = video.HasSubtitles;
}
2017-08-10 11:01:31 -07:00
if (video.AdditionalParts.Length != 0)
2014-11-30 12:01:33 -07:00
{
2017-08-10 11:01:31 -07:00
dto.PartCount = video.AdditionalParts.Length + 1;
2014-11-30 12:01:33 -07:00
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.MediaSourceCount))
2014-11-30 12:01:33 -07:00
{
2016-05-01 13:56:06 -07:00
var mediaSourceCount = video.MediaSourceCount;
if (mediaSourceCount != 1)
2014-12-01 05:43:34 -07:00
{
2016-05-01 13:56:06 -07:00
dto.MediaSourceCount = mediaSourceCount;
2014-12-01 05:43:34 -07:00
}
2014-11-30 12:01:33 -07:00
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.Chapters))
2014-03-20 08:55:22 -07:00
{
2018-09-12 10:26:21 -07:00
dto.Chapters = _itemRepo.GetChapters(item);
2014-03-20 08:55:22 -07:00
}
2016-03-18 21:22:33 -07:00
2017-06-11 13:40:25 -07:00
if (video.ExtraType.HasValue)
{
dto.ExtraType = video.ExtraType.Value.ToString();
}
2013-02-20 18:33:05 -07:00
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.MediaStreams))
2013-02-20 18:33:05 -07:00
{
2013-09-04 10:02:19 -07:00
// Add VideoInfo
2014-06-11 12:31:33 -07:00
var iHasMediaSources = item as IHasMediaSources;
2013-02-20 18:33:05 -07:00
2014-06-11 12:31:33 -07:00
if (iHasMediaSources != null)
2013-09-04 10:02:19 -07:00
{
2017-08-19 12:43:35 -07:00
MediaStream[] mediaStreams;
2014-03-20 20:31:40 -07:00
2018-09-12 10:26:21 -07:00
if (dto.MediaSources != null && dto.MediaSources.Length > 0)
2013-12-05 20:39:44 -07:00
{
2018-09-12 10:26:21 -07: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 10:26:21 -07:00
.SelectMany(i => i.MediaStreams)
.ToArray();
}
2014-03-20 20:31:40 -07:00
}
else
{
mediaStreams = _mediaSourceManager().GetStaticMediaSources(item, true)[0].MediaStreams.ToArray();
2014-03-20 20:31:40 -07:00
}
2013-12-05 20:39:44 -07:00
2014-03-20 20:31:40 -07:00
dto.MediaStreams = mediaStreams;
2013-09-04 10:02:19 -07:00
}
}
2018-09-12 10:26:21 -07:00
BaseItem[] allExtras = null;
if (options.ContainsField(ItemFields.SpecialFeatureCount))
{
2018-09-12 10:26:21 -07:00
if (allExtras == null)
{
allExtras = item.GetExtras().ToArray();
}
2018-09-12 10:26:21 -07:00
dto.SpecialFeatureCount = allExtras.Count(i => i.ExtraType.HasValue && BaseItem.DisplayExtraTypes.Contains(i.ExtraType.Value));
}
if (options.ContainsField(ItemFields.LocalTrailerCount))
{
int trailerCount = 0;
2018-09-12 10:26:21 -07:00
if (allExtras == null)
{
2018-09-12 10:26:21 -07:00
allExtras = item.GetExtras().ToArray();
}
2018-09-12 10:26:21 -07:00
trailerCount += allExtras.Count(i => i.ExtraType.HasValue && i.ExtraType.Value == ExtraType.Trailer);
if (item is IHasTrailers hasTrailers)
{
trailerCount += hasTrailers.GetTrailerCount();
}
dto.LocalTrailerCount = trailerCount;
2013-02-20 18:33:05 -07:00
}
2013-09-04 10:02:19 -07:00
// Add EpisodeInfo
if (item is Episode episode)
2013-02-20 18:33:05 -07:00
{
2013-09-04 10:02:19 -07:00
dto.IndexNumberEnd = episode.IndexNumberEnd;
2016-05-22 15:37:50 -07:00
dto.SeriesName = episode.SeriesName;
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.SpecialEpisodeNumbers))
2014-12-18 21:20:07 -07:00
{
dto.AirsAfterSeasonNumber = episode.AirsAfterSeasonNumber;
dto.AirsBeforeEpisodeNumber = episode.AirsBeforeEpisodeNumber;
dto.AirsBeforeSeasonNumber = episode.AirsBeforeSeasonNumber;
}
2016-07-04 13:11:30 -07:00
dto.SeasonName = episode.SeasonName;
2018-09-12 10:26:21 -07:00
dto.SeasonId = episode.SeasonId;
dto.SeriesId = episode.SeriesId;
2016-07-04 23:01:31 -07:00
2016-07-04 22:40:18 -07:00
Series episodeSeries = null;
2016-05-22 15:37:50 -07:00
2019-10-05 09:24:04 -07:00
// this block will add the series poster for episodes without a poster
// TODO maybe remove the if statement entirely
//if (options.ContainsField(ItemFields.SeriesPrimaryImage))
2016-07-04 22:40:18 -07:00
{
episodeSeries = episodeSeries ?? episode.Series;
if (episodeSeries != null)
2014-12-01 05:43:34 -07:00
{
2016-05-22 15:37:50 -07:00
dto.SeriesPrimaryImageTag = GetImageCacheTag(episodeSeries, ImageType.Primary);
2014-12-01 05:43:34 -07:00
}
2016-07-04 22:40:18 -07:00
}
2014-12-01 05:43:34 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.SeriesStudio))
2016-07-04 22:40:18 -07:00
{
episodeSeries = episodeSeries ?? episode.Series;
if (episodeSeries != null)
2014-12-01 05:43:34 -07:00
{
2016-05-22 15:37:50 -07:00
dto.SeriesStudio = episodeSeries.Studios.FirstOrDefault();
2014-12-01 05:43:34 -07:00
}
}
}
2013-02-20 18:33:05 -07:00
2016-05-22 15:37:50 -07:00
// Add SeriesInfo
if (item is Series series)
2016-05-22 15:37:50 -07:00
{
2017-08-13 13:15:07 -07:00
dto.AirDays = series.AirDays;
dto.AirTime = series.AirTime;
dto.Status = series.Status.HasValue ? series.Status.Value.ToString() : null;
2016-05-22 15:37:50 -07:00
}
2013-09-04 10:02:19 -07:00
// Add SeasonInfo
if (item is Season season)
2013-02-20 18:33:05 -07:00
{
2016-07-04 13:11:30 -07:00
dto.SeriesName = season.SeriesName;
2018-09-12 10:26:21 -07:00
dto.SeriesId = season.SeriesId;
2016-07-04 23:01:31 -07:00
series = null;
2016-07-04 13:11:30 -07:00
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.SeriesStudio))
2016-07-04 23:01:31 -07:00
{
series = series ?? season.Series;
if (series != null)
2016-07-04 13:11:30 -07:00
{
dto.SeriesStudio = series.Studios.FirstOrDefault();
}
2016-07-04 23:01:31 -07:00
}
2013-11-09 11:44:09 -07:00
2019-10-05 09:24:04 -07:00
// this block will add the series poster for seasons without a poster
// TODO maybe remove the if statement entirely
//if (options.ContainsField(ItemFields.SeriesPrimaryImage))
2016-07-04 23:01:31 -07:00
{
series = series ?? season.Series;
if (series != null)
2014-12-01 05:43:34 -07:00
{
dto.SeriesPrimaryImageTag = GetImageCacheTag(series, ImageType.Primary);
}
}
2013-02-20 18:33:05 -07:00
}
if (item is MusicVideo musicVideo)
2013-09-04 10:02:19 -07:00
{
SetMusicVideoProperties(dto, musicVideo);
2013-02-20 18:33:05 -07:00
}
if (item is Book book)
2013-09-04 10:02:19 -07:00
{
SetBookProperties(dto, book);
}
2018-09-12 10:26:21 -07:00
if (options.ContainsField(ItemFields.ProductionLocations))
2016-10-08 11:51:07 -07:00
{
if (item.ProductionLocations.Length > 0 || item is Movie)
{
dto.ProductionLocations = item.ProductionLocations;
}
2016-10-08 11:51:07 -07:00
}
2018-09-12 10:26:21 -07: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-16 21:08:33 -07:00
dto.ChannelId = item.ChannelId;
2015-10-29 06:28:05 -07:00
2018-09-12 10:26:21 -07:00
if (item.SourceType == SourceType.Channel)
2014-05-17 14:23:48 -07:00
{
2016-03-18 22:04:38 -07:00
var channel = _libraryManager.GetItemById(item.ChannelId);
if (channel != null)
{
dto.ChannelName = channel.Name;
}
2014-05-17 14:23:48 -07:00
}
}
2017-06-29 12:10:58 -07:00
private BaseItem GetImageDisplayParent(BaseItem currentItem, BaseItem originalItem)
2016-12-17 01:27:41 -07:00
{
if (currentItem is MusicAlbum musicAlbum)
2016-12-17 01:27:41 -07:00
{
2017-05-21 21:54:02 -07:00
var artist = musicAlbum.GetMusicArtist(new DtoOptions(false));
2016-12-17 01:27:41 -07:00
if (artist != null)
{
return artist;
}
}
2017-01-15 14:29:00 -07:00
2018-09-12 10:26:21 -07:00
var parent = currentItem.DisplayParent ?? currentItem.GetOwner() ?? currentItem.GetParent();
2017-06-29 12:10:58 -07:00
if (parent == null && !(originalItem is UserRootFolder) && !(originalItem is UserView) && !(originalItem is AggregateFolder) && !(originalItem is ICollectionFolder) && !(originalItem is Channel))
{
parent = _libraryManager.GetCollectionFolders(originalItem).FirstOrDefault();
}
return parent;
2016-12-17 01:27:41 -07:00
}
2016-07-04 22:40:18 -07:00
private void AddInheritedImages(BaseItemDto dto, BaseItem item, DtoOptions options, BaseItem owner)
{
2016-11-21 01:54:53 -07:00
if (!item.SupportsInheritedParentImages)
{
return;
}
2016-07-04 22:40:18 -07: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 01:54:53 -07:00
// For now. Emby apps are not using this
artLimit = 0;
2016-07-04 22:40:18 -07:00
if (logoLimit == 0 && artLimit == 0 && thumbLimit == 0 && backdropLimit == 0)
{
return;
}
BaseItem parent = null;
var isFirst = true;
2017-08-10 13:06:36 -07:00
var imageTags = dto.ImageTags;
while (((!(imageTags != null && imageTags.ContainsKey(ImageType.Logo)) && logoLimit > 0) || (!(imageTags != null && imageTags.ContainsKey(ImageType.Art)) && artLimit > 0) || (!(imageTags != null && imageTags.ContainsKey(ImageType.Thumb)) && thumbLimit > 0) || parent is Series) &&
2017-06-29 12:10:58 -07:00
(parent = parent ?? (isFirst ? GetImageDisplayParent(item, item) ?? owner : parent)) != null)
2016-07-04 22:40:18 -07:00
{
2016-07-07 20:22:02 -07:00
if (parent == null)
{
break;
}
var allImages = parent.ImageInfos;
2017-08-10 13:06:36 -07:00
if (logoLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Logo)) && dto.ParentLogoItemId == null)
2016-07-04 22:40:18 -07:00
{
2016-07-07 20:22:02 -07:00
var image = allImages.FirstOrDefault(i => i.Type == ImageType.Logo);
2016-07-04 22:40:18 -07:00
if (image != null)
{
dto.ParentLogoItemId = GetDtoId(parent);
dto.ParentLogoImageTag = GetImageCacheTag(parent, image);
}
}
2017-08-10 13:06:36 -07:00
if (artLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Art)) && dto.ParentArtItemId == null)
2016-07-04 22:40:18 -07:00
{
2016-07-07 20:22:02 -07:00
var image = allImages.FirstOrDefault(i => i.Type == ImageType.Art);
2016-07-04 22:40:18 -07:00
if (image != null)
{
dto.ParentArtItemId = GetDtoId(parent);
dto.ParentArtImageTag = GetImageCacheTag(parent, image);
}
}
2017-08-10 13:06:36 -07:00
if (thumbLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Thumb)) && (dto.ParentThumbItemId == null || parent is Series) && !(parent is ICollectionFolder) && !(parent is UserView))
2016-07-04 22:40:18 -07:00
{
2016-07-07 20:22:02 -07:00
var image = allImages.FirstOrDefault(i => i.Type == ImageType.Thumb);
2016-07-04 22:40:18 -07:00
if (image != null)
{
dto.ParentThumbItemId = GetDtoId(parent);
dto.ParentThumbImageTag = GetImageCacheTag(parent, image);
}
}
2017-08-10 13:06:36 -07:00
if (backdropLimit > 0 && !((dto.BackdropImageTags != null && dto.BackdropImageTags.Length > 0) || (dto.ParentBackdropImageTags != null && dto.ParentBackdropImageTags.Length > 0)))
2016-07-04 22:40:18 -07:00
{
2016-07-07 20:22:02 -07:00
var images = allImages.Where(i => i.Type == ImageType.Backdrop).Take(backdropLimit).ToList();
2016-07-04 22:40:18 -07:00
if (images.Count > 0)
{
dto.ParentBackdropItemId = GetDtoId(parent);
dto.ParentBackdropImageTags = GetImageTags(parent, images);
}
}
isFirst = false;
2016-11-21 01:54:53 -07:00
if (!parent.SupportsInheritedParentImages)
{
break;
}
2017-06-29 12:10:58 -07:00
parent = GetImageDisplayParent(parent, item);
2016-07-04 22:40:18 -07:00
}
}
2017-02-20 13:50:58 -07:00
private string GetMappedPath(BaseItem item, BaseItem ownerItem)
2014-03-20 08:55:22 -07:00
{
2014-03-20 20:31:40 -07:00
var path = item.Path;
2014-03-20 08:55:22 -07:00
2018-09-12 10:26:21 -07:00
if (item.IsFileProtocol)
2014-03-20 08:55:22 -07:00
{
2017-02-20 13:50:58 -07:00
path = _libraryManager.GetPathAfterNetworkSubstitution(path, ownerItem ?? item);
2014-03-20 08:55:22 -07:00
}
return path;
}
2013-02-20 18:33:05 -07:00
/// <summary>
2013-09-04 10:02:19 -07:00
/// Attaches the primary image aspect ratio.
2013-02-20 18:33:05 -07:00
/// </summary>
2013-09-04 10:02:19 -07:00
/// <param name="dto">The dto.</param>
2013-02-20 18:33:05 -07:00
/// <param name="item">The item.</param>
2013-09-04 10:02:19 -07:00
/// <returns>Task.</returns>
2017-10-21 23:22:43 -07:00
public void AttachPrimaryImageAspectRatio(IItemDto dto, BaseItem item)
2016-01-15 22:01:57 -07:00
{
dto.PrimaryImageAspectRatio = GetPrimaryImageAspectRatio(item);
}
2017-10-21 23:22:43 -07:00
public double? GetPrimaryImageAspectRatio(BaseItem item)
2013-02-20 18:33:05 -07:00
{
2014-02-07 13:30:41 -07:00
var imageInfo = item.GetImageInfo(ImageType.Primary, 0);
2013-05-04 21:49:49 -07:00
2017-06-11 13:40:25 -07:00
if (imageInfo == null)
2013-09-04 10:02:19 -07:00
{
2016-01-15 22:01:57 -07:00
return null;
2013-09-04 10:02:19 -07:00
}
var supportedEnhancers = _imageProcessor.GetSupportedEnhancers(item, ImageType.Primary).ToArray();
2017-02-10 13:06:52 -07:00
ImageDimensions size;
2017-05-15 14:14:51 -07:00
var defaultAspectRatio = item.GetDefaultPrimaryImageAspectRatio();
2017-02-10 13:06:52 -07:00
2018-09-12 10:26:21 -07:00
if (defaultAspectRatio > 0)
2017-05-15 14:14:51 -07:00
{
2018-09-12 10:26:21 -07:00
if (supportedEnhancers.Length == 0)
2017-02-10 13:06:52 -07:00
{
2018-09-12 10:26:21 -07:00
return defaultAspectRatio;
2017-02-10 13:06:52 -07:00
}
int dummyWidth = 200;
int dummyHeight = Convert.ToInt32(dummyWidth / defaultAspectRatio);
size = new ImageDimensions(dummyWidth, dummyHeight);
2013-09-04 10:02:19 -07:00
}
2017-05-15 14:14:51 -07:00
else
{
2017-06-11 13:40:25 -07:00
if (!imageInfo.IsLocalFile)
{
return null;
}
2017-05-15 14:14:51 -07:00
try
{
size = _imageProcessor.GetImageDimensions(item, imageInfo);
2017-10-21 23:22:43 -07:00
if (size.Width <= 0 || size.Height <= 0)
{
return null;
}
2017-05-15 14:14:51 -07:00
}
2017-10-22 17:43:52 -07:00
catch (Exception ex)
2017-05-15 14:14:51 -07:00
{
2018-12-20 05:11:26 -07:00
_logger.LogError(ex, "Failed to determine primary image aspect ratio for {0}", imageInfo.Path);
2017-05-15 14:14:51 -07:00
return null;
}
2013-09-04 10:02:19 -07:00
}
foreach (var enhancer in supportedEnhancers)
{
try
{
size = enhancer.GetEnhancedImageSize(item, ImageType.Primary, 0, size);
}
catch (Exception ex)
{
2018-12-20 05:11:26 -07:00
_logger.LogError(ex, "Error in image enhancer: {0}", enhancer.GetType().Name);
2013-09-04 10:02:19 -07:00
}
2013-05-04 21:49:49 -07:00
}
2013-09-04 10:02:19 -07:00
2016-01-21 01:23:02 -07:00
var width = size.Width;
var height = size.Height;
if (width <= 0 || height <= 0)
2015-01-30 14:17:19 -07:00
{
2016-01-21 01:23:02 -07:00
return null;
2015-01-30 14:17:19 -07:00
}
2016-01-21 01:23:02 -07:00
return width / height;
2013-05-04 21:49:49 -07:00
}
2013-02-20 18:33:05 -07:00
}
}