2020-06-19 08:00:50 -07:00
using System ;
2019-01-13 12:24:58 -07:00
using System.Collections.Generic ;
using System.Linq ;
using System.Threading ;
2020-06-08 11:20:33 -07:00
using Jellyfin.Api.Constants ;
2019-01-13 12:24:58 -07:00
using MediaBrowser.Controller.Configuration ;
2014-12-21 23:50:29 -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-22 20:58:14 -07:00
using MediaBrowser.Controller.LiveTv ;
2014-12-20 22:57:06 -07:00
using MediaBrowser.Controller.Providers ;
2013-06-27 12:06:37 -07:00
using MediaBrowser.Model.Dto ;
2014-12-21 11:58:17 -07:00
using MediaBrowser.Model.Entities ;
2016-10-23 19:45:23 -07:00
using MediaBrowser.Model.Globalization ;
2018-09-12 10:26:21 -07:00
using MediaBrowser.Model.IO ;
2020-06-08 11:20:33 -07:00
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.AspNetCore.Mvc.ModelBinding ;
2013-06-27 12:06:37 -07:00
2020-06-08 11:20:33 -07:00
namespace Jellyfin.Api.Controllers
2013-06-27 12:06:37 -07:00
{
2020-06-08 11:20:33 -07:00
/// <summary>
/// Item update controller.
/// </summary>
[Authorize(Policy = Policies.RequiresElevation)]
public class ItemUpdateController : BaseJellyfinApiController
2013-06-27 12:06:37 -07:00
{
private readonly ILibraryManager _libraryManager ;
2014-12-20 22:57:06 -07:00
private readonly IProviderManager _providerManager ;
private readonly ILocalizationManager _localizationManager ;
2018-09-12 10:26:21 -07:00
private readonly IFileSystem _fileSystem ;
2020-06-08 11:20:33 -07:00
private readonly IServerConfigurationManager _serverConfigurationManager ;
/// <summary>
/// Initializes a new instance of the <see cref="ItemUpdateController"/> class.
/// </summary>
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
/// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param>
/// <param name="localizationManager">Instance of the <see cref="ILocalizationManager"/> interface.</param>
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
public ItemUpdateController (
2019-11-17 15:05:39 -07:00
IFileSystem fileSystem ,
ILibraryManager libraryManager ,
IProviderManager providerManager ,
2020-06-08 11:20:33 -07:00
ILocalizationManager localizationManager ,
IServerConfigurationManager serverConfigurationManager )
2013-06-27 12:06:37 -07:00
{
_libraryManager = libraryManager ;
2014-12-20 22:57:06 -07:00
_providerManager = providerManager ;
_localizationManager = localizationManager ;
2018-09-12 10:26:21 -07:00
_fileSystem = fileSystem ;
2020-06-08 11:20:33 -07:00
_serverConfigurationManager = serverConfigurationManager ;
2014-12-20 22:57:06 -07:00
}
2020-06-08 11:20:33 -07:00
/// <summary>
/// Updates an item.
/// </summary>
/// <param name="itemId">The item id.</param>
/// <param name="request">The new item properties.</param>
2020-06-20 14:14:43 -07:00
/// <response code="204">Item updated.</response>
2020-06-08 11:20:33 -07:00
/// <response code="404">Item not found.</response>
2020-06-20 14:14:43 -07:00
/// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the item could not be found.</returns>
2020-06-08 11:20:33 -07:00
[HttpPost("/Items/{itemId}")]
2020-06-20 14:15:59 -07:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-06-08 11:20:33 -07:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult UpdateItem ( [ FromRoute ] Guid itemId , [ FromBody , BindRequired ] BaseItemDto request )
2014-12-20 22:57:06 -07:00
{
2020-06-08 11:20:33 -07:00
var item = _libraryManager . GetItemById ( itemId ) ;
if ( item = = null )
2014-12-21 11:58:17 -07:00
{
2020-06-08 11:20:33 -07:00
return NotFound ( ) ;
2014-12-21 11:58:17 -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
2018-09-12 10:26:21 -07:00
var series = item as Series ;
2020-06-08 11:20:33 -07:00
var displayOrderChanged = series ! = null & & ! string . Equals (
series . DisplayOrder ? ? string . Empty ,
request . DisplayOrder ? ? string . Empty ,
StringComparison . OrdinalIgnoreCase ) ;
2018-09-12 10:26:21 -07:00
2017-06-05 23:13:49 -07:00
// Do this first so that metadata savers can pull the updates from the database.
2015-06-20 20:35:22 -07:00
if ( request . People ! = null )
{
2020-06-08 11:20:33 -07:00
_libraryManager . UpdatePeople (
item ,
request . People . Select ( x = > new PersonInfo
{
Name = x . Name ,
Role = x . Role ,
Type = x . Type
} ) . ToList ( ) ) ;
2015-06-20 20:35:22 -07:00
}
2017-06-05 23:13:49 -07:00
UpdateItem ( request , item ) ;
2017-09-13 11:41:48 -07:00
item . OnMetadataChanged ( ) ;
2017-10-03 11:39:37 -07:00
item . UpdateToRepository ( ItemUpdateType . MetadataEdit , CancellationToken . None ) ;
2017-06-05 23:13:49 -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 ;
2015-01-24 23:34:50 -07:00
foreach ( var child in folder . GetRecursiveChildren ( ) )
2013-11-30 23:25:05 -07:00
{
2014-04-26 20:42:05 -07:00
child . IsLocked = newLockData ;
2017-10-03 11:39:37 -07:00
child . UpdateToRepository ( ItemUpdateType . MetadataEdit , CancellationToken . None ) ;
2013-11-30 23:25:05 -07:00
}
}
2018-09-12 10:26:21 -07:00
if ( displayOrderChanged )
{
2019-09-10 13:37:53 -07:00
_providerManager . QueueRefresh (
2020-06-08 11:20:33 -07:00
series ! . Id ,
2019-09-10 13:37:53 -07:00
new MetadataRefreshOptions ( new DirectoryService ( _fileSystem ) )
{
MetadataRefreshMode = MetadataRefreshMode . FullRefresh ,
ImageRefreshMode = MetadataRefreshMode . FullRefresh ,
ReplaceAllMetadata = true
} ,
RefreshPriority . High ) ;
2018-09-12 10:26:21 -07:00
}
2020-06-08 11:20:33 -07:00
2020-06-20 14:14:43 -07:00
return NoContent ( ) ;
2013-06-27 12:06:37 -07:00
}
2020-06-08 11:20:33 -07:00
/// <summary>
/// Gets metadata editor info for an item.
/// </summary>
/// <param name="itemId">The item id.</param>
/// <response code="200">Item metadata editor returned.</response>
/// <response code="404">Item not found.</response>
/// <returns>An <see cref="OkResult"/> on success containing the metadata editor, or a <see cref="NotFoundResult"/> if the item could not be found.</returns>
[HttpGet("/Items/{itemId}/MetadataEditor")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult < MetadataEditorInfo > GetMetadataEditorInfo ( [ FromRoute ] Guid itemId )
2014-07-12 21:55:56 -07:00
{
2020-06-08 11:20:33 -07:00
var item = _libraryManager . GetItemById ( itemId ) ;
var info = new MetadataEditorInfo
{
ParentalRatingOptions = _localizationManager . GetParentalRatings ( ) . ToArray ( ) ,
ExternalIdInfos = _providerManager . GetExternalIdInfos ( item ) . ToArray ( ) ,
Countries = _localizationManager . GetCountries ( ) . ToArray ( ) ,
Cultures = _localizationManager . GetCultures ( ) . ToArray ( )
} ;
if ( ! item . IsVirtualItem
& & ! ( item is ICollectionFolder )
& & ! ( item is UserView )
& & ! ( item is AggregateFolder )
& & ! ( item is LiveTvChannel )
& & ! ( item is IItemByName )
& & item . SourceType = = SourceType . Library )
{
var inheritedContentType = _libraryManager . GetInheritedContentType ( item ) ;
var configuredContentType = _libraryManager . GetConfiguredContentType ( item ) ;
if ( string . IsNullOrWhiteSpace ( inheritedContentType ) | |
! string . IsNullOrWhiteSpace ( configuredContentType ) )
{
info . ContentTypeOptions = GetContentTypeOptions ( true ) . ToArray ( ) ;
info . ContentType = configuredContentType ;
if ( string . IsNullOrWhiteSpace ( inheritedContentType )
| | string . Equals ( inheritedContentType , CollectionType . TvShows , StringComparison . OrdinalIgnoreCase ) )
{
info . ContentTypeOptions = info . ContentTypeOptions
. Where ( i = > string . IsNullOrWhiteSpace ( i . Value )
| | string . Equals ( i . Value , CollectionType . TvShows , StringComparison . OrdinalIgnoreCase ) )
. ToArray ( ) ;
}
}
}
return info ;
}
/// <summary>
/// Updates an item's content type.
/// </summary>
/// <param name="itemId">The item id.</param>
/// <param name="contentType">The content type of the item.</param>
2020-06-20 14:14:43 -07:00
/// <response code="204">Item content type updated.</response>
2020-06-08 11:20:33 -07:00
/// <response code="404">Item not found.</response>
2020-06-20 14:14:43 -07:00
/// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the item could not be found.</returns>
2020-06-08 11:20:33 -07:00
[HttpPost("/Items/{itemId}/ContentType")]
2020-06-20 14:15:59 -07:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-06-27 09:50:44 -07:00
public ActionResult UpdateItemContentType ( [ FromRoute ] Guid itemId , [ FromQuery , BindRequired ] string? contentType )
2020-06-08 11:20:33 -07:00
{
var item = _libraryManager . GetItemById ( itemId ) ;
if ( item = = null )
{
return NotFound ( ) ;
}
var path = item . ContainingFolderPath ;
var types = _serverConfigurationManager . Configuration . ContentTypes
. Where ( i = > ! string . IsNullOrWhiteSpace ( i . Name ) )
. Where ( i = > ! string . Equals ( i . Name , path , StringComparison . OrdinalIgnoreCase ) )
. ToList ( ) ;
if ( ! string . IsNullOrWhiteSpace ( contentType ) )
{
types . Add ( new NameValuePair
{
Name = path ,
Value = contentType
} ) ;
}
_serverConfigurationManager . Configuration . ContentTypes = types . ToArray ( ) ;
_serverConfigurationManager . SaveConfiguration ( ) ;
2020-06-20 14:14:43 -07:00
return NoContent ( ) ;
2014-07-12 21:55:56 -07:00
}
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
2016-10-01 00:06:00 -07:00
item . OriginalTitle = string . IsNullOrWhiteSpace ( request . OriginalTitle ) ? null : request . OriginalTitle ;
2016-04-19 22:21:40 -07:00
2016-10-17 09:35:29 -07:00
item . CriticRating = request . CriticRating ;
2013-09-16 19:08:18 -07:00
2013-09-11 10:54:59 -07:00
item . CommunityRating = request . CommunityRating ;
2013-06-27 12:06:37 -07:00
item . IndexNumber = request . IndexNumber ;
item . ParentIndexNumber = request . ParentIndexNumber ;
item . Overview = request . Overview ;
item . Genres = request . Genres ;
2013-12-05 09:50:21 -07:00
2020-04-05 09:07:43 -07:00
if ( item is Episode episode )
2013-12-08 13:33:24 -07:00
{
episode . AirsAfterSeasonNumber = request . AirsAfterSeasonNumber ;
episode . AirsBeforeEpisodeNumber = request . AirsBeforeEpisodeNumber ;
episode . AirsBeforeSeasonNumber = request . AirsBeforeSeasonNumber ;
}
2014-04-26 20:42:05 -07:00
2016-06-02 10:43:29 -07:00
item . Tags = request . Tags ;
2013-11-24 16:37:38 -07:00
2016-10-07 22:57:38 -07:00
if ( request . Taglines ! = null )
2014-06-28 19:30:20 -07:00
{
2016-10-07 22:57:38 -07:00
item . Tagline = request . Taglines . FirstOrDefault ( ) ;
2014-06-28 19:30:20 -07:00
}
2013-11-24 16:37:38 -07:00
if ( request . Studios ! = null )
{
2017-08-09 12:56:38 -07:00
item . Studios = request . Studios . Select ( x = > x . Name ) . ToArray ( ) ;
2013-11-24 16:37:38 -07:00
}
2013-09-03 07:26:17 -07:00
if ( request . DateCreated . HasValue )
{
2014-07-12 21:55:56 -07:00
item . DateCreated = NormalizeDateTime ( request . DateCreated . Value ) ;
2013-09-03 07:26:17 -07:00
}
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 ;
2016-08-26 10:24:04 -07:00
item . OfficialRating = string . IsNullOrWhiteSpace ( request . OfficialRating ) ? null : request . OfficialRating ;
2013-06-27 12:06:37 -07:00
item . CustomRating = request . CustomRating ;
2013-09-03 07:26:17 -07:00
2016-10-21 11:41:49 -07:00
if ( request . ProductionLocations ! = null )
{
2017-08-09 12:56:38 -07:00
item . ProductionLocations = request . ProductionLocations ;
2016-10-21 11:41:49 -07:00
}
2015-09-29 10:35:23 -07:00
item . PreferredMetadataCountryCode = request . PreferredMetadataCountryCode ;
item . PreferredMetadataLanguage = request . PreferredMetadataLanguage ;
2014-01-03 13:32:27 -07:00
2020-04-05 09:07:43 -07:00
if ( item is IHasDisplayOrder hasDisplayOrder )
2014-01-03 13:32:27 -07:00
{
hasDisplayOrder . DisplayOrder = request . DisplayOrder ;
}
2014-04-26 20:42:05 -07:00
2020-04-05 09:07:43 -07:00
if ( item is IHasAspectRatio hasAspectRatio )
2013-11-19 20:15:48 -07:00
{
hasAspectRatio . AspectRatio = request . AspectRatio ;
}
2014-02-18 22:21:03 -07:00
2016-03-27 14:11:27 -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 ;
}
2013-09-13 11:37:44 -07:00
// 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 ;
2020-04-05 09:07:43 -07:00
if ( item is Video video )
2013-07-29 05:34:46 -07:00
{
video . Video3DFormat = request . Video3DFormat ;
}
2013-09-03 07:26:17 -07:00
2015-05-18 15:23:03 -07:00
if ( request . AlbumArtists ! = null )
2013-06-27 12:06:37 -07:00
{
2020-04-05 09:07:43 -07:00
if ( item is IHasAlbumArtist hasAlbumArtists )
2015-05-18 15:23:03 -07:00
{
hasAlbumArtists . AlbumArtists = request
. AlbumArtists
. Select ( i = > i . Name )
2017-08-10 11:01:31 -07:00
. ToArray ( ) ;
2015-05-18 15:23:03 -07:00
}
2013-06-27 12:06:37 -07:00
}
2015-05-18 15:23:03 -07:00
if ( request . ArtistItems ! = null )
2015-03-13 10:25:28 -07:00
{
2020-04-05 09:07:43 -07:00
if ( item is IHasArtist hasArtists )
2015-05-18 15:23:03 -07:00
{
hasArtists . Artists = request
. ArtistItems
. Select ( i = > i . Name )
2017-08-24 12:52:19 -07:00
. ToArray ( ) ;
2015-05-18 15:23:03 -07:00
}
2015-03-13 10:25:28 -07:00
}
2015-03-15 18:48:25 -07:00
2020-06-08 11:20:33 -07:00
switch ( item )
2013-07-10 13:10:55 -07:00
{
2020-06-08 11:20:33 -07:00
case Audio song :
song . Album = request . Album ;
break ;
case MusicVideo musicVideo :
musicVideo . Album = request . Album ;
break ;
case Series series :
{
series . Status = GetSeriesStatus ( request ) ;
2013-07-10 13:10:55 -07:00
2020-06-08 11:20:33 -07:00
if ( request . AirDays ! = null )
{
series . AirDays = request . AirDays ;
series . AirTime = request . AirTime ;
}
2017-08-13 13:15:07 -07:00
2020-06-08 11:20:33 -07:00
break ;
2017-08-13 13:15:07 -07:00
}
2013-06-27 12:06:37 -07:00
}
}
2017-02-18 20:46:09 -07:00
private SeriesStatus ? GetSeriesStatus ( BaseItemDto item )
{
if ( string . IsNullOrEmpty ( item . Status ) )
{
return null ;
}
return ( SeriesStatus ) Enum . Parse ( typeof ( SeriesStatus ) , item . Status , true ) ;
}
2020-06-08 11:20:33 -07:00
private DateTime NormalizeDateTime ( DateTime val )
{
return DateTime . SpecifyKind ( val , DateTimeKind . Utc ) ;
}
private List < NameValuePair > GetContentTypeOptions ( bool isForItem )
{
var list = new List < NameValuePair > ( ) ;
if ( isForItem )
{
list . Add ( new NameValuePair
{
Name = "Inherit" ,
Value = string . Empty
} ) ;
}
list . Add ( new NameValuePair
{
Name = "Movies" ,
Value = "movies"
} ) ;
list . Add ( new NameValuePair
{
Name = "Music" ,
Value = "music"
} ) ;
list . Add ( new NameValuePair
{
Name = "Shows" ,
Value = "tvshows"
} ) ;
if ( ! isForItem )
{
list . Add ( new NameValuePair
{
Name = "Books" ,
Value = "books"
} ) ;
}
list . Add ( new NameValuePair
{
Name = "HomeVideos" ,
Value = "homevideos"
} ) ;
list . Add ( new NameValuePair
{
Name = "MusicVideos" ,
Value = "musicvideos"
} ) ;
list . Add ( new NameValuePair
{
Name = "Photos" ,
Value = "photos"
} ) ;
if ( ! isForItem )
{
list . Add ( new NameValuePair
{
Name = "MixedContent" ,
Value = string . Empty
} ) ;
}
foreach ( var val in list )
{
val . Name = _localizationManager . GetLocalizedString ( val . Name ) ;
}
return list ;
}
2013-06-27 12:06:37 -07:00
}
}