This commit is contained in:
Eric Reed 2013-11-04 13:17:03 -05:00
commit e89a0d080b
6 changed files with 140 additions and 150 deletions

View file

@ -1,7 +1,6 @@
using MediaBrowser.Common.IO; using MediaBrowser.Common.IO;
using MediaBrowser.Controller; using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.IO;
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -22,95 +21,6 @@ namespace MediaBrowser.Api.Library
/// </summary> /// </summary>
private const string ShortcutFileSearch = "*" + ShortcutFileExtension; private const string ShortcutFileSearch = "*" + ShortcutFileExtension;
/// <summary>
/// Adds the virtual folder.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="name">The name.</param>
/// <param name="collectionType">Type of the collection.</param>
/// <param name="user">The user.</param>
/// <param name="appPaths">The app paths.</param>
/// <exception cref="System.ArgumentException">There is already a media collection with the name + name + .</exception>
public static void AddVirtualFolder(IFileSystem fileSystem, string name, string collectionType, User user, IServerApplicationPaths appPaths)
{
name = fileSystem.GetValidFilename(name);
var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
var virtualFolderPath = Path.Combine(rootFolderPath, name);
if (Directory.Exists(virtualFolderPath))
{
throw new ArgumentException("There is already a media collection with the name " + name + ".");
}
Directory.CreateDirectory(virtualFolderPath);
if (!string.IsNullOrEmpty(collectionType))
{
var path = Path.Combine(virtualFolderPath, collectionType + ".collection");
File.Create(path);
}
}
/// <summary>
/// Removes the virtual folder.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="user">The user.</param>
/// <param name="appPaths">The app paths.</param>
/// <exception cref="System.IO.DirectoryNotFoundException">The media folder does not exist</exception>
public static void RemoveVirtualFolder(string name, User user, IServerApplicationPaths appPaths)
{
var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
var path = Path.Combine(rootFolderPath, name);
if (!Directory.Exists(path))
{
throw new DirectoryNotFoundException("The media folder does not exist");
}
Directory.Delete(path, true);
}
/// <summary>
/// Renames the virtual folder.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="newName">The new name.</param>
/// <param name="user">The user.</param>
/// <param name="appPaths">The app paths.</param>
/// <exception cref="System.IO.DirectoryNotFoundException">The media collection does not exist</exception>
/// <exception cref="System.ArgumentException">There is already a media collection with the name + newPath + .</exception>
public static void RenameVirtualFolder(string name, string newName, User user, IServerApplicationPaths appPaths)
{
var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
var currentPath = Path.Combine(rootFolderPath, name);
var newPath = Path.Combine(rootFolderPath, 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 + ".");
}
//Only make a two-phase move when changing capitalization
if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))
{
//Create an unique name
var temporaryName = Guid.NewGuid().ToString();
var temporaryPath = Path.Combine(rootFolderPath, temporaryName);
Directory.Move(currentPath,temporaryPath);
currentPath = temporaryPath;
}
Directory.Move(currentPath, newPath);
}
/// <summary> /// <summary>
/// Deletes a shortcut from within a virtual folder, within either the default view or a user view /// Deletes a shortcut from within a virtual folder, within either the default view or a user view
/// </summary> /// </summary>

View file

@ -3,9 +3,11 @@ using MediaBrowser.Controller;
using MediaBrowser.Controller.IO; using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using ServiceStack.ServiceHost; using ServiceStack.ServiceHost;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -188,6 +190,7 @@ namespace MediaBrowser.Api.Library
private readonly IDirectoryWatchers _directoryWatchers; private readonly IDirectoryWatchers _directoryWatchers;
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
private readonly ILogger _logger;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="LibraryStructureService"/> class. /// Initializes a new instance of the <see cref="LibraryStructureService"/> class.
@ -196,7 +199,7 @@ namespace MediaBrowser.Api.Library
/// <param name="userManager">The user manager.</param> /// <param name="userManager">The user manager.</param>
/// <param name="libraryManager">The library manager.</param> /// <param name="libraryManager">The library manager.</param>
/// <exception cref="System.ArgumentNullException">appPaths</exception> /// <exception cref="System.ArgumentNullException">appPaths</exception>
public LibraryStructureService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IDirectoryWatchers directoryWatchers, IFileSystem fileSystem) public LibraryStructureService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IDirectoryWatchers directoryWatchers, IFileSystem fileSystem, ILogger logger)
{ {
if (appPaths == null) if (appPaths == null)
{ {
@ -208,6 +211,7 @@ namespace MediaBrowser.Api.Library
_libraryManager = libraryManager; _libraryManager = libraryManager;
_directoryWatchers = directoryWatchers; _directoryWatchers = directoryWatchers;
_fileSystem = fileSystem; _fileSystem = fileSystem;
_logger = logger;
} }
/// <summary> /// <summary>
@ -239,19 +243,40 @@ namespace MediaBrowser.Api.Library
/// <param name="request">The request.</param> /// <param name="request">The request.</param>
public void Post(AddVirtualFolder request) public void Post(AddVirtualFolder request)
{ {
var name = _fileSystem.GetValidFilename(request.Name);
string rootFolderPath;
if (string.IsNullOrEmpty(request.UserId))
{
rootFolderPath = _appPaths.DefaultUserViewsPath;
}
else
{
var user = _userManager.GetUserById(new Guid(request.UserId));
rootFolderPath = user.RootFolderPath;
}
var virtualFolderPath = Path.Combine(rootFolderPath, name);
if (Directory.Exists(virtualFolderPath))
{
throw new ArgumentException("There is already a media collection with the name " + name + ".");
}
_directoryWatchers.Stop(); _directoryWatchers.Stop();
_directoryWatchers.TemporarilyIgnore(virtualFolderPath);
try try
{ {
if (string.IsNullOrEmpty(request.UserId)) Directory.CreateDirectory(virtualFolderPath);
{
LibraryHelpers.AddVirtualFolder(_fileSystem, request.Name, request.CollectionType, null, _appPaths);
}
else
{
var user = _userManager.GetUserById(new Guid(request.UserId));
LibraryHelpers.AddVirtualFolder(_fileSystem, request.Name, request.CollectionType, user, _appPaths); if (!string.IsNullOrEmpty(request.CollectionType))
{
var path = Path.Combine(virtualFolderPath, request.CollectionType + ".collection");
File.Create(path);
} }
// Need to add a delay here or directory watchers may still pick up the changes // Need to add a delay here or directory watchers may still pick up the changes
@ -262,6 +287,7 @@ namespace MediaBrowser.Api.Library
finally finally
{ {
_directoryWatchers.Start(); _directoryWatchers.Start();
_directoryWatchers.RemoveTempIgnore(virtualFolderPath);
} }
if (request.RefreshLibrary) if (request.RefreshLibrary)
@ -276,20 +302,49 @@ namespace MediaBrowser.Api.Library
/// <param name="request">The request.</param> /// <param name="request">The request.</param>
public void Post(RenameVirtualFolder request) 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.Stop();
_directoryWatchers.TemporarilyIgnore(currentPath);
_directoryWatchers.TemporarilyIgnore(newPath);
try 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 // Need to add a delay here or directory watchers may still pick up the changes
var task = Task.Delay(1000); var task = Task.Delay(1000);
@ -299,6 +354,8 @@ namespace MediaBrowser.Api.Library
finally finally
{ {
_directoryWatchers.Start(); _directoryWatchers.Start();
_directoryWatchers.RemoveTempIgnore(currentPath);
_directoryWatchers.RemoveTempIgnore(newPath);
} }
if (request.RefreshLibrary) if (request.RefreshLibrary)
@ -313,29 +370,42 @@ namespace MediaBrowser.Api.Library
/// <param name="request">The request.</param> /// <param name="request">The request.</param>
public void Delete(RemoveVirtualFolder request) public void Delete(RemoveVirtualFolder request)
{ {
string rootFolderPath;
if (string.IsNullOrEmpty(request.UserId))
{
rootFolderPath = _appPaths.DefaultUserViewsPath;
}
else
{
var user = _userManager.GetUserById(new Guid(request.UserId));
rootFolderPath = user.RootFolderPath;
}
var path = Path.Combine(rootFolderPath, request.Name);
if (!Directory.Exists(path))
{
throw new DirectoryNotFoundException("The media folder does not exist");
}
_directoryWatchers.Stop(); _directoryWatchers.Stop();
_directoryWatchers.TemporarilyIgnore(path);
try try
{ {
if (string.IsNullOrEmpty(request.UserId)) Directory.Delete(path, true);
{
LibraryHelpers.RemoveVirtualFolder(request.Name, null, _appPaths);
}
else
{
var user = _userManager.GetUserById(new Guid(request.UserId));
LibraryHelpers.RemoveVirtualFolder(request.Name, user, _appPaths);
}
// Need to add a delay here or directory watchers may still pick up the changes // Need to add a delay here or directory watchers may still pick up the changes
var task = Task.Delay(1000); var delayTask = Task.Delay(1000);
// Have to block here to allow exceptions to bubble // Have to block here to allow exceptions to bubble
Task.WaitAll(task); Task.WaitAll(delayTask);
} }
finally finally
{ {
_directoryWatchers.Start(); _directoryWatchers.Start();
_directoryWatchers.RemoveTempIgnore(path);
} }
if (request.RefreshLibrary) if (request.RefreshLibrary)

View file

@ -495,8 +495,7 @@ namespace MediaBrowser.Api
{ {
try try
{ {
await await parent.ValidateChildren(new Progress<double>(), CancellationToken.None)
parent.ValidateChildren(new Progress<double>(), CancellationToken.None)
.ConfigureAwait(false); .ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)

View file

@ -1347,12 +1347,14 @@ namespace MediaBrowser.Controller.Entities
try try
{ {
if (LocationType == LocationType.Remote && string.Equals(Path, path, StringComparison.OrdinalIgnoreCase)) var locationType = LocationType;
if (locationType == LocationType.Remote && string.Equals(Path, path, StringComparison.OrdinalIgnoreCase))
{ {
return this; return this;
} }
if (LocationType != LocationType.Virtual && ResolveArgs.PhysicalLocations.Contains(path, StringComparer.OrdinalIgnoreCase)) if (locationType != LocationType.Virtual && ResolveArgs.PhysicalLocations.Contains(path, StringComparer.OrdinalIgnoreCase))
{ {
return this; return this;
} }

View file

@ -358,39 +358,36 @@ namespace MediaBrowser.Server.Implementations.IO
var tempIgnorePaths = _tempIgnoredPaths.Keys.ToList(); var tempIgnorePaths = _tempIgnoredPaths.Keys.ToList();
if (e.ChangeType == WatcherChangeTypes.Changed) // If the parent of an ignored path has a change event, ignore that too
if (tempIgnorePaths.Any(i =>
{ {
// If the parent of an ignored path has a change event, ignore that too if (string.Equals(i, e.FullPath, StringComparison.OrdinalIgnoreCase))
if (tempIgnorePaths.Any(i =>
{ {
if (string.Equals(i, e.FullPath, StringComparison.OrdinalIgnoreCase)) return true;
{ }
return true;
}
// Go up a level // Go up a level
var parent = Path.GetDirectoryName(i); var parent = Path.GetDirectoryName(i);
if (string.Equals(parent, e.FullPath, StringComparison.OrdinalIgnoreCase))
{
return true;
}
// Go up another level
if (!string.IsNullOrEmpty(parent))
{
parent = Path.GetDirectoryName(i);
if (string.Equals(parent, e.FullPath, StringComparison.OrdinalIgnoreCase)) if (string.Equals(parent, e.FullPath, StringComparison.OrdinalIgnoreCase))
{ {
return true; return true;
} }
// Go up another level
if (!string.IsNullOrEmpty(parent))
{
parent = Path.GetDirectoryName(i);
if (string.Equals(parent, e.FullPath, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}))
{
return;
} }
return false;
}))
{
return;
} }
if (tempIgnorePaths.Contains(e.FullPath, StringComparer.OrdinalIgnoreCase)) if (tempIgnorePaths.Contains(e.FullPath, StringComparer.OrdinalIgnoreCase))

View file

@ -1430,10 +1430,22 @@ namespace MediaBrowser.Server.Implementations.Library
.OfType<CollectionFolder>() .OfType<CollectionFolder>()
.Where(i => .Where(i =>
{ {
var locationType = i.LocationType;
if (locationType == LocationType.Remote || locationType == LocationType.Virtual)
{
return false;
}
if (string.Equals(i.Path, item.Path, StringComparison.OrdinalIgnoreCase))
{
return true;
}
try try
{ {
return i.LocationType != LocationType.Remote && i.LocationType != LocationType.Virtual &&
i.ResolveArgs.PhysicalLocations.Contains(item.Path); return i.ResolveArgs.PhysicalLocations.Contains(item.Path);
} }
catch (IOException ex) catch (IOException ex)
{ {