| | | 1 | | #pragma warning disable CS1591 |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Frozen; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Linq; |
| | | 9 | | using Jellyfin.Data.Enums; |
| | | 10 | | using Jellyfin.Database.Implementations.Entities; |
| | | 11 | | using Jellyfin.Extensions; |
| | | 12 | | using MediaBrowser.Common; |
| | | 13 | | using MediaBrowser.Controller.Channels; |
| | | 14 | | using MediaBrowser.Controller.Chapters; |
| | | 15 | | using MediaBrowser.Controller.Drawing; |
| | | 16 | | using MediaBrowser.Controller.Dto; |
| | | 17 | | using MediaBrowser.Controller.Entities; |
| | | 18 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 19 | | using MediaBrowser.Controller.Library; |
| | | 20 | | using MediaBrowser.Controller.LiveTv; |
| | | 21 | | using MediaBrowser.Controller.Playlists; |
| | | 22 | | using MediaBrowser.Controller.Providers; |
| | | 23 | | using MediaBrowser.Controller.Trickplay; |
| | | 24 | | using MediaBrowser.Model.Dto; |
| | | 25 | | using MediaBrowser.Model.Entities; |
| | | 26 | | using MediaBrowser.Model.Querying; |
| | | 27 | | using Microsoft.Extensions.Logging; |
| | | 28 | | using Book = MediaBrowser.Controller.Entities.Book; |
| | | 29 | | using Episode = MediaBrowser.Controller.Entities.TV.Episode; |
| | | 30 | | using Movie = MediaBrowser.Controller.Entities.Movies.Movie; |
| | | 31 | | using MusicAlbum = MediaBrowser.Controller.Entities.Audio.MusicAlbum; |
| | | 32 | | using Person = MediaBrowser.Controller.Entities.Person; |
| | | 33 | | using Photo = MediaBrowser.Controller.Entities.Photo; |
| | | 34 | | using Season = MediaBrowser.Controller.Entities.TV.Season; |
| | | 35 | | using Series = MediaBrowser.Controller.Entities.TV.Series; |
| | | 36 | | |
| | | 37 | | namespace Emby.Server.Implementations.Dto |
| | | 38 | | { |
| | | 39 | | public class DtoService : IDtoService |
| | | 40 | | { |
| | 1 | 41 | | private static readonly FrozenDictionary<BaseItemKind, BaseItemKind[]> _relatedItemKinds = new Dictionary<BaseIt |
| | 1 | 42 | | { |
| | 1 | 43 | | { |
| | 1 | 44 | | BaseItemKind.Genre, [ |
| | 1 | 45 | | BaseItemKind.Audio, |
| | 1 | 46 | | BaseItemKind.Episode, |
| | 1 | 47 | | BaseItemKind.Movie, |
| | 1 | 48 | | BaseItemKind.LiveTvProgram, |
| | 1 | 49 | | BaseItemKind.MusicAlbum, |
| | 1 | 50 | | BaseItemKind.MusicArtist, |
| | 1 | 51 | | BaseItemKind.MusicVideo, |
| | 1 | 52 | | BaseItemKind.Series, |
| | 1 | 53 | | BaseItemKind.Trailer |
| | 1 | 54 | | ] |
| | 1 | 55 | | }, |
| | 1 | 56 | | { |
| | 1 | 57 | | BaseItemKind.MusicArtist, [ |
| | 1 | 58 | | BaseItemKind.Audio, |
| | 1 | 59 | | BaseItemKind.MusicAlbum, |
| | 1 | 60 | | BaseItemKind.MusicVideo |
| | 1 | 61 | | ] |
| | 1 | 62 | | }, |
| | 1 | 63 | | { |
| | 1 | 64 | | BaseItemKind.MusicGenre, [ |
| | 1 | 65 | | BaseItemKind.Audio, |
| | 1 | 66 | | BaseItemKind.MusicAlbum, |
| | 1 | 67 | | BaseItemKind.MusicArtist, |
| | 1 | 68 | | BaseItemKind.MusicVideo |
| | 1 | 69 | | ] |
| | 1 | 70 | | }, |
| | 1 | 71 | | { |
| | 1 | 72 | | BaseItemKind.Person, [ |
| | 1 | 73 | | BaseItemKind.Audio, |
| | 1 | 74 | | BaseItemKind.Episode, |
| | 1 | 75 | | BaseItemKind.Movie, |
| | 1 | 76 | | BaseItemKind.LiveTvProgram, |
| | 1 | 77 | | BaseItemKind.MusicAlbum, |
| | 1 | 78 | | BaseItemKind.MusicArtist, |
| | 1 | 79 | | BaseItemKind.MusicVideo, |
| | 1 | 80 | | BaseItemKind.Series, |
| | 1 | 81 | | BaseItemKind.Trailer |
| | 1 | 82 | | ] |
| | 1 | 83 | | }, |
| | 1 | 84 | | { |
| | 1 | 85 | | BaseItemKind.Studio, [ |
| | 1 | 86 | | BaseItemKind.Audio, |
| | 1 | 87 | | BaseItemKind.Episode, |
| | 1 | 88 | | BaseItemKind.Movie, |
| | 1 | 89 | | BaseItemKind.LiveTvProgram, |
| | 1 | 90 | | BaseItemKind.MusicAlbum, |
| | 1 | 91 | | BaseItemKind.MusicArtist, |
| | 1 | 92 | | BaseItemKind.MusicVideo, |
| | 1 | 93 | | BaseItemKind.Series, |
| | 1 | 94 | | BaseItemKind.Trailer |
| | 1 | 95 | | ] |
| | 1 | 96 | | }, |
| | 1 | 97 | | { |
| | 1 | 98 | | BaseItemKind.Year, [ |
| | 1 | 99 | | BaseItemKind.Audio, |
| | 1 | 100 | | BaseItemKind.Episode, |
| | 1 | 101 | | BaseItemKind.Movie, |
| | 1 | 102 | | BaseItemKind.LiveTvProgram, |
| | 1 | 103 | | BaseItemKind.MusicAlbum, |
| | 1 | 104 | | BaseItemKind.MusicArtist, |
| | 1 | 105 | | BaseItemKind.MusicVideo, |
| | 1 | 106 | | BaseItemKind.Series, |
| | 1 | 107 | | BaseItemKind.Trailer |
| | 1 | 108 | | ] |
| | 1 | 109 | | } |
| | 1 | 110 | | }.ToFrozenDictionary(); |
| | | 111 | | |
| | | 112 | | private readonly ILogger<DtoService> _logger; |
| | | 113 | | private readonly ILibraryManager _libraryManager; |
| | | 114 | | private readonly IUserDataManager _userDataRepository; |
| | | 115 | | |
| | | 116 | | private readonly IImageProcessor _imageProcessor; |
| | | 117 | | private readonly IProviderManager _providerManager; |
| | | 118 | | private readonly IRecordingsManager _recordingsManager; |
| | | 119 | | |
| | | 120 | | private readonly IApplicationHost _appHost; |
| | | 121 | | private readonly IMediaSourceManager _mediaSourceManager; |
| | | 122 | | private readonly Lazy<ILiveTvManager> _livetvManagerFactory; |
| | | 123 | | |
| | | 124 | | private readonly ITrickplayManager _trickplayManager; |
| | | 125 | | private readonly IChapterManager _chapterManager; |
| | | 126 | | |
| | | 127 | | public DtoService( |
| | | 128 | | ILogger<DtoService> logger, |
| | | 129 | | ILibraryManager libraryManager, |
| | | 130 | | IUserDataManager userDataRepository, |
| | | 131 | | IImageProcessor imageProcessor, |
| | | 132 | | IProviderManager providerManager, |
| | | 133 | | IRecordingsManager recordingsManager, |
| | | 134 | | IApplicationHost appHost, |
| | | 135 | | IMediaSourceManager mediaSourceManager, |
| | | 136 | | Lazy<ILiveTvManager> livetvManagerFactory, |
| | | 137 | | ITrickplayManager trickplayManager, |
| | | 138 | | IChapterManager chapterManager) |
| | | 139 | | { |
| | 21 | 140 | | _logger = logger; |
| | 21 | 141 | | _libraryManager = libraryManager; |
| | 21 | 142 | | _userDataRepository = userDataRepository; |
| | 21 | 143 | | _imageProcessor = imageProcessor; |
| | 21 | 144 | | _providerManager = providerManager; |
| | 21 | 145 | | _recordingsManager = recordingsManager; |
| | 21 | 146 | | _appHost = appHost; |
| | 21 | 147 | | _mediaSourceManager = mediaSourceManager; |
| | 21 | 148 | | _livetvManagerFactory = livetvManagerFactory; |
| | 21 | 149 | | _trickplayManager = trickplayManager; |
| | 21 | 150 | | _chapterManager = chapterManager; |
| | 21 | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | private ILiveTvManager LivetvManager => _livetvManagerFactory.Value; |
| | | 154 | | |
| | | 155 | | /// <inheritdoc /> |
| | | 156 | | public IReadOnlyList<BaseItemDto> GetBaseItemDtos( |
| | | 157 | | IReadOnlyList<BaseItem> items, |
| | | 158 | | DtoOptions options, |
| | | 159 | | User? user = null, |
| | | 160 | | BaseItem? owner = null, |
| | | 161 | | bool skipVisibilityCheck = false) |
| | | 162 | | { |
| | 4 | 163 | | var accessibleItems = skipVisibilityCheck || user is null ? items : items.Where(x => x.IsVisible(user)).ToLi |
| | 4 | 164 | | var returnItems = new BaseItemDto[accessibleItems.Count]; |
| | 4 | 165 | | List<(BaseItem, BaseItemDto)>? programTuples = null; |
| | 4 | 166 | | List<(BaseItemDto, LiveTvChannel)>? channelTuples = null; |
| | | 167 | | |
| | | 168 | | // Batch-fetch user data for all items |
| | 4 | 169 | | Dictionary<Guid, UserItemData>? userDataBatch = null; |
| | 4 | 170 | | if (user is not null && options.EnableUserData) |
| | | 171 | | { |
| | 4 | 172 | | userDataBatch = _userDataRepository.GetUserDataBatch(accessibleItems, user); |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | // Pre-compute collection folders once to avoid N+1 queries in CanDelete |
| | 4 | 176 | | List<Folder>? allCollectionFolders = null; |
| | 4 | 177 | | if (user is not null && options.ContainsField(ItemFields.CanDelete)) |
| | | 178 | | { |
| | 0 | 179 | | allCollectionFolders = _libraryManager.GetUserRootFolder().Children.OfType<Folder>().ToList(); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | // Batch-fetch child counts for all folders to avoid N+1 queries |
| | 4 | 183 | | Dictionary<Guid, int>? childCountBatch = null; |
| | 4 | 184 | | if (options.ContainsField(ItemFields.ChildCount)) |
| | | 185 | | { |
| | 0 | 186 | | var folderIds = accessibleItems.OfType<Folder>().Select(f => f.Id).ToList(); |
| | 0 | 187 | | if (folderIds.Count > 0) |
| | | 188 | | { |
| | 0 | 189 | | childCountBatch = _libraryManager.GetChildCountBatch(folderIds, user?.Id); |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | // Batch-fetch played/total counts for all folders to avoid N+1 queries |
| | 4 | 194 | | Dictionary<Guid, (int Played, int Total)>? playedCountBatch = null; |
| | 4 | 195 | | if (user is not null && options.EnableUserData) |
| | | 196 | | { |
| | 4 | 197 | | var folderIds = accessibleItems.OfType<Folder>() |
| | 4 | 198 | | .Where(f => f.SupportsUserDataFromChildren && (f.SupportsPlayedStatus || options.ContainsField(ItemF |
| | 4 | 199 | | .Select(f => f.Id).ToList(); |
| | 4 | 200 | | if (folderIds.Count > 0) |
| | | 201 | | { |
| | 0 | 202 | | playedCountBatch = _libraryManager.GetPlayedAndTotalCountBatch(folderIds, user); |
| | | 203 | | } |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | // Batch-fetch MusicArtist lookups across all items to avoid N+1 queries. |
| | 4 | 207 | | IReadOnlyDictionary<string, MusicArtist[]>? artistsBatch = null; |
| | 4 | 208 | | var artistNames = new HashSet<string>(StringComparer.Ordinal); |
| | 14 | 209 | | foreach (var item in accessibleItems) |
| | | 210 | | { |
| | 3 | 211 | | if (item is IHasArtist hasArtist) |
| | | 212 | | { |
| | 0 | 213 | | foreach (var name in hasArtist.Artists) |
| | | 214 | | { |
| | 0 | 215 | | if (!string.IsNullOrWhiteSpace(name)) |
| | | 216 | | { |
| | 0 | 217 | | artistNames.Add(name); |
| | | 218 | | } |
| | | 219 | | } |
| | | 220 | | } |
| | | 221 | | |
| | 3 | 222 | | if (item is IHasAlbumArtist hasAlbumArtist) |
| | | 223 | | { |
| | 0 | 224 | | foreach (var name in hasAlbumArtist.AlbumArtists) |
| | | 225 | | { |
| | 0 | 226 | | if (!string.IsNullOrWhiteSpace(name)) |
| | | 227 | | { |
| | 0 | 228 | | artistNames.Add(name); |
| | | 229 | | } |
| | | 230 | | } |
| | | 231 | | } |
| | | 232 | | } |
| | | 233 | | |
| | 4 | 234 | | if (artistNames.Count > 0) |
| | | 235 | | { |
| | 0 | 236 | | artistsBatch = _libraryManager.GetArtists(artistNames.ToArray()); |
| | | 237 | | } |
| | | 238 | | |
| | 14 | 239 | | for (int index = 0; index < accessibleItems.Count; index++) |
| | | 240 | | { |
| | 3 | 241 | | var item = accessibleItems[index]; |
| | 3 | 242 | | var dto = GetBaseItemDtoInternal( |
| | 3 | 243 | | item, |
| | 3 | 244 | | options, |
| | 3 | 245 | | user, |
| | 3 | 246 | | owner, |
| | 3 | 247 | | userDataBatch?.GetValueOrDefault(item.Id), |
| | 3 | 248 | | allCollectionFolders, |
| | 3 | 249 | | childCountBatch, |
| | 3 | 250 | | playedCountBatch, |
| | 3 | 251 | | artistsBatch); |
| | | 252 | | |
| | 3 | 253 | | if (item is LiveTvChannel tvChannel) |
| | | 254 | | { |
| | 0 | 255 | | (channelTuples ??= []).Add((dto, tvChannel)); |
| | | 256 | | } |
| | 3 | 257 | | else if (item is LiveTvProgram) |
| | | 258 | | { |
| | 0 | 259 | | (programTuples ??= []).Add((item, dto)); |
| | | 260 | | } |
| | | 261 | | |
| | 3 | 262 | | if (options.ContainsField(ItemFields.ItemCounts)) |
| | | 263 | | { |
| | 0 | 264 | | SetItemByNameInfo(dto, user); |
| | | 265 | | } |
| | | 266 | | |
| | 3 | 267 | | returnItems[index] = dto; |
| | | 268 | | } |
| | | 269 | | |
| | 4 | 270 | | if (programTuples is not null) |
| | | 271 | | { |
| | 0 | 272 | | LivetvManager.AddInfoToProgramDto(programTuples, options.Fields, user).GetAwaiter().GetResult(); |
| | | 273 | | } |
| | | 274 | | |
| | 4 | 275 | | if (channelTuples is not null) |
| | | 276 | | { |
| | 0 | 277 | | LivetvManager.AddChannelInfo(channelTuples, options, user); |
| | | 278 | | } |
| | | 279 | | |
| | 4 | 280 | | return returnItems; |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | public BaseItemDto GetBaseItemDto(BaseItem item, DtoOptions options, User? user = null, BaseItem? owner = null) |
| | | 284 | | { |
| | 6 | 285 | | var dto = GetBaseItemDtoInternal(item, options, user, owner, null); |
| | 6 | 286 | | if (item is LiveTvChannel tvChannel) |
| | | 287 | | { |
| | 0 | 288 | | LivetvManager.AddChannelInfo(new[] { (dto, tvChannel) }, options, user); |
| | | 289 | | } |
| | 6 | 290 | | else if (item is LiveTvProgram) |
| | | 291 | | { |
| | 0 | 292 | | LivetvManager.AddInfoToProgramDto(new[] { (item, dto) }, options.Fields, user).GetAwaiter().GetResult(); |
| | | 293 | | } |
| | | 294 | | |
| | 6 | 295 | | if (options.ContainsField(ItemFields.ItemCounts)) |
| | | 296 | | { |
| | 6 | 297 | | SetItemByNameInfo(dto, user); |
| | | 298 | | } |
| | | 299 | | |
| | 6 | 300 | | return dto; |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | private BaseItemDto GetBaseItemDtoInternal( |
| | | 304 | | BaseItem item, |
| | | 305 | | DtoOptions options, |
| | | 306 | | User? user = null, |
| | | 307 | | BaseItem? owner = null, |
| | | 308 | | UserItemData? userData = null, |
| | | 309 | | List<Folder>? allCollectionFolders = null, |
| | | 310 | | Dictionary<Guid, int>? childCountBatch = null, |
| | | 311 | | Dictionary<Guid, (int Played, int Total)>? playedCountBatch = null, |
| | | 312 | | IReadOnlyDictionary<string, MusicArtist[]>? artistsBatch = null) |
| | | 313 | | { |
| | 9 | 314 | | var dto = new BaseItemDto |
| | 9 | 315 | | { |
| | 9 | 316 | | ServerId = _appHost.SystemId |
| | 9 | 317 | | }; |
| | | 318 | | |
| | 9 | 319 | | if (item.SourceType == SourceType.Channel) |
| | | 320 | | { |
| | 0 | 321 | | dto.SourceType = item.SourceType.ToString(); |
| | | 322 | | } |
| | | 323 | | |
| | 9 | 324 | | if (options.ContainsField(ItemFields.People)) |
| | | 325 | | { |
| | 6 | 326 | | AttachPeople(dto, item, user); |
| | | 327 | | } |
| | | 328 | | |
| | 9 | 329 | | if (options.ContainsField(ItemFields.PrimaryImageAspectRatio)) |
| | | 330 | | { |
| | | 331 | | try |
| | | 332 | | { |
| | 6 | 333 | | AttachPrimaryImageAspectRatio(dto, item); |
| | 6 | 334 | | } |
| | 0 | 335 | | catch (Exception ex) |
| | | 336 | | { |
| | | 337 | | // Have to use a catch-all unfortunately because some .net image methods throw plain Exceptions |
| | 0 | 338 | | _logger.LogError(ex, "Error generating PrimaryImageAspectRatio for {ItemName}", item.Name); |
| | 0 | 339 | | } |
| | | 340 | | } |
| | | 341 | | |
| | 9 | 342 | | if (options.ContainsField(ItemFields.DisplayPreferencesId)) |
| | | 343 | | { |
| | 6 | 344 | | dto.DisplayPreferencesId = item.DisplayPreferencesId.ToString("N", CultureInfo.InvariantCulture); |
| | | 345 | | } |
| | | 346 | | |
| | 9 | 347 | | if (user is not null) |
| | | 348 | | { |
| | 9 | 349 | | AttachUserSpecificInfo( |
| | 9 | 350 | | dto, |
| | 9 | 351 | | item, |
| | 9 | 352 | | user, |
| | 9 | 353 | | options, |
| | 9 | 354 | | userData, |
| | 9 | 355 | | childCountBatch, |
| | 9 | 356 | | playedCountBatch); |
| | | 357 | | } |
| | | 358 | | |
| | 9 | 359 | | if (item is IHasMediaSources |
| | 9 | 360 | | && options.ContainsField(ItemFields.MediaSources)) |
| | | 361 | | { |
| | 0 | 362 | | dto.MediaSources = _mediaSourceManager.GetStaticMediaSources(item, true, user).ToArray(); |
| | | 363 | | |
| | 0 | 364 | | NormalizeMediaSourceContainers(dto); |
| | | 365 | | } |
| | | 366 | | |
| | 9 | 367 | | if (options.ContainsField(ItemFields.Studios)) |
| | | 368 | | { |
| | 6 | 369 | | AttachStudios(dto, item); |
| | | 370 | | } |
| | | 371 | | |
| | 9 | 372 | | AttachBasicFields(dto, item, owner, options, artistsBatch); |
| | | 373 | | |
| | 9 | 374 | | if (options.ContainsField(ItemFields.CanDelete)) |
| | | 375 | | { |
| | 6 | 376 | | dto.CanDelete = user is null |
| | 6 | 377 | | ? item.CanDelete() |
| | 6 | 378 | | : allCollectionFolders is not null |
| | 6 | 379 | | ? item.CanDelete(user, allCollectionFolders) |
| | 6 | 380 | | : item.CanDelete(user); |
| | | 381 | | } |
| | | 382 | | |
| | 9 | 383 | | if (options.ContainsField(ItemFields.CanDownload)) |
| | | 384 | | { |
| | 6 | 385 | | dto.CanDownload = user is null |
| | 6 | 386 | | ? item.CanDownload() |
| | 6 | 387 | | : item.CanDownload(user); |
| | | 388 | | } |
| | | 389 | | |
| | 9 | 390 | | if (options.ContainsField(ItemFields.Etag)) |
| | | 391 | | { |
| | 6 | 392 | | dto.Etag = item.GetEtag(user); |
| | | 393 | | } |
| | | 394 | | |
| | 9 | 395 | | var activeRecording = _recordingsManager.GetActiveRecordingInfo(item.Path); |
| | 9 | 396 | | if (activeRecording is not null) |
| | | 397 | | { |
| | 0 | 398 | | dto.Type = BaseItemKind.Recording; |
| | 0 | 399 | | dto.CanDownload = false; |
| | 0 | 400 | | dto.RunTimeTicks = null; |
| | | 401 | | |
| | 0 | 402 | | if (!string.IsNullOrEmpty(dto.SeriesName)) |
| | | 403 | | { |
| | 0 | 404 | | dto.EpisodeTitle = dto.Name; |
| | 0 | 405 | | dto.Name = dto.SeriesName; |
| | | 406 | | } |
| | | 407 | | |
| | 0 | 408 | | LivetvManager.AddInfoToRecordingDto(item, dto, activeRecording, user); |
| | | 409 | | } |
| | | 410 | | |
| | 9 | 411 | | if (item is Audio audio) |
| | | 412 | | { |
| | 0 | 413 | | dto.HasLyrics = audio.GetMediaStreams().Any(s => s.Type == MediaStreamType.Lyric); |
| | | 414 | | } |
| | | 415 | | |
| | 9 | 416 | | return dto; |
| | | 417 | | } |
| | | 418 | | |
| | | 419 | | private static void NormalizeMediaSourceContainers(BaseItemDto dto) |
| | | 420 | | { |
| | 0 | 421 | | foreach (var mediaSource in dto.MediaSources) |
| | | 422 | | { |
| | 0 | 423 | | var container = mediaSource.Container; |
| | 0 | 424 | | if (string.IsNullOrEmpty(container)) |
| | | 425 | | { |
| | | 426 | | continue; |
| | | 427 | | } |
| | | 428 | | |
| | 0 | 429 | | var containers = container.Split(','); |
| | 0 | 430 | | if (containers.Length < 2) |
| | | 431 | | { |
| | | 432 | | continue; |
| | | 433 | | } |
| | | 434 | | |
| | 0 | 435 | | var path = mediaSource.Path; |
| | 0 | 436 | | string? fileExtensionContainer = null; |
| | | 437 | | |
| | 0 | 438 | | if (!string.IsNullOrEmpty(path)) |
| | | 439 | | { |
| | 0 | 440 | | path = Path.GetExtension(path); |
| | 0 | 441 | | if (!string.IsNullOrEmpty(path)) |
| | | 442 | | { |
| | 0 | 443 | | path = Path.GetExtension(path); |
| | 0 | 444 | | if (!string.IsNullOrEmpty(path)) |
| | | 445 | | { |
| | 0 | 446 | | path = path.TrimStart('.'); |
| | | 447 | | } |
| | | 448 | | |
| | 0 | 449 | | if (!string.IsNullOrEmpty(path) && containers.Contains(path, StringComparison.OrdinalIgnoreCase) |
| | | 450 | | { |
| | 0 | 451 | | fileExtensionContainer = path; |
| | | 452 | | } |
| | | 453 | | } |
| | | 454 | | } |
| | | 455 | | |
| | 0 | 456 | | mediaSource.Container = fileExtensionContainer ?? containers[0]; |
| | | 457 | | } |
| | 0 | 458 | | } |
| | | 459 | | |
| | | 460 | | /// <inheritdoc /> |
| | | 461 | | /// TODO refactor this to use the new SetItemByNameInfo. |
| | | 462 | | /// Some callers already have the counts extracted so no reason to retrieve them again. |
| | | 463 | | public BaseItemDto GetItemByNameDto(BaseItem item, DtoOptions options, List<BaseItem>? taggedItems, User? user = |
| | | 464 | | { |
| | 0 | 465 | | var dto = GetBaseItemDtoInternal(item, options, user); |
| | | 466 | | |
| | 0 | 467 | | if (options.ContainsField(ItemFields.ItemCounts) |
| | 0 | 468 | | && taggedItems is not null |
| | 0 | 469 | | && taggedItems.Count != 0) |
| | | 470 | | { |
| | 0 | 471 | | SetItemByNameInfo(item, dto, taggedItems); |
| | | 472 | | } |
| | | 473 | | |
| | 0 | 474 | | return dto; |
| | | 475 | | } |
| | | 476 | | |
| | | 477 | | private void SetItemByNameInfo(BaseItemDto dto, User? user) |
| | | 478 | | { |
| | 6 | 479 | | if (!_relatedItemKinds.TryGetValue(dto.Type, out var relatedItemKinds)) |
| | | 480 | | { |
| | 6 | 481 | | return; |
| | | 482 | | } |
| | | 483 | | |
| | 0 | 484 | | var counts = _libraryManager.GetItemCountsForNameItem(dto.Type, dto.Id, relatedItemKinds, user); |
| | | 485 | | |
| | 0 | 486 | | dto.AlbumCount = counts.AlbumCount; |
| | 0 | 487 | | dto.ArtistCount = counts.ArtistCount; |
| | 0 | 488 | | dto.EpisodeCount = counts.EpisodeCount; |
| | 0 | 489 | | dto.MovieCount = counts.MovieCount; |
| | 0 | 490 | | dto.MusicVideoCount = counts.MusicVideoCount; |
| | 0 | 491 | | dto.ProgramCount = counts.ProgramCount; |
| | 0 | 492 | | dto.SeriesCount = counts.SeriesCount; |
| | 0 | 493 | | dto.SongCount = counts.SongCount; |
| | 0 | 494 | | dto.TrailerCount = counts.TrailerCount; |
| | 0 | 495 | | dto.ChildCount = counts.TotalItemCount(); |
| | 0 | 496 | | } |
| | | 497 | | |
| | | 498 | | private static void SetItemByNameInfo(BaseItem item, BaseItemDto dto, IReadOnlyList<BaseItem> taggedItems) |
| | | 499 | | { |
| | 0 | 500 | | if (item is MusicArtist) |
| | | 501 | | { |
| | 0 | 502 | | dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum); |
| | 0 | 503 | | dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo); |
| | 0 | 504 | | dto.SongCount = taggedItems.Count(i => i is Audio); |
| | | 505 | | } |
| | 0 | 506 | | else if (item is MusicGenre) |
| | | 507 | | { |
| | 0 | 508 | | dto.ArtistCount = taggedItems.Count(i => i is MusicArtist); |
| | 0 | 509 | | dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum); |
| | 0 | 510 | | dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo); |
| | 0 | 511 | | dto.SongCount = taggedItems.Count(i => i is Audio); |
| | | 512 | | } |
| | | 513 | | else |
| | | 514 | | { |
| | | 515 | | // This populates them all and covers Genre, Person, Studio, Year |
| | | 516 | | |
| | 0 | 517 | | dto.ArtistCount = taggedItems.Count(i => i is MusicArtist); |
| | 0 | 518 | | dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum); |
| | 0 | 519 | | dto.EpisodeCount = taggedItems.Count(i => i is Episode); |
| | 0 | 520 | | dto.MovieCount = taggedItems.Count(i => i is Movie); |
| | 0 | 521 | | dto.TrailerCount = taggedItems.Count(i => i is Trailer); |
| | 0 | 522 | | dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo); |
| | 0 | 523 | | dto.SeriesCount = taggedItems.Count(i => i is Series); |
| | 0 | 524 | | dto.ProgramCount = taggedItems.Count(i => i is LiveTvProgram); |
| | 0 | 525 | | dto.SongCount = taggedItems.Count(i => i is Audio); |
| | | 526 | | } |
| | | 527 | | |
| | 0 | 528 | | dto.ChildCount = taggedItems.Count; |
| | 0 | 529 | | } |
| | | 530 | | |
| | | 531 | | /// <summary> |
| | | 532 | | /// Attaches the user specific info. |
| | | 533 | | /// </summary> |
| | | 534 | | private void AttachUserSpecificInfo( |
| | | 535 | | BaseItemDto dto, |
| | | 536 | | BaseItem item, |
| | | 537 | | User user, |
| | | 538 | | DtoOptions options, |
| | | 539 | | UserItemData? userData = null, |
| | | 540 | | Dictionary<Guid, int>? childCountBatch = null, |
| | | 541 | | Dictionary<Guid, (int Played, int Total)>? playedCountBatch = null) |
| | | 542 | | { |
| | 9 | 543 | | if (item.IsFolder) |
| | | 544 | | { |
| | 9 | 545 | | var folder = (Folder)item; |
| | | 546 | | |
| | 9 | 547 | | if (options.EnableUserData) |
| | | 548 | | { |
| | 9 | 549 | | if (userData is not null) |
| | | 550 | | { |
| | | 551 | | // Use pre-fetched user data |
| | 3 | 552 | | dto.UserData = GetUserItemDataDto(userData, item.Id); |
| | 3 | 553 | | (int Played, int Total)? precomputed = playedCountBatch is not null |
| | 3 | 554 | | && playedCountBatch.TryGetValue(item.Id, out var counts) ? counts : null; |
| | 3 | 555 | | item.FillUserDataDtoValues(dto.UserData, userData, dto, user, options, precomputed); |
| | | 556 | | } |
| | | 557 | | else |
| | | 558 | | { |
| | | 559 | | // Fall back to individual fetch |
| | 6 | 560 | | dto.UserData = _userDataRepository.GetUserDataDto(item, dto, user, options); |
| | | 561 | | } |
| | | 562 | | } |
| | | 563 | | |
| | 9 | 564 | | if (!dto.ChildCount.HasValue && item.SourceType == SourceType.Library) |
| | | 565 | | { |
| | | 566 | | // For these types we can try to optimize and assume these values will be equal |
| | 9 | 567 | | if (item is MusicAlbum || item is Season || item is Playlist) |
| | | 568 | | { |
| | 0 | 569 | | dto.ChildCount = dto.RecursiveItemCount; |
| | 0 | 570 | | var folderChildCount = folder.LinkedChildren.Length; |
| | | 571 | | // The default is an empty array, so we can't reliably use the count when it's empty |
| | 0 | 572 | | if (folderChildCount > 0) |
| | | 573 | | { |
| | 0 | 574 | | dto.ChildCount ??= folderChildCount; |
| | | 575 | | } |
| | | 576 | | } |
| | | 577 | | |
| | 9 | 578 | | if (options.ContainsField(ItemFields.ChildCount)) |
| | | 579 | | { |
| | 6 | 580 | | dto.ChildCount ??= GetChildCount(folder, user, childCountBatch); |
| | | 581 | | } |
| | | 582 | | } |
| | | 583 | | |
| | 9 | 584 | | if (options.ContainsField(ItemFields.CumulativeRunTimeTicks)) |
| | | 585 | | { |
| | 6 | 586 | | dto.CumulativeRunTimeTicks = item.RunTimeTicks; |
| | | 587 | | } |
| | | 588 | | |
| | 9 | 589 | | if (options.ContainsField(ItemFields.DateLastMediaAdded)) |
| | | 590 | | { |
| | 6 | 591 | | dto.DateLastMediaAdded = folder.DateLastMediaAdded; |
| | | 592 | | } |
| | | 593 | | } |
| | | 594 | | else |
| | | 595 | | { |
| | 0 | 596 | | if (options.EnableUserData) |
| | | 597 | | { |
| | 0 | 598 | | if (userData is not null) |
| | | 599 | | { |
| | | 600 | | // Use pre-fetched user data |
| | 0 | 601 | | dto.UserData = GetUserItemDataDto(userData, item.Id); |
| | 0 | 602 | | item.FillUserDataDtoValues(dto.UserData, userData, dto, user, options); |
| | | 603 | | } |
| | | 604 | | else |
| | | 605 | | { |
| | | 606 | | // Fall back to individual fetch |
| | 0 | 607 | | dto.UserData = _userDataRepository.GetUserDataDto(item, user); |
| | | 608 | | } |
| | | 609 | | } |
| | | 610 | | } |
| | | 611 | | |
| | 9 | 612 | | if (options.ContainsField(ItemFields.PlayAccess)) |
| | | 613 | | { |
| | 6 | 614 | | dto.PlayAccess = item.GetPlayAccess(user); |
| | | 615 | | } |
| | 9 | 616 | | } |
| | | 617 | | |
| | | 618 | | private static UserItemDataDto GetUserItemDataDto(UserItemData data, Guid itemId) |
| | | 619 | | { |
| | 3 | 620 | | ArgumentNullException.ThrowIfNull(data); |
| | | 621 | | |
| | 3 | 622 | | return new UserItemDataDto |
| | 3 | 623 | | { |
| | 3 | 624 | | IsFavorite = data.IsFavorite, |
| | 3 | 625 | | Likes = data.Likes, |
| | 3 | 626 | | PlaybackPositionTicks = data.PlaybackPositionTicks, |
| | 3 | 627 | | PlayCount = data.PlayCount, |
| | 3 | 628 | | Rating = data.Rating, |
| | 3 | 629 | | Played = data.Played, |
| | 3 | 630 | | LastPlayedDate = data.LastPlayedDate, |
| | 3 | 631 | | ItemId = itemId, |
| | 3 | 632 | | Key = data.Key |
| | 3 | 633 | | }; |
| | | 634 | | } |
| | | 635 | | |
| | | 636 | | private static int GetChildCount(Folder folder, User user, Dictionary<Guid, int>? childCountBatch) |
| | | 637 | | { |
| | | 638 | | // Right now this is too slow to calculate for top level folders on a per-user basis |
| | | 639 | | // Just return something so that apps that are expecting a value won't think the folders are empty |
| | 6 | 640 | | if (folder is ICollectionFolder || folder is UserView) |
| | | 641 | | { |
| | 0 | 642 | | return Random.Shared.Next(1, 10); |
| | | 643 | | } |
| | | 644 | | |
| | | 645 | | // Use pre-fetched batch data if available |
| | 6 | 646 | | if (childCountBatch is not null && childCountBatch.TryGetValue(folder.Id, out var count)) |
| | | 647 | | { |
| | 0 | 648 | | return count; |
| | | 649 | | } |
| | | 650 | | |
| | | 651 | | // Fall back to individual query for special cases (Series, Season, etc.) |
| | 6 | 652 | | return folder.GetChildCount(user); |
| | | 653 | | } |
| | | 654 | | |
| | | 655 | | private static void SetBookProperties(BaseItemDto dto, Book item) |
| | | 656 | | { |
| | 0 | 657 | | dto.SeriesName = item.SeriesName; |
| | 0 | 658 | | } |
| | | 659 | | |
| | | 660 | | private static void SetPhotoProperties(BaseItemDto dto, Photo item) |
| | | 661 | | { |
| | 0 | 662 | | dto.CameraMake = item.CameraMake; |
| | 0 | 663 | | dto.CameraModel = item.CameraModel; |
| | 0 | 664 | | dto.Software = item.Software; |
| | 0 | 665 | | dto.ExposureTime = item.ExposureTime; |
| | 0 | 666 | | dto.FocalLength = item.FocalLength; |
| | 0 | 667 | | dto.ImageOrientation = item.Orientation; |
| | 0 | 668 | | dto.Aperture = item.Aperture; |
| | 0 | 669 | | dto.ShutterSpeed = item.ShutterSpeed; |
| | | 670 | | |
| | 0 | 671 | | dto.Latitude = item.Latitude; |
| | 0 | 672 | | dto.Longitude = item.Longitude; |
| | 0 | 673 | | dto.Altitude = item.Altitude; |
| | 0 | 674 | | dto.IsoSpeedRating = item.IsoSpeedRating; |
| | | 675 | | |
| | 0 | 676 | | var album = item.AlbumEntity; |
| | | 677 | | |
| | 0 | 678 | | if (album is not null) |
| | | 679 | | { |
| | 0 | 680 | | dto.Album = album.Name; |
| | 0 | 681 | | dto.AlbumId = album.Id; |
| | | 682 | | } |
| | 0 | 683 | | } |
| | | 684 | | |
| | | 685 | | private void SetMusicVideoProperties(BaseItemDto dto, MusicVideo item) |
| | | 686 | | { |
| | 0 | 687 | | if (!string.IsNullOrEmpty(item.Album)) |
| | | 688 | | { |
| | 0 | 689 | | var parentAlbumIds = _libraryManager.GetItemIds(new InternalItemsQuery |
| | 0 | 690 | | { |
| | 0 | 691 | | IncludeItemTypes = new[] { BaseItemKind.MusicAlbum }, |
| | 0 | 692 | | Name = item.Album, |
| | 0 | 693 | | Limit = 1 |
| | 0 | 694 | | }); |
| | | 695 | | |
| | 0 | 696 | | if (parentAlbumIds.Count > 0) |
| | | 697 | | { |
| | 0 | 698 | | dto.AlbumId = parentAlbumIds[0]; |
| | | 699 | | } |
| | | 700 | | } |
| | | 701 | | |
| | 0 | 702 | | dto.Album = item.Album; |
| | 0 | 703 | | } |
| | | 704 | | |
| | | 705 | | private string[] GetImageTags(BaseItem item, List<ItemImageInfo> images) |
| | | 706 | | { |
| | 9 | 707 | | return images |
| | 9 | 708 | | .Select(p => GetImageCacheTag(item, p)) |
| | 9 | 709 | | .Where(i => i is not null) |
| | 9 | 710 | | .ToArray()!; // null values got filtered out |
| | | 711 | | } |
| | | 712 | | |
| | | 713 | | private string? GetImageCacheTag(BaseItem item, ItemImageInfo image) |
| | | 714 | | { |
| | | 715 | | try |
| | | 716 | | { |
| | 0 | 717 | | return _imageProcessor.GetImageCacheTag(item, image); |
| | | 718 | | } |
| | 0 | 719 | | catch (Exception ex) |
| | | 720 | | { |
| | 0 | 721 | | _logger.LogError(ex, "Error getting {ImageType} image info for {Path}", image.Type, image.Path); |
| | 0 | 722 | | return null; |
| | | 723 | | } |
| | 0 | 724 | | } |
| | | 725 | | |
| | | 726 | | /// <summary> |
| | | 727 | | /// Attaches People DTO's to a DTOBaseItem. |
| | | 728 | | /// </summary> |
| | | 729 | | /// <param name="dto">The dto.</param> |
| | | 730 | | /// <param name="item">The item.</param> |
| | | 731 | | /// <param name="user">The requesting user.</param> |
| | | 732 | | private void AttachPeople(BaseItemDto dto, BaseItem item, User? user = null) |
| | | 733 | | { |
| | | 734 | | // Ordering by person type to ensure actors and artists are at the front. |
| | | 735 | | // This is taking advantage of the fact that they both begin with A |
| | | 736 | | // This should be improved in the future |
| | 6 | 737 | | var people = _libraryManager.GetPeople(item).OrderBy(i => i.SortOrder ?? int.MaxValue) |
| | 6 | 738 | | .ThenBy(i => |
| | 6 | 739 | | { |
| | 6 | 740 | | if (i.IsType(PersonKind.Actor)) |
| | 6 | 741 | | { |
| | 6 | 742 | | return 0; |
| | 6 | 743 | | } |
| | 6 | 744 | | |
| | 6 | 745 | | if (i.IsType(PersonKind.GuestStar)) |
| | 6 | 746 | | { |
| | 6 | 747 | | return 1; |
| | 6 | 748 | | } |
| | 6 | 749 | | |
| | 6 | 750 | | if (i.IsType(PersonKind.Director)) |
| | 6 | 751 | | { |
| | 6 | 752 | | return 2; |
| | 6 | 753 | | } |
| | 6 | 754 | | |
| | 6 | 755 | | if (i.IsType(PersonKind.Writer)) |
| | 6 | 756 | | { |
| | 6 | 757 | | return 3; |
| | 6 | 758 | | } |
| | 6 | 759 | | |
| | 6 | 760 | | if (i.IsType(PersonKind.Producer)) |
| | 6 | 761 | | { |
| | 6 | 762 | | return 4; |
| | 6 | 763 | | } |
| | 6 | 764 | | |
| | 6 | 765 | | if (i.IsType(PersonKind.Composer)) |
| | 6 | 766 | | { |
| | 6 | 767 | | return 4; |
| | 6 | 768 | | } |
| | 6 | 769 | | |
| | 6 | 770 | | return 10; |
| | 6 | 771 | | }) |
| | 6 | 772 | | .ToList(); |
| | | 773 | | |
| | 6 | 774 | | var list = new List<BaseItemPerson>(); |
| | | 775 | | |
| | 6 | 776 | | Dictionary<string, Person> dictionary = people.Select(p => p.Name) |
| | 6 | 777 | | .Distinct(StringComparer.OrdinalIgnoreCase).Select(c => |
| | 6 | 778 | | { |
| | 6 | 779 | | try |
| | 6 | 780 | | { |
| | 6 | 781 | | return _libraryManager.GetPerson(c); |
| | 6 | 782 | | } |
| | 6 | 783 | | catch (Exception ex) |
| | 6 | 784 | | { |
| | 6 | 785 | | _logger.LogError(ex, "Error getting person {Name}", c); |
| | 6 | 786 | | return null; |
| | 6 | 787 | | } |
| | 6 | 788 | | }).Where(i => i is not null) |
| | 6 | 789 | | .Where(i => user is null || i!.IsVisible(user)) |
| | 6 | 790 | | .DistinctBy(x => x!.Name, StringComparer.OrdinalIgnoreCase) |
| | 6 | 791 | | .ToDictionary(i => i!.Name, StringComparer.OrdinalIgnoreCase)!; // null values got filtered out |
| | | 792 | | |
| | 12 | 793 | | for (var i = 0; i < people.Count; i++) |
| | | 794 | | { |
| | 0 | 795 | | var person = people[i]; |
| | | 796 | | |
| | 0 | 797 | | var baseItemPerson = new BaseItemPerson |
| | 0 | 798 | | { |
| | 0 | 799 | | Name = person.Name, |
| | 0 | 800 | | Role = person.Role, |
| | 0 | 801 | | Type = person.Type |
| | 0 | 802 | | }; |
| | | 803 | | |
| | 0 | 804 | | if (dictionary.TryGetValue(person.Name, out Person? entity)) |
| | | 805 | | { |
| | 0 | 806 | | baseItemPerson.PrimaryImageTag = GetTagAndFillBlurhash(dto, entity, ImageType.Primary); |
| | 0 | 807 | | baseItemPerson.Id = entity.Id; |
| | 0 | 808 | | if (dto.ImageBlurHashes is not null) |
| | | 809 | | { |
| | | 810 | | // Only add BlurHash for the person's image. |
| | 0 | 811 | | baseItemPerson.ImageBlurHashes = []; |
| | 0 | 812 | | foreach (var (imageType, blurHash) in dto.ImageBlurHashes) |
| | | 813 | | { |
| | 0 | 814 | | if (blurHash is not null) |
| | | 815 | | { |
| | 0 | 816 | | baseItemPerson.ImageBlurHashes[imageType] = []; |
| | 0 | 817 | | foreach (var (imageId, blurHashValue) in blurHash) |
| | | 818 | | { |
| | 0 | 819 | | if (string.Equals(baseItemPerson.PrimaryImageTag, imageId, StringComparison.OrdinalI |
| | | 820 | | { |
| | 0 | 821 | | baseItemPerson.ImageBlurHashes[imageType][imageId] = blurHashValue; |
| | | 822 | | } |
| | | 823 | | } |
| | | 824 | | } |
| | | 825 | | } |
| | | 826 | | } |
| | | 827 | | |
| | 0 | 828 | | list.Add(baseItemPerson); |
| | | 829 | | } |
| | | 830 | | } |
| | | 831 | | |
| | 6 | 832 | | dto.People = list.ToArray(); |
| | 6 | 833 | | } |
| | | 834 | | |
| | | 835 | | /// <summary> |
| | | 836 | | /// Attaches the studios. |
| | | 837 | | /// </summary> |
| | | 838 | | /// <param name="dto">The dto.</param> |
| | | 839 | | /// <param name="item">The item.</param> |
| | | 840 | | private void AttachStudios(BaseItemDto dto, BaseItem item) |
| | | 841 | | { |
| | 6 | 842 | | dto.Studios = item.Studios |
| | 6 | 843 | | .Where(i => !string.IsNullOrEmpty(i)) |
| | 6 | 844 | | .Select(i => new NameGuidPair |
| | 6 | 845 | | { |
| | 6 | 846 | | Name = i, |
| | 6 | 847 | | Id = _libraryManager.GetStudioId(i) |
| | 6 | 848 | | }) |
| | 6 | 849 | | .ToArray(); |
| | 6 | 850 | | } |
| | | 851 | | |
| | | 852 | | private void AttachGenreItems(BaseItemDto dto, BaseItem item) |
| | | 853 | | { |
| | 6 | 854 | | dto.GenreItems = item.Genres |
| | 6 | 855 | | .Where(i => !string.IsNullOrEmpty(i)) |
| | 6 | 856 | | .Select(i => new NameGuidPair |
| | 6 | 857 | | { |
| | 6 | 858 | | Name = i, |
| | 6 | 859 | | Id = GetGenreId(i, item) |
| | 6 | 860 | | }) |
| | 6 | 861 | | .ToArray(); |
| | 6 | 862 | | } |
| | | 863 | | |
| | | 864 | | private Guid GetGenreId(string name, BaseItem owner) |
| | | 865 | | { |
| | 0 | 866 | | if (owner is IHasMusicGenres) |
| | | 867 | | { |
| | 0 | 868 | | return _libraryManager.GetMusicGenreId(name); |
| | | 869 | | } |
| | | 870 | | |
| | 0 | 871 | | return _libraryManager.GetGenreId(name); |
| | | 872 | | } |
| | | 873 | | |
| | | 874 | | private string? GetTagAndFillBlurhash(BaseItemDto dto, BaseItem item, ImageType imageType, int imageIndex = 0) |
| | | 875 | | { |
| | 0 | 876 | | var image = item.GetImageInfo(imageType, imageIndex); |
| | 0 | 877 | | if (image is not null) |
| | | 878 | | { |
| | 0 | 879 | | return GetTagAndFillBlurhash(dto, item, image); |
| | | 880 | | } |
| | | 881 | | |
| | 0 | 882 | | return null; |
| | | 883 | | } |
| | | 884 | | |
| | | 885 | | private string? GetTagAndFillBlurhash(BaseItemDto dto, BaseItem item, ItemImageInfo image) |
| | | 886 | | { |
| | 0 | 887 | | var tag = GetImageCacheTag(item, image); |
| | 0 | 888 | | if (tag is null) |
| | | 889 | | { |
| | 0 | 890 | | return null; |
| | | 891 | | } |
| | | 892 | | |
| | 0 | 893 | | if (!string.IsNullOrEmpty(image.BlurHash)) |
| | | 894 | | { |
| | 0 | 895 | | dto.ImageBlurHashes ??= []; |
| | | 896 | | |
| | 0 | 897 | | if (!dto.ImageBlurHashes.TryGetValue(image.Type, out var value)) |
| | | 898 | | { |
| | 0 | 899 | | value = []; |
| | 0 | 900 | | dto.ImageBlurHashes[image.Type] = value; |
| | | 901 | | } |
| | | 902 | | |
| | 0 | 903 | | value[tag] = image.BlurHash; |
| | | 904 | | } |
| | | 905 | | |
| | 0 | 906 | | return tag; |
| | | 907 | | } |
| | | 908 | | |
| | | 909 | | private string[] GetTagsAndFillBlurhashes(BaseItemDto dto, BaseItem item, ImageType imageType, int limit) |
| | | 910 | | { |
| | 9 | 911 | | return GetTagsAndFillBlurhashes(dto, item, imageType, item.GetImages(imageType).Take(limit).ToList()); |
| | | 912 | | } |
| | | 913 | | |
| | | 914 | | private string[] GetTagsAndFillBlurhashes(BaseItemDto dto, BaseItem item, ImageType imageType, List<ItemImageInf |
| | | 915 | | { |
| | 9 | 916 | | var tags = GetImageTags(item, images); |
| | 9 | 917 | | var hashes = new Dictionary<string, string>(); |
| | 18 | 918 | | for (int i = 0; i < images.Count; i++) |
| | | 919 | | { |
| | 0 | 920 | | var img = images[i]; |
| | 0 | 921 | | if (!string.IsNullOrEmpty(img.BlurHash)) |
| | | 922 | | { |
| | 0 | 923 | | var tag = tags[i]; |
| | 0 | 924 | | hashes[tag] = img.BlurHash; |
| | | 925 | | } |
| | | 926 | | } |
| | | 927 | | |
| | 9 | 928 | | if (hashes.Count > 0) |
| | | 929 | | { |
| | 0 | 930 | | dto.ImageBlurHashes ??= []; |
| | | 931 | | |
| | 0 | 932 | | dto.ImageBlurHashes[imageType] = hashes; |
| | | 933 | | } |
| | | 934 | | |
| | 9 | 935 | | return tags; |
| | | 936 | | } |
| | | 937 | | |
| | | 938 | | /// <summary> |
| | | 939 | | /// Sets simple property values on a DTOBaseItem. |
| | | 940 | | /// </summary> |
| | | 941 | | /// <param name="dto">The dto.</param> |
| | | 942 | | /// <param name="item">The item.</param> |
| | | 943 | | /// <param name="owner">The owner.</param> |
| | | 944 | | /// <param name="options">The options.</param> |
| | | 945 | | /// <param name="artistsBatch">Optional pre-fetched artist lookup shared across a batch of items.</param> |
| | | 946 | | private void AttachBasicFields(BaseItemDto dto, BaseItem item, BaseItem? owner, DtoOptions options, IReadOnlyDic |
| | | 947 | | { |
| | 9 | 948 | | if (options.ContainsField(ItemFields.DateCreated)) |
| | | 949 | | { |
| | 6 | 950 | | dto.DateCreated = item.DateCreated; |
| | | 951 | | } |
| | | 952 | | |
| | 9 | 953 | | if (options.ContainsField(ItemFields.Settings)) |
| | | 954 | | { |
| | 6 | 955 | | dto.LockedFields = item.LockedFields; |
| | 6 | 956 | | dto.LockData = item.IsLocked; |
| | 6 | 957 | | dto.ForcedSortName = item.ForcedSortName; |
| | | 958 | | } |
| | | 959 | | |
| | 9 | 960 | | dto.Container = item.Container; |
| | 9 | 961 | | dto.EndDate = item.EndDate; |
| | | 962 | | |
| | 9 | 963 | | if (options.ContainsField(ItemFields.ExternalUrls)) |
| | | 964 | | { |
| | 6 | 965 | | dto.ExternalUrls = _providerManager.GetExternalUrls(item).ToArray(); |
| | | 966 | | } |
| | | 967 | | |
| | 9 | 968 | | if (options.ContainsField(ItemFields.Tags)) |
| | | 969 | | { |
| | 6 | 970 | | dto.Tags = item.Tags; |
| | | 971 | | } |
| | | 972 | | |
| | 9 | 973 | | if (item is IHasAspectRatio hasAspectRatio) |
| | | 974 | | { |
| | 0 | 975 | | dto.AspectRatio = hasAspectRatio.AspectRatio; |
| | | 976 | | } |
| | | 977 | | |
| | 9 | 978 | | dto.ImageBlurHashes = []; |
| | | 979 | | |
| | 9 | 980 | | var backdropLimit = options.GetImageLimit(ImageType.Backdrop); |
| | 9 | 981 | | if (backdropLimit > 0) |
| | | 982 | | { |
| | 9 | 983 | | dto.BackdropImageTags = GetTagsAndFillBlurhashes(dto, item, ImageType.Backdrop, backdropLimit); |
| | | 984 | | } |
| | | 985 | | |
| | 9 | 986 | | if (options.ContainsField(ItemFields.Genres)) |
| | | 987 | | { |
| | 6 | 988 | | dto.Genres = item.Genres; |
| | 6 | 989 | | AttachGenreItems(dto, item); |
| | | 990 | | } |
| | | 991 | | |
| | 9 | 992 | | if (options.EnableImages) |
| | | 993 | | { |
| | 9 | 994 | | dto.ImageTags = []; |
| | | 995 | | |
| | | 996 | | // Prevent implicitly captured closure |
| | 9 | 997 | | var currentItem = item; |
| | 18 | 998 | | foreach (var image in currentItem.ImageInfos.Where(i => !currentItem.AllowsMultipleImages(i.Type))) |
| | | 999 | | { |
| | 0 | 1000 | | if (options.GetImageLimit(image.Type) > 0) |
| | | 1001 | | { |
| | 0 | 1002 | | var tag = GetTagAndFillBlurhash(dto, item, image); |
| | | 1003 | | |
| | 0 | 1004 | | if (tag is not null) |
| | | 1005 | | { |
| | 0 | 1006 | | dto.ImageTags[image.Type] = tag; |
| | | 1007 | | } |
| | | 1008 | | } |
| | | 1009 | | } |
| | | 1010 | | } |
| | | 1011 | | |
| | 9 | 1012 | | dto.Id = item.Id; |
| | 9 | 1013 | | dto.IndexNumber = item.IndexNumber; |
| | 9 | 1014 | | dto.ParentIndexNumber = item.ParentIndexNumber; |
| | | 1015 | | |
| | 9 | 1016 | | if (item.IsFolder) |
| | | 1017 | | { |
| | 9 | 1018 | | dto.IsFolder = true; |
| | | 1019 | | } |
| | 0 | 1020 | | else if (item is IHasMediaSources) |
| | | 1021 | | { |
| | 0 | 1022 | | dto.IsFolder = false; |
| | | 1023 | | } |
| | | 1024 | | |
| | 9 | 1025 | | dto.MediaType = item.MediaType; |
| | | 1026 | | |
| | 9 | 1027 | | if (item is not LiveTvProgram) |
| | | 1028 | | { |
| | 9 | 1029 | | dto.LocationType = item.LocationType; |
| | | 1030 | | } |
| | | 1031 | | |
| | 9 | 1032 | | dto.Audio = item.Audio; |
| | | 1033 | | |
| | 9 | 1034 | | if (options.ContainsField(ItemFields.Settings)) |
| | | 1035 | | { |
| | 6 | 1036 | | dto.PreferredMetadataCountryCode = item.PreferredMetadataCountryCode; |
| | 6 | 1037 | | dto.PreferredMetadataLanguage = item.PreferredMetadataLanguage; |
| | | 1038 | | } |
| | | 1039 | | |
| | 9 | 1040 | | dto.CriticRating = item.CriticRating; |
| | | 1041 | | |
| | 9 | 1042 | | if (item is IHasDisplayOrder hasDisplayOrder) |
| | | 1043 | | { |
| | 0 | 1044 | | dto.DisplayOrder = hasDisplayOrder.DisplayOrder; |
| | | 1045 | | } |
| | | 1046 | | |
| | 9 | 1047 | | if (item is IHasCollectionType hasCollectionType) |
| | | 1048 | | { |
| | 3 | 1049 | | dto.CollectionType = hasCollectionType.CollectionType; |
| | | 1050 | | } |
| | | 1051 | | |
| | 9 | 1052 | | if (options.ContainsField(ItemFields.RemoteTrailers)) |
| | | 1053 | | { |
| | 6 | 1054 | | dto.RemoteTrailers = item.RemoteTrailers; |
| | | 1055 | | } |
| | | 1056 | | |
| | 9 | 1057 | | dto.Name = item.Name; |
| | 9 | 1058 | | dto.OfficialRating = item.OfficialRating; |
| | | 1059 | | |
| | 9 | 1060 | | if (options.ContainsField(ItemFields.Overview)) |
| | | 1061 | | { |
| | 6 | 1062 | | dto.Overview = item.Overview; |
| | | 1063 | | } |
| | | 1064 | | |
| | 9 | 1065 | | if (options.ContainsField(ItemFields.OriginalTitle)) |
| | | 1066 | | { |
| | 6 | 1067 | | dto.OriginalTitle = item.OriginalTitle; |
| | | 1068 | | } |
| | | 1069 | | |
| | 9 | 1070 | | if (options.ContainsField(ItemFields.ParentId)) |
| | | 1071 | | { |
| | 6 | 1072 | | dto.ParentId = item.DisplayParentId; |
| | | 1073 | | } |
| | | 1074 | | |
| | 9 | 1075 | | AddInheritedImages(dto, item, options, owner); |
| | | 1076 | | |
| | 9 | 1077 | | if (options.ContainsField(ItemFields.Path)) |
| | | 1078 | | { |
| | 6 | 1079 | | dto.Path = GetMappedPath(item, owner); |
| | | 1080 | | } |
| | | 1081 | | |
| | 9 | 1082 | | if (options.ContainsField(ItemFields.EnableMediaSourceDisplay)) |
| | | 1083 | | { |
| | 6 | 1084 | | dto.EnableMediaSourceDisplay = item.EnableMediaSourceDisplay; |
| | | 1085 | | } |
| | | 1086 | | |
| | 9 | 1087 | | dto.PremiereDate = item.PremiereDate; |
| | 9 | 1088 | | dto.ProductionYear = item.ProductionYear; |
| | | 1089 | | |
| | 9 | 1090 | | if (options.ContainsField(ItemFields.ProviderIds)) |
| | | 1091 | | { |
| | 6 | 1092 | | dto.ProviderIds = item.ProviderIds; |
| | | 1093 | | } |
| | | 1094 | | |
| | 9 | 1095 | | dto.RunTimeTicks = item.RunTimeTicks; |
| | | 1096 | | |
| | 9 | 1097 | | if (options.ContainsField(ItemFields.SortName)) |
| | | 1098 | | { |
| | 6 | 1099 | | dto.SortName = item.SortName; |
| | | 1100 | | } |
| | | 1101 | | |
| | 9 | 1102 | | if (options.ContainsField(ItemFields.CustomRating)) |
| | | 1103 | | { |
| | 6 | 1104 | | dto.CustomRating = item.CustomRating; |
| | | 1105 | | } |
| | | 1106 | | |
| | 9 | 1107 | | if (options.ContainsField(ItemFields.Taglines)) |
| | | 1108 | | { |
| | 6 | 1109 | | if (!string.IsNullOrEmpty(item.Tagline)) |
| | | 1110 | | { |
| | 0 | 1111 | | dto.Taglines = new string[] { item.Tagline }; |
| | | 1112 | | } |
| | | 1113 | | |
| | 6 | 1114 | | dto.Taglines ??= Array.Empty<string>(); |
| | | 1115 | | } |
| | | 1116 | | |
| | 9 | 1117 | | dto.Type = item.GetBaseItemKind(); |
| | 9 | 1118 | | if ((item.CommunityRating ?? 0) > 0) |
| | | 1119 | | { |
| | 0 | 1120 | | dto.CommunityRating = item.CommunityRating; |
| | | 1121 | | } |
| | | 1122 | | |
| | 9 | 1123 | | if (item is ISupportsPlaceHolders supportsPlaceHolders && supportsPlaceHolders.IsPlaceHolder) |
| | | 1124 | | { |
| | 0 | 1125 | | dto.IsPlaceHolder = supportsPlaceHolders.IsPlaceHolder; |
| | | 1126 | | } |
| | | 1127 | | |
| | 9 | 1128 | | if (item.LUFS.HasValue) |
| | | 1129 | | { |
| | | 1130 | | // -18 LUFS reference, same as ReplayGain 2.0, compatible with ReplayGain 1.0 |
| | 0 | 1131 | | dto.NormalizationGain = -18f - item.LUFS; |
| | | 1132 | | } |
| | 9 | 1133 | | else if (item.NormalizationGain.HasValue) |
| | | 1134 | | { |
| | 0 | 1135 | | dto.NormalizationGain = item.NormalizationGain; |
| | | 1136 | | } |
| | | 1137 | | |
| | | 1138 | | // Add audio info |
| | 9 | 1139 | | if (item is Audio audio) |
| | | 1140 | | { |
| | 0 | 1141 | | dto.Album = audio.Album; |
| | 0 | 1142 | | dto.ExtraType = audio.ExtraType; |
| | | 1143 | | |
| | 0 | 1144 | | var albumParent = audio.AlbumEntity; |
| | | 1145 | | |
| | 0 | 1146 | | if (albumParent is not null) |
| | | 1147 | | { |
| | 0 | 1148 | | dto.AlbumId = albumParent.Id; |
| | 0 | 1149 | | dto.AlbumPrimaryImageTag = GetTagAndFillBlurhash(dto, albumParent, ImageType.Primary); |
| | 0 | 1150 | | if (albumParent.LUFS.HasValue) |
| | | 1151 | | { |
| | | 1152 | | // -18 LUFS reference, same as ReplayGain 2.0, compatible with ReplayGain 1.0 |
| | 0 | 1153 | | dto.AlbumNormalizationGain = -18f - albumParent.LUFS; |
| | | 1154 | | } |
| | 0 | 1155 | | else if (albumParent.NormalizationGain.HasValue) |
| | | 1156 | | { |
| | 0 | 1157 | | dto.AlbumNormalizationGain = albumParent.NormalizationGain; |
| | | 1158 | | } |
| | | 1159 | | } |
| | | 1160 | | |
| | | 1161 | | // if (options.ContainsField(ItemFields.MediaSourceCount)) |
| | | 1162 | | // { |
| | | 1163 | | // Songs always have one |
| | | 1164 | | // } |
| | | 1165 | | } |
| | | 1166 | | |
| | 9 | 1167 | | if (item is IHasArtist hasArtist) |
| | | 1168 | | { |
| | 0 | 1169 | | dto.Artists = hasArtist.Artists; |
| | | 1170 | | |
| | | 1171 | | // var artistItems = _libraryManager.GetArtists(new InternalItemsQuery |
| | | 1172 | | // { |
| | | 1173 | | // EnableTotalRecordCount = false, |
| | | 1174 | | // ItemIds = new[] { item.Id.ToString("N", CultureInfo.InvariantCulture) } |
| | | 1175 | | // }); |
| | | 1176 | | |
| | | 1177 | | // dto.ArtistItems = artistItems.Items |
| | | 1178 | | // .Select(i => |
| | | 1179 | | // { |
| | | 1180 | | // var artist = i.Item1; |
| | | 1181 | | // return new NameIdPair |
| | | 1182 | | // { |
| | | 1183 | | // Name = artist.Name, |
| | | 1184 | | // Id = artist.Id.ToString("N", CultureInfo.InvariantCulture) |
| | | 1185 | | // }; |
| | | 1186 | | // }) |
| | | 1187 | | // .ToList(); |
| | | 1188 | | |
| | | 1189 | | // Include artists that are not in the database yet, e.g., just added via metadata editor |
| | | 1190 | | // var foundArtists = artistItems.Items.Select(i => i.Item1.Name).ToList(); |
| | 0 | 1191 | | var artistsLookup = artistsBatch |
| | 0 | 1192 | | ?? _libraryManager.GetArtists([.. hasArtist.Artists.Where(e => !string.IsNullOrWhiteSpace(e))]); |
| | | 1193 | | |
| | 0 | 1194 | | dto.ArtistItems = hasArtist.Artists |
| | 0 | 1195 | | .Where(name => !string.IsNullOrWhiteSpace(name)) |
| | 0 | 1196 | | .Distinct() |
| | 0 | 1197 | | .Select(name => artistsLookup.TryGetValue(name, out var artists) && artists.Length > 0 |
| | 0 | 1198 | | ? new NameGuidPair { Name = name, Id = artists[0].Id } |
| | 0 | 1199 | | : null) |
| | 0 | 1200 | | .Where(item => item is not null) |
| | 0 | 1201 | | .ToArray(); |
| | | 1202 | | } |
| | | 1203 | | |
| | 9 | 1204 | | if (item is IHasAlbumArtist hasAlbumArtist) |
| | | 1205 | | { |
| | 0 | 1206 | | dto.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault(); |
| | | 1207 | | |
| | | 1208 | | // var artistItems = _libraryManager.GetAlbumArtists(new InternalItemsQuery |
| | | 1209 | | // { |
| | | 1210 | | // EnableTotalRecordCount = false, |
| | | 1211 | | // ItemIds = new[] { item.Id.ToString("N", CultureInfo.InvariantCulture) } |
| | | 1212 | | // }); |
| | | 1213 | | |
| | | 1214 | | // dto.AlbumArtists = artistItems.Items |
| | | 1215 | | // .Select(i => |
| | | 1216 | | // { |
| | | 1217 | | // var artist = i.Item1; |
| | | 1218 | | // return new NameIdPair |
| | | 1219 | | // { |
| | | 1220 | | // Name = artist.Name, |
| | | 1221 | | // Id = artist.Id.ToString("N", CultureInfo.InvariantCulture) |
| | | 1222 | | // }; |
| | | 1223 | | // }) |
| | | 1224 | | // .ToList(); |
| | | 1225 | | |
| | 0 | 1226 | | var albumArtistsLookup = artistsBatch |
| | 0 | 1227 | | ?? _libraryManager.GetArtists([.. hasAlbumArtist.AlbumArtists.Where(e => !string.IsNullOrWhiteSpace( |
| | | 1228 | | |
| | 0 | 1229 | | dto.AlbumArtists = hasAlbumArtist.AlbumArtists |
| | 0 | 1230 | | .Where(name => !string.IsNullOrWhiteSpace(name)) |
| | 0 | 1231 | | .Distinct() |
| | 0 | 1232 | | .Select(name => albumArtistsLookup.TryGetValue(name, out var albumArtists) && albumArtists.Length > |
| | 0 | 1233 | | ? new NameGuidPair { Name = name, Id = albumArtists[0].Id } |
| | 0 | 1234 | | : null) |
| | 0 | 1235 | | .Where(item => item is not null) |
| | 0 | 1236 | | .ToArray(); |
| | | 1237 | | } |
| | | 1238 | | |
| | | 1239 | | // Add video info |
| | 9 | 1240 | | if (item is Video video) |
| | | 1241 | | { |
| | 0 | 1242 | | dto.VideoType = video.VideoType; |
| | 0 | 1243 | | dto.Video3DFormat = video.Video3DFormat; |
| | 0 | 1244 | | dto.IsoType = video.IsoType; |
| | | 1245 | | |
| | 0 | 1246 | | if (video.HasSubtitles) |
| | | 1247 | | { |
| | 0 | 1248 | | dto.HasSubtitles = video.HasSubtitles; |
| | | 1249 | | } |
| | | 1250 | | |
| | 0 | 1251 | | if (video.AdditionalParts.Length != 0) |
| | | 1252 | | { |
| | 0 | 1253 | | dto.PartCount = video.AdditionalParts.Length + 1; |
| | | 1254 | | } |
| | | 1255 | | |
| | 0 | 1256 | | if (options.ContainsField(ItemFields.MediaSourceCount)) |
| | | 1257 | | { |
| | 0 | 1258 | | var mediaSourceCount = video.MediaSourceCount; |
| | 0 | 1259 | | if (mediaSourceCount != 1) |
| | | 1260 | | { |
| | 0 | 1261 | | dto.MediaSourceCount = mediaSourceCount; |
| | | 1262 | | } |
| | | 1263 | | } |
| | | 1264 | | |
| | 0 | 1265 | | if (options.ContainsField(ItemFields.Trickplay)) |
| | | 1266 | | { |
| | 0 | 1267 | | var trickplay = _trickplayManager.GetTrickplayManifest(item).GetAwaiter().GetResult(); |
| | 0 | 1268 | | dto.Trickplay = trickplay.ToDictionary( |
| | 0 | 1269 | | mediaStream => mediaStream.Key, |
| | 0 | 1270 | | mediaStream => mediaStream.Value.ToDictionary( |
| | 0 | 1271 | | width => width.Key, |
| | 0 | 1272 | | width => new TrickplayInfoDto(width.Value))); |
| | | 1273 | | } |
| | | 1274 | | |
| | 0 | 1275 | | dto.ExtraType = video.ExtraType; |
| | | 1276 | | } |
| | | 1277 | | |
| | 9 | 1278 | | if (options.ContainsField(ItemFields.Chapters)) |
| | | 1279 | | { |
| | 6 | 1280 | | dto.Chapters = _chapterManager.GetChapters(item.Id).ToList(); |
| | | 1281 | | } |
| | | 1282 | | |
| | 9 | 1283 | | if (options.ContainsField(ItemFields.MediaStreams)) |
| | | 1284 | | { |
| | | 1285 | | // Add VideoInfo |
| | 6 | 1286 | | if (item is IHasMediaSources) |
| | | 1287 | | { |
| | | 1288 | | MediaStream[] mediaStreams; |
| | | 1289 | | |
| | 0 | 1290 | | if (dto.MediaSources is not null && dto.MediaSources.Length > 0) |
| | | 1291 | | { |
| | 0 | 1292 | | if (item.SourceType == SourceType.Channel) |
| | | 1293 | | { |
| | 0 | 1294 | | mediaStreams = dto.MediaSources[0].MediaStreams.ToArray(); |
| | | 1295 | | } |
| | | 1296 | | else |
| | | 1297 | | { |
| | 0 | 1298 | | string id = item.Id.ToString("N", CultureInfo.InvariantCulture); |
| | 0 | 1299 | | mediaStreams = dto.MediaSources.Where(i => string.Equals(i.Id, id, StringComparison.OrdinalI |
| | 0 | 1300 | | .SelectMany(i => i.MediaStreams) |
| | 0 | 1301 | | .ToArray(); |
| | | 1302 | | } |
| | | 1303 | | } |
| | | 1304 | | else |
| | | 1305 | | { |
| | 0 | 1306 | | mediaStreams = _mediaSourceManager.GetStaticMediaSources(item, true)[0].MediaStreams.ToArray(); |
| | | 1307 | | } |
| | | 1308 | | |
| | 0 | 1309 | | dto.MediaStreams = mediaStreams; |
| | | 1310 | | } |
| | | 1311 | | } |
| | | 1312 | | |
| | 9 | 1313 | | BaseItem[]? allExtras = null; |
| | | 1314 | | |
| | 9 | 1315 | | if (options.ContainsField(ItemFields.SpecialFeatureCount)) |
| | | 1316 | | { |
| | 6 | 1317 | | allExtras = item.GetExtras().ToArray(); |
| | 6 | 1318 | | dto.SpecialFeatureCount = allExtras.Count(i => i.ExtraType.HasValue && BaseItem.DisplayExtraTypes.Contai |
| | | 1319 | | } |
| | | 1320 | | |
| | 9 | 1321 | | if (options.ContainsField(ItemFields.LocalTrailerCount)) |
| | | 1322 | | { |
| | 6 | 1323 | | if (item is IHasTrailers hasTrailers) |
| | | 1324 | | { |
| | 0 | 1325 | | dto.LocalTrailerCount = hasTrailers.LocalTrailers.Count; |
| | | 1326 | | } |
| | | 1327 | | else |
| | | 1328 | | { |
| | 6 | 1329 | | dto.LocalTrailerCount = (allExtras ?? item.GetExtras()).Count(i => i.ExtraType == ExtraType.Trailer) |
| | | 1330 | | } |
| | | 1331 | | } |
| | | 1332 | | |
| | | 1333 | | // Add EpisodeInfo |
| | 9 | 1334 | | if (item is Episode episode) |
| | | 1335 | | { |
| | 0 | 1336 | | dto.IndexNumberEnd = episode.IndexNumberEnd; |
| | 0 | 1337 | | dto.SeriesName = episode.SeriesName; |
| | | 1338 | | |
| | 0 | 1339 | | if (options.ContainsField(ItemFields.SpecialEpisodeNumbers)) |
| | | 1340 | | { |
| | 0 | 1341 | | dto.AirsAfterSeasonNumber = episode.AirsAfterSeasonNumber; |
| | 0 | 1342 | | dto.AirsBeforeEpisodeNumber = episode.AirsBeforeEpisodeNumber; |
| | 0 | 1343 | | dto.AirsBeforeSeasonNumber = episode.AirsBeforeSeasonNumber; |
| | | 1344 | | } |
| | | 1345 | | |
| | 0 | 1346 | | dto.SeasonName = episode.SeasonName; |
| | 0 | 1347 | | dto.SeasonId = episode.SeasonId; |
| | 0 | 1348 | | dto.SeriesId = episode.SeriesId; |
| | | 1349 | | |
| | 0 | 1350 | | Series? episodeSeries = null; |
| | | 1351 | | |
| | | 1352 | | // this block will add the series poster for episodes without a poster |
| | | 1353 | | // TODO maybe remove the if statement entirely |
| | | 1354 | | // if (options.ContainsField(ItemFields.SeriesPrimaryImage)) |
| | | 1355 | | { |
| | 0 | 1356 | | episodeSeries ??= episode.Series; |
| | 0 | 1357 | | if (episodeSeries is not null) |
| | | 1358 | | { |
| | 0 | 1359 | | dto.SeriesPrimaryImageTag = GetTagAndFillBlurhash(dto, episodeSeries, ImageType.Primary); |
| | 0 | 1360 | | if (dto.ImageTags is null || !dto.ImageTags.ContainsKey(ImageType.Primary)) |
| | | 1361 | | { |
| | 0 | 1362 | | AttachPrimaryImageAspectRatio(dto, episodeSeries); |
| | | 1363 | | } |
| | | 1364 | | } |
| | | 1365 | | } |
| | | 1366 | | |
| | 0 | 1367 | | if (options.ContainsField(ItemFields.SeriesStudio)) |
| | | 1368 | | { |
| | 0 | 1369 | | episodeSeries ??= episode.Series; |
| | 0 | 1370 | | if (episodeSeries is not null) |
| | | 1371 | | { |
| | 0 | 1372 | | dto.SeriesStudio = episodeSeries.Studios.FirstOrDefault(); |
| | | 1373 | | } |
| | | 1374 | | } |
| | | 1375 | | } |
| | | 1376 | | |
| | | 1377 | | // Add SeriesInfo |
| | | 1378 | | Series? series; |
| | 9 | 1379 | | if (item is Series tmp) |
| | | 1380 | | { |
| | 0 | 1381 | | series = tmp; |
| | 0 | 1382 | | dto.AirDays = series.AirDays; |
| | 0 | 1383 | | dto.AirTime = series.AirTime; |
| | 0 | 1384 | | dto.Status = series.Status?.ToString(); |
| | | 1385 | | } |
| | | 1386 | | |
| | | 1387 | | // Add SeasonInfo |
| | 9 | 1388 | | if (item is Season season) |
| | | 1389 | | { |
| | 0 | 1390 | | dto.SeriesName = season.SeriesName; |
| | 0 | 1391 | | dto.SeriesId = season.SeriesId; |
| | | 1392 | | |
| | 0 | 1393 | | series = null; |
| | | 1394 | | |
| | 0 | 1395 | | if (options.ContainsField(ItemFields.SeriesStudio)) |
| | | 1396 | | { |
| | 0 | 1397 | | series ??= season.Series; |
| | 0 | 1398 | | if (series is not null) |
| | | 1399 | | { |
| | 0 | 1400 | | dto.SeriesStudio = series.Studios.FirstOrDefault(); |
| | | 1401 | | } |
| | | 1402 | | } |
| | | 1403 | | |
| | | 1404 | | // this block will add the series poster for seasons without a poster |
| | | 1405 | | // TODO maybe remove the if statement entirely |
| | | 1406 | | // if (options.ContainsField(ItemFields.SeriesPrimaryImage)) |
| | | 1407 | | { |
| | 0 | 1408 | | series ??= season.Series; |
| | 0 | 1409 | | if (series is not null) |
| | | 1410 | | { |
| | 0 | 1411 | | dto.SeriesPrimaryImageTag = GetTagAndFillBlurhash(dto, series, ImageType.Primary); |
| | 0 | 1412 | | if (dto.ImageTags is null || !dto.ImageTags.ContainsKey(ImageType.Primary)) |
| | | 1413 | | { |
| | 0 | 1414 | | AttachPrimaryImageAspectRatio(dto, series); |
| | | 1415 | | } |
| | | 1416 | | } |
| | | 1417 | | } |
| | | 1418 | | } |
| | | 1419 | | |
| | 9 | 1420 | | if (item is MusicVideo musicVideo) |
| | | 1421 | | { |
| | 0 | 1422 | | SetMusicVideoProperties(dto, musicVideo); |
| | | 1423 | | } |
| | | 1424 | | |
| | 9 | 1425 | | if (item is Book book) |
| | | 1426 | | { |
| | 0 | 1427 | | SetBookProperties(dto, book); |
| | | 1428 | | } |
| | | 1429 | | |
| | 9 | 1430 | | if (options.ContainsField(ItemFields.ProductionLocations)) |
| | | 1431 | | { |
| | 6 | 1432 | | if (item.ProductionLocations.Length > 0 || item is Movie) |
| | | 1433 | | { |
| | 0 | 1434 | | dto.ProductionLocations = item.ProductionLocations; |
| | | 1435 | | } |
| | | 1436 | | } |
| | | 1437 | | |
| | 9 | 1438 | | if (options.ContainsField(ItemFields.Width)) |
| | | 1439 | | { |
| | 6 | 1440 | | var width = item.Width; |
| | 6 | 1441 | | if (width > 0) |
| | | 1442 | | { |
| | 0 | 1443 | | dto.Width = width; |
| | | 1444 | | } |
| | | 1445 | | } |
| | | 1446 | | |
| | 9 | 1447 | | if (options.ContainsField(ItemFields.Height)) |
| | | 1448 | | { |
| | 6 | 1449 | | var height = item.Height; |
| | 6 | 1450 | | if (height > 0) |
| | | 1451 | | { |
| | 0 | 1452 | | dto.Height = height; |
| | | 1453 | | } |
| | | 1454 | | } |
| | | 1455 | | |
| | 9 | 1456 | | if (options.ContainsField(ItemFields.IsHD)) |
| | | 1457 | | { |
| | | 1458 | | // Compatibility |
| | 6 | 1459 | | if (item.IsHD) |
| | | 1460 | | { |
| | 0 | 1461 | | dto.IsHD = true; |
| | | 1462 | | } |
| | | 1463 | | } |
| | | 1464 | | |
| | 9 | 1465 | | if (item is Photo photo) |
| | | 1466 | | { |
| | 0 | 1467 | | SetPhotoProperties(dto, photo); |
| | | 1468 | | } |
| | | 1469 | | |
| | 9 | 1470 | | dto.ChannelId = item.ChannelId; |
| | | 1471 | | |
| | 9 | 1472 | | if (item.SourceType == SourceType.Channel) |
| | | 1473 | | { |
| | 0 | 1474 | | var channel = _libraryManager.GetItemById(item.ChannelId); |
| | 0 | 1475 | | if (channel is not null) |
| | | 1476 | | { |
| | 0 | 1477 | | dto.ChannelName = channel.Name; |
| | | 1478 | | } |
| | | 1479 | | } |
| | 9 | 1480 | | } |
| | | 1481 | | |
| | | 1482 | | private BaseItem? GetImageDisplayParent(BaseItem currentItem, BaseItem originalItem) |
| | | 1483 | | { |
| | 0 | 1484 | | if (currentItem is MusicAlbum musicAlbum) |
| | | 1485 | | { |
| | 0 | 1486 | | var artist = musicAlbum.GetMusicArtist(new DtoOptions(false)); |
| | 0 | 1487 | | if (artist is not null) |
| | | 1488 | | { |
| | 0 | 1489 | | return artist; |
| | | 1490 | | } |
| | | 1491 | | } |
| | | 1492 | | |
| | 0 | 1493 | | var parent = currentItem.DisplayParent ?? currentItem.GetOwner() ?? currentItem.GetParent(); |
| | | 1494 | | |
| | 0 | 1495 | | if (parent is null && originalItem is not UserRootFolder && originalItem is not UserView && originalItem is |
| | | 1496 | | { |
| | 0 | 1497 | | parent = _libraryManager.GetCollectionFolders(originalItem).FirstOrDefault(); |
| | | 1498 | | } |
| | | 1499 | | |
| | 0 | 1500 | | return parent; |
| | | 1501 | | } |
| | | 1502 | | |
| | | 1503 | | private void AddInheritedImages(BaseItemDto dto, BaseItem item, DtoOptions options, BaseItem? owner) |
| | | 1504 | | { |
| | 9 | 1505 | | if (!item.SupportsInheritedParentImages) |
| | | 1506 | | { |
| | 9 | 1507 | | return; |
| | | 1508 | | } |
| | | 1509 | | |
| | 0 | 1510 | | var logoLimit = options.GetImageLimit(ImageType.Logo); |
| | 0 | 1511 | | var artLimit = options.GetImageLimit(ImageType.Art); |
| | 0 | 1512 | | var thumbLimit = options.GetImageLimit(ImageType.Thumb); |
| | 0 | 1513 | | var backdropLimit = options.GetImageLimit(ImageType.Backdrop); |
| | | 1514 | | |
| | | 1515 | | // For now. Emby apps are not using this |
| | 0 | 1516 | | artLimit = 0; |
| | | 1517 | | |
| | 0 | 1518 | | if (logoLimit == 0 && artLimit == 0 && thumbLimit == 0 && backdropLimit == 0) |
| | | 1519 | | { |
| | 0 | 1520 | | return; |
| | | 1521 | | } |
| | | 1522 | | |
| | 0 | 1523 | | BaseItem? parent = null; |
| | 0 | 1524 | | var isFirst = true; |
| | | 1525 | | |
| | 0 | 1526 | | var imageTags = dto.ImageTags; |
| | | 1527 | | |
| | 0 | 1528 | | while ((!(imageTags is not null && imageTags.ContainsKey(ImageType.Logo)) && logoLimit > 0) |
| | 0 | 1529 | | || (!(imageTags is not null && imageTags.ContainsKey(ImageType.Art)) && artLimit > 0) |
| | 0 | 1530 | | || (!(imageTags is not null && imageTags.ContainsKey(ImageType.Thumb)) && thumbLimit > 0) |
| | 0 | 1531 | | || parent is Series) |
| | | 1532 | | { |
| | 0 | 1533 | | parent ??= isFirst ? GetImageDisplayParent(item, item) ?? owner : parent; |
| | 0 | 1534 | | if (parent is null) |
| | | 1535 | | { |
| | | 1536 | | break; |
| | | 1537 | | } |
| | | 1538 | | |
| | 0 | 1539 | | var allImages = parent.ImageInfos; |
| | | 1540 | | |
| | 0 | 1541 | | if (logoLimit > 0 && !(imageTags is not null && imageTags.ContainsKey(ImageType.Logo)) && dto.ParentLogo |
| | | 1542 | | { |
| | 0 | 1543 | | var image = allImages.FirstOrDefault(i => i.Type == ImageType.Logo); |
| | | 1544 | | |
| | 0 | 1545 | | if (image is not null) |
| | | 1546 | | { |
| | 0 | 1547 | | dto.ParentLogoItemId = parent.Id; |
| | 0 | 1548 | | dto.ParentLogoImageTag = GetTagAndFillBlurhash(dto, parent, image); |
| | | 1549 | | } |
| | | 1550 | | } |
| | | 1551 | | |
| | 0 | 1552 | | if (artLimit > 0 && !(imageTags is not null && imageTags.ContainsKey(ImageType.Art)) && dto.ParentArtIte |
| | | 1553 | | { |
| | 0 | 1554 | | var image = allImages.FirstOrDefault(i => i.Type == ImageType.Art); |
| | | 1555 | | |
| | 0 | 1556 | | if (image is not null) |
| | | 1557 | | { |
| | 0 | 1558 | | dto.ParentArtItemId = parent.Id; |
| | 0 | 1559 | | dto.ParentArtImageTag = GetTagAndFillBlurhash(dto, parent, image); |
| | | 1560 | | } |
| | | 1561 | | } |
| | | 1562 | | |
| | 0 | 1563 | | if (thumbLimit > 0 && !(imageTags is not null && imageTags.ContainsKey(ImageType.Thumb)) && (dto.ParentT |
| | | 1564 | | { |
| | 0 | 1565 | | var image = allImages.FirstOrDefault(i => i.Type == ImageType.Thumb); |
| | | 1566 | | |
| | 0 | 1567 | | if (image is not null) |
| | | 1568 | | { |
| | 0 | 1569 | | dto.ParentThumbItemId = parent.Id; |
| | 0 | 1570 | | dto.ParentThumbImageTag = GetTagAndFillBlurhash(dto, parent, image); |
| | | 1571 | | } |
| | | 1572 | | } |
| | | 1573 | | |
| | 0 | 1574 | | if (backdropLimit > 0 && !((dto.BackdropImageTags is not null && dto.BackdropImageTags.Length > 0) || (d |
| | | 1575 | | { |
| | 0 | 1576 | | var images = allImages.Where(i => i.Type == ImageType.Backdrop).Take(backdropLimit).ToList(); |
| | | 1577 | | |
| | 0 | 1578 | | if (images.Count > 0) |
| | | 1579 | | { |
| | 0 | 1580 | | dto.ParentBackdropItemId = parent.Id; |
| | 0 | 1581 | | dto.ParentBackdropImageTags = GetTagsAndFillBlurhashes(dto, parent, ImageType.Backdrop, images); |
| | | 1582 | | } |
| | | 1583 | | } |
| | | 1584 | | |
| | 0 | 1585 | | isFirst = false; |
| | | 1586 | | |
| | 0 | 1587 | | if (!parent.SupportsInheritedParentImages) |
| | | 1588 | | { |
| | | 1589 | | break; |
| | | 1590 | | } |
| | | 1591 | | |
| | 0 | 1592 | | parent = GetImageDisplayParent(parent, item); |
| | | 1593 | | } |
| | 0 | 1594 | | } |
| | | 1595 | | |
| | | 1596 | | private string GetMappedPath(BaseItem item, BaseItem? ownerItem) |
| | | 1597 | | { |
| | 6 | 1598 | | var path = item.Path; |
| | | 1599 | | |
| | 6 | 1600 | | if (item.IsFileProtocol) |
| | | 1601 | | { |
| | 6 | 1602 | | path = _libraryManager.GetPathAfterNetworkSubstitution(path, ownerItem ?? item); |
| | | 1603 | | } |
| | | 1604 | | |
| | 6 | 1605 | | return path; |
| | | 1606 | | } |
| | | 1607 | | |
| | | 1608 | | /// <summary> |
| | | 1609 | | /// Attaches the primary image aspect ratio. |
| | | 1610 | | /// </summary> |
| | | 1611 | | /// <param name="dto">The dto.</param> |
| | | 1612 | | /// <param name="item">The item.</param> |
| | | 1613 | | public void AttachPrimaryImageAspectRatio(IItemDto dto, BaseItem item) |
| | | 1614 | | { |
| | 6 | 1615 | | dto.PrimaryImageAspectRatio = GetPrimaryImageAspectRatio(item); |
| | 6 | 1616 | | } |
| | | 1617 | | |
| | | 1618 | | public double? GetPrimaryImageAspectRatio(BaseItem item) |
| | | 1619 | | { |
| | 6 | 1620 | | var imageInfo = item.GetImageInfo(ImageType.Primary, 0); |
| | | 1621 | | |
| | 6 | 1622 | | if (imageInfo is null) |
| | | 1623 | | { |
| | 6 | 1624 | | return null; |
| | | 1625 | | } |
| | | 1626 | | |
| | 0 | 1627 | | if (!imageInfo.IsLocalFile) |
| | | 1628 | | { |
| | 0 | 1629 | | return item.GetDefaultPrimaryImageAspectRatio(); |
| | | 1630 | | } |
| | | 1631 | | |
| | | 1632 | | try |
| | | 1633 | | { |
| | 0 | 1634 | | var size = _imageProcessor.GetImageDimensions(item, imageInfo); |
| | 0 | 1635 | | var width = size.Width; |
| | 0 | 1636 | | var height = size.Height; |
| | 0 | 1637 | | if (width > 0 && height > 0) |
| | | 1638 | | { |
| | 0 | 1639 | | return (double)width / height; |
| | | 1640 | | } |
| | 0 | 1641 | | } |
| | 0 | 1642 | | catch (Exception ex) |
| | | 1643 | | { |
| | 0 | 1644 | | _logger.LogError(ex, "Failed to determine primary image aspect ratio for {ImagePath}", imageInfo.Path); |
| | 0 | 1645 | | } |
| | | 1646 | | |
| | 0 | 1647 | | return item.GetDefaultPrimaryImageAspectRatio(); |
| | 0 | 1648 | | } |
| | | 1649 | | } |
| | | 1650 | | } |