|
|
|
@ -3,9 +3,11 @@ using MediaBrowser.Controller;
|
|
|
|
|
using MediaBrowser.Controller.IO;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
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;
|
|
|
|
@ -188,6 +190,7 @@ namespace MediaBrowser.Api.Library
|
|
|
|
|
private readonly IDirectoryWatchers _directoryWatchers;
|
|
|
|
|
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 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="libraryManager">The library manager.</param>
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
@ -208,6 +211,7 @@ namespace MediaBrowser.Api.Library
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_directoryWatchers = directoryWatchers;
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -239,19 +243,40 @@ namespace MediaBrowser.Api.Library
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
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.TemporarilyIgnore(virtualFolderPath);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(request.UserId))
|
|
|
|
|
{
|
|
|
|
|
LibraryHelpers.AddVirtualFolder(_fileSystem, request.Name, request.CollectionType, null, _appPaths);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Directory.CreateDirectory(virtualFolderPath);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(request.CollectionType))
|
|
|
|
|
{
|
|
|
|
|
var user = _userManager.GetUserById(new Guid(request.UserId));
|
|
|
|
|
var path = Path.Combine(virtualFolderPath, request.CollectionType + ".collection");
|
|
|
|
|
|
|
|
|
|
LibraryHelpers.AddVirtualFolder(_fileSystem, request.Name, request.CollectionType, user, _appPaths);
|
|
|
|
|
File.Create(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Need to add a delay here or directory watchers may still pick up the changes
|
|
|
|
@ -262,6 +287,7 @@ namespace MediaBrowser.Api.Library
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_directoryWatchers.Start();
|
|
|
|
|
_directoryWatchers.RemoveTempIgnore(virtualFolderPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.RefreshLibrary)
|
|
|
|
@ -276,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);
|
|
|
|
@ -299,6 +354,8 @@ namespace MediaBrowser.Api.Library
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_directoryWatchers.Start();
|
|
|
|
|
_directoryWatchers.RemoveTempIgnore(currentPath);
|
|
|
|
|
_directoryWatchers.RemoveTempIgnore(newPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.RefreshLibrary)
|
|
|
|
@ -313,29 +370,42 @@ namespace MediaBrowser.Api.Library
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
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.TemporarilyIgnore(path);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(request.UserId))
|
|
|
|
|
{
|
|
|
|
|
LibraryHelpers.RemoveVirtualFolder(request.Name, null, _appPaths);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var user = _userManager.GetUserById(new Guid(request.UserId));
|
|
|
|
|
|
|
|
|
|
LibraryHelpers.RemoveVirtualFolder(request.Name, user, _appPaths);
|
|
|
|
|
}
|
|
|
|
|
Directory.Delete(path, true);
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
Task.WaitAll(delayTask);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_directoryWatchers.Start();
|
|
|
|
|
_directoryWatchers.RemoveTempIgnore(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.RefreshLibrary)
|
|
|
|
|