jellyfin/MediaBrowser.Api/ItemUpdateService.cs

305 lines
10 KiB
C#
Raw Normal View History

2014-07-02 11:34:08 -07:00
using MediaBrowser.Controller.Entities;
2013-06-27 12:06:37 -07:00
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
2014-12-20 22:57:06 -07:00
using MediaBrowser.Controller.Localization;
2014-07-02 11:34:08 -07:00
using MediaBrowser.Controller.Net;
2014-12-20 22:57:06 -07:00
using MediaBrowser.Controller.Providers;
2013-06-27 12:06:37 -07:00
using MediaBrowser.Model.Dto;
2013-12-07 08:52:38 -07:00
using ServiceStack;
2013-06-27 12:29:58 -07:00
using System;
2014-07-02 11:34:08 -07:00
using System.Collections.Generic;
2013-06-27 12:29:58 -07:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2013-06-27 12:06:37 -07:00
namespace MediaBrowser.Api
{
2014-04-26 20:42:05 -07:00
[Route("/Items/{ItemId}", "POST", Summary = "Updates an item")]
2013-06-27 12:06:37 -07:00
public class UpdateItem : BaseItemDto, IReturnVoid
{
[ApiMember(Name = "ItemId", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string ItemId { get; set; }
}
2014-12-20 22:57:06 -07:00
[Route("/Items/{ItemId}/MetadataEditor", "GET", Summary = "Gets metadata editor info for an item")]
public class GetMetadataEditorInfo : IReturn<MetadataEditorInfo>
{
[ApiMember(Name = "ItemId", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string ItemId { get; set; }
}
2014-07-02 11:34:08 -07:00
[Authenticated]
2013-06-27 12:06:37 -07:00
public class ItemUpdateService : BaseApiService
{
private readonly ILibraryManager _libraryManager;
2014-12-20 22:57:06 -07:00
private readonly IProviderManager _providerManager;
private readonly ILocalizationManager _localizationManager;
2013-06-27 12:06:37 -07:00
2014-12-20 22:57:06 -07:00
public ItemUpdateService(ILibraryManager libraryManager, IProviderManager providerManager, ILocalizationManager localizationManager)
2013-06-27 12:06:37 -07:00
{
_libraryManager = libraryManager;
2014-12-20 22:57:06 -07:00
_providerManager = providerManager;
_localizationManager = localizationManager;
}
public object Get(GetMetadataEditorInfo request)
{
var item = _libraryManager.GetItemById(request.ItemId);
var info = new MetadataEditorInfo
{
ParentalRatingOptions = _localizationManager.GetParentalRatings().ToList(),
ExternalIdInfos = _providerManager.GetExternalIdInfos(item).ToList(),
Countries = _localizationManager.GetCountries().ToList(),
Cultures = _localizationManager.GetCultures().ToList()
};
return ToOptimizedResult(info);
2013-06-27 12:06:37 -07:00
}
public void Post(UpdateItem request)
{
var task = UpdateItem(request);
Task.WaitAll(task);
}
2013-11-30 23:25:05 -07:00
private async Task UpdateItem(UpdateItem request)
2013-06-27 12:06:37 -07:00
{
2014-04-25 13:15:50 -07:00
var item = _libraryManager.GetItemById(request.ItemId);
2013-06-27 12:06:37 -07:00
2014-02-18 22:21:03 -07:00
var newLockData = request.LockData ?? false;
2014-04-26 20:42:05 -07:00
var isLockedChanged = item.IsLocked != newLockData;
2013-11-30 23:25:05 -07:00
2013-06-27 12:06:37 -07:00
UpdateItem(request, item);
2014-04-26 20:42:05 -07:00
if (isLockedChanged && item.IsLocked)
{
item.IsUnidentified = false;
}
2014-03-09 19:33:49 -07:00
await item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
2013-11-30 23:25:05 -07:00
2014-04-26 20:42:05 -07:00
if (isLockedChanged && item.IsFolder)
2013-11-30 23:25:05 -07:00
{
var folder = (Folder)item;
foreach (var child in folder.RecursiveChildren.ToList())
{
2014-04-26 20:42:05 -07:00
child.IsLocked = newLockData;
2014-03-09 19:33:49 -07:00
await child.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
2013-11-30 23:25:05 -07:00
}
}
2013-06-27 12:06:37 -07:00
}
2014-07-12 21:55:56 -07:00
private DateTime NormalizeDateTime(DateTime val)
{
return DateTime.SpecifyKind(val, DateTimeKind.Utc);
}
2013-06-27 12:06:37 -07:00
private void UpdateItem(BaseItemDto request, BaseItem item)
{
item.Name = request.Name;
2014-03-24 10:54:45 -07:00
item.ForcedSortName = request.ForcedSortName;
2013-06-27 12:06:37 -07:00
2013-12-02 09:16:03 -07:00
var hasBudget = item as IHasBudget;
if (hasBudget != null)
{
hasBudget.Budget = request.Budget;
hasBudget.Revenue = request.Revenue;
}
2013-09-11 10:54:59 -07:00
2013-11-06 09:06:16 -07:00
var hasCriticRating = item as IHasCriticRating;
if (hasCriticRating != null)
{
hasCriticRating.CriticRating = request.CriticRating;
hasCriticRating.CriticRatingSummary = request.CriticRatingSummary;
}
2013-09-11 10:54:59 -07:00
item.DisplayMediaType = request.DisplayMediaType;
item.CommunityRating = request.CommunityRating;
item.VoteCount = request.VoteCount;
2013-09-11 10:54:59 -07:00
item.HomePageUrl = request.HomePageUrl;
2013-06-27 12:06:37 -07:00
item.IndexNumber = request.IndexNumber;
item.ParentIndexNumber = request.ParentIndexNumber;
item.Overview = request.Overview;
item.Genres = request.Genres;
var episode = item as Episode;
if (episode != null)
{
episode.DvdSeasonNumber = request.DvdSeasonNumber;
episode.DvdEpisodeNumber = request.DvdEpisodeNumber;
episode.AirsAfterSeasonNumber = request.AirsAfterSeasonNumber;
episode.AirsBeforeEpisodeNumber = request.AirsBeforeEpisodeNumber;
episode.AirsBeforeSeasonNumber = request.AirsBeforeSeasonNumber;
episode.AbsoluteEpisodeNumber = request.AbsoluteEpisodeNumber;
}
2014-04-26 20:42:05 -07:00
var hasTags = item as IHasTags;
if (hasTags != null)
{
hasTags.Tags = request.Tags;
}
2014-06-28 19:30:20 -07:00
var hasTaglines = item as IHasTaglines;
if (hasTaglines != null)
{
hasTaglines.Taglines = request.Taglines;
}
2014-06-23 21:18:02 -07:00
var hasShortOverview = item as IHasShortOverview;
if (hasShortOverview != null)
{
hasShortOverview.ShortOverview = request.ShortOverview;
}
2014-01-14 08:50:39 -07:00
var hasKeywords = item as IHasKeywords;
if (hasKeywords != null)
{
hasKeywords.Keywords = request.Keywords;
}
if (request.Studios != null)
{
item.Studios = request.Studios.Select(x => x.Name).ToList();
}
if (request.People != null)
{
item.People = request.People.Select(x => new PersonInfo { Name = x.Name, Role = x.Role, Type = x.Type }).ToList();
}
2013-06-27 12:06:37 -07:00
if (request.DateCreated.HasValue)
{
2014-07-12 21:55:56 -07:00
item.DateCreated = NormalizeDateTime(request.DateCreated.Value);
}
2014-07-12 21:55:56 -07:00
item.EndDate = request.EndDate.HasValue ? NormalizeDateTime(request.EndDate.Value) : (DateTime?)null;
item.PremiereDate = request.PremiereDate.HasValue ? NormalizeDateTime(request.PremiereDate.Value) : (DateTime?)null;
2013-06-27 12:06:37 -07:00
item.ProductionYear = request.ProductionYear;
item.OfficialRating = request.OfficialRating;
item.CustomRating = request.CustomRating;
SetProductionLocations(item, request);
2013-12-28 09:58:13 -07:00
var hasLang = item as IHasPreferredMetadataLanguage;
if (hasLang != null)
{
2013-12-28 09:58:13 -07:00
hasLang.PreferredMetadataCountryCode = request.PreferredMetadataCountryCode;
hasLang.PreferredMetadataLanguage = request.PreferredMetadataLanguage;
}
var hasDisplayOrder = item as IHasDisplayOrder;
if (hasDisplayOrder != null)
{
hasDisplayOrder.DisplayOrder = request.DisplayOrder;
}
2014-04-26 20:42:05 -07:00
var hasAspectRatio = item as IHasAspectRatio;
if (hasAspectRatio != null)
{
hasAspectRatio.AspectRatio = request.AspectRatio;
}
2014-02-18 22:21:03 -07:00
2014-04-26 20:42:05 -07:00
item.IsLocked = (request.LockData ?? false);
2014-02-18 22:21:03 -07:00
if (request.LockedFields != null)
2013-06-27 12:06:37 -07:00
{
item.LockedFields = request.LockedFields;
}
// Only allow this for series. Runtimes for media comes from ffprobe.
if (item is Series)
{
item.RunTimeTicks = request.RunTimeTicks;
}
2013-06-27 12:06:37 -07:00
foreach (var pair in request.ProviderIds.ToList())
{
if (string.IsNullOrEmpty(pair.Value))
{
request.ProviderIds.Remove(pair.Key);
}
}
item.ProviderIds = request.ProviderIds;
var video = item as Video;
if (video != null)
{
video.Video3DFormat = request.Video3DFormat;
}
2014-01-14 22:01:58 -07:00
var hasMetascore = item as IHasMetascore;
if (hasMetascore != null)
{
hasMetascore.Metascore = request.Metascore;
}
var hasAwards = item as IHasAwards;
if (hasAwards != null)
{
hasAwards.AwardSummary = request.AwardSummary;
}
2013-06-27 12:06:37 -07:00
var game = item as Game;
if (game != null)
{
game.PlayersSupported = request.Players;
}
var song = item as Audio;
if (song != null)
{
song.Album = request.Album;
2014-06-23 09:05:19 -07:00
song.AlbumArtists = string.IsNullOrWhiteSpace(request.AlbumArtist) ? new List<string>() : new List<string> { request.AlbumArtist };
song.Artists = request.Artists.ToList();
2013-06-27 12:06:37 -07:00
}
var musicVideo = item as MusicVideo;
if (musicVideo != null)
{
2014-10-20 13:23:40 -07:00
musicVideo.Artists = request.Artists.ToList();
musicVideo.Album = request.Album;
}
2013-06-27 12:06:37 -07:00
var series = item as Series;
if (series != null)
{
series.Status = request.Status;
series.AirDays = request.AirDays;
series.AirTime = request.AirTime;
if (request.DisplaySpecialsWithSeasons.HasValue)
{
series.DisplaySpecialsWithSeasons = request.DisplaySpecialsWithSeasons.Value;
}
2013-06-27 12:06:37 -07:00
}
}
private void SetProductionLocations(BaseItem item, BaseItemDto request)
{
var hasProductionLocations = item as IHasProductionLocations;
if (hasProductionLocations != null)
{
hasProductionLocations.ProductionLocations = request.ProductionLocations;
}
var person = item as Person;
if (person != null)
{
person.PlaceOfBirth = request.ProductionLocations == null
? null
: request.ProductionLocations.FirstOrDefault();
}
}
2013-06-27 12:06:37 -07:00
}
}