ItemUpdateService.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System.Collections.Generic;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Entities.TV;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Model.Dto;
  7. using ServiceStack;
  8. using System;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Api
  13. {
  14. [Route("/Items/{ItemId}", "POST", Summary = "Updates an item")]
  15. public class UpdateItem : BaseItemDto, IReturnVoid
  16. {
  17. [ApiMember(Name = "ItemId", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  18. public string ItemId { get; set; }
  19. }
  20. public class ItemUpdateService : BaseApiService
  21. {
  22. private readonly ILibraryManager _libraryManager;
  23. public ItemUpdateService(ILibraryManager libraryManager)
  24. {
  25. _libraryManager = libraryManager;
  26. }
  27. public void Post(UpdateItem request)
  28. {
  29. var task = UpdateItem(request);
  30. Task.WaitAll(task);
  31. }
  32. private async Task UpdateItem(UpdateItem request)
  33. {
  34. var item = _libraryManager.GetItemById(request.ItemId);
  35. var newLockData = request.LockData ?? false;
  36. var isLockedChanged = item.IsLocked != newLockData;
  37. UpdateItem(request, item);
  38. if (isLockedChanged && item.IsLocked)
  39. {
  40. item.IsUnidentified = false;
  41. }
  42. await item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
  43. if (isLockedChanged && item.IsFolder)
  44. {
  45. var folder = (Folder)item;
  46. foreach (var child in folder.RecursiveChildren.ToList())
  47. {
  48. child.IsLocked = newLockData;
  49. await child.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
  50. }
  51. }
  52. }
  53. private void UpdateItem(BaseItemDto request, BaseItem item)
  54. {
  55. item.Name = request.Name;
  56. item.ForcedSortName = request.ForcedSortName;
  57. var hasBudget = item as IHasBudget;
  58. if (hasBudget != null)
  59. {
  60. hasBudget.Budget = request.Budget;
  61. hasBudget.Revenue = request.Revenue;
  62. }
  63. var hasCriticRating = item as IHasCriticRating;
  64. if (hasCriticRating != null)
  65. {
  66. hasCriticRating.CriticRating = request.CriticRating;
  67. hasCriticRating.CriticRatingSummary = request.CriticRatingSummary;
  68. }
  69. item.DisplayMediaType = request.DisplayMediaType;
  70. item.CommunityRating = request.CommunityRating;
  71. item.VoteCount = request.VoteCount;
  72. item.HomePageUrl = request.HomePageUrl;
  73. item.IndexNumber = request.IndexNumber;
  74. item.ParentIndexNumber = request.ParentIndexNumber;
  75. item.Overview = request.Overview;
  76. item.Genres = request.Genres;
  77. var episode = item as Episode;
  78. if (episode != null)
  79. {
  80. episode.DvdSeasonNumber = request.DvdSeasonNumber;
  81. episode.DvdEpisodeNumber = request.DvdEpisodeNumber;
  82. episode.AirsAfterSeasonNumber = request.AirsAfterSeasonNumber;
  83. episode.AirsBeforeEpisodeNumber = request.AirsBeforeEpisodeNumber;
  84. episode.AirsBeforeSeasonNumber = request.AirsBeforeSeasonNumber;
  85. episode.AbsoluteEpisodeNumber = request.AbsoluteEpisodeNumber;
  86. }
  87. var hasTags = item as IHasTags;
  88. if (hasTags != null)
  89. {
  90. hasTags.Tags = request.Tags;
  91. }
  92. var hasShortOverview = item as IHasShortOverview;
  93. if (hasShortOverview != null)
  94. {
  95. hasShortOverview.ShortOverview = request.ShortOverview;
  96. }
  97. var hasKeywords = item as IHasKeywords;
  98. if (hasKeywords != null)
  99. {
  100. hasKeywords.Keywords = request.Keywords;
  101. }
  102. if (request.Studios != null)
  103. {
  104. item.Studios = request.Studios.Select(x => x.Name).ToList();
  105. }
  106. if (request.People != null)
  107. {
  108. item.People = request.People.Select(x => new PersonInfo { Name = x.Name, Role = x.Role, Type = x.Type }).ToList();
  109. }
  110. if (request.DateCreated.HasValue)
  111. {
  112. item.DateCreated = request.DateCreated.Value.ToUniversalTime();
  113. }
  114. item.EndDate = request.EndDate.HasValue ? request.EndDate.Value.ToUniversalTime() : (DateTime?)null;
  115. item.PremiereDate = request.PremiereDate.HasValue ? request.PremiereDate.Value.ToUniversalTime() : (DateTime?)null;
  116. item.ProductionYear = request.ProductionYear;
  117. item.OfficialRating = request.OfficialRating;
  118. item.CustomRating = request.CustomRating;
  119. SetProductionLocations(item, request);
  120. var hasLang = item as IHasPreferredMetadataLanguage;
  121. if (hasLang != null)
  122. {
  123. hasLang.PreferredMetadataCountryCode = request.PreferredMetadataCountryCode;
  124. hasLang.PreferredMetadataLanguage = request.PreferredMetadataLanguage;
  125. }
  126. var hasDisplayOrder = item as IHasDisplayOrder;
  127. if (hasDisplayOrder != null)
  128. {
  129. hasDisplayOrder.DisplayOrder = request.DisplayOrder;
  130. }
  131. var hasAspectRatio = item as IHasAspectRatio;
  132. if (hasAspectRatio != null)
  133. {
  134. hasAspectRatio.AspectRatio = request.AspectRatio;
  135. }
  136. item.IsLocked = (request.LockData ?? false);
  137. if (request.LockedFields != null)
  138. {
  139. item.LockedFields = request.LockedFields;
  140. }
  141. // Only allow this for series. Runtimes for media comes from ffprobe.
  142. if (item is Series)
  143. {
  144. item.RunTimeTicks = request.RunTimeTicks;
  145. }
  146. foreach (var pair in request.ProviderIds.ToList())
  147. {
  148. if (string.IsNullOrEmpty(pair.Value))
  149. {
  150. request.ProviderIds.Remove(pair.Key);
  151. }
  152. }
  153. item.ProviderIds = request.ProviderIds;
  154. var video = item as Video;
  155. if (video != null)
  156. {
  157. video.Video3DFormat = request.Video3DFormat;
  158. }
  159. var hasMetascore = item as IHasMetascore;
  160. if (hasMetascore != null)
  161. {
  162. hasMetascore.Metascore = request.Metascore;
  163. }
  164. var hasAwards = item as IHasAwards;
  165. if (hasAwards != null)
  166. {
  167. hasAwards.AwardSummary = request.AwardSummary;
  168. }
  169. var game = item as Game;
  170. if (game != null)
  171. {
  172. game.PlayersSupported = request.Players;
  173. }
  174. var song = item as Audio;
  175. if (song != null)
  176. {
  177. song.Album = request.Album;
  178. song.AlbumArtists = string.IsNullOrWhiteSpace(request.AlbumArtist) ? new List<string>() : new List<string> { request.AlbumArtist };
  179. song.Artists = request.Artists.ToList();
  180. }
  181. var musicVideo = item as MusicVideo;
  182. if (musicVideo != null)
  183. {
  184. musicVideo.Artist = request.Artists[0];
  185. musicVideo.Album = request.Album;
  186. }
  187. var series = item as Series;
  188. if (series != null)
  189. {
  190. series.Status = request.Status;
  191. series.AirDays = request.AirDays;
  192. series.AirTime = request.AirTime;
  193. if (request.DisplaySpecialsWithSeasons.HasValue)
  194. {
  195. series.DisplaySpecialsWithSeasons = request.DisplaySpecialsWithSeasons.Value;
  196. }
  197. }
  198. }
  199. private void SetProductionLocations(BaseItem item, BaseItemDto request)
  200. {
  201. var hasProductionLocations = item as IHasProductionLocations;
  202. if (hasProductionLocations != null)
  203. {
  204. hasProductionLocations.ProductionLocations = request.ProductionLocations;
  205. }
  206. var person = item as Person;
  207. if (person != null)
  208. {
  209. person.PlaceOfBirth = request.ProductionLocations == null
  210. ? null
  211. : request.ProductionLocations.FirstOrDefault();
  212. }
  213. }
  214. }
  215. }