DtoService.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using Jellyfin.Data.Enums;
  8. using Jellyfin.Database.Implementations.Entities;
  9. using Jellyfin.Extensions;
  10. using MediaBrowser.Common;
  11. using MediaBrowser.Controller.Channels;
  12. using MediaBrowser.Controller.Chapters;
  13. using MediaBrowser.Controller.Drawing;
  14. using MediaBrowser.Controller.Dto;
  15. using MediaBrowser.Controller.Entities;
  16. using MediaBrowser.Controller.Entities.Audio;
  17. using MediaBrowser.Controller.Library;
  18. using MediaBrowser.Controller.LiveTv;
  19. using MediaBrowser.Controller.Persistence;
  20. using MediaBrowser.Controller.Playlists;
  21. using MediaBrowser.Controller.Providers;
  22. using MediaBrowser.Controller.Trickplay;
  23. using MediaBrowser.Model.Dto;
  24. using MediaBrowser.Model.Entities;
  25. using MediaBrowser.Model.Querying;
  26. using Microsoft.Extensions.Logging;
  27. using Book = MediaBrowser.Controller.Entities.Book;
  28. using Episode = MediaBrowser.Controller.Entities.TV.Episode;
  29. using Movie = MediaBrowser.Controller.Entities.Movies.Movie;
  30. using MusicAlbum = MediaBrowser.Controller.Entities.Audio.MusicAlbum;
  31. using Person = MediaBrowser.Controller.Entities.Person;
  32. using Photo = MediaBrowser.Controller.Entities.Photo;
  33. using Season = MediaBrowser.Controller.Entities.TV.Season;
  34. using Series = MediaBrowser.Controller.Entities.TV.Series;
  35. namespace Emby.Server.Implementations.Dto
  36. {
  37. public class DtoService : IDtoService
  38. {
  39. private readonly ILogger<DtoService> _logger;
  40. private readonly ILibraryManager _libraryManager;
  41. private readonly IUserDataManager _userDataRepository;
  42. private readonly IImageProcessor _imageProcessor;
  43. private readonly IProviderManager _providerManager;
  44. private readonly IRecordingsManager _recordingsManager;
  45. private readonly IApplicationHost _appHost;
  46. private readonly IMediaSourceManager _mediaSourceManager;
  47. private readonly Lazy<ILiveTvManager> _livetvManagerFactory;
  48. private readonly ITrickplayManager _trickplayManager;
  49. private readonly IChapterRepository _chapterRepository;
  50. public DtoService(
  51. ILogger<DtoService> logger,
  52. ILibraryManager libraryManager,
  53. IUserDataManager userDataRepository,
  54. IImageProcessor imageProcessor,
  55. IProviderManager providerManager,
  56. IRecordingsManager recordingsManager,
  57. IApplicationHost appHost,
  58. IMediaSourceManager mediaSourceManager,
  59. Lazy<ILiveTvManager> livetvManagerFactory,
  60. ITrickplayManager trickplayManager,
  61. IChapterRepository chapterRepository)
  62. {
  63. _logger = logger;
  64. _libraryManager = libraryManager;
  65. _userDataRepository = userDataRepository;
  66. _imageProcessor = imageProcessor;
  67. _providerManager = providerManager;
  68. _recordingsManager = recordingsManager;
  69. _appHost = appHost;
  70. _mediaSourceManager = mediaSourceManager;
  71. _livetvManagerFactory = livetvManagerFactory;
  72. _trickplayManager = trickplayManager;
  73. _chapterRepository = chapterRepository;
  74. }
  75. private ILiveTvManager LivetvManager => _livetvManagerFactory.Value;
  76. /// <inheritdoc />
  77. public IReadOnlyList<BaseItemDto> GetBaseItemDtos(IReadOnlyList<BaseItem> items, DtoOptions options, User? user = null, BaseItem? owner = null)
  78. {
  79. var accessibleItems = user is null ? items : items.Where(x => x.IsVisible(user)).ToList();
  80. var returnItems = new BaseItemDto[accessibleItems.Count];
  81. List<(BaseItem, BaseItemDto)>? programTuples = null;
  82. List<(BaseItemDto, LiveTvChannel)>? channelTuples = null;
  83. for (int index = 0; index < accessibleItems.Count; index++)
  84. {
  85. var item = accessibleItems[index];
  86. var dto = GetBaseItemDtoInternal(item, options, user, owner);
  87. if (item is LiveTvChannel tvChannel)
  88. {
  89. (channelTuples ??= []).Add((dto, tvChannel));
  90. }
  91. else if (item is LiveTvProgram)
  92. {
  93. (programTuples ??= []).Add((item, dto));
  94. }
  95. if (item is IItemByName byName)
  96. {
  97. if (options.ContainsField(ItemFields.ItemCounts))
  98. {
  99. var libraryItems = byName.GetTaggedItems(new InternalItemsQuery(user)
  100. {
  101. Recursive = true,
  102. DtoOptions = new DtoOptions(false)
  103. {
  104. EnableImages = false
  105. }
  106. });
  107. SetItemByNameInfo(item, dto, libraryItems);
  108. }
  109. }
  110. returnItems[index] = dto;
  111. }
  112. if (programTuples is not null)
  113. {
  114. LivetvManager.AddInfoToProgramDto(programTuples, options.Fields, user).GetAwaiter().GetResult();
  115. }
  116. if (channelTuples is not null)
  117. {
  118. LivetvManager.AddChannelInfo(channelTuples, options, user);
  119. }
  120. return returnItems;
  121. }
  122. public BaseItemDto GetBaseItemDto(BaseItem item, DtoOptions options, User? user = null, BaseItem? owner = null)
  123. {
  124. var dto = GetBaseItemDtoInternal(item, options, user, owner);
  125. if (item is LiveTvChannel tvChannel)
  126. {
  127. LivetvManager.AddChannelInfo(new[] { (dto, tvChannel) }, options, user);
  128. }
  129. else if (item is LiveTvProgram)
  130. {
  131. LivetvManager.AddInfoToProgramDto(new[] { (item, dto) }, options.Fields, user).GetAwaiter().GetResult();
  132. }
  133. if (item is IItemByName itemByName
  134. && options.ContainsField(ItemFields.ItemCounts))
  135. {
  136. SetItemByNameInfo(
  137. item,
  138. dto,
  139. GetTaggedItems(
  140. itemByName,
  141. user,
  142. new DtoOptions(false)
  143. {
  144. EnableImages = false
  145. }));
  146. }
  147. return dto;
  148. }
  149. private static IReadOnlyList<BaseItem> GetTaggedItems(IItemByName byName, User? user, DtoOptions options)
  150. {
  151. return byName.GetTaggedItems(
  152. new InternalItemsQuery(user)
  153. {
  154. Recursive = true,
  155. DtoOptions = options
  156. });
  157. }
  158. private BaseItemDto GetBaseItemDtoInternal(BaseItem item, DtoOptions options, User? user = null, BaseItem? owner = null)
  159. {
  160. var dto = new BaseItemDto
  161. {
  162. ServerId = _appHost.SystemId
  163. };
  164. if (item.SourceType == SourceType.Channel)
  165. {
  166. dto.SourceType = item.SourceType.ToString();
  167. }
  168. if (options.ContainsField(ItemFields.People))
  169. {
  170. AttachPeople(dto, item, user);
  171. }
  172. if (options.ContainsField(ItemFields.PrimaryImageAspectRatio))
  173. {
  174. try
  175. {
  176. AttachPrimaryImageAspectRatio(dto, item);
  177. }
  178. catch (Exception ex)
  179. {
  180. // Have to use a catch-all unfortunately because some .net image methods throw plain Exceptions
  181. _logger.LogError(ex, "Error generating PrimaryImageAspectRatio for {ItemName}", item.Name);
  182. }
  183. }
  184. if (options.ContainsField(ItemFields.DisplayPreferencesId))
  185. {
  186. dto.DisplayPreferencesId = item.DisplayPreferencesId.ToString("N", CultureInfo.InvariantCulture);
  187. }
  188. if (user is not null)
  189. {
  190. AttachUserSpecificInfo(dto, item, user, options);
  191. }
  192. if (item is IHasMediaSources
  193. && options.ContainsField(ItemFields.MediaSources))
  194. {
  195. dto.MediaSources = _mediaSourceManager.GetStaticMediaSources(item, true, user).ToArray();
  196. NormalizeMediaSourceContainers(dto);
  197. }
  198. if (options.ContainsField(ItemFields.Studios))
  199. {
  200. AttachStudios(dto, item);
  201. }
  202. AttachBasicFields(dto, item, owner, options);
  203. if (options.ContainsField(ItemFields.CanDelete))
  204. {
  205. dto.CanDelete = user is null
  206. ? item.CanDelete()
  207. : item.CanDelete(user);
  208. }
  209. if (options.ContainsField(ItemFields.CanDownload))
  210. {
  211. dto.CanDownload = user is null
  212. ? item.CanDownload()
  213. : item.CanDownload(user);
  214. }
  215. if (options.ContainsField(ItemFields.Etag))
  216. {
  217. dto.Etag = item.GetEtag(user);
  218. }
  219. var activeRecording = _recordingsManager.GetActiveRecordingInfo(item.Path);
  220. if (activeRecording is not null)
  221. {
  222. dto.Type = BaseItemKind.Recording;
  223. dto.CanDownload = false;
  224. dto.RunTimeTicks = null;
  225. if (!string.IsNullOrEmpty(dto.SeriesName))
  226. {
  227. dto.EpisodeTitle = dto.Name;
  228. dto.Name = dto.SeriesName;
  229. }
  230. LivetvManager.AddInfoToRecordingDto(item, dto, activeRecording, user);
  231. }
  232. if (item is Audio audio)
  233. {
  234. dto.HasLyrics = audio.GetMediaStreams().Any(s => s.Type == MediaStreamType.Lyric);
  235. }
  236. return dto;
  237. }
  238. private static void NormalizeMediaSourceContainers(BaseItemDto dto)
  239. {
  240. foreach (var mediaSource in dto.MediaSources)
  241. {
  242. var container = mediaSource.Container;
  243. if (string.IsNullOrEmpty(container))
  244. {
  245. continue;
  246. }
  247. var containers = container.Split(',');
  248. if (containers.Length < 2)
  249. {
  250. continue;
  251. }
  252. var path = mediaSource.Path;
  253. string? fileExtensionContainer = null;
  254. if (!string.IsNullOrEmpty(path))
  255. {
  256. path = Path.GetExtension(path);
  257. if (!string.IsNullOrEmpty(path))
  258. {
  259. path = Path.GetExtension(path);
  260. if (!string.IsNullOrEmpty(path))
  261. {
  262. path = path.TrimStart('.');
  263. }
  264. if (!string.IsNullOrEmpty(path) && containers.Contains(path, StringComparison.OrdinalIgnoreCase))
  265. {
  266. fileExtensionContainer = path;
  267. }
  268. }
  269. }
  270. mediaSource.Container = fileExtensionContainer ?? containers[0];
  271. }
  272. }
  273. /// <inheritdoc />
  274. public BaseItemDto GetItemByNameDto(BaseItem item, DtoOptions options, List<BaseItem>? taggedItems, User? user = null)
  275. {
  276. var dto = GetBaseItemDtoInternal(item, options, user);
  277. if (taggedItems is not null && options.ContainsField(ItemFields.ItemCounts))
  278. {
  279. SetItemByNameInfo(item, dto, taggedItems);
  280. }
  281. return dto;
  282. }
  283. private static void SetItemByNameInfo(BaseItem item, BaseItemDto dto, IReadOnlyList<BaseItem> taggedItems)
  284. {
  285. if (item is MusicArtist)
  286. {
  287. dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum);
  288. dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo);
  289. dto.SongCount = taggedItems.Count(i => i is Audio);
  290. }
  291. else if (item is MusicGenre)
  292. {
  293. dto.ArtistCount = taggedItems.Count(i => i is MusicArtist);
  294. dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum);
  295. dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo);
  296. dto.SongCount = taggedItems.Count(i => i is Audio);
  297. }
  298. else
  299. {
  300. // This populates them all and covers Genre, Person, Studio, Year
  301. dto.ArtistCount = taggedItems.Count(i => i is MusicArtist);
  302. dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum);
  303. dto.EpisodeCount = taggedItems.Count(i => i is Episode);
  304. dto.MovieCount = taggedItems.Count(i => i is Movie);
  305. dto.TrailerCount = taggedItems.Count(i => i is Trailer);
  306. dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo);
  307. dto.SeriesCount = taggedItems.Count(i => i is Series);
  308. dto.ProgramCount = taggedItems.Count(i => i is LiveTvProgram);
  309. dto.SongCount = taggedItems.Count(i => i is Audio);
  310. }
  311. dto.ChildCount = taggedItems.Count;
  312. }
  313. /// <summary>
  314. /// Attaches the user specific info.
  315. /// </summary>
  316. private void AttachUserSpecificInfo(BaseItemDto dto, BaseItem item, User user, DtoOptions options)
  317. {
  318. if (item.IsFolder)
  319. {
  320. var folder = (Folder)item;
  321. if (options.EnableUserData)
  322. {
  323. dto.UserData = _userDataRepository.GetUserDataDto(item, dto, user, options);
  324. }
  325. if (!dto.ChildCount.HasValue && item.SourceType == SourceType.Library)
  326. {
  327. // For these types we can try to optimize and assume these values will be equal
  328. if (item is MusicAlbum || item is Season || item is Playlist)
  329. {
  330. dto.ChildCount = dto.RecursiveItemCount;
  331. var folderChildCount = folder.LinkedChildren.Length;
  332. // The default is an empty array, so we can't reliably use the count when it's empty
  333. if (folderChildCount > 0)
  334. {
  335. dto.ChildCount ??= folderChildCount;
  336. }
  337. }
  338. if (options.ContainsField(ItemFields.ChildCount))
  339. {
  340. dto.ChildCount ??= GetChildCount(folder, user);
  341. }
  342. }
  343. if (options.ContainsField(ItemFields.CumulativeRunTimeTicks))
  344. {
  345. dto.CumulativeRunTimeTicks = item.RunTimeTicks;
  346. }
  347. if (options.ContainsField(ItemFields.DateLastMediaAdded))
  348. {
  349. dto.DateLastMediaAdded = folder.DateLastMediaAdded;
  350. }
  351. }
  352. else
  353. {
  354. if (options.EnableUserData)
  355. {
  356. dto.UserData = _userDataRepository.GetUserDataDto(item, user);
  357. }
  358. }
  359. if (options.ContainsField(ItemFields.PlayAccess))
  360. {
  361. dto.PlayAccess = item.GetPlayAccess(user);
  362. }
  363. }
  364. private static int GetChildCount(Folder folder, User user)
  365. {
  366. // Right now this is too slow to calculate for top level folders on a per-user basis
  367. // Just return something so that apps that are expecting a value won't think the folders are empty
  368. if (folder is ICollectionFolder || folder is UserView)
  369. {
  370. return Random.Shared.Next(1, 10);
  371. }
  372. return folder.GetChildCount(user);
  373. }
  374. private static void SetBookProperties(BaseItemDto dto, Book item)
  375. {
  376. dto.SeriesName = item.SeriesName;
  377. }
  378. private static void SetPhotoProperties(BaseItemDto dto, Photo item)
  379. {
  380. dto.CameraMake = item.CameraMake;
  381. dto.CameraModel = item.CameraModel;
  382. dto.Software = item.Software;
  383. dto.ExposureTime = item.ExposureTime;
  384. dto.FocalLength = item.FocalLength;
  385. dto.ImageOrientation = item.Orientation;
  386. dto.Aperture = item.Aperture;
  387. dto.ShutterSpeed = item.ShutterSpeed;
  388. dto.Latitude = item.Latitude;
  389. dto.Longitude = item.Longitude;
  390. dto.Altitude = item.Altitude;
  391. dto.IsoSpeedRating = item.IsoSpeedRating;
  392. var album = item.AlbumEntity;
  393. if (album is not null)
  394. {
  395. dto.Album = album.Name;
  396. dto.AlbumId = album.Id;
  397. }
  398. }
  399. private void SetMusicVideoProperties(BaseItemDto dto, MusicVideo item)
  400. {
  401. if (!string.IsNullOrEmpty(item.Album))
  402. {
  403. var parentAlbumIds = _libraryManager.GetItemIds(new InternalItemsQuery
  404. {
  405. IncludeItemTypes = new[] { BaseItemKind.MusicAlbum },
  406. Name = item.Album,
  407. Limit = 1
  408. });
  409. if (parentAlbumIds.Count > 0)
  410. {
  411. dto.AlbumId = parentAlbumIds[0];
  412. }
  413. }
  414. dto.Album = item.Album;
  415. }
  416. private string[] GetImageTags(BaseItem item, List<ItemImageInfo> images)
  417. {
  418. return images
  419. .Select(p => GetImageCacheTag(item, p))
  420. .Where(i => i is not null)
  421. .ToArray()!; // null values got filtered out
  422. }
  423. private string? GetImageCacheTag(BaseItem item, ItemImageInfo image)
  424. {
  425. try
  426. {
  427. return _imageProcessor.GetImageCacheTag(item, image);
  428. }
  429. catch (Exception ex)
  430. {
  431. _logger.LogError(ex, "Error getting {ImageType} image info for {Path}", image.Type, image.Path);
  432. return null;
  433. }
  434. }
  435. /// <summary>
  436. /// Attaches People DTO's to a DTOBaseItem.
  437. /// </summary>
  438. /// <param name="dto">The dto.</param>
  439. /// <param name="item">The item.</param>
  440. /// <param name="user">The requesting user.</param>
  441. private void AttachPeople(BaseItemDto dto, BaseItem item, User? user = null)
  442. {
  443. // Ordering by person type to ensure actors and artists are at the front.
  444. // This is taking advantage of the fact that they both begin with A
  445. // This should be improved in the future
  446. var people = _libraryManager.GetPeople(item).OrderBy(i => i.SortOrder ?? int.MaxValue)
  447. .ThenBy(i =>
  448. {
  449. if (i.IsType(PersonKind.Actor))
  450. {
  451. return 0;
  452. }
  453. if (i.IsType(PersonKind.GuestStar))
  454. {
  455. return 1;
  456. }
  457. if (i.IsType(PersonKind.Director))
  458. {
  459. return 2;
  460. }
  461. if (i.IsType(PersonKind.Writer))
  462. {
  463. return 3;
  464. }
  465. if (i.IsType(PersonKind.Producer))
  466. {
  467. return 4;
  468. }
  469. if (i.IsType(PersonKind.Composer))
  470. {
  471. return 4;
  472. }
  473. return 10;
  474. })
  475. .ToList();
  476. var list = new List<BaseItemPerson>();
  477. Dictionary<string, Person> dictionary = people.Select(p => p.Name)
  478. .Distinct(StringComparer.OrdinalIgnoreCase).Select(c =>
  479. {
  480. try
  481. {
  482. return _libraryManager.GetPerson(c);
  483. }
  484. catch (Exception ex)
  485. {
  486. _logger.LogError(ex, "Error getting person {Name}", c);
  487. return null;
  488. }
  489. }).Where(i => i is not null)
  490. .Where(i => user is null || i!.IsVisible(user))
  491. .DistinctBy(x => x!.Name, StringComparer.OrdinalIgnoreCase)
  492. .ToDictionary(i => i!.Name, StringComparer.OrdinalIgnoreCase)!; // null values got filtered out
  493. for (var i = 0; i < people.Count; i++)
  494. {
  495. var person = people[i];
  496. var baseItemPerson = new BaseItemPerson
  497. {
  498. Name = person.Name,
  499. Role = person.Role,
  500. Type = person.Type
  501. };
  502. if (dictionary.TryGetValue(person.Name, out Person? entity))
  503. {
  504. baseItemPerson.PrimaryImageTag = GetTagAndFillBlurhash(dto, entity, ImageType.Primary);
  505. baseItemPerson.Id = entity.Id;
  506. if (dto.ImageBlurHashes is not null)
  507. {
  508. // Only add BlurHash for the person's image.
  509. baseItemPerson.ImageBlurHashes = [];
  510. foreach (var (imageType, blurHash) in dto.ImageBlurHashes)
  511. {
  512. if (blurHash is not null)
  513. {
  514. baseItemPerson.ImageBlurHashes[imageType] = [];
  515. foreach (var (imageId, blurHashValue) in blurHash)
  516. {
  517. if (string.Equals(baseItemPerson.PrimaryImageTag, imageId, StringComparison.OrdinalIgnoreCase))
  518. {
  519. baseItemPerson.ImageBlurHashes[imageType][imageId] = blurHashValue;
  520. }
  521. }
  522. }
  523. }
  524. }
  525. list.Add(baseItemPerson);
  526. }
  527. }
  528. dto.People = list.ToArray();
  529. }
  530. /// <summary>
  531. /// Attaches the studios.
  532. /// </summary>
  533. /// <param name="dto">The dto.</param>
  534. /// <param name="item">The item.</param>
  535. private void AttachStudios(BaseItemDto dto, BaseItem item)
  536. {
  537. dto.Studios = item.Studios
  538. .Where(i => !string.IsNullOrEmpty(i))
  539. .Select(i => new NameGuidPair
  540. {
  541. Name = i,
  542. Id = _libraryManager.GetStudioId(i)
  543. })
  544. .ToArray();
  545. }
  546. private void AttachGenreItems(BaseItemDto dto, BaseItem item)
  547. {
  548. dto.GenreItems = item.Genres
  549. .Where(i => !string.IsNullOrEmpty(i))
  550. .Select(i => new NameGuidPair
  551. {
  552. Name = i,
  553. Id = GetGenreId(i, item)
  554. })
  555. .ToArray();
  556. }
  557. private Guid GetGenreId(string name, BaseItem owner)
  558. {
  559. if (owner is IHasMusicGenres)
  560. {
  561. return _libraryManager.GetMusicGenreId(name);
  562. }
  563. return _libraryManager.GetGenreId(name);
  564. }
  565. private string? GetTagAndFillBlurhash(BaseItemDto dto, BaseItem item, ImageType imageType, int imageIndex = 0)
  566. {
  567. var image = item.GetImageInfo(imageType, imageIndex);
  568. if (image is not null)
  569. {
  570. return GetTagAndFillBlurhash(dto, item, image);
  571. }
  572. return null;
  573. }
  574. private string? GetTagAndFillBlurhash(BaseItemDto dto, BaseItem item, ItemImageInfo image)
  575. {
  576. var tag = GetImageCacheTag(item, image);
  577. if (tag is null)
  578. {
  579. return null;
  580. }
  581. if (!string.IsNullOrEmpty(image.BlurHash))
  582. {
  583. dto.ImageBlurHashes ??= [];
  584. if (!dto.ImageBlurHashes.TryGetValue(image.Type, out var value))
  585. {
  586. value = [];
  587. dto.ImageBlurHashes[image.Type] = value;
  588. }
  589. value[tag] = image.BlurHash;
  590. }
  591. return tag;
  592. }
  593. private string[] GetTagsAndFillBlurhashes(BaseItemDto dto, BaseItem item, ImageType imageType, int limit)
  594. {
  595. return GetTagsAndFillBlurhashes(dto, item, imageType, item.GetImages(imageType).Take(limit).ToList());
  596. }
  597. private string[] GetTagsAndFillBlurhashes(BaseItemDto dto, BaseItem item, ImageType imageType, List<ItemImageInfo> images)
  598. {
  599. var tags = GetImageTags(item, images);
  600. var hashes = new Dictionary<string, string>();
  601. for (int i = 0; i < images.Count; i++)
  602. {
  603. var img = images[i];
  604. if (!string.IsNullOrEmpty(img.BlurHash))
  605. {
  606. var tag = tags[i];
  607. hashes[tag] = img.BlurHash;
  608. }
  609. }
  610. if (hashes.Count > 0)
  611. {
  612. dto.ImageBlurHashes ??= [];
  613. dto.ImageBlurHashes[imageType] = hashes;
  614. }
  615. return tags;
  616. }
  617. /// <summary>
  618. /// Sets simple property values on a DTOBaseItem.
  619. /// </summary>
  620. /// <param name="dto">The dto.</param>
  621. /// <param name="item">The item.</param>
  622. /// <param name="owner">The owner.</param>
  623. /// <param name="options">The options.</param>
  624. private void AttachBasicFields(BaseItemDto dto, BaseItem item, BaseItem? owner, DtoOptions options)
  625. {
  626. if (options.ContainsField(ItemFields.DateCreated))
  627. {
  628. dto.DateCreated = item.DateCreated;
  629. }
  630. if (options.ContainsField(ItemFields.Settings))
  631. {
  632. dto.LockedFields = item.LockedFields;
  633. dto.LockData = item.IsLocked;
  634. dto.ForcedSortName = item.ForcedSortName;
  635. }
  636. dto.Container = item.Container;
  637. dto.EndDate = item.EndDate;
  638. if (options.ContainsField(ItemFields.ExternalUrls))
  639. {
  640. dto.ExternalUrls = _providerManager.GetExternalUrls(item).ToArray();
  641. }
  642. if (options.ContainsField(ItemFields.Tags))
  643. {
  644. dto.Tags = item.Tags;
  645. }
  646. if (item is IHasAspectRatio hasAspectRatio)
  647. {
  648. dto.AspectRatio = hasAspectRatio.AspectRatio;
  649. }
  650. dto.ImageBlurHashes = [];
  651. var backdropLimit = options.GetImageLimit(ImageType.Backdrop);
  652. if (backdropLimit > 0)
  653. {
  654. dto.BackdropImageTags = GetTagsAndFillBlurhashes(dto, item, ImageType.Backdrop, backdropLimit);
  655. }
  656. if (options.ContainsField(ItemFields.Genres))
  657. {
  658. dto.Genres = item.Genres;
  659. AttachGenreItems(dto, item);
  660. }
  661. if (options.EnableImages)
  662. {
  663. dto.ImageTags = [];
  664. // Prevent implicitly captured closure
  665. var currentItem = item;
  666. foreach (var image in currentItem.ImageInfos.Where(i => !currentItem.AllowsMultipleImages(i.Type)))
  667. {
  668. if (options.GetImageLimit(image.Type) > 0)
  669. {
  670. var tag = GetTagAndFillBlurhash(dto, item, image);
  671. if (tag is not null)
  672. {
  673. dto.ImageTags[image.Type] = tag;
  674. }
  675. }
  676. }
  677. }
  678. dto.Id = item.Id;
  679. dto.IndexNumber = item.IndexNumber;
  680. dto.ParentIndexNumber = item.ParentIndexNumber;
  681. if (item.IsFolder)
  682. {
  683. dto.IsFolder = true;
  684. }
  685. else if (item is IHasMediaSources)
  686. {
  687. dto.IsFolder = false;
  688. }
  689. dto.MediaType = item.MediaType;
  690. if (item is not LiveTvProgram)
  691. {
  692. dto.LocationType = item.LocationType;
  693. }
  694. dto.Audio = item.Audio;
  695. if (options.ContainsField(ItemFields.Settings))
  696. {
  697. dto.PreferredMetadataCountryCode = item.PreferredMetadataCountryCode;
  698. dto.PreferredMetadataLanguage = item.PreferredMetadataLanguage;
  699. }
  700. dto.CriticRating = item.CriticRating;
  701. if (item is IHasDisplayOrder hasDisplayOrder)
  702. {
  703. dto.DisplayOrder = hasDisplayOrder.DisplayOrder;
  704. }
  705. if (item is IHasCollectionType hasCollectionType)
  706. {
  707. dto.CollectionType = hasCollectionType.CollectionType;
  708. }
  709. if (options.ContainsField(ItemFields.RemoteTrailers))
  710. {
  711. dto.RemoteTrailers = item.RemoteTrailers;
  712. }
  713. dto.Name = item.Name;
  714. dto.OfficialRating = item.OfficialRating;
  715. if (options.ContainsField(ItemFields.Overview))
  716. {
  717. dto.Overview = item.Overview;
  718. }
  719. if (options.ContainsField(ItemFields.OriginalTitle))
  720. {
  721. dto.OriginalTitle = item.OriginalTitle;
  722. }
  723. if (options.ContainsField(ItemFields.ParentId))
  724. {
  725. dto.ParentId = item.DisplayParentId;
  726. }
  727. AddInheritedImages(dto, item, options, owner);
  728. if (options.ContainsField(ItemFields.Path))
  729. {
  730. dto.Path = GetMappedPath(item, owner);
  731. }
  732. if (options.ContainsField(ItemFields.EnableMediaSourceDisplay))
  733. {
  734. dto.EnableMediaSourceDisplay = item.EnableMediaSourceDisplay;
  735. }
  736. dto.PremiereDate = item.PremiereDate;
  737. dto.ProductionYear = item.ProductionYear;
  738. if (options.ContainsField(ItemFields.ProviderIds))
  739. {
  740. dto.ProviderIds = item.ProviderIds;
  741. }
  742. dto.RunTimeTicks = item.RunTimeTicks;
  743. if (options.ContainsField(ItemFields.SortName))
  744. {
  745. dto.SortName = item.SortName;
  746. }
  747. if (options.ContainsField(ItemFields.CustomRating))
  748. {
  749. dto.CustomRating = item.CustomRating;
  750. }
  751. if (options.ContainsField(ItemFields.Taglines))
  752. {
  753. if (!string.IsNullOrEmpty(item.Tagline))
  754. {
  755. dto.Taglines = new string[] { item.Tagline };
  756. }
  757. dto.Taglines ??= Array.Empty<string>();
  758. }
  759. dto.Type = item.GetBaseItemKind();
  760. if ((item.CommunityRating ?? 0) > 0)
  761. {
  762. dto.CommunityRating = item.CommunityRating;
  763. }
  764. if (item is ISupportsPlaceHolders supportsPlaceHolders && supportsPlaceHolders.IsPlaceHolder)
  765. {
  766. dto.IsPlaceHolder = supportsPlaceHolders.IsPlaceHolder;
  767. }
  768. if (item.LUFS.HasValue)
  769. {
  770. // -18 LUFS reference, same as ReplayGain 2.0, compatible with ReplayGain 1.0
  771. dto.NormalizationGain = -18f - item.LUFS;
  772. }
  773. else if (item.NormalizationGain.HasValue)
  774. {
  775. dto.NormalizationGain = item.NormalizationGain;
  776. }
  777. // Add audio info
  778. if (item is Audio audio)
  779. {
  780. dto.Album = audio.Album;
  781. dto.ExtraType = audio.ExtraType;
  782. var albumParent = audio.AlbumEntity;
  783. if (albumParent is not null)
  784. {
  785. dto.AlbumId = albumParent.Id;
  786. dto.AlbumPrimaryImageTag = GetTagAndFillBlurhash(dto, albumParent, ImageType.Primary);
  787. }
  788. // if (options.ContainsField(ItemFields.MediaSourceCount))
  789. // {
  790. // Songs always have one
  791. // }
  792. }
  793. if (item is IHasArtist hasArtist)
  794. {
  795. dto.Artists = hasArtist.Artists;
  796. // var artistItems = _libraryManager.GetArtists(new InternalItemsQuery
  797. // {
  798. // EnableTotalRecordCount = false,
  799. // ItemIds = new[] { item.Id.ToString("N", CultureInfo.InvariantCulture) }
  800. // });
  801. // dto.ArtistItems = artistItems.Items
  802. // .Select(i =>
  803. // {
  804. // var artist = i.Item1;
  805. // return new NameIdPair
  806. // {
  807. // Name = artist.Name,
  808. // Id = artist.Id.ToString("N", CultureInfo.InvariantCulture)
  809. // };
  810. // })
  811. // .ToList();
  812. // Include artists that are not in the database yet, e.g., just added via metadata editor
  813. // var foundArtists = artistItems.Items.Select(i => i.Item1.Name).ToList();
  814. dto.ArtistItems = hasArtist.Artists
  815. // .Except(foundArtists, new DistinctNameComparer())
  816. .Select(i =>
  817. {
  818. // This should not be necessary but we're seeing some cases of it
  819. if (string.IsNullOrEmpty(i))
  820. {
  821. return null;
  822. }
  823. var artist = _libraryManager.GetArtist(i, new DtoOptions(false)
  824. {
  825. EnableImages = false
  826. });
  827. if (artist is not null)
  828. {
  829. return new NameGuidPair
  830. {
  831. Name = artist.Name,
  832. Id = artist.Id
  833. };
  834. }
  835. return null;
  836. }).Where(i => i is not null).ToArray();
  837. }
  838. if (item is IHasAlbumArtist hasAlbumArtist)
  839. {
  840. dto.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault();
  841. // var artistItems = _libraryManager.GetAlbumArtists(new InternalItemsQuery
  842. // {
  843. // EnableTotalRecordCount = false,
  844. // ItemIds = new[] { item.Id.ToString("N", CultureInfo.InvariantCulture) }
  845. // });
  846. // dto.AlbumArtists = artistItems.Items
  847. // .Select(i =>
  848. // {
  849. // var artist = i.Item1;
  850. // return new NameIdPair
  851. // {
  852. // Name = artist.Name,
  853. // Id = artist.Id.ToString("N", CultureInfo.InvariantCulture)
  854. // };
  855. // })
  856. // .ToList();
  857. dto.AlbumArtists = hasAlbumArtist.AlbumArtists
  858. // .Except(foundArtists, new DistinctNameComparer())
  859. .Select(i =>
  860. {
  861. // This should not be necessary but we're seeing some cases of it
  862. if (string.IsNullOrEmpty(i))
  863. {
  864. return null;
  865. }
  866. var artist = _libraryManager.GetArtist(i, new DtoOptions(false)
  867. {
  868. EnableImages = false
  869. });
  870. if (artist is not null)
  871. {
  872. return new NameGuidPair
  873. {
  874. Name = artist.Name,
  875. Id = artist.Id
  876. };
  877. }
  878. return null;
  879. }).Where(i => i is not null).ToArray();
  880. }
  881. // Add video info
  882. if (item is Video video)
  883. {
  884. dto.VideoType = video.VideoType;
  885. dto.Video3DFormat = video.Video3DFormat;
  886. dto.IsoType = video.IsoType;
  887. if (video.HasSubtitles)
  888. {
  889. dto.HasSubtitles = video.HasSubtitles;
  890. }
  891. if (video.AdditionalParts.Length != 0)
  892. {
  893. dto.PartCount = video.AdditionalParts.Length + 1;
  894. }
  895. if (options.ContainsField(ItemFields.MediaSourceCount))
  896. {
  897. var mediaSourceCount = video.MediaSourceCount;
  898. if (mediaSourceCount != 1)
  899. {
  900. dto.MediaSourceCount = mediaSourceCount;
  901. }
  902. }
  903. if (options.ContainsField(ItemFields.Chapters))
  904. {
  905. dto.Chapters = _chapterRepository.GetChapters(item.Id).ToList();
  906. }
  907. if (options.ContainsField(ItemFields.Trickplay))
  908. {
  909. dto.Trickplay = _trickplayManager.GetTrickplayManifest(item).GetAwaiter().GetResult();
  910. }
  911. dto.ExtraType = video.ExtraType;
  912. }
  913. if (options.ContainsField(ItemFields.MediaStreams))
  914. {
  915. // Add VideoInfo
  916. if (item is IHasMediaSources)
  917. {
  918. MediaStream[] mediaStreams;
  919. if (dto.MediaSources is not null && dto.MediaSources.Length > 0)
  920. {
  921. if (item.SourceType == SourceType.Channel)
  922. {
  923. mediaStreams = dto.MediaSources[0].MediaStreams.ToArray();
  924. }
  925. else
  926. {
  927. string id = item.Id.ToString("N", CultureInfo.InvariantCulture);
  928. mediaStreams = dto.MediaSources.Where(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase))
  929. .SelectMany(i => i.MediaStreams)
  930. .ToArray();
  931. }
  932. }
  933. else
  934. {
  935. mediaStreams = _mediaSourceManager.GetStaticMediaSources(item, true)[0].MediaStreams.ToArray();
  936. }
  937. dto.MediaStreams = mediaStreams;
  938. }
  939. }
  940. BaseItem[]? allExtras = null;
  941. if (options.ContainsField(ItemFields.SpecialFeatureCount))
  942. {
  943. allExtras = item.GetExtras().ToArray();
  944. dto.SpecialFeatureCount = allExtras.Count(i => i.ExtraType.HasValue && BaseItem.DisplayExtraTypes.Contains(i.ExtraType.Value));
  945. }
  946. if (options.ContainsField(ItemFields.LocalTrailerCount))
  947. {
  948. if (item is IHasTrailers hasTrailers)
  949. {
  950. dto.LocalTrailerCount = hasTrailers.LocalTrailers.Count;
  951. }
  952. else
  953. {
  954. dto.LocalTrailerCount = (allExtras ?? item.GetExtras()).Count(i => i.ExtraType == ExtraType.Trailer);
  955. }
  956. }
  957. // Add EpisodeInfo
  958. if (item is Episode episode)
  959. {
  960. dto.IndexNumberEnd = episode.IndexNumberEnd;
  961. dto.SeriesName = episode.SeriesName;
  962. if (options.ContainsField(ItemFields.SpecialEpisodeNumbers))
  963. {
  964. dto.AirsAfterSeasonNumber = episode.AirsAfterSeasonNumber;
  965. dto.AirsBeforeEpisodeNumber = episode.AirsBeforeEpisodeNumber;
  966. dto.AirsBeforeSeasonNumber = episode.AirsBeforeSeasonNumber;
  967. }
  968. dto.SeasonName = episode.SeasonName;
  969. dto.SeasonId = episode.SeasonId;
  970. dto.SeriesId = episode.SeriesId;
  971. Series? episodeSeries = null;
  972. // this block will add the series poster for episodes without a poster
  973. // TODO maybe remove the if statement entirely
  974. // if (options.ContainsField(ItemFields.SeriesPrimaryImage))
  975. {
  976. episodeSeries ??= episode.Series;
  977. if (episodeSeries is not null)
  978. {
  979. dto.SeriesPrimaryImageTag = GetTagAndFillBlurhash(dto, episodeSeries, ImageType.Primary);
  980. if (dto.ImageTags is null || !dto.ImageTags.ContainsKey(ImageType.Primary))
  981. {
  982. AttachPrimaryImageAspectRatio(dto, episodeSeries);
  983. }
  984. }
  985. }
  986. if (options.ContainsField(ItemFields.SeriesStudio))
  987. {
  988. episodeSeries ??= episode.Series;
  989. if (episodeSeries is not null)
  990. {
  991. dto.SeriesStudio = episodeSeries.Studios.FirstOrDefault();
  992. }
  993. }
  994. }
  995. // Add SeriesInfo
  996. Series? series;
  997. if (item is Series tmp)
  998. {
  999. series = tmp;
  1000. dto.AirDays = series.AirDays;
  1001. dto.AirTime = series.AirTime;
  1002. dto.Status = series.Status?.ToString();
  1003. }
  1004. // Add SeasonInfo
  1005. if (item is Season season)
  1006. {
  1007. dto.SeriesName = season.SeriesName;
  1008. dto.SeriesId = season.SeriesId;
  1009. series = null;
  1010. if (options.ContainsField(ItemFields.SeriesStudio))
  1011. {
  1012. series ??= season.Series;
  1013. if (series is not null)
  1014. {
  1015. dto.SeriesStudio = series.Studios.FirstOrDefault();
  1016. }
  1017. }
  1018. // this block will add the series poster for seasons without a poster
  1019. // TODO maybe remove the if statement entirely
  1020. // if (options.ContainsField(ItemFields.SeriesPrimaryImage))
  1021. {
  1022. series ??= season.Series;
  1023. if (series is not null)
  1024. {
  1025. dto.SeriesPrimaryImageTag = GetTagAndFillBlurhash(dto, series, ImageType.Primary);
  1026. if (dto.ImageTags is null || !dto.ImageTags.ContainsKey(ImageType.Primary))
  1027. {
  1028. AttachPrimaryImageAspectRatio(dto, series);
  1029. }
  1030. }
  1031. }
  1032. }
  1033. if (item is MusicVideo musicVideo)
  1034. {
  1035. SetMusicVideoProperties(dto, musicVideo);
  1036. }
  1037. if (item is Book book)
  1038. {
  1039. SetBookProperties(dto, book);
  1040. }
  1041. if (options.ContainsField(ItemFields.ProductionLocations))
  1042. {
  1043. if (item.ProductionLocations.Length > 0 || item is Movie)
  1044. {
  1045. dto.ProductionLocations = item.ProductionLocations;
  1046. }
  1047. }
  1048. if (options.ContainsField(ItemFields.Width))
  1049. {
  1050. var width = item.Width;
  1051. if (width > 0)
  1052. {
  1053. dto.Width = width;
  1054. }
  1055. }
  1056. if (options.ContainsField(ItemFields.Height))
  1057. {
  1058. var height = item.Height;
  1059. if (height > 0)
  1060. {
  1061. dto.Height = height;
  1062. }
  1063. }
  1064. if (options.ContainsField(ItemFields.IsHD))
  1065. {
  1066. // Compatibility
  1067. if (item.IsHD)
  1068. {
  1069. dto.IsHD = true;
  1070. }
  1071. }
  1072. if (item is Photo photo)
  1073. {
  1074. SetPhotoProperties(dto, photo);
  1075. }
  1076. dto.ChannelId = item.ChannelId;
  1077. if (item.SourceType == SourceType.Channel)
  1078. {
  1079. var channel = _libraryManager.GetItemById(item.ChannelId);
  1080. if (channel is not null)
  1081. {
  1082. dto.ChannelName = channel.Name;
  1083. }
  1084. }
  1085. }
  1086. private BaseItem? GetImageDisplayParent(BaseItem currentItem, BaseItem originalItem)
  1087. {
  1088. if (currentItem is MusicAlbum musicAlbum)
  1089. {
  1090. var artist = musicAlbum.GetMusicArtist(new DtoOptions(false));
  1091. if (artist is not null)
  1092. {
  1093. return artist;
  1094. }
  1095. }
  1096. var parent = currentItem.DisplayParent ?? currentItem.GetOwner() ?? currentItem.GetParent();
  1097. if (parent is null && originalItem is not UserRootFolder && originalItem is not UserView && originalItem is not AggregateFolder && originalItem is not ICollectionFolder && originalItem is not Channel)
  1098. {
  1099. parent = _libraryManager.GetCollectionFolders(originalItem).FirstOrDefault();
  1100. }
  1101. return parent;
  1102. }
  1103. private void AddInheritedImages(BaseItemDto dto, BaseItem item, DtoOptions options, BaseItem? owner)
  1104. {
  1105. if (!item.SupportsInheritedParentImages)
  1106. {
  1107. return;
  1108. }
  1109. var logoLimit = options.GetImageLimit(ImageType.Logo);
  1110. var artLimit = options.GetImageLimit(ImageType.Art);
  1111. var thumbLimit = options.GetImageLimit(ImageType.Thumb);
  1112. var backdropLimit = options.GetImageLimit(ImageType.Backdrop);
  1113. // For now. Emby apps are not using this
  1114. artLimit = 0;
  1115. if (logoLimit == 0 && artLimit == 0 && thumbLimit == 0 && backdropLimit == 0)
  1116. {
  1117. return;
  1118. }
  1119. BaseItem? parent = null;
  1120. var isFirst = true;
  1121. var imageTags = dto.ImageTags;
  1122. while ((!(imageTags is not null && imageTags.ContainsKey(ImageType.Logo)) && logoLimit > 0)
  1123. || (!(imageTags is not null && imageTags.ContainsKey(ImageType.Art)) && artLimit > 0)
  1124. || (!(imageTags is not null && imageTags.ContainsKey(ImageType.Thumb)) && thumbLimit > 0)
  1125. || parent is Series)
  1126. {
  1127. parent ??= isFirst ? GetImageDisplayParent(item, item) ?? owner : parent;
  1128. if (parent is null)
  1129. {
  1130. break;
  1131. }
  1132. var allImages = parent.ImageInfos;
  1133. if (logoLimit > 0 && !(imageTags is not null && imageTags.ContainsKey(ImageType.Logo)) && dto.ParentLogoItemId is null)
  1134. {
  1135. var image = allImages.FirstOrDefault(i => i.Type == ImageType.Logo);
  1136. if (image is not null)
  1137. {
  1138. dto.ParentLogoItemId = parent.Id;
  1139. dto.ParentLogoImageTag = GetTagAndFillBlurhash(dto, parent, image);
  1140. }
  1141. }
  1142. if (artLimit > 0 && !(imageTags is not null && imageTags.ContainsKey(ImageType.Art)) && dto.ParentArtItemId is null)
  1143. {
  1144. var image = allImages.FirstOrDefault(i => i.Type == ImageType.Art);
  1145. if (image is not null)
  1146. {
  1147. dto.ParentArtItemId = parent.Id;
  1148. dto.ParentArtImageTag = GetTagAndFillBlurhash(dto, parent, image);
  1149. }
  1150. }
  1151. if (thumbLimit > 0 && !(imageTags is not null && imageTags.ContainsKey(ImageType.Thumb)) && (dto.ParentThumbItemId is null || parent is Series) && parent is not ICollectionFolder && parent is not UserView)
  1152. {
  1153. var image = allImages.FirstOrDefault(i => i.Type == ImageType.Thumb);
  1154. if (image is not null)
  1155. {
  1156. dto.ParentThumbItemId = parent.Id;
  1157. dto.ParentThumbImageTag = GetTagAndFillBlurhash(dto, parent, image);
  1158. }
  1159. }
  1160. if (backdropLimit > 0 && !((dto.BackdropImageTags is not null && dto.BackdropImageTags.Length > 0) || (dto.ParentBackdropImageTags is not null && dto.ParentBackdropImageTags.Length > 0)))
  1161. {
  1162. var images = allImages.Where(i => i.Type == ImageType.Backdrop).Take(backdropLimit).ToList();
  1163. if (images.Count > 0)
  1164. {
  1165. dto.ParentBackdropItemId = parent.Id;
  1166. dto.ParentBackdropImageTags = GetTagsAndFillBlurhashes(dto, parent, ImageType.Backdrop, images);
  1167. }
  1168. }
  1169. isFirst = false;
  1170. if (!parent.SupportsInheritedParentImages)
  1171. {
  1172. break;
  1173. }
  1174. parent = GetImageDisplayParent(parent, item);
  1175. }
  1176. }
  1177. private string GetMappedPath(BaseItem item, BaseItem? ownerItem)
  1178. {
  1179. var path = item.Path;
  1180. if (item.IsFileProtocol)
  1181. {
  1182. path = _libraryManager.GetPathAfterNetworkSubstitution(path, ownerItem ?? item);
  1183. }
  1184. return path;
  1185. }
  1186. /// <summary>
  1187. /// Attaches the primary image aspect ratio.
  1188. /// </summary>
  1189. /// <param name="dto">The dto.</param>
  1190. /// <param name="item">The item.</param>
  1191. public void AttachPrimaryImageAspectRatio(IItemDto dto, BaseItem item)
  1192. {
  1193. dto.PrimaryImageAspectRatio = GetPrimaryImageAspectRatio(item);
  1194. }
  1195. public double? GetPrimaryImageAspectRatio(BaseItem item)
  1196. {
  1197. var imageInfo = item.GetImageInfo(ImageType.Primary, 0);
  1198. if (imageInfo is null)
  1199. {
  1200. return null;
  1201. }
  1202. if (!imageInfo.IsLocalFile)
  1203. {
  1204. return item.GetDefaultPrimaryImageAspectRatio();
  1205. }
  1206. try
  1207. {
  1208. var size = _imageProcessor.GetImageDimensions(item, imageInfo);
  1209. var width = size.Width;
  1210. var height = size.Height;
  1211. if (width > 0 && height > 0)
  1212. {
  1213. return (double)width / height;
  1214. }
  1215. }
  1216. catch (Exception ex)
  1217. {
  1218. _logger.LogError(ex, "Failed to determine primary image aspect ratio for {ImagePath}", imageInfo.Path);
  1219. }
  1220. return item.GetDefaultPrimaryImageAspectRatio();
  1221. }
  1222. }
  1223. }