ItemUpdateService.cs 15 KB

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