| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.ComponentModel.DataAnnotations; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Jellyfin.Api.Extensions; |
| | | 10 | | using Jellyfin.Api.Helpers; |
| | | 11 | | using Jellyfin.Api.ModelBinders; |
| | | 12 | | using Jellyfin.Api.Models.LibraryStructureDto; |
| | | 13 | | using Jellyfin.Data; |
| | | 14 | | using Jellyfin.Data.Enums; |
| | | 15 | | using Jellyfin.Database.Implementations.Entities; |
| | | 16 | | using Jellyfin.Database.Implementations.Enums; |
| | | 17 | | using MediaBrowser.Common.Api; |
| | | 18 | | using MediaBrowser.Controller; |
| | | 19 | | using MediaBrowser.Controller.Configuration; |
| | | 20 | | using MediaBrowser.Controller.Entities; |
| | | 21 | | using MediaBrowser.Controller.IO; |
| | | 22 | | using MediaBrowser.Controller.Library; |
| | | 23 | | using MediaBrowser.Controller.Providers; |
| | | 24 | | using MediaBrowser.Model.Configuration; |
| | | 25 | | using MediaBrowser.Model.Entities; |
| | | 26 | | using Microsoft.AspNetCore.Authorization; |
| | | 27 | | using Microsoft.AspNetCore.Http; |
| | | 28 | | using Microsoft.AspNetCore.Mvc; |
| | | 29 | | |
| | | 30 | | namespace Jellyfin.Api.Controllers; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// The library structure controller. |
| | | 34 | | /// </summary> |
| | | 35 | | [Route("Library/VirtualFolders")] |
| | | 36 | | [Authorize(Policy = Policies.FirstTimeSetupOrElevated)] |
| | | 37 | | public class LibraryStructureController : BaseJellyfinApiController |
| | | 38 | | { |
| | | 39 | | private readonly IServerApplicationPaths _appPaths; |
| | | 40 | | private readonly ILibraryManager _libraryManager; |
| | | 41 | | private readonly ILibraryMonitor _libraryMonitor; |
| | | 42 | | private readonly IDirectoryService _directoryService; |
| | | 43 | | private readonly IUserManager _userManager; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Initializes a new instance of the <see cref="LibraryStructureController"/> class. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="serverConfigurationManager">Instance of <see cref="IServerConfigurationManager"/> interface.</param |
| | | 49 | | /// <param name="libraryManager">Instance of <see cref="ILibraryManager"/> interface.</param> |
| | | 50 | | /// <param name="libraryMonitor">Instance of <see cref="ILibraryMonitor"/> interface.</param> |
| | | 51 | | /// <param name="directoryService">Instance of <see cref="IDirectoryService"/> interface.</param> |
| | | 52 | | /// <param name="userManager">Instance of <see cref="IUserManager"/> interface.</param> |
| | 35 | 53 | | public LibraryStructureController( |
| | 35 | 54 | | IServerConfigurationManager serverConfigurationManager, |
| | 35 | 55 | | ILibraryManager libraryManager, |
| | 35 | 56 | | ILibraryMonitor libraryMonitor, |
| | 35 | 57 | | IDirectoryService directoryService, |
| | 35 | 58 | | IUserManager userManager) |
| | | 59 | | { |
| | 35 | 60 | | _appPaths = serverConfigurationManager.ApplicationPaths; |
| | 35 | 61 | | _libraryManager = libraryManager; |
| | 35 | 62 | | _libraryMonitor = libraryMonitor; |
| | 35 | 63 | | _directoryService = directoryService; |
| | 35 | 64 | | _userManager = userManager; |
| | 35 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets all virtual folders. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <response code="200">Virtual folders retrieved.</response> |
| | | 71 | | /// <returns>An <see cref="IEnumerable{VirtualFolderInfo}"/> with the virtual folders.</returns> |
| | | 72 | | [HttpGet] |
| | | 73 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 74 | | public ActionResult<IEnumerable<VirtualFolderInfo>> GetVirtualFolders() |
| | | 75 | | { |
| | 1 | 76 | | return _libraryManager.GetVirtualFolders(true); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Adds a virtual folder. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="name">The name of the virtual folder.</param> |
| | | 83 | | /// <param name="collectionType">The type of the collection.</param> |
| | | 84 | | /// <param name="paths">The paths of the virtual folder.</param> |
| | | 85 | | /// <param name="libraryOptionsDto">The library options.</param> |
| | | 86 | | /// <param name="refreshLibrary">Whether to refresh the library.</param> |
| | | 87 | | /// <response code="204">Folder added.</response> |
| | | 88 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 89 | | [HttpPost] |
| | | 90 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 91 | | public async Task<ActionResult> AddVirtualFolder( |
| | | 92 | | [FromQuery] |
| | | 93 | | [RegularExpression(@"^(?:\S(?:.*\S)?)$", ErrorMessage = "Library name cannot be empty or have leading/trailing s |
| | | 94 | | string name, |
| | | 95 | | [FromQuery] CollectionTypeOptions? collectionType, |
| | | 96 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] string[] paths, |
| | | 97 | | [FromBody] AddVirtualFolderDto? libraryOptionsDto, |
| | | 98 | | [FromQuery] bool refreshLibrary = false) |
| | | 99 | | { |
| | 5 | 100 | | var libraryOptions = libraryOptionsDto?.LibraryOptions ?? new LibraryOptions(); |
| | | 101 | | |
| | 5 | 102 | | if (paths is not null && paths.Length > 0) |
| | | 103 | | { |
| | 0 | 104 | | libraryOptions.PathInfos = Array.ConvertAll(paths, i => new MediaPathInfo(i)); |
| | | 105 | | } |
| | | 106 | | |
| | 5 | 107 | | await _libraryManager.AddVirtualFolder(name, collectionType, libraryOptions, refreshLibrary).ConfigureAwait(fals |
| | | 108 | | |
| | 5 | 109 | | return NoContent(); |
| | 5 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Removes a virtual folder. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="name">The name of the folder.</param> |
| | | 116 | | /// <param name="refreshLibrary">Whether to refresh the library.</param> |
| | | 117 | | /// <response code="204">Folder removed.</response> |
| | | 118 | | /// <response code="404">Folder not found.</response> |
| | | 119 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 120 | | [HttpDelete] |
| | | 121 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 122 | | public async Task<ActionResult> RemoveVirtualFolder( |
| | | 123 | | [FromQuery] string name, |
| | | 124 | | [FromQuery] bool refreshLibrary = false) |
| | | 125 | | { |
| | | 126 | | // TODO: refactor! this relies on an FileNotFound exception to return NotFound when attempting to remove a libra |
| | 10 | 127 | | await _libraryManager.RemoveVirtualFolder(name, refreshLibrary).ConfigureAwait(false); |
| | | 128 | | |
| | 4 | 129 | | return NoContent(); |
| | 4 | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Renames a virtual folder. |
| | | 134 | | /// </summary> |
| | | 135 | | /// <param name="name">The name of the virtual folder.</param> |
| | | 136 | | /// <param name="newName">The new name.</param> |
| | | 137 | | /// <param name="refreshLibrary">Whether to refresh the library.</param> |
| | | 138 | | /// <response code="204">Folder renamed.</response> |
| | | 139 | | /// <response code="400">The new name is not a valid library name.</response> |
| | | 140 | | /// <response code="404">Library doesn't exist.</response> |
| | | 141 | | /// <response code="409">Library already exists.</response> |
| | | 142 | | /// <returns>A <see cref="NoContentResult"/> on success, a <see cref="BadRequestResult"/> if the new name is invalid |
| | | 143 | | /// <exception cref="ArgumentNullException">The new name may not be null.</exception> |
| | | 144 | | [HttpPost("Name")] |
| | | 145 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 146 | | [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| | | 147 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 148 | | [ProducesResponseType(StatusCodes.Status409Conflict)] |
| | | 149 | | public async Task<ActionResult> RenameVirtualFolder( |
| | | 150 | | [FromQuery] string? name, |
| | | 151 | | [FromQuery] string? newName, |
| | | 152 | | [FromQuery] bool refreshLibrary = false) |
| | | 153 | | { |
| | 13 | 154 | | if (string.IsNullOrWhiteSpace(name)) |
| | | 155 | | { |
| | 1 | 156 | | throw new ArgumentNullException(nameof(name)); |
| | | 157 | | } |
| | | 158 | | |
| | 12 | 159 | | if (string.IsNullOrWhiteSpace(newName)) |
| | | 160 | | { |
| | 1 | 161 | | throw new ArgumentNullException(nameof(newName)); |
| | | 162 | | } |
| | | 163 | | |
| | 11 | 164 | | var rootFolderPath = _appPaths.DefaultUserViewsPath; |
| | | 165 | | |
| | | 166 | | // Both names are caller supplied, so they have to be confined to the libraries root. |
| | 11 | 167 | | var newPath = FileSystemHelper.GetChildPath(rootFolderPath, newName); |
| | 11 | 168 | | if (newPath is null) |
| | | 169 | | { |
| | 5 | 170 | | return BadRequest("The new name is not a valid library name."); |
| | | 171 | | } |
| | | 172 | | |
| | 6 | 173 | | var currentPath = FileSystemHelper.GetChildPath(rootFolderPath, name); |
| | 6 | 174 | | if (currentPath is null || !Directory.Exists(currentPath)) |
| | | 175 | | { |
| | 4 | 176 | | return NotFound("The media collection does not exist."); |
| | | 177 | | } |
| | | 178 | | |
| | 2 | 179 | | if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(newPath)) |
| | | 180 | | { |
| | 0 | 181 | | return Conflict($"The media library already exists at {newPath}."); |
| | | 182 | | } |
| | | 183 | | |
| | 2 | 184 | | var oldLibrary = FindLibrary(currentPath); |
| | | 185 | | // Use the stored ID when possible. The case sensitivity setting may have changed since the library was created. |
| | 2 | 186 | | var oldLibraryId = oldLibrary?.Id ?? _libraryManager.GetNewItemId(currentPath, typeof(CollectionFolder)); |
| | 2 | 187 | | var oldLibraryName = oldLibrary?.Name ?? Path.GetFileName(currentPath); |
| | 2 | 188 | | var newLibraryId = _libraryManager.GetNewItemId(newPath, typeof(CollectionFolder)); |
| | | 189 | | |
| | 2 | 190 | | _libraryMonitor.Stop(); |
| | | 191 | | |
| | | 192 | | try |
| | | 193 | | { |
| | | 194 | | // Changing capitalization. Handle windows case insensitivity |
| | 2 | 195 | | if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase)) |
| | | 196 | | { |
| | 1 | 197 | | var tempPath = Path.Combine( |
| | 1 | 198 | | rootFolderPath, |
| | 1 | 199 | | Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture)); |
| | 1 | 200 | | _directoryService.Move(currentPath, tempPath); |
| | 1 | 201 | | currentPath = tempPath; |
| | | 202 | | } |
| | | 203 | | |
| | 2 | 204 | | _directoryService.Move(currentPath, newPath); |
| | | 205 | | |
| | 2 | 206 | | await ReplaceLibraryIdInUserPreferencesAsync( |
| | 2 | 207 | | oldLibraryId, |
| | 2 | 208 | | newLibraryId, |
| | 2 | 209 | | oldLibraryName, |
| | 2 | 210 | | Path.GetFileName(newPath), |
| | 2 | 211 | | oldLibrary?.CollectionType).ConfigureAwait(false); |
| | 2 | 212 | | } |
| | | 213 | | finally |
| | | 214 | | { |
| | 2 | 215 | | CollectionFolder.OnCollectionFolderChange(); |
| | | 216 | | |
| | 2 | 217 | | _ = Task.Run(() => RefreshAfterRenameAsync(newPath, refreshLibrary)); |
| | | 218 | | } |
| | | 219 | | |
| | 2 | 220 | | return NoContent(); |
| | 11 | 221 | | } |
| | | 222 | | |
| | | 223 | | private async Task RefreshAfterRenameAsync(string newPath, bool refreshLibrary) |
| | | 224 | | { |
| | | 225 | | // No need to start if scanning the library because it will handle it |
| | 2 | 226 | | if (refreshLibrary) |
| | | 227 | | { |
| | 1 | 228 | | await _libraryManager.ValidateTopLibraryFolders(CancellationToken.None, true).ConfigureAwait(false); |
| | 1 | 229 | | var newLib = _libraryManager.GetUserRootFolder().Children.FirstOrDefault(f => f.Path.Equals(newPath, StringC |
| | 1 | 230 | | if (newLib is CollectionFolder folder) |
| | | 231 | | { |
| | 1 | 232 | | _libraryManager.ClearIgnoreRuleCache(); |
| | 2 | 233 | | foreach (var child in folder.GetPhysicalFolders()) |
| | | 234 | | { |
| | 0 | 235 | | await child.RefreshMetadata(CancellationToken.None).ConfigureAwait(false); |
| | 0 | 236 | | await child.ValidateChildren(new Progress<double>(), CancellationToken.None).ConfigureAwait(false); |
| | 0 | 237 | | } |
| | | 238 | | } |
| | | 239 | | else |
| | | 240 | | { |
| | 0 | 241 | | _libraryManager.ClearIgnoreRuleCache(); |
| | | 242 | | // We don't know if this one can be validated individually, trigger a new validation |
| | 0 | 243 | | _libraryManager.QueueLibraryScan(); |
| | | 244 | | } |
| | | 245 | | |
| | 1 | 246 | | _libraryManager.ClearIgnoreRuleCache(); |
| | | 247 | | } |
| | | 248 | | else |
| | | 249 | | { |
| | | 250 | | // Need to add a delay here or directory watchers may still pick up the changes |
| | | 251 | | // Have to block here to allow exceptions to bubble |
| | 1 | 252 | | await Task.Delay(1000).ConfigureAwait(false); |
| | 1 | 253 | | _libraryMonitor.Start(); |
| | | 254 | | } |
| | 2 | 255 | | } |
| | | 256 | | |
| | | 257 | | private CollectionFolder? FindLibrary(string path) |
| | 2 | 258 | | => _libraryManager.GetUserRootFolder().Children |
| | 2 | 259 | | .OfType<CollectionFolder>() |
| | 2 | 260 | | .FirstOrDefault(f => string.Equals(f.Path, path, StringComparison.OrdinalIgnoreCase)); |
| | | 261 | | |
| | | 262 | | private async Task ReplaceLibraryIdInUserPreferencesAsync( |
| | | 263 | | Guid oldId, |
| | | 264 | | Guid newId, |
| | | 265 | | string oldName, |
| | | 266 | | string newName, |
| | | 267 | | CollectionType? viewType) |
| | | 268 | | { |
| | 2 | 269 | | var shadowView = ( |
| | 2 | 270 | | Old: _libraryManager.GetShadowViewId(oldName, oldId, viewType), |
| | 2 | 271 | | New: _libraryManager.GetShadowViewId(newName, newId, viewType)); |
| | | 272 | | |
| | 2 | 273 | | if (oldId.Equals(newId) && shadowView.Old.Equals(shadowView.New)) |
| | | 274 | | { |
| | 0 | 275 | | return; |
| | | 276 | | } |
| | | 277 | | |
| | 2 | 278 | | PreferenceKind[] libraryPreferences = |
| | 2 | 279 | | [ |
| | 2 | 280 | | PreferenceKind.EnabledFolders, |
| | 2 | 281 | | PreferenceKind.BlockedMediaFolders, |
| | 2 | 282 | | PreferenceKind.EnableContentDeletionFromFolders, |
| | 2 | 283 | | PreferenceKind.LatestItemExcludes, |
| | 2 | 284 | | PreferenceKind.MyMediaExcludes, |
| | 2 | 285 | | PreferenceKind.GroupedFolders, |
| | 2 | 286 | | PreferenceKind.OrderedViews |
| | 2 | 287 | | ]; |
| | | 288 | | |
| | | 289 | | // These lists hold what the "My Media" row shows, which includes the views built from a library. |
| | 2 | 290 | | PreferenceKind[] viewPreferences = [PreferenceKind.MyMediaExcludes, PreferenceKind.OrderedViews]; |
| | | 291 | | |
| | 14 | 292 | | foreach (var user in _userManager.GetUsers()) |
| | | 293 | | { |
| | 5 | 294 | | var libraryMap = new Dictionary<Guid, Guid> { [oldId] = newId }; |
| | 5 | 295 | | var viewMap = new Dictionary<Guid, Guid>(libraryMap) |
| | 5 | 296 | | { |
| | 5 | 297 | | [shadowView.Old] = shadowView.New, |
| | 5 | 298 | | [_libraryManager.GetNamedViewId(user, oldId, viewType)] = _libraryManager.GetNamedViewId(user, newId, vi |
| | 5 | 299 | | }; |
| | | 300 | | |
| | 5 | 301 | | var changed = false; |
| | | 302 | | |
| | 80 | 303 | | foreach (var kind in libraryPreferences) |
| | | 304 | | { |
| | 35 | 305 | | changed |= ReplaceIds(user, kind, viewPreferences.Contains(kind) ? viewMap : libraryMap); |
| | | 306 | | } |
| | | 307 | | |
| | 5 | 308 | | if (changed) |
| | | 309 | | { |
| | 2 | 310 | | await _userManager.UpdateUserAsync(user).ConfigureAwait(false); |
| | | 311 | | } |
| | | 312 | | } |
| | 2 | 313 | | } |
| | | 314 | | |
| | | 315 | | private static bool ReplaceIds(User user, PreferenceKind kind, Dictionary<Guid, Guid> map) |
| | | 316 | | { |
| | 35 | 317 | | var values = user.GetPreference(kind); |
| | 35 | 318 | | var rewritten = new List<string>(values.Length); |
| | 35 | 319 | | var seen = new HashSet<Guid>(); |
| | 35 | 320 | | var touched = false; |
| | | 321 | | |
| | 108 | 322 | | foreach (var value in values) |
| | | 323 | | { |
| | | 324 | | // Clients write these in both the dashed and the plain form, so compare them parsed. |
| | 19 | 325 | | if (!Guid.TryParse(value, out var parsed)) |
| | | 326 | | { |
| | 0 | 327 | | rewritten.Add(value); |
| | 0 | 328 | | continue; |
| | | 329 | | } |
| | | 330 | | |
| | 19 | 331 | | var isReplaced = map.TryGetValue(parsed, out var replacement) && !replacement.Equals(parsed); |
| | 19 | 332 | | if (isReplaced) |
| | | 333 | | { |
| | 12 | 334 | | parsed = replacement; |
| | 12 | 335 | | touched = true; |
| | | 336 | | } |
| | | 337 | | |
| | 19 | 338 | | if (!seen.Add(parsed)) |
| | | 339 | | { |
| | 1 | 340 | | touched = true; |
| | 1 | 341 | | continue; |
| | | 342 | | } |
| | | 343 | | |
| | 18 | 344 | | var format = value.Contains('-', StringComparison.Ordinal) ? "D" : "N"; |
| | 18 | 345 | | rewritten.Add(isReplaced ? parsed.ToString(format, CultureInfo.InvariantCulture) : value); |
| | | 346 | | } |
| | | 347 | | |
| | 35 | 348 | | if (touched) |
| | | 349 | | { |
| | 9 | 350 | | user.SetPreference(kind, rewritten.ToArray()); |
| | | 351 | | } |
| | | 352 | | |
| | 35 | 353 | | return touched; |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | /// <summary> |
| | | 357 | | /// Add a media path to a library. |
| | | 358 | | /// </summary> |
| | | 359 | | /// <param name="mediaPathDto">The media path dto.</param> |
| | | 360 | | /// <param name="refreshLibrary">Whether to refresh the library.</param> |
| | | 361 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 362 | | /// <response code="204">Media path added.</response> |
| | | 363 | | /// <exception cref="ArgumentNullException">The name of the library may not be empty.</exception> |
| | | 364 | | [HttpPost("Paths")] |
| | | 365 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 366 | | public ActionResult AddMediaPath( |
| | | 367 | | [FromBody, Required] MediaPathDto mediaPathDto, |
| | | 368 | | [FromQuery] bool refreshLibrary = false) |
| | | 369 | | { |
| | 1 | 370 | | _libraryMonitor.Stop(); |
| | | 371 | | |
| | | 372 | | try |
| | | 373 | | { |
| | 1 | 374 | | var mediaPath = mediaPathDto.PathInfo ?? new MediaPathInfo(mediaPathDto.Path ?? throw new ArgumentException( |
| | | 375 | | |
| | 1 | 376 | | _libraryManager.AddMediaPath(mediaPathDto.Name, mediaPath); |
| | 0 | 377 | | } |
| | | 378 | | finally |
| | | 379 | | { |
| | 1 | 380 | | Task.Run(async () => |
| | 1 | 381 | | { |
| | 1 | 382 | | // No need to start if scanning the library because it will handle it |
| | 1 | 383 | | if (refreshLibrary) |
| | 1 | 384 | | { |
| | 1 | 385 | | _libraryManager.QueueLibraryScan(); |
| | 1 | 386 | | } |
| | 1 | 387 | | else |
| | 1 | 388 | | { |
| | 1 | 389 | | // Need to add a delay here or directory watchers may still pick up the changes |
| | 1 | 390 | | // Have to block here to allow exceptions to bubble |
| | 1 | 391 | | await Task.Delay(1000).ConfigureAwait(false); |
| | 1 | 392 | | _libraryMonitor.Start(); |
| | 1 | 393 | | } |
| | 1 | 394 | | }); |
| | 1 | 395 | | } |
| | | 396 | | |
| | 0 | 397 | | return NoContent(); |
| | | 398 | | } |
| | | 399 | | |
| | | 400 | | /// <summary> |
| | | 401 | | /// Updates a media path. |
| | | 402 | | /// </summary> |
| | | 403 | | /// <param name="mediaPathRequestDto">The name of the library and path infos.</param> |
| | | 404 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 405 | | /// <response code="204">Media path updated.</response> |
| | | 406 | | /// <exception cref="ArgumentNullException">The name of the library may not be empty.</exception> |
| | | 407 | | [HttpPost("Paths/Update")] |
| | | 408 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 409 | | public ActionResult UpdateMediaPath([FromBody, Required] UpdateMediaPathRequestDto mediaPathRequestDto) |
| | | 410 | | { |
| | 0 | 411 | | if (string.IsNullOrWhiteSpace(mediaPathRequestDto.Name)) |
| | | 412 | | { |
| | 0 | 413 | | throw new ArgumentNullException(nameof(mediaPathRequestDto), "Name must not be null or empty"); |
| | | 414 | | } |
| | | 415 | | |
| | 0 | 416 | | _libraryManager.UpdateMediaPath(mediaPathRequestDto.Name, mediaPathRequestDto.PathInfo); |
| | 0 | 417 | | return NoContent(); |
| | | 418 | | } |
| | | 419 | | |
| | | 420 | | /// <summary> |
| | | 421 | | /// Remove a media path. |
| | | 422 | | /// </summary> |
| | | 423 | | /// <param name="name">The name of the library.</param> |
| | | 424 | | /// <param name="path">The path to remove.</param> |
| | | 425 | | /// <param name="refreshLibrary">Whether to refresh the library.</param> |
| | | 426 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 427 | | /// <response code="204">Media path removed.</response> |
| | | 428 | | /// <exception cref="ArgumentException">The name of the library and path may not be empty.</exception> |
| | | 429 | | [HttpDelete("Paths")] |
| | | 430 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 431 | | public ActionResult RemoveMediaPath( |
| | | 432 | | [FromQuery] string name, |
| | | 433 | | [FromQuery] string path, |
| | | 434 | | [FromQuery] bool refreshLibrary = false) |
| | | 435 | | { |
| | 1 | 436 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | 1 | 437 | | ArgumentException.ThrowIfNullOrWhiteSpace(path); |
| | | 438 | | |
| | 1 | 439 | | _libraryMonitor.Stop(); |
| | | 440 | | |
| | | 441 | | try |
| | | 442 | | { |
| | 1 | 443 | | _libraryManager.RemoveMediaPath(name, path); |
| | 0 | 444 | | } |
| | | 445 | | finally |
| | | 446 | | { |
| | 1 | 447 | | Task.Run(async () => |
| | 1 | 448 | | { |
| | 1 | 449 | | // No need to start if scanning the library because it will handle it |
| | 1 | 450 | | if (refreshLibrary) |
| | 1 | 451 | | { |
| | 1 | 452 | | _libraryManager.QueueLibraryScan(); |
| | 1 | 453 | | } |
| | 1 | 454 | | else |
| | 1 | 455 | | { |
| | 1 | 456 | | // Need to add a delay here or directory watchers may still pick up the changes |
| | 1 | 457 | | // Have to block here to allow exceptions to bubble |
| | 1 | 458 | | await Task.Delay(1000).ConfigureAwait(false); |
| | 1 | 459 | | _libraryMonitor.Start(); |
| | 1 | 460 | | } |
| | 1 | 461 | | }); |
| | 1 | 462 | | } |
| | | 463 | | |
| | 0 | 464 | | return NoContent(); |
| | | 465 | | } |
| | | 466 | | |
| | | 467 | | /// <summary> |
| | | 468 | | /// Update library options. |
| | | 469 | | /// </summary> |
| | | 470 | | /// <param name="request">The library name and options.</param> |
| | | 471 | | /// <response code="204">Library updated.</response> |
| | | 472 | | /// <response code="404">Item not found.</response> |
| | | 473 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 474 | | [HttpPost("LibraryOptions")] |
| | | 475 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 476 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 477 | | public ActionResult UpdateLibraryOptions( |
| | | 478 | | [FromBody] UpdateLibraryOptionsDto request) |
| | | 479 | | { |
| | 2 | 480 | | var item = _libraryManager.GetItemById<CollectionFolder>(request.Id); |
| | 2 | 481 | | if (item is null) |
| | | 482 | | { |
| | 1 | 483 | | return NotFound(); |
| | | 484 | | } |
| | | 485 | | |
| | 1 | 486 | | LibraryOptions options = item.GetLibraryOptions(); |
| | 2 | 487 | | foreach (var mediaPath in request.LibraryOptions!.PathInfos) |
| | | 488 | | { |
| | 0 | 489 | | if (options.PathInfos.Any(i => i.Path == mediaPath.Path)) |
| | | 490 | | { |
| | | 491 | | continue; |
| | | 492 | | } |
| | | 493 | | |
| | 0 | 494 | | _libraryManager.CreateShortcut(item.Path, mediaPath); |
| | | 495 | | } |
| | | 496 | | |
| | 1 | 497 | | item.UpdateLibraryOptions(request.LibraryOptions); |
| | 1 | 498 | | return NoContent(); |
| | | 499 | | } |
| | | 500 | | } |