ItemUpdateService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. var locationType = item.LocationType;
  65. if (locationType == LocationType.FileSystem ||
  66. locationType == LocationType.Offline)
  67. {
  68. if (!(item is ICollectionFolder) && !(item is UserView) && !(item is AggregateFolder) && !(item is LiveTvChannel) && !(item is IItemByName))
  69. {
  70. var inheritedContentType = _libraryManager.GetInheritedContentType(item);
  71. var configuredContentType = _libraryManager.GetConfiguredContentType(item);
  72. if (string.IsNullOrWhiteSpace(inheritedContentType) || string.Equals(inheritedContentType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase) || !string.IsNullOrWhiteSpace(configuredContentType))
  73. {
  74. info.ContentTypeOptions = GetContentTypeOptions(true);
  75. info.ContentType = configuredContentType;
  76. if (string.Equals(inheritedContentType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  77. {
  78. info.ContentTypeOptions = info.ContentTypeOptions
  79. .Where(i => string.IsNullOrWhiteSpace(i.Value) || string.Equals(i.Value, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  80. .ToList();
  81. }
  82. }
  83. }
  84. }
  85. return ToOptimizedResult(info);
  86. }
  87. public void Post(UpdateItemContentType request)
  88. {
  89. var item = _libraryManager.GetItemById(request.ItemId);
  90. var path = item.ContainingFolderPath;
  91. var types = _config.Configuration.ContentTypes
  92. .Where(i => !string.Equals(i.Name, path, StringComparison.OrdinalIgnoreCase))
  93. .ToList();
  94. if (!string.IsNullOrWhiteSpace(request.ContentType))
  95. {
  96. types.Add(new NameValuePair
  97. {
  98. Name = path,
  99. Value = request.ContentType
  100. });
  101. }
  102. _config.Configuration.ContentTypes = types.ToArray();
  103. _config.SaveConfiguration();
  104. }
  105. private List<NameValuePair> GetContentTypeOptions(bool isForItem)
  106. {
  107. var list = new List<NameValuePair>();
  108. if (isForItem)
  109. {
  110. list.Add(new NameValuePair
  111. {
  112. Name = "FolderTypeInherit",
  113. Value = ""
  114. });
  115. }
  116. list.Add(new NameValuePair
  117. {
  118. Name = "FolderTypeMovies",
  119. Value = "movies"
  120. });
  121. list.Add(new NameValuePair
  122. {
  123. Name = "FolderTypeMusic",
  124. Value = "music"
  125. });
  126. list.Add(new NameValuePair
  127. {
  128. Name = "FolderTypeTvShows",
  129. Value = "tvshows"
  130. });
  131. if (!isForItem)
  132. {
  133. list.Add(new NameValuePair
  134. {
  135. Name = "FolderTypeBooks",
  136. Value = "books"
  137. });
  138. list.Add(new NameValuePair
  139. {
  140. Name = "FolderTypeGames",
  141. Value = "games"
  142. });
  143. }
  144. list.Add(new NameValuePair
  145. {
  146. Name = "FolderTypeHomeVideos",
  147. Value = "homevideos"
  148. });
  149. list.Add(new NameValuePair
  150. {
  151. Name = "FolderTypeMusicVideos",
  152. Value = "musicvideos"
  153. });
  154. list.Add(new NameValuePair
  155. {
  156. Name = "FolderTypePhotos",
  157. Value = "photos"
  158. });
  159. if (!isForItem)
  160. {
  161. list.Add(new NameValuePair
  162. {
  163. Name = "FolderTypeMixed",
  164. Value = ""
  165. });
  166. }
  167. foreach (var val in list)
  168. {
  169. val.Name = _localizationManager.GetLocalizedString(val.Name);
  170. }
  171. return list;
  172. }
  173. public void Post(UpdateItem request)
  174. {
  175. var task = UpdateItem(request);
  176. Task.WaitAll(task);
  177. }
  178. private async Task UpdateItem(UpdateItem request)
  179. {
  180. var item = _libraryManager.GetItemById(request.ItemId);
  181. var newLockData = request.LockData ?? false;
  182. var isLockedChanged = item.IsLocked != newLockData;
  183. UpdateItem(request, item);
  184. await item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
  185. if (request.People != null)
  186. {
  187. await _libraryManager.UpdatePeople(item, request.People.Select(x => new PersonInfo { Name = x.Name, Role = x.Role, Type = x.Type }).ToList());
  188. }
  189. if (isLockedChanged && item.IsFolder)
  190. {
  191. var folder = (Folder)item;
  192. foreach (var child in folder.GetRecursiveChildren())
  193. {
  194. child.IsLocked = newLockData;
  195. await child.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
  196. }
  197. }
  198. }
  199. private DateTime NormalizeDateTime(DateTime val)
  200. {
  201. return DateTime.SpecifyKind(val, DateTimeKind.Utc);
  202. }
  203. private void UpdateItem(BaseItemDto request, BaseItem item)
  204. {
  205. item.Name = request.Name;
  206. item.ForcedSortName = request.ForcedSortName;
  207. var hasBudget = item as IHasBudget;
  208. if (hasBudget != null)
  209. {
  210. hasBudget.Budget = request.Budget;
  211. hasBudget.Revenue = request.Revenue;
  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. var hasTags = item as IHasTags;
  238. if (hasTags != null)
  239. {
  240. hasTags.Tags = request.Tags;
  241. }
  242. var hasTaglines = item as IHasTaglines;
  243. if (hasTaglines != null)
  244. {
  245. hasTaglines.Taglines = request.Taglines;
  246. }
  247. var hasShortOverview = item as IHasShortOverview;
  248. if (hasShortOverview != null)
  249. {
  250. hasShortOverview.ShortOverview = request.ShortOverview;
  251. }
  252. var hasKeywords = item as IHasKeywords;
  253. if (hasKeywords != null)
  254. {
  255. hasKeywords.Keywords = request.Keywords;
  256. }
  257. if (request.Studios != null)
  258. {
  259. item.Studios = request.Studios.Select(x => x.Name).ToList();
  260. }
  261. if (request.DateCreated.HasValue)
  262. {
  263. item.DateCreated = NormalizeDateTime(request.DateCreated.Value);
  264. }
  265. item.EndDate = request.EndDate.HasValue ? NormalizeDateTime(request.EndDate.Value) : (DateTime?)null;
  266. item.PremiereDate = request.PremiereDate.HasValue ? NormalizeDateTime(request.PremiereDate.Value) : (DateTime?)null;
  267. item.ProductionYear = request.ProductionYear;
  268. item.OfficialRating = request.OfficialRating;
  269. item.CustomRating = request.CustomRating;
  270. SetProductionLocations(item, request);
  271. item.PreferredMetadataCountryCode = request.PreferredMetadataCountryCode;
  272. item.PreferredMetadataLanguage = request.PreferredMetadataLanguage;
  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. if (request.AlbumArtists != null)
  322. {
  323. var hasAlbumArtists = item as IHasAlbumArtist;
  324. if (hasAlbumArtists != null)
  325. {
  326. hasAlbumArtists.AlbumArtists = request
  327. .AlbumArtists
  328. .Select(i => i.Name)
  329. .ToList();
  330. }
  331. }
  332. if (request.ArtistItems != null)
  333. {
  334. var hasArtists = item as IHasArtist;
  335. if (hasArtists != null)
  336. {
  337. hasArtists.Artists = request
  338. .ArtistItems
  339. .Select(i => i.Name)
  340. .ToList();
  341. }
  342. }
  343. var song = item as Audio;
  344. if (song != null)
  345. {
  346. song.Album = request.Album;
  347. }
  348. var musicVideo = item as MusicVideo;
  349. if (musicVideo != null)
  350. {
  351. musicVideo.Album = request.Album;
  352. }
  353. var series = item as Series;
  354. if (series != null)
  355. {
  356. series.Status = request.SeriesStatus;
  357. series.AirDays = request.AirDays;
  358. series.AirTime = request.AirTime;
  359. if (request.DisplaySpecialsWithSeasons.HasValue)
  360. {
  361. series.DisplaySpecialsWithSeasons = request.DisplaySpecialsWithSeasons.Value;
  362. }
  363. }
  364. }
  365. private void SetProductionLocations(BaseItem item, BaseItemDto request)
  366. {
  367. var hasProductionLocations = item as IHasProductionLocations;
  368. if (hasProductionLocations != null)
  369. {
  370. hasProductionLocations.ProductionLocations = request.ProductionLocations;
  371. }
  372. var person = item as Person;
  373. if (person != null)
  374. {
  375. person.PlaceOfBirth = request.ProductionLocations == null
  376. ? null
  377. : request.ProductionLocations.FirstOrDefault();
  378. }
  379. }
  380. }
  381. }