DtoService.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Channels;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Drawing;
  6. using MediaBrowser.Controller.Dto;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Entities.Audio;
  9. using MediaBrowser.Controller.Entities.Movies;
  10. using MediaBrowser.Controller.Entities.TV;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.LiveTv;
  13. using MediaBrowser.Controller.Persistence;
  14. using MediaBrowser.Controller.Playlists;
  15. using MediaBrowser.Controller.Providers;
  16. using MediaBrowser.Controller.Sync;
  17. using MediaBrowser.Model.Drawing;
  18. using MediaBrowser.Model.Dto;
  19. using MediaBrowser.Model.Entities;
  20. using MediaBrowser.Model.Logging;
  21. using MediaBrowser.Model.Querying;
  22. using MoreLinq;
  23. using System;
  24. using System.Collections.Generic;
  25. using System.IO;
  26. using System.Linq;
  27. namespace MediaBrowser.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 IServerConfigurationManager _config;
  37. private readonly IFileSystem _fileSystem;
  38. private readonly IProviderManager _providerManager;
  39. private readonly Func<IChannelManager> _channelManagerFactory;
  40. private readonly ISyncManager _syncManager;
  41. private readonly IApplicationHost _appHost;
  42. public DtoService(ILogger logger, ILibraryManager libraryManager, IUserDataManager userDataRepository, IItemRepository itemRepo, IImageProcessor imageProcessor, IServerConfigurationManager config, IFileSystem fileSystem, IProviderManager providerManager, Func<IChannelManager> channelManagerFactory, ISyncManager syncManager, IApplicationHost appHost)
  43. {
  44. _logger = logger;
  45. _libraryManager = libraryManager;
  46. _userDataRepository = userDataRepository;
  47. _itemRepo = itemRepo;
  48. _imageProcessor = imageProcessor;
  49. _config = config;
  50. _fileSystem = fileSystem;
  51. _providerManager = providerManager;
  52. _channelManagerFactory = channelManagerFactory;
  53. _syncManager = syncManager;
  54. _appHost = appHost;
  55. }
  56. /// <summary>
  57. /// Converts a BaseItem to a DTOBaseItem
  58. /// </summary>
  59. /// <param name="item">The item.</param>
  60. /// <param name="fields">The fields.</param>
  61. /// <param name="user">The user.</param>
  62. /// <param name="owner">The owner.</param>
  63. /// <returns>Task{DtoBaseItem}.</returns>
  64. /// <exception cref="System.ArgumentNullException">item</exception>
  65. public BaseItemDto GetBaseItemDto(BaseItem item, List<ItemFields> fields, User user = null, BaseItem owner = null)
  66. {
  67. var dto = GetBaseItemDtoInternal(item, fields, user, owner);
  68. var byName = item as IItemByName;
  69. if (byName != null && !(item is LiveTvChannel))
  70. {
  71. var libraryItems = user != null ?
  72. user.RootFolder.GetRecursiveChildren(user) :
  73. _libraryManager.RootFolder.RecursiveChildren;
  74. SetItemByNameInfo(item, dto, byName.GetTaggedItems(libraryItems).ToList(), user);
  75. return dto;
  76. }
  77. return dto;
  78. }
  79. private BaseItemDto GetBaseItemDtoInternal(BaseItem item, List<ItemFields> fields, User user = null, BaseItem owner = null)
  80. {
  81. if (item == null)
  82. {
  83. throw new ArgumentNullException("item");
  84. }
  85. if (fields == null)
  86. {
  87. throw new ArgumentNullException("fields");
  88. }
  89. var dto = new BaseItemDto
  90. {
  91. ServerId = _appHost.SystemId
  92. };
  93. dto.SupportsPlaylists = item.SupportsAddingToPlaylist;
  94. if (fields.Contains(ItemFields.People))
  95. {
  96. AttachPeople(dto, item);
  97. }
  98. if (fields.Contains(ItemFields.PrimaryImageAspectRatio))
  99. {
  100. try
  101. {
  102. AttachPrimaryImageAspectRatio(dto, item);
  103. }
  104. catch (Exception ex)
  105. {
  106. // Have to use a catch-all unfortunately because some .net image methods throw plain Exceptions
  107. _logger.ErrorException("Error generating PrimaryImageAspectRatio for {0}", ex, item.Name);
  108. }
  109. }
  110. if (fields.Contains(ItemFields.DisplayPreferencesId))
  111. {
  112. dto.DisplayPreferencesId = item.DisplayPreferencesId.ToString("N");
  113. }
  114. if (user != null)
  115. {
  116. AttachUserSpecificInfo(dto, item, user, fields);
  117. }
  118. var hasMediaSources = item as IHasMediaSources;
  119. if (hasMediaSources != null)
  120. {
  121. if (fields.Contains(ItemFields.MediaSources))
  122. {
  123. if (user == null)
  124. {
  125. dto.MediaSources = hasMediaSources.GetMediaSources(true).ToList();
  126. }
  127. else
  128. {
  129. dto.MediaSources = hasMediaSources.GetMediaSources(true, user).ToList();
  130. }
  131. }
  132. }
  133. if (fields.Contains(ItemFields.Studios))
  134. {
  135. AttachStudios(dto, item);
  136. }
  137. AttachBasicFields(dto, item, owner, fields);
  138. if (fields.Contains(ItemFields.SyncInfo))
  139. {
  140. dto.SupportsSync = _syncManager.SupportsSync(item);
  141. }
  142. if (fields.Contains(ItemFields.SoundtrackIds))
  143. {
  144. var hasSoundtracks = item as IHasSoundtracks;
  145. if (hasSoundtracks != null)
  146. {
  147. dto.SoundtrackIds = hasSoundtracks.SoundtrackIds
  148. .Select(i => i.ToString("N"))
  149. .ToArray();
  150. }
  151. }
  152. if (item is Playlist)
  153. {
  154. AttachLinkedChildImages(dto, (Folder)item, user);
  155. }
  156. return dto;
  157. }
  158. public BaseItemDto GetItemByNameDto<T>(T item, List<ItemFields> fields, List<BaseItem> taggedItems, User user = null)
  159. where T : BaseItem, IItemByName
  160. {
  161. var dto = GetBaseItemDtoInternal(item, fields, user);
  162. SetItemByNameInfo(item, dto, taggedItems, user);
  163. return dto;
  164. }
  165. private void SetItemByNameInfo(BaseItem item, BaseItemDto dto, List<BaseItem> taggedItems, User user = null)
  166. {
  167. if (item is MusicArtist || item is MusicGenre)
  168. {
  169. dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum);
  170. dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo);
  171. dto.SongCount = taggedItems.Count(i => i is Audio);
  172. }
  173. else if (item is GameGenre)
  174. {
  175. dto.GameCount = taggedItems.Count(i => i is Game);
  176. }
  177. else
  178. {
  179. // This populates them all and covers Genre, Person, Studio, Year
  180. dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum);
  181. dto.EpisodeCount = taggedItems.Count(i => i is Episode);
  182. dto.GameCount = taggedItems.Count(i => i is Game);
  183. dto.MovieCount = taggedItems.Count(i => i is Movie);
  184. dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo);
  185. dto.SeriesCount = taggedItems.Count(i => i is Series);
  186. dto.SongCount = taggedItems.Count(i => i is Audio);
  187. dto.TrailerCount = taggedItems.Count(i => i is Trailer);
  188. }
  189. dto.ChildCount = taggedItems.Count;
  190. }
  191. /// <summary>
  192. /// Attaches the user specific info.
  193. /// </summary>
  194. /// <param name="dto">The dto.</param>
  195. /// <param name="item">The item.</param>
  196. /// <param name="user">The user.</param>
  197. /// <param name="fields">The fields.</param>
  198. private void AttachUserSpecificInfo(BaseItemDto dto, BaseItem item, User user, List<ItemFields> fields)
  199. {
  200. if (item.IsFolder)
  201. {
  202. var userData = _userDataRepository.GetUserData(user.Id, item.GetUserDataKey());
  203. // Skip the user data manager because we've already looped through the recursive tree and don't want to do it twice
  204. // TODO: Improve in future
  205. dto.UserData = GetUserItemDataDto(userData);
  206. var folder = (Folder)item;
  207. dto.ChildCount = GetChildCount(folder, user);
  208. // These are just far too slow.
  209. // TODO: Disable for CollectionFolder
  210. if (!(folder is UserRootFolder) && !(folder is UserView))
  211. {
  212. SetSpecialCounts(folder, user, dto, fields);
  213. }
  214. dto.UserData.Played = dto.UserData.PlayedPercentage.HasValue && dto.UserData.PlayedPercentage.Value >= 100;
  215. }
  216. else
  217. {
  218. dto.UserData = _userDataRepository.GetUserDataDto(item, user);
  219. }
  220. dto.PlayAccess = item.GetPlayAccess(user);
  221. }
  222. private int GetChildCount(Folder folder, User user)
  223. {
  224. return folder.GetChildren(user, true)
  225. .Count();
  226. }
  227. /// <summary>
  228. /// Gets client-side Id of a server-side BaseItem
  229. /// </summary>
  230. /// <param name="item">The item.</param>
  231. /// <returns>System.String.</returns>
  232. /// <exception cref="System.ArgumentNullException">item</exception>
  233. public string GetDtoId(BaseItem item)
  234. {
  235. if (item == null)
  236. {
  237. throw new ArgumentNullException("item");
  238. }
  239. return item.Id.ToString("N");
  240. }
  241. /// <summary>
  242. /// Converts a UserItemData to a DTOUserItemData
  243. /// </summary>
  244. /// <param name="data">The data.</param>
  245. /// <returns>DtoUserItemData.</returns>
  246. /// <exception cref="System.ArgumentNullException"></exception>
  247. public UserItemDataDto GetUserItemDataDto(UserItemData data)
  248. {
  249. if (data == null)
  250. {
  251. throw new ArgumentNullException("data");
  252. }
  253. return new UserItemDataDto
  254. {
  255. IsFavorite = data.IsFavorite,
  256. Likes = data.Likes,
  257. PlaybackPositionTicks = data.PlaybackPositionTicks,
  258. PlayCount = data.PlayCount,
  259. Rating = data.Rating,
  260. Played = data.Played,
  261. LastPlayedDate = data.LastPlayedDate,
  262. Key = data.Key
  263. };
  264. }
  265. private void SetBookProperties(BaseItemDto dto, Book item)
  266. {
  267. dto.SeriesName = item.SeriesName;
  268. }
  269. private void SetPhotoProperties(BaseItemDto dto, Photo item)
  270. {
  271. dto.Width = item.Width;
  272. dto.Height = item.Height;
  273. dto.CameraMake = item.CameraMake;
  274. dto.CameraModel = item.CameraModel;
  275. dto.Software = item.Software;
  276. dto.ExposureTime = item.ExposureTime;
  277. dto.FocalLength = item.FocalLength;
  278. dto.ImageOrientation = item.Orientation;
  279. dto.Aperture = item.Aperture;
  280. dto.ShutterSpeed = item.ShutterSpeed;
  281. dto.Latitude = item.Latitude;
  282. dto.Longitude = item.Longitude;
  283. dto.Altitude = item.Altitude;
  284. dto.IsoSpeedRating = item.IsoSpeedRating;
  285. var album = item.Album;
  286. if (album != null)
  287. {
  288. dto.Album = album.Name;
  289. dto.AlbumId = album.Id.ToString("N");
  290. }
  291. }
  292. private void SetMusicVideoProperties(BaseItemDto dto, MusicVideo item)
  293. {
  294. if (!string.IsNullOrEmpty(item.Album))
  295. {
  296. var parentAlbum = _libraryManager.RootFolder
  297. .GetRecursiveChildren()
  298. .Where(i => i is MusicAlbum)
  299. .FirstOrDefault(i => string.Equals(i.Name, item.Album, StringComparison.OrdinalIgnoreCase));
  300. if (parentAlbum != null)
  301. {
  302. dto.AlbumId = GetDtoId(parentAlbum);
  303. }
  304. }
  305. dto.Album = item.Album;
  306. dto.Artists = item.Artists;
  307. }
  308. private void SetGameProperties(BaseItemDto dto, Game item)
  309. {
  310. dto.Players = item.PlayersSupported;
  311. dto.GameSystem = item.GameSystem;
  312. dto.MultiPartGameFiles = item.MultiPartGameFiles;
  313. }
  314. private void SetGameSystemProperties(BaseItemDto dto, GameSystem item)
  315. {
  316. dto.GameSystem = item.GameSystemName;
  317. }
  318. /// <summary>
  319. /// Gets the backdrop image tags.
  320. /// </summary>
  321. /// <param name="item">The item.</param>
  322. /// <returns>List{System.String}.</returns>
  323. private List<string> GetBackdropImageTags(BaseItem item)
  324. {
  325. return GetCacheTags(item, ImageType.Backdrop).ToList();
  326. }
  327. /// <summary>
  328. /// Gets the screenshot image tags.
  329. /// </summary>
  330. /// <param name="item">The item.</param>
  331. /// <returns>List{Guid}.</returns>
  332. private List<string> GetScreenshotImageTags(BaseItem item)
  333. {
  334. var hasScreenshots = item as IHasScreenshots;
  335. if (hasScreenshots == null)
  336. {
  337. return new List<string>();
  338. }
  339. return GetCacheTags(item, ImageType.Screenshot).ToList();
  340. }
  341. private IEnumerable<string> GetCacheTags(BaseItem item, ImageType type)
  342. {
  343. return item.GetImages(type)
  344. .Select(p => GetImageCacheTag(item, p))
  345. .Where(i => i != null)
  346. .ToList();
  347. }
  348. private string GetImageCacheTag(BaseItem item, ImageType type)
  349. {
  350. try
  351. {
  352. return _imageProcessor.GetImageCacheTag(item, type);
  353. }
  354. catch (Exception ex)
  355. {
  356. _logger.ErrorException("Error getting {0} image info", ex, type);
  357. return null;
  358. }
  359. }
  360. private string GetImageCacheTag(BaseItem item, ItemImageInfo image)
  361. {
  362. try
  363. {
  364. return _imageProcessor.GetImageCacheTag(item, image);
  365. }
  366. catch (Exception ex)
  367. {
  368. _logger.ErrorException("Error getting {0} image info for {1}", ex, image.Type, image.Path);
  369. return null;
  370. }
  371. }
  372. /// <summary>
  373. /// Attaches People DTO's to a DTOBaseItem
  374. /// </summary>
  375. /// <param name="dto">The dto.</param>
  376. /// <param name="item">The item.</param>
  377. /// <returns>Task.</returns>
  378. private void AttachPeople(BaseItemDto dto, BaseItem item)
  379. {
  380. // Ordering by person type to ensure actors and artists are at the front.
  381. // This is taking advantage of the fact that they both begin with A
  382. // This should be improved in the future
  383. var people = item.People.OrderBy(i => i.SortOrder ?? int.MaxValue)
  384. .ThenBy(i =>
  385. {
  386. if (i.IsType(PersonType.Actor))
  387. {
  388. return 0;
  389. }
  390. if (i.IsType(PersonType.GuestStar))
  391. {
  392. return 1;
  393. }
  394. if (i.IsType(PersonType.Director))
  395. {
  396. return 2;
  397. }
  398. if (i.IsType(PersonType.Writer))
  399. {
  400. return 3;
  401. }
  402. if (i.IsType(PersonType.Producer))
  403. {
  404. return 4;
  405. }
  406. if (i.IsType(PersonType.Composer))
  407. {
  408. return 4;
  409. }
  410. return 10;
  411. })
  412. .ToList();
  413. var list = new List<BaseItemPerson>();
  414. var dictionary = people.Select(p => p.Name)
  415. .Distinct(StringComparer.OrdinalIgnoreCase).Select(c =>
  416. {
  417. try
  418. {
  419. return _libraryManager.GetPerson(c);
  420. }
  421. catch (Exception ex)
  422. {
  423. _logger.ErrorException("Error getting person {0}", ex, c);
  424. return null;
  425. }
  426. }).Where(i => i != null)
  427. .DistinctBy(i => i.Name)
  428. .ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase);
  429. for (var i = 0; i < people.Count; i++)
  430. {
  431. var person = people[i];
  432. var baseItemPerson = new BaseItemPerson
  433. {
  434. Name = person.Name,
  435. Role = person.Role,
  436. Type = person.Type
  437. };
  438. Person entity;
  439. if (dictionary.TryGetValue(person.Name, out entity))
  440. {
  441. baseItemPerson.PrimaryImageTag = GetImageCacheTag(entity, ImageType.Primary);
  442. baseItemPerson.Id = entity.Id.ToString("N");
  443. list.Add(baseItemPerson);
  444. }
  445. }
  446. dto.People = list.ToArray();
  447. }
  448. /// <summary>
  449. /// Attaches the studios.
  450. /// </summary>
  451. /// <param name="dto">The dto.</param>
  452. /// <param name="item">The item.</param>
  453. /// <returns>Task.</returns>
  454. private void AttachStudios(BaseItemDto dto, BaseItem item)
  455. {
  456. var studios = item.Studios.ToList();
  457. dto.Studios = new StudioDto[studios.Count];
  458. var dictionary = studios.Distinct(StringComparer.OrdinalIgnoreCase).Select(name =>
  459. {
  460. try
  461. {
  462. return _libraryManager.GetStudio(name);
  463. }
  464. catch (IOException ex)
  465. {
  466. _logger.ErrorException("Error getting studio {0}", ex, name);
  467. return null;
  468. }
  469. })
  470. .Where(i => i != null)
  471. .ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase);
  472. for (var i = 0; i < studios.Count; i++)
  473. {
  474. var studio = studios[i];
  475. var studioDto = new StudioDto
  476. {
  477. Name = studio
  478. };
  479. Studio entity;
  480. if (dictionary.TryGetValue(studio, out entity))
  481. {
  482. studioDto.Id = entity.Id.ToString("N");
  483. studioDto.PrimaryImageTag = GetImageCacheTag(entity, ImageType.Primary);
  484. }
  485. dto.Studios[i] = studioDto;
  486. }
  487. }
  488. /// <summary>
  489. /// If an item does not any backdrops, this can be used to find the first parent that does have one
  490. /// </summary>
  491. /// <param name="item">The item.</param>
  492. /// <param name="owner">The owner.</param>
  493. /// <returns>BaseItem.</returns>
  494. private BaseItem GetParentBackdropItem(BaseItem item, BaseItem owner)
  495. {
  496. var parent = item.Parent ?? owner;
  497. while (parent != null)
  498. {
  499. if (parent.GetImages(ImageType.Backdrop).Any())
  500. {
  501. return parent;
  502. }
  503. parent = parent.Parent;
  504. }
  505. return null;
  506. }
  507. /// <summary>
  508. /// If an item does not have a logo, this can be used to find the first parent that does have one
  509. /// </summary>
  510. /// <param name="item">The item.</param>
  511. /// <param name="type">The type.</param>
  512. /// <param name="owner">The owner.</param>
  513. /// <returns>BaseItem.</returns>
  514. private BaseItem GetParentImageItem(BaseItem item, ImageType type, BaseItem owner)
  515. {
  516. var parent = item.Parent ?? owner;
  517. while (parent != null)
  518. {
  519. if (parent.HasImage(type))
  520. {
  521. return parent;
  522. }
  523. parent = parent.Parent;
  524. }
  525. return null;
  526. }
  527. /// <summary>
  528. /// Gets the chapter info dto.
  529. /// </summary>
  530. /// <param name="chapterInfo">The chapter info.</param>
  531. /// <param name="item">The item.</param>
  532. /// <returns>ChapterInfoDto.</returns>
  533. private ChapterInfoDto GetChapterInfoDto(ChapterInfo chapterInfo, BaseItem item)
  534. {
  535. var dto = new ChapterInfoDto
  536. {
  537. Name = chapterInfo.Name,
  538. StartPositionTicks = chapterInfo.StartPositionTicks
  539. };
  540. if (!string.IsNullOrEmpty(chapterInfo.ImagePath))
  541. {
  542. dto.ImageTag = GetImageCacheTag(item, new ItemImageInfo
  543. {
  544. Path = chapterInfo.ImagePath,
  545. Type = ImageType.Chapter,
  546. DateModified = _fileSystem.GetLastWriteTimeUtc(chapterInfo.ImagePath)
  547. });
  548. }
  549. return dto;
  550. }
  551. public List<ChapterInfoDto> GetChapterInfoDtos(BaseItem item)
  552. {
  553. return _itemRepo.GetChapters(item.Id)
  554. .Select(c => GetChapterInfoDto(c, item))
  555. .ToList();
  556. }
  557. /// <summary>
  558. /// Sets simple property values on a DTOBaseItem
  559. /// </summary>
  560. /// <param name="dto">The dto.</param>
  561. /// <param name="item">The item.</param>
  562. /// <param name="owner">The owner.</param>
  563. /// <param name="fields">The fields.</param>
  564. private void AttachBasicFields(BaseItemDto dto, BaseItem item, BaseItem owner, List<ItemFields> fields)
  565. {
  566. if (fields.Contains(ItemFields.DateCreated))
  567. {
  568. dto.DateCreated = item.DateCreated;
  569. }
  570. if (fields.Contains(ItemFields.DisplayMediaType))
  571. {
  572. dto.DisplayMediaType = item.DisplayMediaType;
  573. }
  574. dto.IsUnidentified = item.IsUnidentified;
  575. if (fields.Contains(ItemFields.Settings))
  576. {
  577. dto.LockedFields = item.LockedFields;
  578. dto.LockData = item.IsLocked;
  579. dto.ForcedSortName = item.ForcedSortName;
  580. }
  581. var hasBudget = item as IHasBudget;
  582. if (hasBudget != null)
  583. {
  584. if (fields.Contains(ItemFields.Budget))
  585. {
  586. dto.Budget = hasBudget.Budget;
  587. }
  588. if (fields.Contains(ItemFields.Revenue))
  589. {
  590. dto.Revenue = hasBudget.Revenue;
  591. }
  592. }
  593. dto.EndDate = item.EndDate;
  594. if (fields.Contains(ItemFields.HomePageUrl))
  595. {
  596. dto.HomePageUrl = item.HomePageUrl;
  597. }
  598. if (fields.Contains(ItemFields.ExternalUrls))
  599. {
  600. dto.ExternalUrls = _providerManager.GetExternalUrls(item).ToArray();
  601. }
  602. if (fields.Contains(ItemFields.Tags))
  603. {
  604. var hasTags = item as IHasTags;
  605. if (hasTags != null)
  606. {
  607. dto.Tags = hasTags.Tags;
  608. }
  609. if (dto.Tags == null)
  610. {
  611. dto.Tags = new List<string>();
  612. }
  613. }
  614. if (fields.Contains(ItemFields.Keywords))
  615. {
  616. var hasTags = item as IHasKeywords;
  617. if (hasTags != null)
  618. {
  619. dto.Keywords = hasTags.Keywords;
  620. }
  621. if (dto.Keywords == null)
  622. {
  623. dto.Keywords = new List<string>();
  624. }
  625. }
  626. if (fields.Contains(ItemFields.ProductionLocations))
  627. {
  628. SetProductionLocations(item, dto);
  629. }
  630. var hasAspectRatio = item as IHasAspectRatio;
  631. if (hasAspectRatio != null)
  632. {
  633. dto.AspectRatio = hasAspectRatio.AspectRatio;
  634. }
  635. var hasMetascore = item as IHasMetascore;
  636. if (hasMetascore != null)
  637. {
  638. dto.Metascore = hasMetascore.Metascore;
  639. }
  640. if (fields.Contains(ItemFields.AwardSummary))
  641. {
  642. var hasAwards = item as IHasAwards;
  643. if (hasAwards != null)
  644. {
  645. dto.AwardSummary = hasAwards.AwardSummary;
  646. }
  647. }
  648. dto.BackdropImageTags = GetBackdropImageTags(item);
  649. if (fields.Contains(ItemFields.ScreenshotImageTags))
  650. {
  651. dto.ScreenshotImageTags = GetScreenshotImageTags(item);
  652. }
  653. if (fields.Contains(ItemFields.Genres))
  654. {
  655. dto.Genres = item.Genres;
  656. }
  657. dto.ImageTags = new Dictionary<ImageType, string>();
  658. // Prevent implicitly captured closure
  659. var currentItem = item;
  660. foreach (var image in currentItem.ImageInfos.Where(i => !currentItem.AllowsMultipleImages(i.Type)))
  661. {
  662. var tag = GetImageCacheTag(item, image);
  663. if (tag != null)
  664. {
  665. dto.ImageTags[image.Type] = tag;
  666. }
  667. }
  668. dto.Id = GetDtoId(item);
  669. dto.IndexNumber = item.IndexNumber;
  670. dto.IsFolder = item.IsFolder;
  671. dto.MediaType = item.MediaType;
  672. dto.LocationType = item.LocationType;
  673. var hasLang = item as IHasPreferredMetadataLanguage;
  674. if (hasLang != null)
  675. {
  676. dto.PreferredMetadataCountryCode = hasLang.PreferredMetadataCountryCode;
  677. dto.PreferredMetadataLanguage = hasLang.PreferredMetadataLanguage;
  678. }
  679. var hasCriticRating = item as IHasCriticRating;
  680. if (hasCriticRating != null)
  681. {
  682. dto.CriticRating = hasCriticRating.CriticRating;
  683. if (fields.Contains(ItemFields.CriticRatingSummary))
  684. {
  685. dto.CriticRatingSummary = hasCriticRating.CriticRatingSummary;
  686. }
  687. }
  688. var hasTrailers = item as IHasTrailers;
  689. if (hasTrailers != null)
  690. {
  691. dto.LocalTrailerCount = hasTrailers.LocalTrailerIds.Count;
  692. }
  693. var hasDisplayOrder = item as IHasDisplayOrder;
  694. if (hasDisplayOrder != null)
  695. {
  696. dto.DisplayOrder = hasDisplayOrder.DisplayOrder;
  697. }
  698. var collectionFolder = item as ICollectionFolder;
  699. if (collectionFolder != null)
  700. {
  701. dto.CollectionType = collectionFolder.CollectionType;
  702. }
  703. var userView = item as UserView;
  704. if (userView != null)
  705. {
  706. dto.CollectionType = userView.ViewType;
  707. }
  708. if (fields.Contains(ItemFields.RemoteTrailers))
  709. {
  710. dto.RemoteTrailers = hasTrailers != null ?
  711. hasTrailers.RemoteTrailers :
  712. new List<MediaUrl>();
  713. }
  714. dto.Name = item.Name;
  715. dto.OfficialRating = item.OfficialRating;
  716. if (fields.Contains(ItemFields.Overview))
  717. {
  718. dto.Overview = item.Overview;
  719. }
  720. if (fields.Contains(ItemFields.ShortOverview))
  721. {
  722. var hasShortOverview = item as IHasShortOverview;
  723. if (hasShortOverview != null)
  724. {
  725. dto.ShortOverview = hasShortOverview.ShortOverview;
  726. }
  727. }
  728. // If there are no backdrops, indicate what parent has them in case the Ui wants to allow inheritance
  729. if (dto.BackdropImageTags.Count == 0)
  730. {
  731. var parentWithBackdrop = GetParentBackdropItem(item, owner);
  732. if (parentWithBackdrop != null)
  733. {
  734. dto.ParentBackdropItemId = GetDtoId(parentWithBackdrop);
  735. dto.ParentBackdropImageTags = GetBackdropImageTags(parentWithBackdrop);
  736. }
  737. }
  738. if (fields.Contains(ItemFields.ParentId))
  739. {
  740. var displayParent = item.DisplayParent;
  741. if (displayParent != null)
  742. {
  743. dto.ParentId = GetDtoId(displayParent);
  744. }
  745. }
  746. dto.ParentIndexNumber = item.ParentIndexNumber;
  747. // If there is no logo, indicate what parent has one in case the Ui wants to allow inheritance
  748. if (!dto.HasLogo)
  749. {
  750. var parentWithLogo = GetParentImageItem(item, ImageType.Logo, owner);
  751. if (parentWithLogo != null)
  752. {
  753. dto.ParentLogoItemId = GetDtoId(parentWithLogo);
  754. dto.ParentLogoImageTag = GetImageCacheTag(parentWithLogo, ImageType.Logo);
  755. }
  756. }
  757. // If there is no art, indicate what parent has one in case the Ui wants to allow inheritance
  758. if (!dto.HasArtImage)
  759. {
  760. var parentWithImage = GetParentImageItem(item, ImageType.Art, owner);
  761. if (parentWithImage != null)
  762. {
  763. dto.ParentArtItemId = GetDtoId(parentWithImage);
  764. dto.ParentArtImageTag = GetImageCacheTag(parentWithImage, ImageType.Art);
  765. }
  766. }
  767. // If there is no thumb, indicate what parent has one in case the Ui wants to allow inheritance
  768. if (!dto.HasThumb)
  769. {
  770. var parentWithImage = GetParentImageItem(item, ImageType.Thumb, owner);
  771. if (parentWithImage != null)
  772. {
  773. dto.ParentThumbItemId = GetDtoId(parentWithImage);
  774. dto.ParentThumbImageTag = GetImageCacheTag(parentWithImage, ImageType.Thumb);
  775. }
  776. }
  777. if (fields.Contains(ItemFields.Path))
  778. {
  779. dto.Path = GetMappedPath(item);
  780. }
  781. dto.PremiereDate = item.PremiereDate;
  782. dto.ProductionYear = item.ProductionYear;
  783. if (fields.Contains(ItemFields.ProviderIds))
  784. {
  785. dto.ProviderIds = item.ProviderIds;
  786. }
  787. dto.RunTimeTicks = item.RunTimeTicks;
  788. if (fields.Contains(ItemFields.SortName))
  789. {
  790. dto.SortName = item.SortName;
  791. }
  792. if (fields.Contains(ItemFields.CustomRating))
  793. {
  794. dto.CustomRating = item.CustomRating;
  795. }
  796. if (fields.Contains(ItemFields.Taglines))
  797. {
  798. var hasTagline = item as IHasTaglines;
  799. if (hasTagline != null)
  800. {
  801. dto.Taglines = hasTagline.Taglines;
  802. }
  803. if (dto.Taglines == null)
  804. {
  805. dto.Taglines = new List<string>();
  806. }
  807. }
  808. dto.Type = item.GetClientTypeName();
  809. dto.CommunityRating = item.CommunityRating;
  810. dto.VoteCount = item.VoteCount;
  811. if (item.IsFolder)
  812. {
  813. var folder = (Folder)item;
  814. if (fields.Contains(ItemFields.IndexOptions))
  815. {
  816. dto.IndexOptions = folder.IndexByOptionStrings.ToArray();
  817. }
  818. }
  819. var supportsPlaceHolders = item as ISupportsPlaceHolders;
  820. if (supportsPlaceHolders != null)
  821. {
  822. dto.IsPlaceHolder = supportsPlaceHolders.IsPlaceHolder;
  823. }
  824. // Add audio info
  825. var audio = item as Audio;
  826. if (audio != null)
  827. {
  828. dto.Album = audio.Album;
  829. dto.Artists = audio.Artists;
  830. var albumParent = audio.FindParent<MusicAlbum>();
  831. if (albumParent != null)
  832. {
  833. dto.AlbumId = GetDtoId(albumParent);
  834. dto.AlbumPrimaryImageTag = GetImageCacheTag(albumParent, ImageType.Primary);
  835. }
  836. dto.MediaSourceCount = 1;
  837. }
  838. var album = item as MusicAlbum;
  839. if (album != null)
  840. {
  841. dto.Artists = album.Artists;
  842. dto.SoundtrackIds = album.SoundtrackIds
  843. .Select(i => i.ToString("N"))
  844. .ToArray();
  845. }
  846. var hasAlbumArtist = item as IHasAlbumArtist;
  847. if (hasAlbumArtist != null)
  848. {
  849. dto.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault();
  850. }
  851. // Add video info
  852. var video = item as Video;
  853. if (video != null)
  854. {
  855. dto.VideoType = video.VideoType;
  856. dto.Video3DFormat = video.Video3DFormat;
  857. dto.IsoType = video.IsoType;
  858. dto.IsHD = video.IsHD;
  859. dto.PartCount = video.AdditionalPartIds.Count + 1;
  860. dto.MediaSourceCount = video.MediaSourceCount;
  861. if (fields.Contains(ItemFields.Chapters))
  862. {
  863. dto.Chapters = GetChapterInfoDtos(item);
  864. }
  865. }
  866. if (fields.Contains(ItemFields.MediaStreams))
  867. {
  868. // Add VideoInfo
  869. var iHasMediaSources = item as IHasMediaSources;
  870. if (iHasMediaSources != null)
  871. {
  872. List<MediaStream> mediaStreams;
  873. if (dto.MediaSources != null && dto.MediaSources.Count > 0)
  874. {
  875. mediaStreams = dto.MediaSources.Where(i => new Guid(i.Id) == item.Id)
  876. .SelectMany(i => i.MediaStreams)
  877. .ToList();
  878. }
  879. else
  880. {
  881. mediaStreams = iHasMediaSources.GetMediaSources(true).First().MediaStreams;
  882. }
  883. dto.MediaStreams = mediaStreams;
  884. }
  885. }
  886. // Add MovieInfo
  887. var movie = item as Movie;
  888. if (movie != null)
  889. {
  890. if (fields.Contains(ItemFields.TmdbCollectionName))
  891. {
  892. dto.TmdbCollectionName = movie.TmdbCollectionName;
  893. }
  894. }
  895. var hasSpecialFeatures = item as IHasSpecialFeatures;
  896. if (hasSpecialFeatures != null)
  897. {
  898. var specialFeatureCount = hasSpecialFeatures.SpecialFeatureIds.Count;
  899. if (specialFeatureCount > 0)
  900. {
  901. dto.SpecialFeatureCount = specialFeatureCount;
  902. }
  903. }
  904. // Add EpisodeInfo
  905. var episode = item as Episode;
  906. if (episode != null)
  907. {
  908. dto.IndexNumberEnd = episode.IndexNumberEnd;
  909. dto.DvdSeasonNumber = episode.DvdSeasonNumber;
  910. dto.DvdEpisodeNumber = episode.DvdEpisodeNumber;
  911. dto.AirsAfterSeasonNumber = episode.AirsAfterSeasonNumber;
  912. dto.AirsBeforeEpisodeNumber = episode.AirsBeforeEpisodeNumber;
  913. dto.AirsBeforeSeasonNumber = episode.AirsBeforeSeasonNumber;
  914. dto.AbsoluteEpisodeNumber = episode.AbsoluteEpisodeNumber;
  915. var episodeSeason = episode.Season;
  916. if (episodeSeason != null)
  917. {
  918. dto.SeasonId = episodeSeason.Id.ToString("N");
  919. dto.SeasonName = episodeSeason.Name;
  920. }
  921. }
  922. // Add SeriesInfo
  923. var series = item as Series;
  924. if (series != null)
  925. {
  926. dto.AirDays = series.AirDays;
  927. dto.AirTime = series.AirTime;
  928. dto.Status = series.Status;
  929. dto.SeasonCount = series.SeasonCount;
  930. if (fields.Contains(ItemFields.Settings))
  931. {
  932. dto.DisplaySpecialsWithSeasons = series.DisplaySpecialsWithSeasons;
  933. }
  934. dto.AnimeSeriesIndex = series.AnimeSeriesIndex;
  935. }
  936. if (episode != null)
  937. {
  938. series = episode.Series;
  939. if (series != null)
  940. {
  941. dto.SeriesId = GetDtoId(series);
  942. dto.SeriesName = series.Name;
  943. dto.AirTime = series.AirTime;
  944. dto.SeriesStudio = series.Studios.FirstOrDefault();
  945. dto.SeriesThumbImageTag = GetImageCacheTag(series, ImageType.Thumb);
  946. dto.SeriesPrimaryImageTag = GetImageCacheTag(series, ImageType.Primary);
  947. }
  948. }
  949. // Add SeasonInfo
  950. var season = item as Season;
  951. if (season != null)
  952. {
  953. series = season.Series;
  954. if (series != null)
  955. {
  956. dto.SeriesId = GetDtoId(series);
  957. dto.SeriesName = series.Name;
  958. dto.AirTime = series.AirTime;
  959. dto.SeriesStudio = series.Studios.FirstOrDefault();
  960. dto.SeriesPrimaryImageTag = GetImageCacheTag(series, ImageType.Primary);
  961. }
  962. }
  963. var game = item as Game;
  964. if (game != null)
  965. {
  966. SetGameProperties(dto, game);
  967. }
  968. var gameSystem = item as GameSystem;
  969. if (gameSystem != null)
  970. {
  971. SetGameSystemProperties(dto, gameSystem);
  972. }
  973. var musicVideo = item as MusicVideo;
  974. if (musicVideo != null)
  975. {
  976. SetMusicVideoProperties(dto, musicVideo);
  977. }
  978. var book = item as Book;
  979. if (book != null)
  980. {
  981. SetBookProperties(dto, book);
  982. }
  983. var photo = item as Photo;
  984. if (photo != null)
  985. {
  986. SetPhotoProperties(dto, photo);
  987. }
  988. var tvChannel = item as LiveTvChannel;
  989. if (tvChannel != null)
  990. {
  991. dto.MediaSources = tvChannel.GetMediaSources(true).ToList();
  992. }
  993. var channelItem = item as IChannelItem;
  994. if (channelItem != null)
  995. {
  996. dto.ChannelId = channelItem.ChannelId;
  997. dto.ChannelName = _channelManagerFactory().GetChannel(channelItem.ChannelId).Name;
  998. }
  999. var channelMediaItem = item as IChannelMediaItem;
  1000. if (channelMediaItem != null)
  1001. {
  1002. dto.ExtraType = channelMediaItem.ExtraType;
  1003. }
  1004. }
  1005. private void AttachLinkedChildImages(BaseItemDto dto, Folder folder, User user)
  1006. {
  1007. List<BaseItem> linkedChildren = null;
  1008. if (dto.BackdropImageTags.Count == 0)
  1009. {
  1010. if (linkedChildren == null)
  1011. {
  1012. linkedChildren = user == null
  1013. ? folder.GetRecursiveChildren().ToList()
  1014. : folder.GetRecursiveChildren(user, true).ToList();
  1015. }
  1016. var parentWithBackdrop = linkedChildren.FirstOrDefault(i => i.GetImages(ImageType.Backdrop).Any());
  1017. if (parentWithBackdrop != null)
  1018. {
  1019. dto.ParentBackdropItemId = GetDtoId(parentWithBackdrop);
  1020. dto.ParentBackdropImageTags = GetBackdropImageTags(parentWithBackdrop);
  1021. }
  1022. }
  1023. if (!dto.ImageTags.ContainsKey(ImageType.Primary))
  1024. {
  1025. if (linkedChildren == null)
  1026. {
  1027. linkedChildren = user == null
  1028. ? folder.GetRecursiveChildren().ToList()
  1029. : folder.GetRecursiveChildren(user, true).ToList();
  1030. }
  1031. var parentWithImage = linkedChildren.FirstOrDefault(i => i.GetImages(ImageType.Primary).Any());
  1032. if (parentWithImage != null)
  1033. {
  1034. dto.ParentPrimaryImageItemId = GetDtoId(parentWithImage);
  1035. dto.ParentPrimaryImageTag = GetImageCacheTag(parentWithImage, ImageType.Primary);
  1036. }
  1037. }
  1038. }
  1039. private string GetMappedPath(IHasMetadata item)
  1040. {
  1041. var path = item.Path;
  1042. var locationType = item.LocationType;
  1043. if (locationType == LocationType.FileSystem || locationType == LocationType.Offline)
  1044. {
  1045. foreach (var map in _config.Configuration.PathSubstitutions)
  1046. {
  1047. path = _fileSystem.SubstitutePath(path, map.From, map.To);
  1048. }
  1049. }
  1050. return path;
  1051. }
  1052. private void SetProductionLocations(BaseItem item, BaseItemDto dto)
  1053. {
  1054. var hasProductionLocations = item as IHasProductionLocations;
  1055. if (hasProductionLocations != null)
  1056. {
  1057. dto.ProductionLocations = hasProductionLocations.ProductionLocations;
  1058. }
  1059. var person = item as Person;
  1060. if (person != null)
  1061. {
  1062. dto.ProductionLocations = new List<string>();
  1063. if (!string.IsNullOrEmpty(person.PlaceOfBirth))
  1064. {
  1065. dto.ProductionLocations.Add(person.PlaceOfBirth);
  1066. }
  1067. }
  1068. if (dto.ProductionLocations == null)
  1069. {
  1070. dto.ProductionLocations = new List<string>();
  1071. }
  1072. }
  1073. /// <summary>
  1074. /// Since it can be slow to make all of these calculations independently, this method will provide a way to do them all at once
  1075. /// </summary>
  1076. /// <param name="folder">The folder.</param>
  1077. /// <param name="user">The user.</param>
  1078. /// <param name="dto">The dto.</param>
  1079. /// <param name="fields">The fields.</param>
  1080. /// <returns>Task.</returns>
  1081. private void SetSpecialCounts(Folder folder, User user, BaseItemDto dto, List<ItemFields> fields)
  1082. {
  1083. var recursiveItemCount = 0;
  1084. var unplayed = 0;
  1085. long runtime = 0;
  1086. DateTime? dateLastMediaAdded = null;
  1087. double totalPercentPlayed = 0;
  1088. IEnumerable<BaseItem> children;
  1089. var season = folder as Season;
  1090. if (season != null)
  1091. {
  1092. children = season.GetEpisodes(user).Where(i => i.LocationType != LocationType.Virtual);
  1093. }
  1094. else
  1095. {
  1096. children = folder.GetRecursiveChildren(user)
  1097. .Where(i => !i.IsFolder && i.LocationType != LocationType.Virtual);
  1098. }
  1099. // Loop through each recursive child
  1100. foreach (var child in children)
  1101. {
  1102. if (!dateLastMediaAdded.HasValue)
  1103. {
  1104. dateLastMediaAdded = child.DateCreated;
  1105. }
  1106. else
  1107. {
  1108. dateLastMediaAdded = new[] { dateLastMediaAdded.Value, child.DateCreated }.Max();
  1109. }
  1110. var userdata = _userDataRepository.GetUserData(user.Id, child.GetUserDataKey());
  1111. recursiveItemCount++;
  1112. var isUnplayed = true;
  1113. // Incrememt totalPercentPlayed
  1114. if (userdata != null)
  1115. {
  1116. if (userdata.Played)
  1117. {
  1118. totalPercentPlayed += 100;
  1119. isUnplayed = false;
  1120. }
  1121. else if (userdata.PlaybackPositionTicks > 0 && child.RunTimeTicks.HasValue && child.RunTimeTicks.Value > 0)
  1122. {
  1123. double itemPercent = userdata.PlaybackPositionTicks;
  1124. itemPercent /= child.RunTimeTicks.Value;
  1125. totalPercentPlayed += itemPercent;
  1126. }
  1127. }
  1128. if (isUnplayed)
  1129. {
  1130. unplayed++;
  1131. }
  1132. runtime += child.RunTimeTicks ?? 0;
  1133. }
  1134. dto.RecursiveItemCount = recursiveItemCount;
  1135. dto.UserData.UnplayedItemCount = unplayed;
  1136. dto.RecursiveUnplayedItemCount = unplayed;
  1137. if (recursiveItemCount > 0)
  1138. {
  1139. dto.UserData.PlayedPercentage = totalPercentPlayed / recursiveItemCount;
  1140. }
  1141. if (runtime > 0 && fields.Contains(ItemFields.CumulativeRunTimeTicks))
  1142. {
  1143. dto.CumulativeRunTimeTicks = runtime;
  1144. }
  1145. if (fields.Contains(ItemFields.DateLastMediaAdded))
  1146. {
  1147. dto.DateLastMediaAdded = dateLastMediaAdded;
  1148. }
  1149. }
  1150. /// <summary>
  1151. /// Attaches the primary image aspect ratio.
  1152. /// </summary>
  1153. /// <param name="dto">The dto.</param>
  1154. /// <param name="item">The item.</param>
  1155. /// <returns>Task.</returns>
  1156. public void AttachPrimaryImageAspectRatio(IItemDto dto, IHasImages item)
  1157. {
  1158. var imageInfo = item.GetImageInfo(ImageType.Primary, 0);
  1159. if (imageInfo == null)
  1160. {
  1161. return;
  1162. }
  1163. var path = imageInfo.Path;
  1164. // See if we can avoid a file system lookup by looking for the file in ResolveArgs
  1165. var dateModified = imageInfo.DateModified;
  1166. ImageSize size;
  1167. try
  1168. {
  1169. size = _imageProcessor.GetImageSize(path, dateModified);
  1170. }
  1171. catch (FileNotFoundException)
  1172. {
  1173. _logger.Error("Image file does not exist: {0}", path);
  1174. return;
  1175. }
  1176. catch (Exception ex)
  1177. {
  1178. _logger.ErrorException("Failed to determine primary image aspect ratio for {0}", ex, path);
  1179. return;
  1180. }
  1181. dto.OriginalPrimaryImageAspectRatio = size.Width / size.Height;
  1182. var supportedEnhancers = _imageProcessor.GetSupportedEnhancers(item, ImageType.Primary).ToList();
  1183. foreach (var enhancer in supportedEnhancers)
  1184. {
  1185. try
  1186. {
  1187. size = enhancer.GetEnhancedImageSize(item, ImageType.Primary, 0, size);
  1188. }
  1189. catch (Exception ex)
  1190. {
  1191. _logger.ErrorException("Error in image enhancer: {0}", ex, enhancer.GetType().Name);
  1192. }
  1193. }
  1194. dto.PrimaryImageAspectRatio = size.Width / size.Height;
  1195. }
  1196. }
  1197. }