jellyfin-server/MediaBrowser.Api/ItemUpdateService.cs

415 lines
14 KiB
C#
Raw Normal View History

2014-12-22 06:50:29 +00:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
2013-06-27 19:06:37 +00:00
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
2014-12-23 03:58:14 +00:00
using MediaBrowser.Controller.LiveTv;
2014-12-21 05:57:06 +00:00
using MediaBrowser.Controller.Localization;
2014-07-02 18:34:08 +00:00
using MediaBrowser.Controller.Net;
2014-12-21 05:57:06 +00:00
using MediaBrowser.Controller.Providers;
2013-06-27 19:06:37 +00:00
using MediaBrowser.Model.Dto;
2014-12-21 18:58:17 +00:00
using MediaBrowser.Model.Entities;
2013-12-07 15:52:38 +00:00
using ServiceStack;
2013-06-27 19:29:58 +00:00
using System;
2014-07-02 18:34:08 +00:00
using System.Collections.Generic;
2013-06-27 19:29:58 +00:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2013-06-27 19:06:37 +00:00
namespace MediaBrowser.Api
{
2014-04-27 03:42:05 +00:00
[Route("/Items/{ItemId}", "POST", Summary = "Updates an item")]
2013-06-27 19:06:37 +00: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-21 05:57:06 +00: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-12-21 18:58:17 +00:00
[Route("/Items/{ItemId}/ContentType", "POST", Summary = "Updates an item's content type")]
public class UpdateItemContentType : IReturnVoid
{
[ApiMember(Name = "ItemId", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string ItemId { get; set; }
[ApiMember(Name = "ContentType", Description = "The content type of the item", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ContentType { get; set; }
}
2015-03-16 01:48:25 +00:00
[Authenticated(Roles = "admin")]
2013-06-27 19:06:37 +00:00
public class ItemUpdateService : BaseApiService
{
private readonly ILibraryManager _libraryManager;
2014-12-21 05:57:06 +00:00
private readonly IProviderManager _providerManager;
private readonly ILocalizationManager _localizationManager;
2014-12-22 06:50:29 +00:00
private readonly IServerConfigurationManager _config;
2013-06-27 19:06:37 +00:00
2014-12-22 06:50:29 +00:00
public ItemUpdateService(ILibraryManager libraryManager, IProviderManager providerManager, ILocalizationManager localizationManager, IServerConfigurationManager config)
2013-06-27 19:06:37 +00:00
{
_libraryManager = libraryManager;
2014-12-21 05:57:06 +00:00
_providerManager = providerManager;
_localizationManager = localizationManager;
2014-12-22 06:50:29 +00:00
_config = config;
2014-12-21 05:57:06 +00:00
}
public object Get(GetMetadataEditorInfo request)
{
var item = _libraryManager.GetItemById(request.ItemId);
2015-03-16 01:48:25 +00:00
2014-12-21 05:57:06 +00:00
var info = new MetadataEditorInfo
{
ParentalRatingOptions = _localizationManager.GetParentalRatings().ToList(),
ExternalIdInfos = _providerManager.GetExternalIdInfos(item).ToList(),
Countries = _localizationManager.GetCountries().ToList(),
Cultures = _localizationManager.GetCultures().ToList()
};
2016-09-03 17:16:36 +00:00
if (!item.IsVirtualItem && !(item is ICollectionFolder) && !(item is UserView) && !(item is AggregateFolder) && !(item is LiveTvChannel) && !(item is IItemByName) &&
item.SourceType == SourceType.Library)
2014-12-21 18:58:17 +00:00
{
var inheritedContentType = _libraryManager.GetInheritedContentType(item);
var configuredContentType = _libraryManager.GetConfiguredContentType(item);
2016-09-03 17:16:36 +00:00
if (string.IsNullOrWhiteSpace(inheritedContentType) || !string.IsNullOrWhiteSpace(configuredContentType))
2014-12-21 18:58:17 +00:00
{
info.ContentTypeOptions = GetContentTypeOptions(true);
info.ContentType = configuredContentType;
2015-01-10 01:38:01 +00:00
if (string.Equals(inheritedContentType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
2014-12-22 06:50:29 +00:00
{
info.ContentTypeOptions = info.ContentTypeOptions
.Where(i => string.IsNullOrWhiteSpace(i.Value) || string.Equals(i.Value, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
.ToList();
2014-12-22 06:50:29 +00:00
}
2014-12-21 18:58:17 +00:00
}
}
2014-12-21 05:57:06 +00:00
return ToOptimizedResult(info);
2013-06-27 19:06:37 +00:00
}
2014-12-21 18:58:17 +00:00
public void Post(UpdateItemContentType request)
{
2014-12-22 06:50:29 +00:00
var item = _libraryManager.GetItemById(request.ItemId);
var path = item.ContainingFolderPath;
var types = _config.Configuration.ContentTypes
.Where(i => !string.Equals(i.Name, path, StringComparison.OrdinalIgnoreCase))
.ToList();
if (!string.IsNullOrWhiteSpace(request.ContentType))
{
types.Add(new NameValuePair
{
Name = path,
Value = request.ContentType
});
}
_config.Configuration.ContentTypes = types.ToArray();
_config.SaveConfiguration();
2014-12-21 18:58:17 +00:00
}
private List<NameValuePair> GetContentTypeOptions(bool isForItem)
{
var list = new List<NameValuePair>();
if (isForItem)
{
list.Add(new NameValuePair
{
Name = "FolderTypeInherit",
Value = ""
});
}
2015-03-16 01:48:25 +00:00
2014-12-21 18:58:17 +00:00
list.Add(new NameValuePair
{
Name = "FolderTypeMovies",
Value = "movies"
});
list.Add(new NameValuePair
{
Name = "FolderTypeMusic",
Value = "music"
});
list.Add(new NameValuePair
{
Name = "FolderTypeTvShows",
Value = "tvshows"
});
if (!isForItem)
{
list.Add(new NameValuePair
{
Name = "FolderTypeBooks",
Value = "books"
});
list.Add(new NameValuePair
{
Name = "FolderTypeGames",
Value = "games"
});
}
list.Add(new NameValuePair
{
Name = "FolderTypeHomeVideos",
Value = "homevideos"
});
list.Add(new NameValuePair
{
Name = "FolderTypeMusicVideos",
Value = "musicvideos"
});
list.Add(new NameValuePair
{
Name = "FolderTypePhotos",
Value = "photos"
});
if (!isForItem)
{
list.Add(new NameValuePair
{
Name = "FolderTypeMixed",
Value = ""
});
}
foreach (var val in list)
{
val.Name = _localizationManager.GetLocalizedString(val.Name);
}
return list;
}
2013-06-27 19:06:37 +00:00
public void Post(UpdateItem request)
{
var task = UpdateItem(request);
Task.WaitAll(task);
}
2013-12-01 06:25:05 +00:00
private async Task UpdateItem(UpdateItem request)
2013-06-27 19:06:37 +00:00
{
2014-04-25 20:15:50 +00:00
var item = _libraryManager.GetItemById(request.ItemId);
2013-06-27 19:06:37 +00:00
2014-02-19 05:21:03 +00:00
var newLockData = request.LockData ?? false;
2014-04-27 03:42:05 +00:00
var isLockedChanged = item.IsLocked != newLockData;
2013-12-01 06:25:05 +00:00
2013-06-27 19:06:37 +00:00
UpdateItem(request, item);
2014-03-10 02:33:49 +00:00
await item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
2013-12-01 06:25:05 +00:00
2015-06-21 03:35:22 +00:00
if (request.People != null)
{
await _libraryManager.UpdatePeople(item, request.People.Select(x => new PersonInfo { Name = x.Name, Role = x.Role, Type = x.Type }).ToList());
}
2014-04-27 03:42:05 +00:00
if (isLockedChanged && item.IsFolder)
2013-12-01 06:25:05 +00:00
{
var folder = (Folder)item;
2015-01-25 06:34:50 +00:00
foreach (var child in folder.GetRecursiveChildren())
2013-12-01 06:25:05 +00:00
{
2014-04-27 03:42:05 +00:00
child.IsLocked = newLockData;
2014-03-10 02:33:49 +00:00
await child.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
2013-12-01 06:25:05 +00:00
}
}
2013-06-27 19:06:37 +00:00
}
2014-07-13 04:55:56 +00:00
private DateTime NormalizeDateTime(DateTime val)
{
return DateTime.SpecifyKind(val, DateTimeKind.Utc);
}
2013-06-27 19:06:37 +00:00
private void UpdateItem(BaseItemDto request, BaseItem item)
{
item.Name = request.Name;
2014-03-24 17:54:45 +00:00
item.ForcedSortName = request.ForcedSortName;
2013-06-27 19:06:37 +00:00
2013-12-02 16:16:03 +00:00
var hasBudget = item as IHasBudget;
if (hasBudget != null)
{
hasBudget.Budget = request.Budget;
hasBudget.Revenue = request.Revenue;
}
2013-09-11 17:54:59 +00:00
2016-10-01 07:06:00 +00:00
item.OriginalTitle = string.IsNullOrWhiteSpace(request.OriginalTitle) ? null : request.OriginalTitle;
2016-04-20 05:21:40 +00:00
2013-11-06 16:06:16 +00:00
var hasCriticRating = item as IHasCriticRating;
if (hasCriticRating != null)
{
hasCriticRating.CriticRating = request.CriticRating;
hasCriticRating.CriticRatingSummary = request.CriticRatingSummary;
}
2013-09-11 17:54:59 +00:00
item.DisplayMediaType = request.DisplayMediaType;
item.CommunityRating = request.CommunityRating;
item.VoteCount = request.VoteCount;
2013-09-11 17:54:59 +00:00
item.HomePageUrl = request.HomePageUrl;
2013-06-27 19:06:37 +00: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-27 03:42:05 +00:00
2016-06-02 17:43:29 +00:00
item.Tags = request.Tags;
2016-10-08 05:57:38 +00:00
if (request.Taglines != null)
2014-06-29 02:30:20 +00:00
{
2016-10-08 05:57:38 +00:00
item.Tagline = request.Taglines.FirstOrDefault();
2014-06-29 02:30:20 +00:00
}
2014-06-24 04:18:02 +00:00
var hasShortOverview = item as IHasShortOverview;
if (hasShortOverview != null)
{
hasShortOverview.ShortOverview = request.ShortOverview;
}
item.Keywords = request.Keywords;
2014-01-14 15:50:39 +00:00
if (request.Studios != null)
{
item.Studios = request.Studios.Select(x => x.Name).ToList();
}
if (request.DateCreated.HasValue)
{
2014-07-13 04:55:56 +00:00
item.DateCreated = NormalizeDateTime(request.DateCreated.Value);
}
2014-07-13 04:55:56 +00: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 19:06:37 +00:00
item.ProductionYear = request.ProductionYear;
2016-08-26 17:24:04 +00:00
item.OfficialRating = string.IsNullOrWhiteSpace(request.OfficialRating) ? null : request.OfficialRating;
2013-06-27 19:06:37 +00:00
item.CustomRating = request.CustomRating;
2015-09-29 17:35:23 +00:00
item.PreferredMetadataCountryCode = request.PreferredMetadataCountryCode;
item.PreferredMetadataLanguage = request.PreferredMetadataLanguage;
var hasDisplayOrder = item as IHasDisplayOrder;
if (hasDisplayOrder != null)
{
hasDisplayOrder.DisplayOrder = request.DisplayOrder;
}
2014-04-27 03:42:05 +00:00
var hasAspectRatio = item as IHasAspectRatio;
if (hasAspectRatio != null)
{
hasAspectRatio.AspectRatio = request.AspectRatio;
}
2014-02-19 05:21:03 +00:00
2016-03-27 21:11:27 +00:00
item.IsLocked = request.LockData ?? false;
2014-02-19 05:21:03 +00:00
if (request.LockedFields != null)
2013-06-27 19:06:37 +00: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 19:06:37 +00: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-15 05:01:58 +00: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 19:06:37 +00:00
var game = item as Game;
if (game != null)
{
game.PlayersSupported = request.Players;
}
2015-05-18 22:23:03 +00:00
if (request.AlbumArtists != null)
2013-06-27 19:06:37 +00:00
{
2015-05-18 22:23:03 +00:00
var hasAlbumArtists = item as IHasAlbumArtist;
if (hasAlbumArtists != null)
{
hasAlbumArtists.AlbumArtists = request
.AlbumArtists
.Select(i => i.Name)
.ToList();
}
2013-06-27 19:06:37 +00:00
}
2015-05-18 22:23:03 +00:00
if (request.ArtistItems != null)
2015-03-13 17:25:28 +00:00
{
2015-05-18 22:23:03 +00:00
var hasArtists = item as IHasArtist;
if (hasArtists != null)
{
hasArtists.Artists = request
.ArtistItems
.Select(i => i.Name)
.ToList();
}
2015-03-13 17:25:28 +00:00
}
2015-03-16 01:48:25 +00:00
2015-03-13 17:25:28 +00:00
var song = item as Audio;
if (song != null)
{
song.Album = request.Album;
}
2015-03-13 17:25:28 +00:00
var musicVideo = item as MusicVideo;
if (musicVideo != null)
{
musicVideo.Album = request.Album;
}
2013-06-27 19:06:37 +00:00
var series = item as Series;
if (series != null)
{
2015-05-31 18:22:51 +00:00
series.Status = request.SeriesStatus;
2013-06-27 19:06:37 +00:00
series.AirDays = request.AirDays;
series.AirTime = request.AirTime;
}
}
}
}