DtoService.cs 54 KB

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