ItemUpdateService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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]
  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 collectionType = _libraryManager.GetInheritedContentType(item);
  71. if (string.IsNullOrWhiteSpace(collectionType))
  72. {
  73. info.ContentTypeOptions = GetContentTypeOptions(true);
  74. info.ContentType = _libraryManager.GetContentType(item);
  75. }
  76. }
  77. }
  78. return ToOptimizedResult(info);
  79. }
  80. public void Post(UpdateItemContentType request)
  81. {
  82. var item = _libraryManager.GetItemById(request.ItemId);
  83. var path = item.ContainingFolderPath;
  84. var types = _config.Configuration.ContentTypes
  85. .Where(i => !string.Equals(i.Name, path, StringComparison.OrdinalIgnoreCase))
  86. .ToList();
  87. if (!string.IsNullOrWhiteSpace(request.ContentType))
  88. {
  89. types.Add(new NameValuePair
  90. {
  91. Name = path,
  92. Value = request.ContentType
  93. });
  94. }
  95. _config.Configuration.ContentTypes = types.ToArray();
  96. _config.SaveConfiguration();
  97. }
  98. private List<NameValuePair> GetContentTypeOptions(bool isForItem)
  99. {
  100. var list = new List<NameValuePair>();
  101. if (isForItem)
  102. {
  103. list.Add(new NameValuePair
  104. {
  105. Name = "FolderTypeInherit",
  106. Value = ""
  107. });
  108. }
  109. list.Add(new NameValuePair
  110. {
  111. Name = "FolderTypeMovies",
  112. Value = "movies"
  113. });
  114. list.Add(new NameValuePair
  115. {
  116. Name = "FolderTypeMusic",
  117. Value = "music"
  118. });
  119. list.Add(new NameValuePair
  120. {
  121. Name = "FolderTypeTvShows",
  122. Value = "tvshows"
  123. });
  124. if (!isForItem)
  125. {
  126. list.Add(new NameValuePair
  127. {
  128. Name = "FolderTypeBooks",
  129. Value = "books"
  130. });
  131. list.Add(new NameValuePair
  132. {
  133. Name = "FolderTypeGames",
  134. Value = "games"
  135. });
  136. }
  137. list.Add(new NameValuePair
  138. {
  139. Name = "FolderTypeHomeVideos",
  140. Value = "homevideos"
  141. });
  142. list.Add(new NameValuePair
  143. {
  144. Name = "FolderTypeMusicVideos",
  145. Value = "musicvideos"
  146. });
  147. list.Add(new NameValuePair
  148. {
  149. Name = "FolderTypePhotos",
  150. Value = "photos"
  151. });
  152. if (!isForItem)
  153. {
  154. list.Add(new NameValuePair
  155. {
  156. Name = "FolderTypeMixed",
  157. Value = ""
  158. });
  159. }
  160. foreach (var val in list)
  161. {
  162. val.Name = _localizationManager.GetLocalizedString(val.Name);
  163. }
  164. return list;
  165. }
  166. public void Post(UpdateItem request)
  167. {
  168. var task = UpdateItem(request);
  169. Task.WaitAll(task);
  170. }
  171. private async Task UpdateItem(UpdateItem request)
  172. {
  173. var item = _libraryManager.GetItemById(request.ItemId);
  174. var newLockData = request.LockData ?? false;
  175. var isLockedChanged = item.IsLocked != newLockData;
  176. UpdateItem(request, item);
  177. if (isLockedChanged && item.IsLocked)
  178. {
  179. item.IsUnidentified = false;
  180. }
  181. await item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
  182. if (isLockedChanged && item.IsFolder)
  183. {
  184. var folder = (Folder)item;
  185. foreach (var child in folder.RecursiveChildren.ToList())
  186. {
  187. child.IsLocked = newLockData;
  188. await child.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
  189. }
  190. }
  191. }
  192. private DateTime NormalizeDateTime(DateTime val)
  193. {
  194. return DateTime.SpecifyKind(val, DateTimeKind.Utc);
  195. }
  196. private void UpdateItem(BaseItemDto request, BaseItem item)
  197. {
  198. item.Name = request.Name;
  199. item.ForcedSortName = request.ForcedSortName;
  200. var hasBudget = item as IHasBudget;
  201. if (hasBudget != null)
  202. {
  203. hasBudget.Budget = request.Budget;
  204. hasBudget.Revenue = request.Revenue;
  205. }
  206. var hasCriticRating = item as IHasCriticRating;
  207. if (hasCriticRating != null)
  208. {
  209. hasCriticRating.CriticRating = request.CriticRating;
  210. hasCriticRating.CriticRatingSummary = request.CriticRatingSummary;
  211. }
  212. item.DisplayMediaType = request.DisplayMediaType;
  213. item.CommunityRating = request.CommunityRating;
  214. item.VoteCount = request.VoteCount;
  215. item.HomePageUrl = request.HomePageUrl;
  216. item.IndexNumber = request.IndexNumber;
  217. item.ParentIndexNumber = request.ParentIndexNumber;
  218. item.Overview = request.Overview;
  219. item.Genres = request.Genres;
  220. var episode = item as Episode;
  221. if (episode != null)
  222. {
  223. episode.DvdSeasonNumber = request.DvdSeasonNumber;
  224. episode.DvdEpisodeNumber = request.DvdEpisodeNumber;
  225. episode.AirsAfterSeasonNumber = request.AirsAfterSeasonNumber;
  226. episode.AirsBeforeEpisodeNumber = request.AirsBeforeEpisodeNumber;
  227. episode.AirsBeforeSeasonNumber = request.AirsBeforeSeasonNumber;
  228. episode.AbsoluteEpisodeNumber = request.AbsoluteEpisodeNumber;
  229. }
  230. var hasTags = item as IHasTags;
  231. if (hasTags != null)
  232. {
  233. hasTags.Tags = request.Tags;
  234. }
  235. var hasTaglines = item as IHasTaglines;
  236. if (hasTaglines != null)
  237. {
  238. hasTaglines.Taglines = request.Taglines;
  239. }
  240. var hasShortOverview = item as IHasShortOverview;
  241. if (hasShortOverview != null)
  242. {
  243. hasShortOverview.ShortOverview = request.ShortOverview;
  244. }
  245. var hasKeywords = item as IHasKeywords;
  246. if (hasKeywords != null)
  247. {
  248. hasKeywords.Keywords = request.Keywords;
  249. }
  250. if (request.Studios != null)
  251. {
  252. item.Studios = request.Studios.Select(x => x.Name).ToList();
  253. }
  254. if (request.People != null)
  255. {
  256. item.People = request.People.Select(x => new PersonInfo { Name = x.Name, Role = x.Role, Type = x.Type }).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. var hasLang = item as IHasPreferredMetadataLanguage;
  269. if (hasLang != null)
  270. {
  271. hasLang.PreferredMetadataCountryCode = request.PreferredMetadataCountryCode;
  272. hasLang.PreferredMetadataLanguage = request.PreferredMetadataLanguage;
  273. }
  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. var song = item as Audio;
  323. if (song != null)
  324. {
  325. song.Album = request.Album;
  326. song.AlbumArtists = string.IsNullOrWhiteSpace(request.AlbumArtist) ? new List<string>() : new List<string> { request.AlbumArtist };
  327. song.Artists = request.Artists.ToList();
  328. }
  329. var musicVideo = item as MusicVideo;
  330. if (musicVideo != null)
  331. {
  332. musicVideo.Artists = request.Artists.ToList();
  333. musicVideo.Album = request.Album;
  334. }
  335. var series = item as Series;
  336. if (series != null)
  337. {
  338. series.Status = request.Status;
  339. series.AirDays = request.AirDays;
  340. series.AirTime = request.AirTime;
  341. if (request.DisplaySpecialsWithSeasons.HasValue)
  342. {
  343. series.DisplaySpecialsWithSeasons = request.DisplaySpecialsWithSeasons.Value;
  344. }
  345. }
  346. }
  347. private void SetProductionLocations(BaseItem item, BaseItemDto request)
  348. {
  349. var hasProductionLocations = item as IHasProductionLocations;
  350. if (hasProductionLocations != null)
  351. {
  352. hasProductionLocations.ProductionLocations = request.ProductionLocations;
  353. }
  354. var person = item as Person;
  355. if (person != null)
  356. {
  357. person.PlaceOfBirth = request.ProductionLocations == null
  358. ? null
  359. : request.ProductionLocations.FirstOrDefault();
  360. }
  361. }
  362. }
  363. }