diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-11-04 11:26:49 -0500 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-11-04 11:26:49 -0500 |
| commit | 4b886ea93f3a9c295ff1da36a6cdf33c677de5e5 (patch) | |
| tree | 3c9788d9213726dad7742e645a9bb34ef3bfb2f6 /MediaBrowser.Api/Library/LibraryStructureService.cs | |
| parent | 8792ed9ab08b44323284344c16793ce3a3071bcd (diff) | |
fix multiple scan restarts when renaming collection
Diffstat (limited to 'MediaBrowser.Api/Library/LibraryStructureService.cs')
| -rw-r--r-- | MediaBrowser.Api/Library/LibraryStructureService.cs | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/MediaBrowser.Api/Library/LibraryStructureService.cs b/MediaBrowser.Api/Library/LibraryStructureService.cs index 5738dcb11..f3306bb63 100644 --- a/MediaBrowser.Api/Library/LibraryStructureService.cs +++ b/MediaBrowser.Api/Library/LibraryStructureService.cs @@ -1,5 +1,4 @@ -using System.IO; -using MediaBrowser.Common.IO; +using MediaBrowser.Common.IO; using MediaBrowser.Controller; using MediaBrowser.Controller.IO; using MediaBrowser.Controller.Library; @@ -8,6 +7,7 @@ using MediaBrowser.Model.Logging; using ServiceStack.ServiceHost; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -302,20 +302,49 @@ namespace MediaBrowser.Api.Library /// <param name="request">The request.</param> public void Post(RenameVirtualFolder request) { + string rootFolderPath; + + if (string.IsNullOrEmpty(request.UserId)) + { + rootFolderPath = _appPaths.DefaultUserViewsPath; + } + else + { + var user = _userManager.GetUserById(new Guid(request.UserId)); + + rootFolderPath = user.RootFolderPath; + } + + var currentPath = Path.Combine(rootFolderPath, request.Name); + var newPath = Path.Combine(rootFolderPath, request.NewName); + + if (!Directory.Exists(currentPath)) + { + throw new DirectoryNotFoundException("The media collection does not exist"); + } + + if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(newPath)) + { + throw new ArgumentException("There is already a media collection with the name " + newPath + "."); + } + _directoryWatchers.Stop(); + _directoryWatchers.TemporarilyIgnore(currentPath); + _directoryWatchers.TemporarilyIgnore(newPath); try { - if (string.IsNullOrEmpty(request.UserId)) + // Only make a two-phase move when changing capitalization + if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase)) { - LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, null, _appPaths); + //Create an unique name + var temporaryName = Guid.NewGuid().ToString(); + var temporaryPath = Path.Combine(rootFolderPath, temporaryName); + Directory.Move(currentPath, temporaryPath); + currentPath = temporaryPath; } - else - { - var user = _userManager.GetUserById(new Guid(request.UserId)); - LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, user, _appPaths); - } + Directory.Move(currentPath, newPath); // Need to add a delay here or directory watchers may still pick up the changes var task = Task.Delay(1000); @@ -325,6 +354,8 @@ namespace MediaBrowser.Api.Library finally { _directoryWatchers.Start(); + _directoryWatchers.RemoveTempIgnore(currentPath); + _directoryWatchers.RemoveTempIgnore(newPath); } if (request.RefreshLibrary) |
