ItemUpdateService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. item.Tags = request.Tags;
  243. var hasTaglines = item as IHasTaglines;
  244. if (hasTaglines != null)
  245. {
  246. hasTaglines.Taglines = request.Taglines;
  247. }
  248. var hasShortOverview = item as IHasShortOverview;
  249. if (hasShortOverview != null)
  250. {
  251. hasShortOverview.ShortOverview = request.ShortOverview;
  252. }
  253. item.Keywords = request.Keywords;
  254. if (request.Studios != null)
  255. {
  256. item.Studios = request.Studios.Select(x => x.Name).ToList();
  257. }
  258. if (request.DateCreated.HasValue)
  259. {
  260. item.DateCreated = NormalizeDateTime(request.DateCreated.Value);
  261. }
  262. item.EndDate = request.EndDate.HasValue ? NormalizeDateTime(request.EndDate.Value) : (DateTime?)null;
  263. item.PremiereDate = request.PremiereDate.HasValue ? NormalizeDateTime(request.PremiereDate.Value) : (DateTime?)null;
  264. item.ProductionYear = request.ProductionYear;
  265. item.OfficialRating = request.OfficialRating;
  266. item.CustomRating = request.CustomRating;
  267. SetProductionLocations(item, request);
  268. item.PreferredMetadataCountryCode = request.PreferredMetadataCountryCode;
  269. item.PreferredMetadataLanguage = request.PreferredMetadataLanguage;
  270. var hasDisplayOrder = item as IHasDisplayOrder;
  271. if (hasDisplayOrder != null)
  272. {
  273. hasDisplayOrder.DisplayOrder = request.DisplayOrder;
  274. }
  275. var hasAspectRatio = item as IHasAspectRatio;
  276. if (hasAspectRatio != null)
  277. {
  278. hasAspectRatio.AspectRatio = request.AspectRatio;
  279. }
  280. item.IsLocked = request.LockData ?? false;
  281. if (request.LockedFields != null)
  282. {
  283. item.LockedFields = request.LockedFields;
  284. }
  285. // Only allow this for series. Runtimes for media comes from ffprobe.
  286. if (item is Series)
  287. {
  288. item.RunTimeTicks = request.RunTimeTicks;
  289. }
  290. foreach (var pair in request.ProviderIds.ToList())
  291. {
  292. if (string.IsNullOrEmpty(pair.Value))
  293. {
  294. request.ProviderIds.Remove(pair.Key);
  295. }
  296. }
  297. item.ProviderIds = request.ProviderIds;
  298. var video = item as Video;
  299. if (video != null)
  300. {
  301. video.Video3DFormat = request.Video3DFormat;
  302. }
  303. var hasMetascore = item as IHasMetascore;
  304. if (hasMetascore != null)
  305. {
  306. hasMetascore.Metascore = request.Metascore;
  307. }
  308. var hasAwards = item as IHasAwards;
  309. if (hasAwards != null)
  310. {
  311. hasAwards.AwardSummary = request.AwardSummary;
  312. }
  313. var game = item as Game;
  314. if (game != null)
  315. {
  316. game.PlayersSupported = request.Players;
  317. }
  318. if (request.AlbumArtists != null)
  319. {
  320. var hasAlbumArtists = item as IHasAlbumArtist;
  321. if (hasAlbumArtists != null)
  322. {
  323. hasAlbumArtists.AlbumArtists = request
  324. .AlbumArtists
  325. .Select(i => i.Name)
  326. .ToList();
  327. }
  328. }
  329. if (request.ArtistItems != null)
  330. {
  331. var hasArtists = item as IHasArtist;
  332. if (hasArtists != null)
  333. {
  334. hasArtists.Artists = request
  335. .ArtistItems
  336. .Select(i => i.Name)
  337. .ToList();
  338. }
  339. }
  340. var song = item as Audio;
  341. if (song != null)
  342. {
  343. song.Album = request.Album;
  344. }
  345. var musicVideo = item as MusicVideo;
  346. if (musicVideo != null)
  347. {
  348. musicVideo.Album = request.Album;
  349. }
  350. var series = item as Series;
  351. if (series != null)
  352. {
  353. series.Status = request.SeriesStatus;
  354. series.AirDays = request.AirDays;
  355. series.AirTime = request.AirTime;
  356. }
  357. }
  358. private void SetProductionLocations(BaseItem item, BaseItemDto request)
  359. {
  360. var hasProductionLocations = item as IHasProductionLocations;
  361. if (hasProductionLocations != null)
  362. {
  363. hasProductionLocations.ProductionLocations = request.ProductionLocations;
  364. }
  365. var person = item as Person;
  366. if (person != null)
  367. {
  368. person.PlaceOfBirth = request.ProductionLocations == null
  369. ? null
  370. : request.ProductionLocations.FirstOrDefault();
  371. }
  372. }
  373. }
  374. }