ItemUpdateService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.Audio;
  3. using MediaBrowser.Controller.Entities.TV;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Localization;
  6. using MediaBrowser.Controller.Net;
  7. using MediaBrowser.Controller.Providers;
  8. using MediaBrowser.Model.Dto;
  9. using MediaBrowser.Model.Entities;
  10. using ServiceStack;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace MediaBrowser.Api
  17. {
  18. [Route("/Items/{ItemId}", "POST", Summary = "Updates an item")]
  19. public class UpdateItem : BaseItemDto, IReturnVoid
  20. {
  21. [ApiMember(Name = "ItemId", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  22. public string ItemId { get; set; }
  23. }
  24. [Route("/Items/{ItemId}/MetadataEditor", "GET", Summary = "Gets metadata editor info for an item")]
  25. public class GetMetadataEditorInfo : IReturn<MetadataEditorInfo>
  26. {
  27. [ApiMember(Name = "ItemId", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  28. public string ItemId { get; set; }
  29. }
  30. [Route("/Items/{ItemId}/ContentType", "POST", Summary = "Updates an item's content type")]
  31. public class UpdateItemContentType : IReturnVoid
  32. {
  33. [ApiMember(Name = "ItemId", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  34. public string ItemId { get; set; }
  35. [ApiMember(Name = "ContentType", Description = "The content type of the item", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  36. public string ContentType { get; set; }
  37. }
  38. [Authenticated]
  39. public class ItemUpdateService : BaseApiService
  40. {
  41. private readonly ILibraryManager _libraryManager;
  42. private readonly IProviderManager _providerManager;
  43. private readonly ILocalizationManager _localizationManager;
  44. public ItemUpdateService(ILibraryManager libraryManager, IProviderManager providerManager, ILocalizationManager localizationManager)
  45. {
  46. _libraryManager = libraryManager;
  47. _providerManager = providerManager;
  48. _localizationManager = localizationManager;
  49. }
  50. public object Get(GetMetadataEditorInfo request)
  51. {
  52. var item = _libraryManager.GetItemById(request.ItemId);
  53. var info = new MetadataEditorInfo
  54. {
  55. ParentalRatingOptions = _localizationManager.GetParentalRatings().ToList(),
  56. ExternalIdInfos = _providerManager.GetExternalIdInfos(item).ToList(),
  57. Countries = _localizationManager.GetCountries().ToList(),
  58. Cultures = _localizationManager.GetCultures().ToList()
  59. };
  60. var locationType = item.LocationType;
  61. if (locationType == LocationType.FileSystem ||
  62. locationType == LocationType.Offline)
  63. {
  64. var collectionType = _libraryManager.GetInheritedContentType(item);
  65. if (string.IsNullOrWhiteSpace(collectionType))
  66. {
  67. info.ContentTypeOptions = GetContentTypeOptions(true);
  68. info.ContentType = _libraryManager.GetContentType(item);
  69. }
  70. }
  71. return ToOptimizedResult(info);
  72. }
  73. public void Post(UpdateItemContentType request)
  74. {
  75. }
  76. private List<NameValuePair> GetContentTypeOptions(bool isForItem)
  77. {
  78. var list = new List<NameValuePair>();
  79. if (isForItem)
  80. {
  81. list.Add(new NameValuePair
  82. {
  83. Name = "FolderTypeInherit",
  84. Value = ""
  85. });
  86. }
  87. list.Add(new NameValuePair
  88. {
  89. Name = "FolderTypeMovies",
  90. Value = "movies"
  91. });
  92. list.Add(new NameValuePair
  93. {
  94. Name = "FolderTypeMusic",
  95. Value = "music"
  96. });
  97. list.Add(new NameValuePair
  98. {
  99. Name = "FolderTypeTvShows",
  100. Value = "tvshows"
  101. });
  102. if (!isForItem)
  103. {
  104. list.Add(new NameValuePair
  105. {
  106. Name = "FolderTypeBooks",
  107. Value = "books"
  108. });
  109. list.Add(new NameValuePair
  110. {
  111. Name = "FolderTypeGames",
  112. Value = "games"
  113. });
  114. }
  115. list.Add(new NameValuePair
  116. {
  117. Name = "FolderTypeHomeVideos",
  118. Value = "homevideos"
  119. });
  120. list.Add(new NameValuePair
  121. {
  122. Name = "FolderTypeMusicVideos",
  123. Value = "musicvideos"
  124. });
  125. list.Add(new NameValuePair
  126. {
  127. Name = "FolderTypePhotos",
  128. Value = "photos"
  129. });
  130. if (!isForItem)
  131. {
  132. list.Add(new NameValuePair
  133. {
  134. Name = "FolderTypeMixed",
  135. Value = ""
  136. });
  137. }
  138. foreach (var val in list)
  139. {
  140. val.Name = _localizationManager.GetLocalizedString(val.Name);
  141. }
  142. return list;
  143. }
  144. public void Post(UpdateItem request)
  145. {
  146. var task = UpdateItem(request);
  147. Task.WaitAll(task);
  148. }
  149. private async Task UpdateItem(UpdateItem request)
  150. {
  151. var item = _libraryManager.GetItemById(request.ItemId);
  152. var newLockData = request.LockData ?? false;
  153. var isLockedChanged = item.IsLocked != newLockData;
  154. UpdateItem(request, item);
  155. if (isLockedChanged && item.IsLocked)
  156. {
  157. item.IsUnidentified = false;
  158. }
  159. await item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
  160. if (isLockedChanged && item.IsFolder)
  161. {
  162. var folder = (Folder)item;
  163. foreach (var child in folder.RecursiveChildren.ToList())
  164. {
  165. child.IsLocked = newLockData;
  166. await child.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
  167. }
  168. }
  169. }
  170. private DateTime NormalizeDateTime(DateTime val)
  171. {
  172. return DateTime.SpecifyKind(val, DateTimeKind.Utc);
  173. }
  174. private void UpdateItem(BaseItemDto request, BaseItem item)
  175. {
  176. item.Name = request.Name;
  177. item.ForcedSortName = request.ForcedSortName;
  178. var hasBudget = item as IHasBudget;
  179. if (hasBudget != null)
  180. {
  181. hasBudget.Budget = request.Budget;
  182. hasBudget.Revenue = request.Revenue;
  183. }
  184. var hasCriticRating = item as IHasCriticRating;
  185. if (hasCriticRating != null)
  186. {
  187. hasCriticRating.CriticRating = request.CriticRating;
  188. hasCriticRating.CriticRatingSummary = request.CriticRatingSummary;
  189. }
  190. item.DisplayMediaType = request.DisplayMediaType;
  191. item.CommunityRating = request.CommunityRating;
  192. item.VoteCount = request.VoteCount;
  193. item.HomePageUrl = request.HomePageUrl;
  194. item.IndexNumber = request.IndexNumber;
  195. item.ParentIndexNumber = request.ParentIndexNumber;
  196. item.Overview = request.Overview;
  197. item.Genres = request.Genres;
  198. var episode = item as Episode;
  199. if (episode != null)
  200. {
  201. episode.DvdSeasonNumber = request.DvdSeasonNumber;
  202. episode.DvdEpisodeNumber = request.DvdEpisodeNumber;
  203. episode.AirsAfterSeasonNumber = request.AirsAfterSeasonNumber;
  204. episode.AirsBeforeEpisodeNumber = request.AirsBeforeEpisodeNumber;
  205. episode.AirsBeforeSeasonNumber = request.AirsBeforeSeasonNumber;
  206. episode.AbsoluteEpisodeNumber = request.AbsoluteEpisodeNumber;
  207. }
  208. var hasTags = item as IHasTags;
  209. if (hasTags != null)
  210. {
  211. hasTags.Tags = request.Tags;
  212. }
  213. var hasTaglines = item as IHasTaglines;
  214. if (hasTaglines != null)
  215. {
  216. hasTaglines.Taglines = request.Taglines;
  217. }
  218. var hasShortOverview = item as IHasShortOverview;
  219. if (hasShortOverview != null)
  220. {
  221. hasShortOverview.ShortOverview = request.ShortOverview;
  222. }
  223. var hasKeywords = item as IHasKeywords;
  224. if (hasKeywords != null)
  225. {
  226. hasKeywords.Keywords = request.Keywords;
  227. }
  228. if (request.Studios != null)
  229. {
  230. item.Studios = request.Studios.Select(x => x.Name).ToList();
  231. }
  232. if (request.People != null)
  233. {
  234. item.People = request.People.Select(x => new PersonInfo { Name = x.Name, Role = x.Role, Type = x.Type }).ToList();
  235. }
  236. if (request.DateCreated.HasValue)
  237. {
  238. item.DateCreated = NormalizeDateTime(request.DateCreated.Value);
  239. }
  240. item.EndDate = request.EndDate.HasValue ? NormalizeDateTime(request.EndDate.Value) : (DateTime?)null;
  241. item.PremiereDate = request.PremiereDate.HasValue ? NormalizeDateTime(request.PremiereDate.Value) : (DateTime?)null;
  242. item.ProductionYear = request.ProductionYear;
  243. item.OfficialRating = request.OfficialRating;
  244. item.CustomRating = request.CustomRating;
  245. SetProductionLocations(item, request);
  246. var hasLang = item as IHasPreferredMetadataLanguage;
  247. if (hasLang != null)
  248. {
  249. hasLang.PreferredMetadataCountryCode = request.PreferredMetadataCountryCode;
  250. hasLang.PreferredMetadataLanguage = request.PreferredMetadataLanguage;
  251. }
  252. var hasDisplayOrder = item as IHasDisplayOrder;
  253. if (hasDisplayOrder != null)
  254. {
  255. hasDisplayOrder.DisplayOrder = request.DisplayOrder;
  256. }
  257. var hasAspectRatio = item as IHasAspectRatio;
  258. if (hasAspectRatio != null)
  259. {
  260. hasAspectRatio.AspectRatio = request.AspectRatio;
  261. }
  262. item.IsLocked = (request.LockData ?? false);
  263. if (request.LockedFields != null)
  264. {
  265. item.LockedFields = request.LockedFields;
  266. }
  267. // Only allow this for series. Runtimes for media comes from ffprobe.
  268. if (item is Series)
  269. {
  270. item.RunTimeTicks = request.RunTimeTicks;
  271. }
  272. foreach (var pair in request.ProviderIds.ToList())
  273. {
  274. if (string.IsNullOrEmpty(pair.Value))
  275. {
  276. request.ProviderIds.Remove(pair.Key);
  277. }
  278. }
  279. item.ProviderIds = request.ProviderIds;
  280. var video = item as Video;
  281. if (video != null)
  282. {
  283. video.Video3DFormat = request.Video3DFormat;
  284. }
  285. var hasMetascore = item as IHasMetascore;
  286. if (hasMetascore != null)
  287. {
  288. hasMetascore.Metascore = request.Metascore;
  289. }
  290. var hasAwards = item as IHasAwards;
  291. if (hasAwards != null)
  292. {
  293. hasAwards.AwardSummary = request.AwardSummary;
  294. }
  295. var game = item as Game;
  296. if (game != null)
  297. {
  298. game.PlayersSupported = request.Players;
  299. }
  300. var song = item as Audio;
  301. if (song != null)
  302. {
  303. song.Album = request.Album;
  304. song.AlbumArtists = string.IsNullOrWhiteSpace(request.AlbumArtist) ? new List<string>() : new List<string> { request.AlbumArtist };
  305. song.Artists = request.Artists.ToList();
  306. }
  307. var musicVideo = item as MusicVideo;
  308. if (musicVideo != null)
  309. {
  310. musicVideo.Artists = request.Artists.ToList();
  311. musicVideo.Album = request.Album;
  312. }
  313. var series = item as Series;
  314. if (series != null)
  315. {
  316. series.Status = request.Status;
  317. series.AirDays = request.AirDays;
  318. series.AirTime = request.AirTime;
  319. if (request.DisplaySpecialsWithSeasons.HasValue)
  320. {
  321. series.DisplaySpecialsWithSeasons = request.DisplaySpecialsWithSeasons.Value;
  322. }
  323. }
  324. }
  325. private void SetProductionLocations(BaseItem item, BaseItemDto request)
  326. {
  327. var hasProductionLocations = item as IHasProductionLocations;
  328. if (hasProductionLocations != null)
  329. {
  330. hasProductionLocations.ProductionLocations = request.ProductionLocations;
  331. }
  332. var person = item as Person;
  333. if (person != null)
  334. {
  335. person.PlaceOfBirth = request.ProductionLocations == null
  336. ? null
  337. : request.ProductionLocations.FirstOrDefault();
  338. }
  339. }
  340. }
  341. }