DtoService.cs 48 KB

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