DtoService.cs 48 KB

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