ItemUpdateService.cs 15 KB

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