/// <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")); 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(); } }); } }
public ActionResult RenameVirtualFolder( [FromQuery] string?name, [FromQuery] string?newName, [FromQuery] bool refreshLibrary = false) { 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(async() => { // No need to start if scanning the library because it will handle it if (refreshLibrary) { await _libraryManager.ValidateMediaLibrary(new SimpleProgress <double>(), CancellationToken.None).ConfigureAwait(false); } else { // Need to add a delay here or directory watchers may still pick up the changes // Have to block here to allow exceptions to bubble await Task.Delay(1000).ConfigureAwait(false); _libraryMonitor.Start(); } }); } return(NoContent()); }