DtoBuilder.cs 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Entities.Movies;
  5. using MediaBrowser.Controller.Entities.TV;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Persistence;
  8. using MediaBrowser.Model.Drawing;
  9. using MediaBrowser.Model.Dto;
  10. using MediaBrowser.Model.Entities;
  11. using MediaBrowser.Model.Logging;
  12. using MediaBrowser.Model.Querying;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Threading.Tasks;
  18. namespace MediaBrowser.Controller.Dto
  19. {
  20. /// <summary>
  21. /// Generates DTO's from domain entities
  22. /// </summary>
  23. public class DtoBuilder
  24. {
  25. /// <summary>
  26. /// The index folder delimeter
  27. /// </summary>
  28. const string IndexFolderDelimeter = "-index-";
  29. private readonly ILogger _logger;
  30. private readonly ILibraryManager _libraryManager;
  31. private readonly IUserDataRepository _userDataRepository;
  32. public DtoBuilder(ILogger logger, ILibraryManager libraryManager, IUserDataRepository userDataRepository)
  33. {
  34. _logger = logger;
  35. _libraryManager = libraryManager;
  36. _userDataRepository = userDataRepository;
  37. }
  38. /// <summary>
  39. /// Gets the dto base item.
  40. /// </summary>
  41. /// <param name="item">The item.</param>
  42. /// <param name="fields">The fields.</param>
  43. /// <returns>Task{DtoBaseItem}.</returns>
  44. /// <exception cref="System.ArgumentNullException">item</exception>
  45. public async Task<BaseItemDto> GetBaseItemDto(BaseItem item, List<ItemFields> fields)
  46. {
  47. if (item == null)
  48. {
  49. throw new ArgumentNullException("item");
  50. }
  51. if (fields == null)
  52. {
  53. throw new ArgumentNullException("fields");
  54. }
  55. var dto = new BaseItemDto();
  56. var tasks = new List<Task>();
  57. if (fields.Contains(ItemFields.Studios))
  58. {
  59. tasks.Add(AttachStudios(dto, item));
  60. }
  61. if (fields.Contains(ItemFields.People))
  62. {
  63. tasks.Add(AttachPeople(dto, item));
  64. }
  65. if (fields.Contains(ItemFields.PrimaryImageAspectRatio))
  66. {
  67. try
  68. {
  69. await AttachPrimaryImageAspectRatio(dto, item, _logger).ConfigureAwait(false);
  70. }
  71. catch (Exception ex)
  72. {
  73. // Have to use a catch-all unfortunately because some .net image methods throw plain Exceptions
  74. _logger.ErrorException("Error generating PrimaryImageAspectRatio for {0}", ex, item.Name);
  75. }
  76. }
  77. AttachBasicFields(dto, item, fields);
  78. // Make sure all the tasks we kicked off have completed.
  79. if (tasks.Count > 0)
  80. {
  81. await Task.WhenAll(tasks).ConfigureAwait(false);
  82. }
  83. return dto;
  84. }
  85. /// <summary>
  86. /// Converts a BaseItem to a DTOBaseItem
  87. /// </summary>
  88. /// <param name="item">The item.</param>
  89. /// <param name="user">The user.</param>
  90. /// <param name="fields">The fields.</param>
  91. /// <returns>Task{DtoBaseItem}.</returns>
  92. /// <exception cref="System.ArgumentNullException">item</exception>
  93. public async Task<BaseItemDto> GetBaseItemDto(BaseItem item, User user, List<ItemFields> fields)
  94. {
  95. if (item == null)
  96. {
  97. throw new ArgumentNullException("item");
  98. }
  99. if (user == null)
  100. {
  101. throw new ArgumentNullException("user");
  102. }
  103. if (fields == null)
  104. {
  105. throw new ArgumentNullException("fields");
  106. }
  107. var dto = new BaseItemDto();
  108. var tasks = new List<Task>();
  109. if (fields.Contains(ItemFields.Studios))
  110. {
  111. tasks.Add(AttachStudios(dto, item));
  112. }
  113. if (fields.Contains(ItemFields.People))
  114. {
  115. tasks.Add(AttachPeople(dto, item));
  116. }
  117. tasks.Add(AttachUserSpecificInfo(dto, item, user, fields));
  118. if (fields.Contains(ItemFields.PrimaryImageAspectRatio))
  119. {
  120. try
  121. {
  122. await AttachPrimaryImageAspectRatio(dto, item, _logger).ConfigureAwait(false);
  123. }
  124. catch (Exception ex)
  125. {
  126. // Have to use a catch-all unfortunately because some .net image methods throw plain Exceptions
  127. _logger.ErrorException("Error generating PrimaryImageAspectRatio for {0}", ex, item.Name);
  128. }
  129. }
  130. AttachBasicFields(dto, item, fields);
  131. // Make sure all the tasks we kicked off have completed.
  132. if (tasks.Count > 0)
  133. {
  134. await Task.WhenAll(tasks).ConfigureAwait(false);
  135. }
  136. return dto;
  137. }
  138. /// <summary>
  139. /// Attaches the user specific info.
  140. /// </summary>
  141. /// <param name="dto">The dto.</param>
  142. /// <param name="item">The item.</param>
  143. /// <param name="user">The user.</param>
  144. /// <param name="fields">The fields.</param>
  145. private async Task AttachUserSpecificInfo(BaseItemDto dto, BaseItem item, User user, List<ItemFields> fields)
  146. {
  147. if (fields.Contains(ItemFields.UserData))
  148. {
  149. var userData = await _userDataRepository.GetUserData(user.Id, item.GetUserDataKey()).ConfigureAwait(false);
  150. dto.UserData = GetUserItemDataDto(userData);
  151. }
  152. if (item.IsFolder && fields.Contains(ItemFields.DisplayPreferencesId))
  153. {
  154. dto.DisplayPreferencesId = ((Folder)item).GetDisplayPreferencesId(user.Id).ToString();
  155. }
  156. if (item.IsFolder)
  157. {
  158. if (fields.Contains(ItemFields.ItemCounts))
  159. {
  160. var folder = (Folder)item;
  161. // Skip sorting since all we want is a count
  162. dto.ChildCount = folder.GetChildren(user).Count();
  163. await SetSpecialCounts(folder, user, dto, _userDataRepository).ConfigureAwait(false);
  164. }
  165. }
  166. }
  167. /// <summary>
  168. /// Attaches the primary image aspect ratio.
  169. /// </summary>
  170. /// <param name="dto">The dto.</param>
  171. /// <param name="item">The item.</param>
  172. /// <param name="logger">The _logger.</param>
  173. /// <returns>Task.</returns>
  174. internal static async Task AttachPrimaryImageAspectRatio(IItemDto dto, BaseItem item, ILogger logger)
  175. {
  176. var path = item.PrimaryImagePath;
  177. if (string.IsNullOrEmpty(path))
  178. {
  179. return;
  180. }
  181. var metaFileEntry = item.ResolveArgs.GetMetaFileByPath(path);
  182. // See if we can avoid a file system lookup by looking for the file in ResolveArgs
  183. var dateModified = metaFileEntry == null ? File.GetLastWriteTimeUtc(path) : metaFileEntry.LastWriteTimeUtc;
  184. ImageSize size;
  185. try
  186. {
  187. size = await Kernel.Instance.ImageManager.GetImageSize(path, dateModified).ConfigureAwait(false);
  188. }
  189. catch (FileNotFoundException)
  190. {
  191. logger.Error("Image file does not exist: {0}", path);
  192. return;
  193. }
  194. catch (Exception ex)
  195. {
  196. logger.ErrorException("Failed to determine primary image aspect ratio for {0}", ex, path);
  197. return;
  198. }
  199. foreach (var enhancer in Kernel.Instance.ImageEnhancers
  200. .Where(i => i.Supports(item, ImageType.Primary)))
  201. {
  202. size = enhancer.GetEnhancedImageSize(item, ImageType.Primary, 0, size);
  203. }
  204. dto.PrimaryImageAspectRatio = size.Width / size.Height;
  205. }
  206. /// <summary>
  207. /// Sets simple property values on a DTOBaseItem
  208. /// </summary>
  209. /// <param name="dto">The dto.</param>
  210. /// <param name="item">The item.</param>
  211. /// <param name="fields">The fields.</param>
  212. private void AttachBasicFields(BaseItemDto dto, BaseItem item, List<ItemFields> fields)
  213. {
  214. if (fields.Contains(ItemFields.DateCreated))
  215. {
  216. dto.DateCreated = item.DateCreated;
  217. }
  218. if (fields.Contains(ItemFields.DisplayMediaType))
  219. {
  220. dto.DisplayMediaType = item.DisplayMediaType;
  221. }
  222. if (fields.Contains(ItemFields.Budget))
  223. {
  224. dto.Budget = item.Budget;
  225. }
  226. if (fields.Contains(ItemFields.Revenue))
  227. {
  228. dto.Revenue = item.Revenue;
  229. }
  230. if (fields.Contains(ItemFields.EndDate))
  231. {
  232. dto.EndDate = item.EndDate;
  233. }
  234. if (fields.Contains(ItemFields.HomePageUrl))
  235. {
  236. dto.HomePageUrl = item.HomePageUrl;
  237. }
  238. if (fields.Contains(ItemFields.Tags))
  239. {
  240. dto.Tags = item.Tags;
  241. }
  242. if (fields.Contains(ItemFields.ProductionLocations))
  243. {
  244. dto.ProductionLocations = item.ProductionLocations;
  245. }
  246. dto.AspectRatio = item.AspectRatio;
  247. dto.BackdropImageTags = GetBackdropImageTags(item);
  248. dto.ScreenshotImageTags = GetScreenshotImageTags(item);
  249. if (fields.Contains(ItemFields.Genres))
  250. {
  251. dto.Genres = item.Genres;
  252. }
  253. if (item.Images != null)
  254. {
  255. dto.ImageTags = new Dictionary<ImageType, Guid>();
  256. foreach (var image in item.Images)
  257. {
  258. var type = image.Key;
  259. dto.ImageTags[type] = Kernel.Instance.ImageManager.GetImageCacheTag(item, type, image.Value);
  260. }
  261. }
  262. dto.Id = GetClientItemId(item);
  263. dto.IndexNumber = item.IndexNumber;
  264. dto.IsFolder = item.IsFolder;
  265. dto.Language = item.Language;
  266. dto.MediaType = item.MediaType;
  267. dto.LocationType = item.LocationType;
  268. dto.CriticRating = item.CriticRating;
  269. if (fields.Contains(ItemFields.CriticRatingSummary))
  270. {
  271. dto.CriticRatingSummary = item.CriticRatingSummary;
  272. }
  273. var localTrailerCount = item.LocalTrailers == null ? 0 : item.LocalTrailers.Count;
  274. if (localTrailerCount > 0)
  275. {
  276. dto.LocalTrailerCount = localTrailerCount;
  277. }
  278. dto.Name = item.Name;
  279. dto.OfficialRating = item.OfficialRating;
  280. var hasOverview = fields.Contains(ItemFields.Overview);
  281. var hasHtmlOverview = fields.Contains(ItemFields.OverviewHtml);
  282. if (hasOverview || hasHtmlOverview)
  283. {
  284. var strippedOverview = string.IsNullOrEmpty(item.Overview) ? item.Overview : item.Overview.StripHtml();
  285. if (hasOverview)
  286. {
  287. dto.Overview = strippedOverview;
  288. }
  289. // Only supply the html version if there was actually html content
  290. if (hasHtmlOverview)
  291. {
  292. dto.OverviewHtml = item.Overview;
  293. }
  294. }
  295. // If there are no backdrops, indicate what parent has them in case the Ui wants to allow inheritance
  296. if (dto.BackdropImageTags.Count == 0)
  297. {
  298. var parentWithBackdrop = GetParentBackdropItem(item);
  299. if (parentWithBackdrop != null)
  300. {
  301. dto.ParentBackdropItemId = GetClientItemId(parentWithBackdrop);
  302. dto.ParentBackdropImageTags = GetBackdropImageTags(parentWithBackdrop);
  303. }
  304. }
  305. if (item.Parent != null && fields.Contains(ItemFields.ParentId))
  306. {
  307. dto.ParentId = GetClientItemId(item.Parent);
  308. }
  309. dto.ParentIndexNumber = item.ParentIndexNumber;
  310. // If there is no logo, indicate what parent has one in case the Ui wants to allow inheritance
  311. if (!dto.HasLogo)
  312. {
  313. var parentWithLogo = GetParentLogoItem(item);
  314. if (parentWithLogo != null)
  315. {
  316. dto.ParentLogoItemId = GetClientItemId(parentWithLogo);
  317. dto.ParentLogoImageTag = Kernel.Instance.ImageManager.GetImageCacheTag(parentWithLogo, ImageType.Logo, parentWithLogo.GetImage(ImageType.Logo));
  318. }
  319. }
  320. if (fields.Contains(ItemFields.Path))
  321. {
  322. dto.Path = item.Path;
  323. }
  324. dto.PremiereDate = item.PremiereDate;
  325. dto.ProductionYear = item.ProductionYear;
  326. if (fields.Contains(ItemFields.ProviderIds))
  327. {
  328. dto.ProviderIds = item.ProviderIds;
  329. }
  330. dto.RunTimeTicks = item.RunTimeTicks;
  331. if (fields.Contains(ItemFields.SortName))
  332. {
  333. dto.SortName = item.SortName;
  334. }
  335. if (fields.Contains(ItemFields.Taglines))
  336. {
  337. dto.Taglines = item.Taglines;
  338. }
  339. if (fields.Contains(ItemFields.TrailerUrls))
  340. {
  341. dto.TrailerUrls = item.TrailerUrls;
  342. }
  343. dto.Type = item.GetType().Name;
  344. dto.CommunityRating = item.CommunityRating;
  345. if (item.IsFolder)
  346. {
  347. var folder = (Folder)item;
  348. if (fields.Contains(ItemFields.IndexOptions))
  349. {
  350. dto.IndexOptions = folder.IndexByOptionStrings.ToArray();
  351. }
  352. }
  353. // Add audio info
  354. if (fields.Contains(ItemFields.AudioInfo))
  355. {
  356. var audio = item as Audio;
  357. if (audio != null)
  358. {
  359. dto.Album = audio.Album;
  360. dto.AlbumArtist = audio.AlbumArtist;
  361. dto.Artists = new[] { audio.Artist };
  362. }
  363. var album = item as MusicAlbum;
  364. if (album != null)
  365. {
  366. var songs = album.RecursiveChildren.OfType<Audio>().ToList();
  367. dto.AlbumArtist = songs.Select(i => i.AlbumArtist).FirstOrDefault(i => !string.IsNullOrEmpty(i));
  368. dto.Artists =
  369. songs.Select(i => i.Artist ?? string.Empty)
  370. .Where(i => !string.IsNullOrEmpty(i))
  371. .Distinct(StringComparer.OrdinalIgnoreCase)
  372. .ToArray();
  373. }
  374. }
  375. // Add video info
  376. var video = item as Video;
  377. if (video != null)
  378. {
  379. dto.VideoType = video.VideoType;
  380. dto.VideoFormat = video.VideoFormat;
  381. dto.IsoType = video.IsoType;
  382. if (fields.Contains(ItemFields.Chapters) && video.Chapters != null)
  383. {
  384. dto.Chapters = video.Chapters.Select(c => GetChapterInfoDto(c, item)).ToList();
  385. }
  386. }
  387. if (fields.Contains(ItemFields.MediaStreams))
  388. {
  389. // Add VideoInfo
  390. var iHasMediaStreams = item as IHasMediaStreams;
  391. if (iHasMediaStreams != null)
  392. {
  393. dto.MediaStreams = iHasMediaStreams.MediaStreams;
  394. }
  395. }
  396. // Add MovieInfo
  397. var movie = item as Movie;
  398. if (movie != null)
  399. {
  400. var specialFeatureCount = movie.SpecialFeatures == null ? 0 : movie.SpecialFeatures.Count;
  401. if (specialFeatureCount > 0)
  402. {
  403. dto.SpecialFeatureCount = specialFeatureCount;
  404. }
  405. }
  406. if (fields.Contains(ItemFields.SeriesInfo))
  407. {
  408. // Add SeriesInfo
  409. var series = item as Series;
  410. if (series != null)
  411. {
  412. dto.AirDays = series.AirDays;
  413. dto.AirTime = series.AirTime;
  414. dto.Status = series.Status;
  415. }
  416. // Add EpisodeInfo
  417. var episode = item as Episode;
  418. if (episode != null)
  419. {
  420. series = item.FindParent<Series>();
  421. dto.SeriesId = GetClientItemId(series);
  422. dto.SeriesName = series.Name;
  423. }
  424. // Add SeasonInfo
  425. var season = item as Season;
  426. if (season != null)
  427. {
  428. series = item.FindParent<Series>();
  429. dto.SeriesId = GetClientItemId(series);
  430. dto.SeriesName = series.Name;
  431. }
  432. }
  433. var game = item as BaseGame;
  434. if (game != null)
  435. {
  436. dto.Players = game.PlayersSupported;
  437. }
  438. }
  439. /// <summary>
  440. /// Since it can be slow to make all of these calculations independently, this method will provide a way to do them all at once
  441. /// </summary>
  442. /// <param name="folder">The folder.</param>
  443. /// <param name="user">The user.</param>
  444. /// <param name="dto">The dto.</param>
  445. /// <param name="userDataRepository">The user data repository.</param>
  446. /// <returns>Task.</returns>
  447. private static async Task SetSpecialCounts(Folder folder, User user, BaseItemDto dto, IUserDataRepository userDataRepository)
  448. {
  449. var rcentlyAddedItemCount = 0;
  450. var recursiveItemCount = 0;
  451. double totalPercentPlayed = 0;
  452. // Loop through each recursive child
  453. foreach (var child in folder.GetRecursiveChildren(user).Where(i => !i.IsFolder).ToList())
  454. {
  455. var userdata = await userDataRepository.GetUserData(user.Id, child.GetUserDataKey()).ConfigureAwait(false);
  456. recursiveItemCount++;
  457. // Check is recently added
  458. if (child.IsRecentlyAdded())
  459. {
  460. rcentlyAddedItemCount++;
  461. }
  462. // Incrememt totalPercentPlayed
  463. if (userdata != null)
  464. {
  465. if (userdata.PlayCount > 0)
  466. {
  467. totalPercentPlayed += 100;
  468. }
  469. else if (userdata.PlaybackPositionTicks > 0 && child.RunTimeTicks.HasValue && child.RunTimeTicks.Value > 0)
  470. {
  471. double itemPercent = userdata.PlaybackPositionTicks;
  472. itemPercent /= child.RunTimeTicks.Value;
  473. totalPercentPlayed += itemPercent;
  474. }
  475. }
  476. }
  477. dto.RecursiveItemCount = recursiveItemCount;
  478. dto.RecentlyAddedItemCount = rcentlyAddedItemCount;
  479. if (recursiveItemCount > 0)
  480. {
  481. dto.PlayedPercentage = totalPercentPlayed / recursiveItemCount;
  482. }
  483. }
  484. /// <summary>
  485. /// Attaches People DTO's to a DTOBaseItem
  486. /// </summary>
  487. /// <param name="dto">The dto.</param>
  488. /// <param name="item">The item.</param>
  489. /// <returns>Task.</returns>
  490. private async Task AttachPeople(BaseItemDto dto, BaseItem item)
  491. {
  492. if (item.People == null)
  493. {
  494. return;
  495. }
  496. // Ordering by person type to ensure actors and artists are at the front.
  497. // This is taking advantage of the fact that they both begin with A
  498. // This should be improved in the future
  499. var people = item.People.OrderBy(i => i.Type).ToList();
  500. // Attach People by transforming them into BaseItemPerson (DTO)
  501. dto.People = new BaseItemPerson[people.Count];
  502. var entities = await Task.WhenAll(people.Select(p => p.Name).Distinct(StringComparer.OrdinalIgnoreCase).Select(c =>
  503. Task.Run(async () =>
  504. {
  505. try
  506. {
  507. return await _libraryManager.GetPerson(c).ConfigureAwait(false);
  508. }
  509. catch (IOException ex)
  510. {
  511. _logger.ErrorException("Error getting person {0}", ex, c);
  512. return null;
  513. }
  514. })
  515. )).ConfigureAwait(false);
  516. var dictionary = entities.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. Person entity;
  527. if (dictionary.TryGetValue(person.Name, out entity))
  528. {
  529. var primaryImagePath = entity.PrimaryImagePath;
  530. if (!string.IsNullOrEmpty(primaryImagePath))
  531. {
  532. baseItemPerson.PrimaryImageTag = Kernel.Instance.ImageManager.GetImageCacheTag(entity, ImageType.Primary, primaryImagePath);
  533. }
  534. }
  535. dto.People[i] = baseItemPerson;
  536. }
  537. }
  538. /// <summary>
  539. /// Attaches the studios.
  540. /// </summary>
  541. /// <param name="dto">The dto.</param>
  542. /// <param name="item">The item.</param>
  543. /// <returns>Task.</returns>
  544. private async Task AttachStudios(BaseItemDto dto, BaseItem item)
  545. {
  546. if (item.Studios == null)
  547. {
  548. return;
  549. }
  550. var studios = item.Studios.ToList();
  551. dto.Studios = new StudioDto[studios.Count];
  552. var entities = await Task.WhenAll(studios.Distinct(StringComparer.OrdinalIgnoreCase).Select(c =>
  553. Task.Run(async () =>
  554. {
  555. try
  556. {
  557. return await _libraryManager.GetStudio(c).ConfigureAwait(false);
  558. }
  559. catch (IOException ex)
  560. {
  561. _logger.ErrorException("Error getting studio {0}", ex, c);
  562. return null;
  563. }
  564. })
  565. )).ConfigureAwait(false);
  566. var dictionary = entities.ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase);
  567. for (var i = 0; i < studios.Count; i++)
  568. {
  569. var studio = studios[i];
  570. var studioDto = new StudioDto
  571. {
  572. Name = studio
  573. };
  574. Studio entity;
  575. if (dictionary.TryGetValue(studio, out entity))
  576. {
  577. var primaryImagePath = entity.PrimaryImagePath;
  578. if (!string.IsNullOrEmpty(primaryImagePath))
  579. {
  580. studioDto.PrimaryImageTag = Kernel.Instance.ImageManager.GetImageCacheTag(entity, ImageType.Primary, primaryImagePath);
  581. }
  582. }
  583. dto.Studios[i] = studioDto;
  584. }
  585. }
  586. /// <summary>
  587. /// If an item does not any backdrops, this can be used to find the first parent that does have one
  588. /// </summary>
  589. /// <param name="item">The item.</param>
  590. /// <returns>BaseItem.</returns>
  591. private BaseItem GetParentBackdropItem(BaseItem item)
  592. {
  593. var parent = item.Parent;
  594. while (parent != null)
  595. {
  596. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Count > 0)
  597. {
  598. return parent;
  599. }
  600. parent = parent.Parent;
  601. }
  602. return null;
  603. }
  604. /// <summary>
  605. /// If an item does not have a logo, this can be used to find the first parent that does have one
  606. /// </summary>
  607. /// <param name="item">The item.</param>
  608. /// <returns>BaseItem.</returns>
  609. private BaseItem GetParentLogoItem(BaseItem item)
  610. {
  611. var parent = item.Parent;
  612. while (parent != null)
  613. {
  614. if (parent.HasImage(ImageType.Logo))
  615. {
  616. return parent;
  617. }
  618. parent = parent.Parent;
  619. }
  620. return null;
  621. }
  622. /// <summary>
  623. /// Converts a UserItemData to a DTOUserItemData
  624. /// </summary>
  625. /// <param name="data">The data.</param>
  626. /// <returns>DtoUserItemData.</returns>
  627. /// <exception cref="System.ArgumentNullException"></exception>
  628. public static UserItemDataDto GetUserItemDataDto(UserItemData data)
  629. {
  630. if (data == null)
  631. {
  632. throw new ArgumentNullException();
  633. }
  634. return new UserItemDataDto
  635. {
  636. IsFavorite = data.IsFavorite,
  637. Likes = data.Likes,
  638. PlaybackPositionTicks = data.PlaybackPositionTicks,
  639. PlayCount = data.PlayCount,
  640. Rating = data.Rating,
  641. Played = data.Played,
  642. LastPlayedDate = data.LastPlayedDate
  643. };
  644. }
  645. /// <summary>
  646. /// Gets the chapter info dto.
  647. /// </summary>
  648. /// <param name="chapterInfo">The chapter info.</param>
  649. /// <param name="item">The item.</param>
  650. /// <returns>ChapterInfoDto.</returns>
  651. private ChapterInfoDto GetChapterInfoDto(ChapterInfo chapterInfo, BaseItem item)
  652. {
  653. var dto = new ChapterInfoDto
  654. {
  655. Name = chapterInfo.Name,
  656. StartPositionTicks = chapterInfo.StartPositionTicks
  657. };
  658. if (!string.IsNullOrEmpty(chapterInfo.ImagePath))
  659. {
  660. dto.ImageTag = Kernel.Instance.ImageManager.GetImageCacheTag(item, ImageType.Chapter, chapterInfo.ImagePath);
  661. }
  662. return dto;
  663. }
  664. /// <summary>
  665. /// Converts a BaseItem to a BaseItemInfo
  666. /// </summary>
  667. /// <param name="item">The item.</param>
  668. /// <returns>BaseItemInfo.</returns>
  669. /// <exception cref="System.ArgumentNullException">item</exception>
  670. public static BaseItemInfo GetBaseItemInfo(BaseItem item)
  671. {
  672. if (item == null)
  673. {
  674. throw new ArgumentNullException("item");
  675. }
  676. var info = new BaseItemInfo
  677. {
  678. Id = GetClientItemId(item),
  679. Name = item.Name,
  680. Type = item.GetType().Name,
  681. IsFolder = item.IsFolder,
  682. RunTimeTicks = item.RunTimeTicks
  683. };
  684. var imagePath = item.PrimaryImagePath;
  685. if (!string.IsNullOrEmpty(imagePath))
  686. {
  687. info.PrimaryImageTag = Kernel.Instance.ImageManager.GetImageCacheTag(item, ImageType.Primary, imagePath);
  688. }
  689. if (item.BackdropImagePaths != null && item.BackdropImagePaths.Count > 0)
  690. {
  691. imagePath = item.BackdropImagePaths[0];
  692. if (!string.IsNullOrEmpty(imagePath))
  693. {
  694. info.BackdropImageTag = Kernel.Instance.ImageManager.GetImageCacheTag(item, ImageType.Backdrop, imagePath);
  695. }
  696. }
  697. return info;
  698. }
  699. /// <summary>
  700. /// Gets client-side Id of a server-side BaseItem
  701. /// </summary>
  702. /// <param name="item">The item.</param>
  703. /// <returns>System.String.</returns>
  704. /// <exception cref="System.ArgumentNullException">item</exception>
  705. public static string GetClientItemId(BaseItem item)
  706. {
  707. if (item == null)
  708. {
  709. throw new ArgumentNullException("item");
  710. }
  711. var indexFolder = item as IndexFolder;
  712. if (indexFolder != null)
  713. {
  714. return GetClientItemId(indexFolder.Parent) + IndexFolderDelimeter + (indexFolder.IndexName ?? string.Empty) + IndexFolderDelimeter + indexFolder.Id;
  715. }
  716. return item.Id.ToString();
  717. }
  718. /// <summary>
  719. /// Gets a BaseItem based upon it's client-side item id
  720. /// </summary>
  721. /// <param name="id">The id.</param>
  722. /// <param name="userManager">The user manager.</param>
  723. /// <param name="libraryManager">The library manager.</param>
  724. /// <param name="userId">The user id.</param>
  725. /// <returns>BaseItem.</returns>
  726. public static BaseItem GetItemByClientId(string id, IUserManager userManager, ILibraryManager libraryManager, Guid? userId = null)
  727. {
  728. if (string.IsNullOrEmpty(id))
  729. {
  730. throw new ArgumentNullException("id");
  731. }
  732. // If the item is an indexed folder we have to do a special routine to get it
  733. var isIndexFolder = id.IndexOf(IndexFolderDelimeter, StringComparison.OrdinalIgnoreCase) != -1;
  734. if (isIndexFolder)
  735. {
  736. if (userId.HasValue)
  737. {
  738. return GetIndexFolder(id, userId.Value, userManager, libraryManager);
  739. }
  740. }
  741. BaseItem item = null;
  742. if (userId.HasValue || !isIndexFolder)
  743. {
  744. item = libraryManager.GetItemById(new Guid(id));
  745. }
  746. // If we still don't find it, look within individual user views
  747. if (item == null && !userId.HasValue && isIndexFolder)
  748. {
  749. foreach (var user in userManager.Users)
  750. {
  751. item = GetItemByClientId(id, userManager, libraryManager, user.Id);
  752. if (item != null)
  753. {
  754. break;
  755. }
  756. }
  757. }
  758. return item;
  759. }
  760. /// <summary>
  761. /// Finds an index folder based on an Id and userId
  762. /// </summary>
  763. /// <param name="id">The id.</param>
  764. /// <param name="userId">The user id.</param>
  765. /// <param name="userManager">The user manager.</param>
  766. /// <param name="libraryManager">The library manager.</param>
  767. /// <returns>BaseItem.</returns>
  768. private static BaseItem GetIndexFolder(string id, Guid userId, IUserManager userManager, ILibraryManager libraryManager)
  769. {
  770. var user = userManager.GetUserById(userId);
  771. var stringSeparators = new[] { IndexFolderDelimeter };
  772. // Split using the delimeter
  773. var values = id.Split(stringSeparators, StringSplitOptions.None).ToList();
  774. // Get the top folder normally using the first id
  775. var folder = GetItemByClientId(values[0], userManager, libraryManager, userId) as Folder;
  776. values.RemoveAt(0);
  777. // Get indexed folders using the remaining values in the id string
  778. return GetIndexFolder(values, folder, user);
  779. }
  780. /// <summary>
  781. /// Gets indexed folders based on a list of index names and folder id's
  782. /// </summary>
  783. /// <param name="values">The values.</param>
  784. /// <param name="parentFolder">The parent folder.</param>
  785. /// <param name="user">The user.</param>
  786. /// <returns>BaseItem.</returns>
  787. private static BaseItem GetIndexFolder(List<string> values, Folder parentFolder, User user)
  788. {
  789. // The index name is first
  790. var indexBy = values[0];
  791. // The index folder id is next
  792. var indexFolderId = new Guid(values[1]);
  793. // Remove them from the lst
  794. values.RemoveRange(0, 2);
  795. // Get the IndexFolder
  796. var indexFolder = parentFolder.GetChildren(user, indexBy).FirstOrDefault(i => i.Id == indexFolderId) as Folder;
  797. // Nested index folder
  798. if (values.Count > 0)
  799. {
  800. return GetIndexFolder(values, indexFolder, user);
  801. }
  802. return indexFolder;
  803. }
  804. /// <summary>
  805. /// Gets the backdrop image tags.
  806. /// </summary>
  807. /// <param name="item">The item.</param>
  808. /// <returns>List{System.String}.</returns>
  809. private List<Guid> GetBackdropImageTags(BaseItem item)
  810. {
  811. if (item.BackdropImagePaths == null)
  812. {
  813. return new List<Guid>();
  814. }
  815. return item.BackdropImagePaths.Select(p => Kernel.Instance.ImageManager.GetImageCacheTag(item, ImageType.Backdrop, p)).ToList();
  816. }
  817. /// <summary>
  818. /// Gets the screenshot image tags.
  819. /// </summary>
  820. /// <param name="item">The item.</param>
  821. /// <returns>List{Guid}.</returns>
  822. private List<Guid> GetScreenshotImageTags(BaseItem item)
  823. {
  824. if (item.ScreenshotImagePaths == null)
  825. {
  826. return new List<Guid>();
  827. }
  828. return item.ScreenshotImagePaths.Select(p => Kernel.Instance.ImageManager.GetImageCacheTag(item, ImageType.Screenshot, p)).ToList();
  829. }
  830. }
  831. }