ItemUpdateService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 hasOriginalTitle = item as IHasOriginalTitle;
  214. if (hasOriginalTitle != null)
  215. {
  216. hasOriginalTitle.OriginalTitle = hasOriginalTitle.OriginalTitle;
  217. }
  218. var hasCriticRating = item as IHasCriticRating;
  219. if (hasCriticRating != null)
  220. {
  221. hasCriticRating.CriticRating = request.CriticRating;
  222. hasCriticRating.CriticRatingSummary = request.CriticRatingSummary;
  223. }
  224. item.DisplayMediaType = request.DisplayMediaType;
  225. item.CommunityRating = request.CommunityRating;
  226. item.VoteCount = request.VoteCount;
  227. item.HomePageUrl = request.HomePageUrl;
  228. item.IndexNumber = request.IndexNumber;
  229. item.ParentIndexNumber = request.ParentIndexNumber;
  230. item.Overview = request.Overview;
  231. item.Genres = request.Genres;
  232. var episode = item as Episode;
  233. if (episode != null)
  234. {
  235. episode.DvdSeasonNumber = request.DvdSeasonNumber;
  236. episode.DvdEpisodeNumber = request.DvdEpisodeNumber;
  237. episode.AirsAfterSeasonNumber = request.AirsAfterSeasonNumber;
  238. episode.AirsBeforeEpisodeNumber = request.AirsBeforeEpisodeNumber;
  239. episode.AirsBeforeSeasonNumber = request.AirsBeforeSeasonNumber;
  240. episode.AbsoluteEpisodeNumber = request.AbsoluteEpisodeNumber;
  241. }
  242. var hasTags = item as IHasTags;
  243. if (hasTags != null)
  244. {
  245. hasTags.Tags = request.Tags;
  246. }
  247. var hasTaglines = item as IHasTaglines;
  248. if (hasTaglines != null)
  249. {
  250. hasTaglines.Taglines = request.Taglines;
  251. }
  252. var hasShortOverview = item as IHasShortOverview;
  253. if (hasShortOverview != null)
  254. {
  255. hasShortOverview.ShortOverview = request.ShortOverview;
  256. }
  257. item.Keywords = request.Keywords;
  258. if (request.Studios != null)
  259. {
  260. item.Studios = request.Studios.Select(x => x.Name).ToList();
  261. }
  262. if (request.DateCreated.HasValue)
  263. {
  264. item.DateCreated = NormalizeDateTime(request.DateCreated.Value);
  265. }
  266. item.EndDate = request.EndDate.HasValue ? NormalizeDateTime(request.EndDate.Value) : (DateTime?)null;
  267. item.PremiereDate = request.PremiereDate.HasValue ? NormalizeDateTime(request.PremiereDate.Value) : (DateTime?)null;
  268. item.ProductionYear = request.ProductionYear;
  269. item.OfficialRating = request.OfficialRating;
  270. item.CustomRating = request.CustomRating;
  271. SetProductionLocations(item, request);
  272. item.PreferredMetadataCountryCode = request.PreferredMetadataCountryCode;
  273. item.PreferredMetadataLanguage = request.PreferredMetadataLanguage;
  274. var hasDisplayOrder = item as IHasDisplayOrder;
  275. if (hasDisplayOrder != null)
  276. {
  277. hasDisplayOrder.DisplayOrder = request.DisplayOrder;
  278. }
  279. var hasAspectRatio = item as IHasAspectRatio;
  280. if (hasAspectRatio != null)
  281. {
  282. hasAspectRatio.AspectRatio = request.AspectRatio;
  283. }
  284. item.IsLocked = request.LockData ?? false;
  285. if (request.LockedFields != null)
  286. {
  287. item.LockedFields = request.LockedFields;
  288. }
  289. // Only allow this for series. Runtimes for media comes from ffprobe.
  290. if (item is Series)
  291. {
  292. item.RunTimeTicks = request.RunTimeTicks;
  293. }
  294. foreach (var pair in request.ProviderIds.ToList())
  295. {
  296. if (string.IsNullOrEmpty(pair.Value))
  297. {
  298. request.ProviderIds.Remove(pair.Key);
  299. }
  300. }
  301. item.ProviderIds = request.ProviderIds;
  302. var video = item as Video;
  303. if (video != null)
  304. {
  305. video.Video3DFormat = request.Video3DFormat;
  306. }
  307. var hasMetascore = item as IHasMetascore;
  308. if (hasMetascore != null)
  309. {
  310. hasMetascore.Metascore = request.Metascore;
  311. }
  312. var hasAwards = item as IHasAwards;
  313. if (hasAwards != null)
  314. {
  315. hasAwards.AwardSummary = request.AwardSummary;
  316. }
  317. var game = item as Game;
  318. if (game != null)
  319. {
  320. game.PlayersSupported = request.Players;
  321. }
  322. if (request.AlbumArtists != null)
  323. {
  324. var hasAlbumArtists = item as IHasAlbumArtist;
  325. if (hasAlbumArtists != null)
  326. {
  327. hasAlbumArtists.AlbumArtists = request
  328. .AlbumArtists
  329. .Select(i => i.Name)
  330. .ToList();
  331. }
  332. }
  333. if (request.ArtistItems != null)
  334. {
  335. var hasArtists = item as IHasArtist;
  336. if (hasArtists != null)
  337. {
  338. hasArtists.Artists = request
  339. .ArtistItems
  340. .Select(i => i.Name)
  341. .ToList();
  342. }
  343. }
  344. var song = item as Audio;
  345. if (song != null)
  346. {
  347. song.Album = request.Album;
  348. }
  349. var musicVideo = item as MusicVideo;
  350. if (musicVideo != null)
  351. {
  352. musicVideo.Album = request.Album;
  353. }
  354. var series = item as Series;
  355. if (series != null)
  356. {
  357. series.Status = request.SeriesStatus;
  358. series.AirDays = request.AirDays;
  359. series.AirTime = request.AirTime;
  360. if (request.DisplaySpecialsWithSeasons.HasValue)
  361. {
  362. series.DisplaySpecialsWithSeasons = request.DisplaySpecialsWithSeasons.Value;
  363. }
  364. }
  365. }
  366. private void SetProductionLocations(BaseItem item, BaseItemDto request)
  367. {
  368. var hasProductionLocations = item as IHasProductionLocations;
  369. if (hasProductionLocations != null)
  370. {
  371. hasProductionLocations.ProductionLocations = request.ProductionLocations;
  372. }
  373. var person = item as Person;
  374. if (person != null)
  375. {
  376. person.PlaceOfBirth = request.ProductionLocations == null
  377. ? null
  378. : request.ProductionLocations.FirstOrDefault();
  379. }
  380. }
  381. }
  382. }