The Free Software Media System - Server Backend & API
Move LibraryStructureService to Jellyfin.Api
David618b893c481a
diff --git a/Jellyfin.Api/Controllers/LibraryStructureController.cs b/Jellyfin.Api/Controllers/LibraryStructureController.csnew file mode 100644index 0000000000..f074a61dbe--- /dev/null+++ b/Jellyfin.Api/Controllers/LibraryStructureController.cs@@ -0,0 +1,347 @@+#pragma warning disable CA1801++using System;+using System.Collections.Generic;+using System.Globalization;+using System.IO;+using System.Linq;+using System.Threading;+using System.Threading.Tasks;+using Jellyfin.Api.Constants;+using MediaBrowser.Common.Progress;+using MediaBrowser.Controller;+using MediaBrowser.Controller.Configuration;+using MediaBrowser.Controller.Entities;+using MediaBrowser.Controller.Library;+using MediaBrowser.Model.Configuration;+using MediaBrowser.Model.Entities;+using Microsoft.AspNetCore.Authorization;+using Microsoft.AspNetCore.Http;+using Microsoft.AspNetCore.Mvc;++namespace Jellyfin.Api.Controllers+{+ /// <summary>+ /// The library structure controller.+ /// </summary>+ [Route("/Library/VirtualFolders")]+ [Authorize(Policy = Policies.FirstTimeSetupOrElevated)]+ public class LibraryStructureController : BaseJellyfinApiController+ {+ private readonly IServerApplicationPaths _appPaths;+ private readonly ILibraryManager _libraryManager;+ private readonly ILibraryMonitor _libraryMonitor;++ /// <summary>+ /// Initializes a new instance of the <see cref="LibraryStructureController"/> class.+ /// </summary>+ /// <param name="serverConfigurationManager">Instance of <see cref="IServerConfigurationManager"/> interface.</param>+ /// <param name="libraryManager">Instance of <see cref="ILibraryManager"/> interface.</param>+ /// <param name="libraryMonitor">Instance of <see cref="ILibraryMonitor"/> interface.</param>+ public LibraryStructureController(+ IServerConfigurationManager serverConfigurationManager,+ ILibraryManager libraryManager,+ ILibraryMonitor libraryMonitor)+ {+ _appPaths = serverConfigurationManager?.ApplicationPaths;+ _libraryManager = libraryManager;+ _libraryMonitor = libraryMonitor;+ }++ /// <summary>+ /// Gets all virtual folders.+ /// </summary>+ /// <param name="userId">The user id.</param>+ /// <response code="200">Virtual folders retrieved.</response>+ /// <returns>An <see cref="IEnumerable{VirtualFolderInfo}"/> with the virtual folders.</returns>+ [HttpGet]+ [ProducesResponseType(StatusCodes.Status200OK)]+ public ActionResult<IEnumerable<VirtualFolderInfo>> GetVirtualFolders([FromQuery] string userId)+ {+ return _libraryManager.GetVirtualFolders(true);+ }++ /// <summary>+ /// Adds a virtual folder.+ /// </summary>+ /// <param name="name">The name of the virtual folder.</param>+ /// <param name="collectionType">The type of the collection.</param>+ /// <param name="refreshLibrary">Whether to refresh the library.</param>+ /// <param name="paths">The paths of the virtual folder.</param>+ /// <param name="libraryOptions">The library options.</param>+ /// <response code="204">Folder added.</response>+ /// <returns>A <see cref="NoContentResult"/>.</returns>+ [HttpPost]+ [ProducesResponseType(StatusCodes.Status204NoContent)]+ public ActionResult AddVirtualFolder(+ [FromQuery] string name,+ [FromQuery] string collectionType,+ [FromQuery] bool refreshLibrary,+ [FromQuery] string[] paths,+ [FromQuery] LibraryOptions libraryOptions)+ {+ libraryOptions ??= new LibraryOptions();++ if (paths != null && paths.Length > 0)+ {+ libraryOptions.PathInfos = paths.Select(i => new MediaPathInfo { Path = i }).ToArray();+ }++ _libraryManager.AddVirtualFolder(name, collectionType, libraryOptions, refreshLibrary);++ return NoContent();+ }++ /// <summary>+ /// Removes a virtual folder.+ /// </summary>+ /// <param name="name">The name of the folder.</param>+ /// <param name="refreshLibrary">Whether to refresh the library.</param>+ /// <response code="204">Folder removed.</response>+ /// <returns>A <see cref="NoContentResult"/>.</returns>+ [HttpDelete]+ [ProducesResponseType(StatusCodes.Status204NoContent)]+ public ActionResult RemoveVirtualFolder(+ [FromQuery] string name,+ [FromQuery] bool refreshLibrary)+ {+ _libraryManager.RemoveVirtualFolder(name, refreshLibrary);+ return NoContent();+ }++ /// <summary>+ /// Renames a virtual folder.+ /// </summary>+ /// <param name="name">The name of the virtual folder.</param>+ /// <param name="newName">The new name.</param>+ /// <param name="refreshLibrary">Whether to refresh the library.</param>+ /// <response code="204">Folder renamed.</response>+ /// <response code="404">Library doesn't exist.</response>+ /// <response code="409">Library already exists.</response>+ /// <returns>A <see cref="NoContentResult"/> on success, a <see cref="NotFoundResult"/> if the library doesn't exist, a <see cref="ConflictResult"/> if the new name is already taken.</returns>+ /// <exception cref="ArgumentNullException">The new name may not be null.</exception>+ [HttpPost("Name")]+ [ProducesResponseType(StatusCodes.Status204NoContent)]+ [ProducesResponseType(StatusCodes.Status404NotFound)]+ [ProducesResponseType(StatusCodes.Status409Conflict)]+ public ActionResult RenameVirtualFolder(+ [FromQuery] string name,+ [FromQuery] string newName,+ [FromQuery] bool refreshLibrary)+ {+ if (string.IsNullOrWhiteSpace(name))+ {+ throw new ArgumentNullException(nameof(name));+ }++ if (string.IsNullOrWhiteSpace(newName))+ {+ throw new ArgumentNullException(nameof(newName));+ }++ var rootFolderPath = _appPaths.DefaultUserViewsPath;++ var currentPath = Path.Combine(rootFolderPath, name);+ var newPath = Path.Combine(rootFolderPath, newName);++ if (!Directory.Exists(currentPath))+ {+ return NotFound("The media collection does not exist.");+ }++ if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(newPath))+ {+ return Conflict($"The media library already exists at {newPath}.");+ }++ _libraryMonitor.Stop();++ try+ {+ // Changing capitalization. Handle windows case insensitivity+ if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))+ {+ var tempPath = Path.Combine(+ rootFolderPath,+ Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));+ Directory.Move(currentPath, tempPath);+ currentPath = tempPath;+ }++ Directory.Move(currentPath, newPath);+ }+ finally+ {+ CollectionFolder.OnCollectionFolderChange();++ Task.Run(() =>+ {+ // No need to start if scanning the library because it will handle it+ if (refreshLibrary)+ {+ _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);+ }+ else+ {+ // Need to add a delay here or directory watchers may still pick up the changes+ var task = Task.Delay(1000);+ // Have to block here to allow exceptions to bubble+ Task.WaitAll(task);++ _libraryMonitor.Start();+ }+ });+ }++ return NoContent();+ }++ /// <summary>+ /// Add a media path to a library.+ /// </summary>+ /// <param name="name">The name of the library.</param>+ /// <param name="path">The path to add.</param>+ /// <param name="pathInfo">The path info.</param>+ /// <param name="refreshLibrary">Whether to refresh the library.</param>+ /// <returns>A <see cref="NoContentResult"/>.</returns>+ /// <response code="204">Media path added.</response>+ /// <exception cref="ArgumentNullException">The name of the library may not be empty.</exception>+ [HttpPost("Paths")]+ [ProducesResponseType(StatusCodes.Status204NoContent)]+ public ActionResult AddMediaPath(+ [FromQuery] string name,+ [FromQuery] string path,+ [FromQuery] MediaPathInfo pathInfo,+ [FromQuery] bool refreshLibrary)+ {+ if (string.IsNullOrWhiteSpace(name))+ {+ throw new ArgumentNullException(nameof(name));+ }++ _libraryMonitor.Stop();++ try+ {+ var mediaPath = pathInfo ?? new MediaPathInfo { Path = path };++ _libraryManager.AddMediaPath(name, mediaPath);+ }+ finally+ {+ Task.Run(() =>+ {+ // No need to start if scanning the library because it will handle it+ if (refreshLibrary)+ {+ _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);+ }+ else+ {+ // Need to add a delay here or directory watchers may still pick up the changes+ var task = Task.Delay(1000);+ // Have to block here to allow exceptions to bubble+ Task.WaitAll(task);++ _libraryMonitor.Start();+ }+ });+ }++ return NoContent();+ }++ /// <summary>+ /// Updates a media path.+ /// </summary>+ /// <param name="name">The name of the library.</param>+ /// <param name="pathInfo">The path info.</param>+ /// <returns>A <see cref="NoContentResult"/>.</returns>+ /// <response code="204">Media path updated.</response>+ /// <exception cref="ArgumentNullException">The name of the library may not be empty.</exception>+ [HttpPost("Paths/Update")]+ [ProducesResponseType(StatusCodes.Status204NoContent)]+ public ActionResult UpdateMediaPath(+ [FromQuery] string name,+ [FromQuery] MediaPathInfo pathInfo)+ {+ if (string.IsNullOrWhiteSpace(name))+ {+ throw new ArgumentNullException(nameof(name));+ }++ _libraryManager.UpdateMediaPath(name, pathInfo);+ return NoContent();+ }++ /// <summary>+ /// Remove a media path.+ /// </summary>+ /// <param name="name">The name of the library.</param>+ /// <param name="path">The path to remove.</param>+ /// <param name="refreshLibrary">Whether to refresh the library.</param>+ /// <returns>A <see cref="NoContentResult"/>.</returns>+ /// <response code="204">Media path removed.</response>+ /// <exception cref="ArgumentNullException">The name of the library may not be empty.</exception>+ [HttpDelete("Paths")]+ [ProducesResponseType(StatusCodes.Status204NoContent)]+ public ActionResult RemoveMediaPath(+ [FromQuery] string name,+ [FromQuery] string path,+ [FromQuery] bool refreshLibrary)+ {+ if (string.IsNullOrWhiteSpace(name))+ {+ throw new ArgumentNullException(nameof(name));+ }++ _libraryMonitor.Stop();++ try+ {+ _libraryManager.RemoveMediaPath(name, path);+ }+ finally+ {+ Task.Run(() =>+ {+ // No need to start if scanning the library because it will handle it+ if (refreshLibrary)+ {+ _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);+ }+ else+ {+ // Need to add a delay here or directory watchers may still pick up the changes+ var task = Task.Delay(1000);+ // Have to block here to allow exceptions to bubble+ Task.WaitAll(task);++ _libraryMonitor.Start();+ }+ });+ }++ return NoContent();+ }++ /// <summary>+ /// Update library options.+ /// </summary>+ /// <param name="id">The library name.</param>+ /// <param name="libraryOptions">The library options.</param>+ /// <response code="204">Library updated.</response>+ /// <returns>A <see cref="NoContentResult"/>.</returns>+ [HttpPost("LibraryOptions")]+ [ProducesResponseType(StatusCodes.Status204NoContent)]+ public ActionResult UpdateLibraryOptions(+ [FromQuery] string id,+ [FromQuery] LibraryOptions libraryOptions)+ {+ var collectionFolder = (CollectionFolder)_libraryManager.GetItemById(id);++ collectionFolder.UpdateLibraryOptions(libraryOptions);+ return NoContent();+ }+ }+}diff --git a/MediaBrowser.Api/Library/LibraryStructureService.cs b/MediaBrowser.Api/Library/LibraryStructureService.csdeleted file mode 100644index 1e300814f6..0000000000--- a/MediaBrowser.Api/Library/LibraryStructureService.cs+++ /dev/null@@ -1,412 +0,0 @@-using System;-using System.Collections.Generic;-using System.Globalization;-using System.IO;-using System.Linq;-using System.Threading;-using System.Threading.Tasks;-using MediaBrowser.Common.Progress;-using MediaBrowser.Controller;-using MediaBrowser.Controller.Configuration;-using MediaBrowser.Controller.Entities;-using MediaBrowser.Controller.Library;-using MediaBrowser.Controller.Net;-using MediaBrowser.Model.Configuration;-using MediaBrowser.Model.Entities;-using MediaBrowser.Model.Services;-using Microsoft.Extensions.Logging;--namespace MediaBrowser.Api.Library-{- /// <summary>- /// Class GetDefaultVirtualFolders- /// </summary>- [Route("/Library/VirtualFolders", "GET")]- public class GetVirtualFolders : IReturn<List<VirtualFolderInfo>>- {- /// <summary>- /// Gets or sets the user id.- /// </summary>- /// <value>The user id.</value>- public string UserId { get; set; }- }-- [Route("/Library/VirtualFolders", "POST")]- public class AddVirtualFolder : IReturnVoid- {- /// <summary>- /// Gets or sets the name.- /// </summary>- /// <value>The name.</value>- public string Name { get; set; }-- /// <summary>- /// Gets or sets the type of the collection.- /// </summary>- /// <value>The type of the collection.</value>- public string CollectionType { get; set; }-- /// <summary>- /// Gets or sets a value indicating whether [refresh library].- /// </summary>- /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>- public bool RefreshLibrary { get; set; }-- /// <summary>- /// Gets or sets the path.- /// </summary>- /// <value>The path.</value>- public string[] Paths { get; set; }-- public LibraryOptions LibraryOptions { get; set; }- }-- [Route("/Library/VirtualFolders", "DELETE")]- public class RemoveVirtualFolder : IReturnVoid- {- /// <summary>- /// Gets or sets the name.- /// </summary>- /// <value>The name.</value>- public string Name { get; set; }-- /// <summary>- /// Gets or sets a value indicating whether [refresh library].- /// </summary>- /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>- public bool RefreshLibrary { get; set; }- }-- [Route("/Library/VirtualFolders/Name", "POST")]- public class RenameVirtualFolder : IReturnVoid- {- /// <summary>- /// Gets or sets the name.- /// </summary>- /// <value>The name.</value>- public string Name { get; set; }-- /// <summary>- /// Gets or sets the name.- /// </summary>- /// <value>The name.</value>- public string NewName { get; set; }-- /// <summary>- /// Gets or sets a value indicating whether [refresh library].- /// </summary>- /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>- public bool RefreshLibrary { get; set; }- }-- [Route("/Library/VirtualFolders/Paths", "POST")]- public class AddMediaPath : IReturnVoid- {- /// <summary>- /// Gets or sets the name.- /// </summary>- /// <value>The name.</value>- public string Name { get; set; }-- /// <summary>- /// Gets or sets the name.- /// </summary>- /// <value>The name.</value>- public string Path { get; set; }-- public MediaPathInfo PathInfo { get; set; }-- /// <summary>- /// Gets or sets a value indicating whether [refresh library].- /// </summary>- /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>- public bool RefreshLibrary { get; set; }- }-- [Route("/Library/VirtualFolders/Paths/Update", "POST")]- public class UpdateMediaPath : IReturnVoid- {- /// <summary>- /// Gets or sets the name.- /// </summary>- /// <value>The name.</value>- public string Name { get; set; }-- public MediaPathInfo PathInfo { get; set; }- }-- [Route("/Library/VirtualFolders/Paths", "DELETE")]- public class RemoveMediaPath : IReturnVoid- {- /// <summary>- /// Gets or sets the name.- /// </summary>- /// <value>The name.</value>- public string Name { get; set; }-- /// <summary>- /// Gets or sets the name.- /// </summary>- /// <value>The name.</value>- public string Path { get; set; }-- /// <summary>- /// Gets or sets a value indicating whether [refresh library].- /// </summary>- /// <value><c>true</c> if [refresh library]; otherwise, <c>false</c>.</value>- public bool RefreshLibrary { get; set; }- }-- [Route("/Library/VirtualFolders/LibraryOptions", "POST")]- public class UpdateLibraryOptions : IReturnVoid- {- public string Id { get; set; }-- public LibraryOptions LibraryOptions { get; set; }- }-- /// <summary>- /// Class LibraryStructureService- /// </summary>- [Authenticated(Roles = "Admin", AllowBeforeStartupWizard = true)]- public class LibraryStructureService : BaseApiService- {- /// <summary>- /// The _app paths- /// </summary>- private readonly IServerApplicationPaths _appPaths;-- /// <summary>- /// The _library manager- /// </summary>- private readonly ILibraryManager _libraryManager;- private readonly ILibraryMonitor _libraryMonitor;--- /// <summary>- /// Initializes a new instance of the <see cref="LibraryStructureService" /> class.- /// </summary>- public LibraryStructureService(- ILogger<LibraryStructureService> logger,- IServerConfigurationManager serverConfigurationManager,- IHttpResultFactory httpResultFactory,- ILibraryManager libraryManager,- ILibraryMonitor libraryMonitor)- : base(logger, serverConfigurationManager, httpResultFactory)- {- _appPaths = serverConfigurationManager.ApplicationPaths;- _libraryManager = libraryManager;- _libraryMonitor = libraryMonitor;- }-- /// <summary>- /// Gets the specified request.- /// </summary>- /// <param name="request">The request.</param>- /// <returns>System.Object.</returns>- public object Get(GetVirtualFolders request)- {- var result = _libraryManager.GetVirtualFolders(true);-- return ToOptimizedResult(result);- }-- public void Post(UpdateLibraryOptions request)- {- var collectionFolder = (CollectionFolder)_libraryManager.GetItemById(request.Id);-- collectionFolder.UpdateLibraryOptions(request.LibraryOptions);- }-- /// <summary>- /// Posts the specified request.- /// </summary>- /// <param name="request">The request.</param>- public Task Post(AddVirtualFolder request)- {- var libraryOptions = request.LibraryOptions ?? new LibraryOptions();-- if (request.Paths != null && request.Paths.Length > 0)- {- libraryOptions.PathInfos = request.Paths.Select(i => new MediaPathInfo { Path = i }).ToArray();- }-- return _libraryManager.AddVirtualFolder(request.Name, request.CollectionType, libraryOptions, request.RefreshLibrary);- }-- /// <summary>- /// Posts the specified request.- /// </summary>- /// <param name="request">The request.</param>- public void Post(RenameVirtualFolder request)- {- if (string.IsNullOrWhiteSpace(request.Name))- {- throw new ArgumentNullException(nameof(request));- }-- if (string.IsNullOrWhiteSpace(request.NewName))- {- throw new ArgumentNullException(nameof(request));- }-- var rootFolderPath = _appPaths.DefaultUserViewsPath;-- var currentPath = Path.Combine(rootFolderPath, request.Name);- var newPath = Path.Combine(rootFolderPath, request.NewName);-- if (!Directory.Exists(currentPath))- {- throw new FileNotFoundException("The media collection does not exist");- }-- if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(newPath))- {- throw new ArgumentException("Media library already exists at " + newPath + ".");- }-- _libraryMonitor.Stop();-- try- {- // Changing capitalization. Handle windows case insensitivity- if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))- {- var tempPath = Path.Combine(rootFolderPath, Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));- Directory.Move(currentPath, tempPath);- currentPath = tempPath;- }-- Directory.Move(currentPath, newPath);- }- finally- {- CollectionFolder.OnCollectionFolderChange();-- Task.Run(() =>- {- // No need to start if scanning the library because it will handle it- if (request.RefreshLibrary)- {- _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);- }- else- {- // Need to add a delay here or directory watchers may still pick up the changes- var task = Task.Delay(1000);- // Have to block here to allow exceptions to bubble- Task.WaitAll(task);-- _libraryMonitor.Start();- }- });- }- }-- /// <summary>- /// Deletes the specified request.- /// </summary>- /// <param name="request">The request.</param>- public Task Delete(RemoveVirtualFolder request)- {- return _libraryManager.RemoveVirtualFolder(request.Name, request.RefreshLibrary);- }-- /// <summary>- /// Posts the specified request.- /// </summary>- /// <param name="request">The request.</param>- public void Post(AddMediaPath request)- {- if (string.IsNullOrWhiteSpace(request.Name))- {- throw new ArgumentNullException(nameof(request));- }-- _libraryMonitor.Stop();-- try- {- var mediaPath = request.PathInfo ?? new MediaPathInfo- {- Path = request.Path- };-- _libraryManager.AddMediaPath(request.Name, mediaPath);- }- finally- {- Task.Run(() =>- {- // No need to start if scanning the library because it will handle it- if (request.RefreshLibrary)- {- _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);- }- else- {- // Need to add a delay here or directory watchers may still pick up the changes- var task = Task.Delay(1000);- // Have to block here to allow exceptions to bubble- Task.WaitAll(task);-- _libraryMonitor.Start();- }- });- }- }-- /// <summary>- /// Posts the specified request.- /// </summary>- /// <param name="request">The request.</param>- public void Post(UpdateMediaPath request)- {- if (string.IsNullOrWhiteSpace(request.Name))- {- throw new ArgumentNullException(nameof(request));- }-- _libraryManager.UpdateMediaPath(request.Name, request.PathInfo);- }-- /// <summary>- /// Deletes the specified request.- /// </summary>- /// <param name="request">The request.</param>- public void Delete(RemoveMediaPath request)- {- if (string.IsNullOrWhiteSpace(request.Name))- {- throw new ArgumentNullException(nameof(request));- }-- _libraryMonitor.Stop();-- try- {- _libraryManager.RemoveMediaPath(request.Name, request.Path);- }- finally- {- Task.Run(() =>- {- // No need to start if scanning the library because it will handle it- if (request.RefreshLibrary)- {- _libraryManager.ValidateMediaLibrary(new SimpleProgress<double>(), CancellationToken.None);- }- else- {- // Need to add a delay here or directory watchers may still pick up the changes- var task = Task.Delay(1000);- // Have to block here to allow exceptions to bubble- Task.WaitAll(task);-- _libraryMonitor.Start();- }- });- }- }- }-}