DtoService.cs 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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. allExtras = item.GetExtras().ToArray();
  908. dto.SpecialFeatureCount = allExtras.Count(i => i.ExtraType.HasValue && BaseItem.DisplayExtraTypes.Contains(i.ExtraType.Value));
  909. }
  910. if (options.ContainsField(ItemFields.LocalTrailerCount))
  911. {
  912. allExtras ??= item.GetExtras().ToArray();
  913. dto.LocalTrailerCount = allExtras.Count(i => i.ExtraType == ExtraType.Trailer);
  914. if (item is IHasTrailers hasTrailers)
  915. {
  916. dto.LocalTrailerCount += hasTrailers.GetTrailerCount();
  917. }
  918. }
  919. // Add EpisodeInfo
  920. if (item is Episode episode)
  921. {
  922. dto.IndexNumberEnd = episode.IndexNumberEnd;
  923. dto.SeriesName = episode.SeriesName;
  924. if (options.ContainsField(ItemFields.SpecialEpisodeNumbers))
  925. {
  926. dto.AirsAfterSeasonNumber = episode.AirsAfterSeasonNumber;
  927. dto.AirsBeforeEpisodeNumber = episode.AirsBeforeEpisodeNumber;
  928. dto.AirsBeforeSeasonNumber = episode.AirsBeforeSeasonNumber;
  929. }
  930. dto.SeasonName = episode.SeasonName;
  931. dto.SeasonId = episode.SeasonId;
  932. dto.SeriesId = episode.SeriesId;
  933. Series episodeSeries = null;
  934. // this block will add the series poster for episodes without a poster
  935. // TODO maybe remove the if statement entirely
  936. //if (options.ContainsField(ItemFields.SeriesPrimaryImage))
  937. {
  938. episodeSeries = episodeSeries ?? episode.Series;
  939. if (episodeSeries != null)
  940. {
  941. dto.SeriesPrimaryImageTag = GetImageCacheTag(episodeSeries, ImageType.Primary);
  942. }
  943. }
  944. if (options.ContainsField(ItemFields.SeriesStudio))
  945. {
  946. episodeSeries = episodeSeries ?? episode.Series;
  947. if (episodeSeries != null)
  948. {
  949. dto.SeriesStudio = episodeSeries.Studios.FirstOrDefault();
  950. }
  951. }
  952. }
  953. // Add SeriesInfo
  954. if (item is Series series)
  955. {
  956. dto.AirDays = series.AirDays;
  957. dto.AirTime = series.AirTime;
  958. dto.Status = series.Status.HasValue ? series.Status.Value.ToString() : null;
  959. }
  960. // Add SeasonInfo
  961. if (item is Season season)
  962. {
  963. dto.SeriesName = season.SeriesName;
  964. dto.SeriesId = season.SeriesId;
  965. series = null;
  966. if (options.ContainsField(ItemFields.SeriesStudio))
  967. {
  968. series = series ?? season.Series;
  969. if (series != null)
  970. {
  971. dto.SeriesStudio = series.Studios.FirstOrDefault();
  972. }
  973. }
  974. // this block will add the series poster for seasons without a poster
  975. // TODO maybe remove the if statement entirely
  976. //if (options.ContainsField(ItemFields.SeriesPrimaryImage))
  977. {
  978. series = series ?? season.Series;
  979. if (series != null)
  980. {
  981. dto.SeriesPrimaryImageTag = GetImageCacheTag(series, ImageType.Primary);
  982. }
  983. }
  984. }
  985. if (item is MusicVideo musicVideo)
  986. {
  987. SetMusicVideoProperties(dto, musicVideo);
  988. }
  989. if (item is Book book)
  990. {
  991. SetBookProperties(dto, book);
  992. }
  993. if (options.ContainsField(ItemFields.ProductionLocations))
  994. {
  995. if (item.ProductionLocations.Length > 0 || item is Movie)
  996. {
  997. dto.ProductionLocations = item.ProductionLocations;
  998. }
  999. }
  1000. if (options.ContainsField(ItemFields.Width))
  1001. {
  1002. var width = item.Width;
  1003. if (width > 0)
  1004. {
  1005. dto.Width = width;
  1006. }
  1007. }
  1008. if (options.ContainsField(ItemFields.Height))
  1009. {
  1010. var height = item.Height;
  1011. if (height > 0)
  1012. {
  1013. dto.Height = height;
  1014. }
  1015. }
  1016. if (options.ContainsField(ItemFields.IsHD))
  1017. {
  1018. // Compatibility
  1019. if (item.IsHD)
  1020. {
  1021. dto.IsHD = true;
  1022. }
  1023. }
  1024. if (item is Photo photo)
  1025. {
  1026. SetPhotoProperties(dto, photo);
  1027. }
  1028. dto.ChannelId = item.ChannelId;
  1029. if (item.SourceType == SourceType.Channel)
  1030. {
  1031. var channel = _libraryManager.GetItemById(item.ChannelId);
  1032. if (channel != null)
  1033. {
  1034. dto.ChannelName = channel.Name;
  1035. }
  1036. }
  1037. }
  1038. private BaseItem GetImageDisplayParent(BaseItem currentItem, BaseItem originalItem)
  1039. {
  1040. if (currentItem is MusicAlbum musicAlbum)
  1041. {
  1042. var artist = musicAlbum.GetMusicArtist(new DtoOptions(false));
  1043. if (artist != null)
  1044. {
  1045. return artist;
  1046. }
  1047. }
  1048. var parent = currentItem.DisplayParent ?? currentItem.GetOwner() ?? currentItem.GetParent();
  1049. if (parent == null && !(originalItem is UserRootFolder) && !(originalItem is UserView) && !(originalItem is AggregateFolder) && !(originalItem is ICollectionFolder) && !(originalItem is Channel))
  1050. {
  1051. parent = _libraryManager.GetCollectionFolders(originalItem).FirstOrDefault();
  1052. }
  1053. return parent;
  1054. }
  1055. private void AddInheritedImages(BaseItemDto dto, BaseItem item, DtoOptions options, BaseItem owner)
  1056. {
  1057. if (!item.SupportsInheritedParentImages)
  1058. {
  1059. return;
  1060. }
  1061. var logoLimit = options.GetImageLimit(ImageType.Logo);
  1062. var artLimit = options.GetImageLimit(ImageType.Art);
  1063. var thumbLimit = options.GetImageLimit(ImageType.Thumb);
  1064. var backdropLimit = options.GetImageLimit(ImageType.Backdrop);
  1065. // For now. Emby apps are not using this
  1066. artLimit = 0;
  1067. if (logoLimit == 0 && artLimit == 0 && thumbLimit == 0 && backdropLimit == 0)
  1068. {
  1069. return;
  1070. }
  1071. BaseItem parent = null;
  1072. var isFirst = true;
  1073. var imageTags = dto.ImageTags;
  1074. 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) &&
  1075. (parent = parent ?? (isFirst ? GetImageDisplayParent(item, item) ?? owner : parent)) != null)
  1076. {
  1077. if (parent == null)
  1078. {
  1079. break;
  1080. }
  1081. var allImages = parent.ImageInfos;
  1082. if (logoLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Logo)) && dto.ParentLogoItemId == null)
  1083. {
  1084. var image = allImages.FirstOrDefault(i => i.Type == ImageType.Logo);
  1085. if (image != null)
  1086. {
  1087. dto.ParentLogoItemId = GetDtoId(parent);
  1088. dto.ParentLogoImageTag = GetImageCacheTag(parent, image);
  1089. }
  1090. }
  1091. if (artLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Art)) && dto.ParentArtItemId == null)
  1092. {
  1093. var image = allImages.FirstOrDefault(i => i.Type == ImageType.Art);
  1094. if (image != null)
  1095. {
  1096. dto.ParentArtItemId = GetDtoId(parent);
  1097. dto.ParentArtImageTag = GetImageCacheTag(parent, image);
  1098. }
  1099. }
  1100. if (thumbLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Thumb)) && (dto.ParentThumbItemId == null || parent is Series) && !(parent is ICollectionFolder) && !(parent is UserView))
  1101. {
  1102. var image = allImages.FirstOrDefault(i => i.Type == ImageType.Thumb);
  1103. if (image != null)
  1104. {
  1105. dto.ParentThumbItemId = GetDtoId(parent);
  1106. dto.ParentThumbImageTag = GetImageCacheTag(parent, image);
  1107. }
  1108. }
  1109. if (backdropLimit > 0 && !((dto.BackdropImageTags != null && dto.BackdropImageTags.Length > 0) || (dto.ParentBackdropImageTags != null && dto.ParentBackdropImageTags.Length > 0)))
  1110. {
  1111. var images = allImages.Where(i => i.Type == ImageType.Backdrop).Take(backdropLimit).ToList();
  1112. if (images.Count > 0)
  1113. {
  1114. dto.ParentBackdropItemId = GetDtoId(parent);
  1115. dto.ParentBackdropImageTags = GetImageTags(parent, images);
  1116. }
  1117. }
  1118. isFirst = false;
  1119. if (!parent.SupportsInheritedParentImages)
  1120. {
  1121. break;
  1122. }
  1123. parent = GetImageDisplayParent(parent, item);
  1124. }
  1125. }
  1126. private string GetMappedPath(BaseItem item, BaseItem ownerItem)
  1127. {
  1128. var path = item.Path;
  1129. if (item.IsFileProtocol)
  1130. {
  1131. path = _libraryManager.GetPathAfterNetworkSubstitution(path, ownerItem ?? item);
  1132. }
  1133. return path;
  1134. }
  1135. /// <summary>
  1136. /// Attaches the primary image aspect ratio.
  1137. /// </summary>
  1138. /// <param name="dto">The dto.</param>
  1139. /// <param name="item">The item.</param>
  1140. /// <returns>Task.</returns>
  1141. public void AttachPrimaryImageAspectRatio(IItemDto dto, BaseItem item)
  1142. {
  1143. dto.PrimaryImageAspectRatio = GetPrimaryImageAspectRatio(item);
  1144. }
  1145. public double? GetPrimaryImageAspectRatio(BaseItem item)
  1146. {
  1147. var imageInfo = item.GetImageInfo(ImageType.Primary, 0);
  1148. if (imageInfo == null)
  1149. {
  1150. return null;
  1151. }
  1152. ImageDimensions size;
  1153. var defaultAspectRatio = item.GetDefaultPrimaryImageAspectRatio();
  1154. if (defaultAspectRatio > 0)
  1155. {
  1156. return defaultAspectRatio;
  1157. }
  1158. if (!imageInfo.IsLocalFile)
  1159. {
  1160. return null;
  1161. }
  1162. try
  1163. {
  1164. size = _imageProcessor.GetImageDimensions(item, imageInfo);
  1165. if (size.Width <= 0 || size.Height <= 0)
  1166. {
  1167. return null;
  1168. }
  1169. }
  1170. catch (Exception ex)
  1171. {
  1172. _logger.LogError(ex, "Failed to determine primary image aspect ratio for {0}", imageInfo.Path);
  1173. return null;
  1174. }
  1175. var width = size.Width;
  1176. var height = size.Height;
  1177. if (width <= 0 || height <= 0)
  1178. {
  1179. return null;
  1180. }
  1181. return width / height;
  1182. }
  1183. }
  1184. }