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