DtoService.cs 48 KB

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