< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.ItemUpdateController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/ItemUpdateController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 261
Coverable lines: 261
Total lines: 545
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 142
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 2/13/2026 - 12:11:21 AM Line coverage: 0% (0/119) Branch coverage: 0% (0/40) Total lines: 5444/19/2026 - 12:14:27 AM Line coverage: 0% (0/260) Branch coverage: 0% (0/140) Total lines: 5445/8/2026 - 12:15:13 AM Line coverage: 0% (0/261) Branch coverage: 0% (0/142) Total lines: 545 2/13/2026 - 12:11:21 AM Line coverage: 0% (0/119) Branch coverage: 0% (0/40) Total lines: 5444/19/2026 - 12:14:27 AM Line coverage: 0% (0/260) Branch coverage: 0% (0/140) Total lines: 5445/8/2026 - 12:15:13 AM Line coverage: 0% (0/261) Branch coverage: 0% (0/142) Total lines: 545

Coverage delta

Coverage delta 1 -1

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
UpdateItem()0%342180%
GetMetadataEditorInfo(...)0%702260%
UpdateItemContentType(...)0%2040%
UpdateItem()0%7140840%
GetSeriesStatus(...)0%620%
NormalizeDateTime(...)100%210%
GetContentTypeOptions(...)0%7280%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/ItemUpdateController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel.DataAnnotations;
 4using System.Linq;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using Jellyfin.Api.Constants;
 8using Jellyfin.Api.Extensions;
 9using Jellyfin.Api.Helpers;
 10using Jellyfin.Data.Enums;
 11using MediaBrowser.Common.Api;
 12using MediaBrowser.Controller.Configuration;
 13using MediaBrowser.Controller.Entities;
 14using MediaBrowser.Controller.Entities.Audio;
 15using MediaBrowser.Controller.Entities.TV;
 16using MediaBrowser.Controller.Library;
 17using MediaBrowser.Controller.LiveTv;
 18using MediaBrowser.Controller.Providers;
 19using MediaBrowser.Model.Dto;
 20using MediaBrowser.Model.Entities;
 21using MediaBrowser.Model.Globalization;
 22using MediaBrowser.Model.IO;
 23using Microsoft.AspNetCore.Authorization;
 24using Microsoft.AspNetCore.Http;
 25using Microsoft.AspNetCore.Mvc;
 26
 27namespace Jellyfin.Api.Controllers;
 28
 29/// <summary>
 30/// Item update controller.
 31/// </summary>
 32[Route("")]
 33[Authorize(Policy = Policies.RequiresElevation)]
 34public class ItemUpdateController : BaseJellyfinApiController
 35{
 36    private readonly ILibraryManager _libraryManager;
 37    private readonly IProviderManager _providerManager;
 38    private readonly ILocalizationManager _localizationManager;
 39    private readonly IFileSystem _fileSystem;
 40    private readonly IServerConfigurationManager _serverConfigurationManager;
 41
 42    /// <summary>
 43    /// Initializes a new instance of the <see cref="ItemUpdateController"/> class.
 44    /// </summary>
 45    /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
 46    /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
 47    /// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param>
 48    /// <param name="localizationManager">Instance of the <see cref="ILocalizationManager"/> interface.</param>
 49    /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</p
 050    public ItemUpdateController(
 051        IFileSystem fileSystem,
 052        ILibraryManager libraryManager,
 053        IProviderManager providerManager,
 054        ILocalizationManager localizationManager,
 055        IServerConfigurationManager serverConfigurationManager)
 56    {
 057        _libraryManager = libraryManager;
 058        _providerManager = providerManager;
 059        _localizationManager = localizationManager;
 060        _fileSystem = fileSystem;
 061        _serverConfigurationManager = serverConfigurationManager;
 062    }
 63
 64    /// <summary>
 65    /// Updates an item.
 66    /// </summary>
 67    /// <param name="itemId">The item id.</param>
 68    /// <param name="request">The new item properties.</param>
 69    /// <response code="204">Item updated.</response>
 70    /// <response code="404">Item not found.</response>
 71    /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the item could not be
 72    [HttpPost("Items/{itemId}")]
 73    [ProducesResponseType(StatusCodes.Status204NoContent)]
 74    [ProducesResponseType(StatusCodes.Status404NotFound)]
 75    public async Task<ActionResult> UpdateItem([FromRoute, Required] Guid itemId, [FromBody, Required] BaseItemDto reque
 76    {
 077        var item = _libraryManager.GetItemById<BaseItem>(itemId, User.GetUserId());
 078        if (item is null)
 79        {
 080            return NotFound();
 81        }
 82
 083        var newLockData = request.LockData ?? false;
 084        var isLockedChanged = item.IsLocked != newLockData;
 85
 086        var series = item as Series;
 087        var displayOrderChanged = series is not null && !string.Equals(
 088            series.DisplayOrder ?? string.Empty,
 089            request.DisplayOrder ?? string.Empty,
 090            StringComparison.OrdinalIgnoreCase);
 91
 92        // Do this first so that metadata savers can pull the updates from the database.
 093        if (request.People is not null)
 94        {
 095            _libraryManager.UpdatePeople(
 096                item,
 097                request.People.Select(x => new PersonInfo
 098                {
 099                    Name = x.Name,
 0100                    Role = x.Role,
 0101                    Type = x.Type
 0102                }).ToList());
 103        }
 104
 0105        await UpdateItem(request, item).ConfigureAwait(false);
 106
 0107        item.OnMetadataChanged();
 108
 0109        await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
 110
 0111        if (isLockedChanged && item.IsFolder)
 112        {
 0113            var folder = (Folder)item;
 114
 0115            foreach (var child in folder.GetRecursiveChildren())
 116            {
 0117                child.IsLocked = newLockData;
 0118                await child.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(
 119            }
 120        }
 121
 0122        if (displayOrderChanged)
 123        {
 0124            _providerManager.QueueRefresh(
 0125                series!.Id,
 0126                new MetadataRefreshOptions(new DirectoryService(_fileSystem))
 0127                {
 0128                    MetadataRefreshMode = MetadataRefreshMode.FullRefresh,
 0129                    ImageRefreshMode = MetadataRefreshMode.FullRefresh,
 0130                    ReplaceAllMetadata = true
 0131                },
 0132                RefreshPriority.High);
 133        }
 134
 0135        return NoContent();
 0136    }
 137
 138    /// <summary>
 139    /// Gets metadata editor info for an item.
 140    /// </summary>
 141    /// <param name="itemId">The item id.</param>
 142    /// <response code="200">Item metadata editor returned.</response>
 143    /// <response code="404">Item not found.</response>
 144    /// <returns>An <see cref="OkResult"/> on success containing the metadata editor, or a <see cref="NotFoundResult"/> 
 145    [HttpGet("Items/{itemId}/MetadataEditor")]
 146    [ProducesResponseType(StatusCodes.Status200OK)]
 147    [ProducesResponseType(StatusCodes.Status404NotFound)]
 148    public ActionResult<MetadataEditorInfo> GetMetadataEditorInfo([FromRoute, Required] Guid itemId)
 149    {
 0150        var item = _libraryManager.GetItemById<BaseItem>(itemId, User.GetUserId());
 0151        if (item is null)
 152        {
 0153            return NotFound();
 154        }
 155
 0156        var info = new MetadataEditorInfo
 0157        {
 0158            ParentalRatingOptions = _localizationManager.GetParentalRatings().ToList(),
 0159            ExternalIdInfos = _providerManager.GetExternalIdInfos(item).ToArray(),
 0160            Countries = _localizationManager.GetCountries().ToArray(),
 0161            Cultures = _localizationManager.GetCultures()
 0162                .DistinctBy(c => c.DisplayName, StringComparer.OrdinalIgnoreCase)
 0163                .OrderBy(c => c.DisplayName)
 0164                .ToArray()
 0165        };
 166
 0167        if (!item.IsVirtualItem
 0168            && item is not ICollectionFolder
 0169            && item is not UserView
 0170            && item is not AggregateFolder
 0171            && item is not LiveTvChannel
 0172            && item is not IItemByName
 0173            && item.SourceType == SourceType.Library)
 174        {
 0175            var inheritedContentType = _libraryManager.GetInheritedContentType(item);
 0176            var configuredContentType = _libraryManager.GetConfiguredContentType(item);
 177
 0178            if (inheritedContentType is null || configuredContentType is not null)
 179            {
 0180                info.ContentTypeOptions = GetContentTypeOptions(true).ToArray();
 0181                info.ContentType = configuredContentType;
 182
 0183                if (inheritedContentType is null
 0184                    || inheritedContentType == CollectionType.tvshows
 0185                    || inheritedContentType == CollectionType.movies)
 186                {
 0187                    info.ContentTypeOptions = info.ContentTypeOptions
 0188                        .Where(i => string.IsNullOrWhiteSpace(i.Value)
 0189                                    || string.Equals(i.Value, "TvShows", StringComparison.OrdinalIgnoreCase)
 0190                                    || string.Equals(i.Value, "Movies", StringComparison.OrdinalIgnoreCase))
 0191                        .ToArray();
 192                }
 193            }
 194        }
 195
 0196        return info;
 197    }
 198
 199    /// <summary>
 200    /// Updates an item's content type.
 201    /// </summary>
 202    /// <param name="itemId">The item id.</param>
 203    /// <param name="contentType">The content type of the item.</param>
 204    /// <response code="204">Item content type updated.</response>
 205    /// <response code="404">Item not found.</response>
 206    /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the item could not be
 207    [HttpPost("Items/{itemId}/ContentType")]
 208    [ProducesResponseType(StatusCodes.Status204NoContent)]
 209    [ProducesResponseType(StatusCodes.Status404NotFound)]
 210    public ActionResult UpdateItemContentType([FromRoute, Required] Guid itemId, [FromQuery] string? contentType)
 211    {
 0212        var item = _libraryManager.GetItemById<BaseItem>(itemId, User.GetUserId());
 0213        if (item is null)
 214        {
 0215            return NotFound();
 216        }
 217
 0218        var path = item.ContainingFolderPath;
 219
 0220        var types = _serverConfigurationManager.Configuration.ContentTypes
 0221            .Where(i => !string.IsNullOrWhiteSpace(i.Name))
 0222            .Where(i => !string.Equals(i.Name, path, StringComparison.OrdinalIgnoreCase))
 0223            .ToList();
 224
 0225        if (!string.IsNullOrWhiteSpace(contentType))
 226        {
 0227            types.Add(new NameValuePair
 0228            {
 0229                Name = path,
 0230                Value = contentType
 0231            });
 232        }
 233
 0234        _serverConfigurationManager.Configuration.ContentTypes = types.ToArray();
 0235        _serverConfigurationManager.SaveConfiguration();
 0236        return NoContent();
 237    }
 238
 239    private async Task UpdateItem(BaseItemDto request, BaseItem item)
 240    {
 0241        item.Name = request.Name;
 0242        item.ForcedSortName = request.ForcedSortName;
 243
 0244        item.OriginalTitle = string.IsNullOrWhiteSpace(request.OriginalTitle) ? null : request.OriginalTitle;
 0245        item.OriginalLanguage = string.IsNullOrWhiteSpace(request.OriginalLanguage) ? null : request.OriginalLanguage;
 246
 0247        item.CriticRating = request.CriticRating;
 248
 0249        item.CommunityRating = request.CommunityRating;
 0250        item.IndexNumber = request.IndexNumber;
 0251        item.ParentIndexNumber = request.ParentIndexNumber;
 0252        item.Overview = request.Overview;
 0253        item.Genres = request.Genres.Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
 254
 0255        if (item is Episode episode)
 256        {
 0257            episode.AirsAfterSeasonNumber = request.AirsAfterSeasonNumber;
 0258            episode.AirsBeforeEpisodeNumber = request.AirsBeforeEpisodeNumber;
 0259            episode.AirsBeforeSeasonNumber = request.AirsBeforeSeasonNumber;
 260        }
 261
 0262        if (request.Height is not null && item is LiveTvChannel channel)
 263        {
 0264            channel.Height = request.Height.Value;
 265        }
 266
 0267        if (request.Taglines is not null)
 268        {
 0269            item.Tagline = request.Taglines.FirstOrDefault();
 270        }
 271
 0272        if (request.Studios is not null)
 273        {
 0274            item.Studios = Array.ConvertAll(request.Studios, x => x.Name).Distinct(StringComparer.OrdinalIgnoreCase).ToA
 275        }
 276
 0277        if (request.DateCreated.HasValue)
 278        {
 0279            item.DateCreated = NormalizeDateTime(request.DateCreated.Value);
 280        }
 281
 0282        item.EndDate = request.EndDate.HasValue ? NormalizeDateTime(request.EndDate.Value) : null;
 0283        item.PremiereDate = request.PremiereDate.HasValue ? NormalizeDateTime(request.PremiereDate.Value) : null;
 0284        item.ProductionYear = request.ProductionYear;
 285
 0286        request.OfficialRating = string.IsNullOrWhiteSpace(request.OfficialRating) ? null : request.OfficialRating;
 0287        item.OfficialRating = request.OfficialRating;
 0288        item.CustomRating = request.CustomRating;
 289
 0290        var currentTags = item.Tags;
 0291        var newTags = request.Tags.Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
 0292        var removedTags = currentTags.Except(newTags).ToList();
 0293        var addedTags = newTags.Except(currentTags).ToList();
 0294        item.Tags = newTags;
 295
 0296        if (item is Series rseries)
 297        {
 0298            foreach (var season in rseries.Children.OfType<Season>())
 299            {
 0300                if (!season.LockedFields.Contains(MetadataField.OfficialRating))
 301                {
 0302                    season.OfficialRating = request.OfficialRating;
 303                }
 304
 0305                season.CustomRating = request.CustomRating;
 306
 0307                if (!season.LockedFields.Contains(MetadataField.Tags))
 308                {
 0309                    season.Tags = season.Tags.Concat(addedTags).Except(removedTags).Distinct(StringComparer.OrdinalIgnor
 310                }
 311
 0312                season.OnMetadataChanged();
 0313                await season.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait
 314
 0315                foreach (var ep in season.Children.OfType<Episode>())
 316                {
 0317                    if (!ep.LockedFields.Contains(MetadataField.OfficialRating))
 318                    {
 0319                        ep.OfficialRating = request.OfficialRating;
 320                    }
 321
 0322                    ep.CustomRating = request.CustomRating;
 323
 0324                    if (!ep.LockedFields.Contains(MetadataField.Tags))
 325                    {
 0326                        ep.Tags = ep.Tags.Concat(addedTags).Except(removedTags).Distinct(StringComparer.OrdinalIgnoreCas
 327                    }
 328
 0329                    ep.OnMetadataChanged();
 0330                    await ep.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait
 331                }
 0332            }
 333        }
 0334        else if (item is Season season)
 335        {
 0336            foreach (var ep in season.Children.OfType<Episode>())
 337            {
 0338                if (!ep.LockedFields.Contains(MetadataField.OfficialRating))
 339                {
 0340                    ep.OfficialRating = request.OfficialRating;
 341                }
 342
 0343                ep.CustomRating = request.CustomRating;
 344
 0345                if (!ep.LockedFields.Contains(MetadataField.Tags))
 346                {
 0347                    ep.Tags = ep.Tags.Concat(addedTags).Except(removedTags).Distinct(StringComparer.OrdinalIgnoreCase).T
 348                }
 349
 0350                ep.OnMetadataChanged();
 0351                await ep.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(fal
 352            }
 353        }
 0354        else if (item is MusicAlbum album)
 355        {
 0356            foreach (BaseItem track in album.Children)
 357            {
 0358                if (!track.LockedFields.Contains(MetadataField.OfficialRating))
 359                {
 0360                    track.OfficialRating = request.OfficialRating;
 361                }
 362
 0363                track.CustomRating = request.CustomRating;
 364
 0365                if (!track.LockedFields.Contains(MetadataField.Tags))
 366                {
 0367                    track.Tags = track.Tags.Concat(addedTags).Except(removedTags).Distinct(StringComparer.OrdinalIgnoreC
 368                }
 369
 0370                track.OnMetadataChanged();
 0371                await track.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(
 372            }
 373        }
 374
 0375        if (request.ProductionLocations is not null)
 376        {
 0377            item.ProductionLocations = request.ProductionLocations.Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
 378        }
 379
 0380        item.PreferredMetadataCountryCode = request.PreferredMetadataCountryCode;
 0381        item.PreferredMetadataLanguage = request.PreferredMetadataLanguage;
 382
 0383        if (item is IHasDisplayOrder hasDisplayOrder)
 384        {
 0385            hasDisplayOrder.DisplayOrder = request.DisplayOrder;
 386        }
 387
 0388        if (item is IHasAspectRatio hasAspectRatio)
 389        {
 0390            hasAspectRatio.AspectRatio = request.AspectRatio;
 391        }
 392
 0393        item.IsLocked = request.LockData ?? false;
 394
 0395        if (request.LockedFields is not null)
 396        {
 0397            item.LockedFields = request.LockedFields;
 398        }
 399
 400        // Only allow this for series. Runtimes for media comes from ffprobe.
 0401        if (item is Series)
 402        {
 0403            item.RunTimeTicks = request.RunTimeTicks;
 404        }
 405
 0406        foreach (var pair in request.ProviderIds.ToList())
 407        {
 0408            if (string.IsNullOrEmpty(pair.Value))
 409            {
 0410                request.ProviderIds.Remove(pair.Key);
 411            }
 412        }
 413
 0414        item.ProviderIds = request.ProviderIds;
 415
 0416        if (item is Video video)
 417        {
 0418            video.Video3DFormat = request.Video3DFormat;
 419        }
 420
 0421        if (request.AlbumArtists is not null)
 422        {
 0423            if (item is IHasAlbumArtist hasAlbumArtists)
 424            {
 0425                hasAlbumArtists.AlbumArtists = Array.ConvertAll(request.AlbumArtists, i => i.Name.Trim()).Distinct(Strin
 426            }
 427        }
 428
 0429        if (request.ArtistItems is not null)
 430        {
 0431            if (item is IHasArtist hasArtists)
 432            {
 0433                hasArtists.Artists = Array.ConvertAll(request.ArtistItems, i => i.Name.Trim()).Distinct(StringComparer.O
 434            }
 435        }
 436
 437        switch (item)
 438        {
 439            case Audio song:
 0440                song.Album = request.Album;
 0441                break;
 442            case MusicVideo musicVideo:
 0443                musicVideo.Album = request.Album;
 0444                break;
 445            case Series series:
 446                {
 0447                    series.Status = GetSeriesStatus(request);
 448
 0449                    if (request.AirDays is not null)
 450                    {
 0451                        series.AirDays = request.AirDays;
 0452                        series.AirTime = request.AirTime;
 453                    }
 454
 455                    break;
 456                }
 457        }
 0458    }
 459
 460    private SeriesStatus? GetSeriesStatus(BaseItemDto item)
 461    {
 0462        if (string.IsNullOrEmpty(item.Status))
 463        {
 0464            return null;
 465        }
 466
 0467        return Enum.Parse<SeriesStatus>(item.Status, true);
 468    }
 469
 470    private DateTime NormalizeDateTime(DateTime val)
 471    {
 0472        return DateTime.SpecifyKind(val, DateTimeKind.Utc);
 473    }
 474
 475    private List<NameValuePair> GetContentTypeOptions(bool isForItem)
 476    {
 0477        var list = new List<NameValuePair>();
 478
 0479        if (isForItem)
 480        {
 0481            list.Add(new NameValuePair
 0482            {
 0483                Name = "Inherit",
 0484                Value = string.Empty
 0485            });
 486        }
 487
 0488        list.Add(new NameValuePair
 0489        {
 0490            Name = "Movies",
 0491            Value = "movies"
 0492        });
 0493        list.Add(new NameValuePair
 0494        {
 0495            Name = "Music",
 0496            Value = "music"
 0497        });
 0498        list.Add(new NameValuePair
 0499        {
 0500            Name = "Shows",
 0501            Value = "tvshows"
 0502        });
 503
 0504        if (!isForItem)
 505        {
 0506            list.Add(new NameValuePair
 0507            {
 0508                Name = "Books",
 0509                Value = "books"
 0510            });
 511        }
 512
 0513        list.Add(new NameValuePair
 0514        {
 0515            Name = "HomeVideos",
 0516            Value = "homevideos"
 0517        });
 0518        list.Add(new NameValuePair
 0519        {
 0520            Name = "MusicVideos",
 0521            Value = "musicvideos"
 0522        });
 0523        list.Add(new NameValuePair
 0524        {
 0525            Name = "Photos",
 0526            Value = "photos"
 0527        });
 528
 0529        if (!isForItem)
 530        {
 0531            list.Add(new NameValuePair
 0532            {
 0533                Name = "MixedContent",
 0534                Value = string.Empty
 0535            });
 536        }
 537
 0538        foreach (var val in list)
 539        {
 0540            val.Name = _localizationManager.GetLocalizedString(val.Name);
 541        }
 542
 0543        return list;
 544    }
 545}