|
|
@ -1,89 +1,113 @@
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
|
|
|
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;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
using MediaBrowser.Controller.IO;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
namespace MediaBrowser.Api.Library
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Makes changes to the user's media library
|
|
|
|
/// Class LibraryHelpers
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public class UpdateMediaLibraryHandler : BaseActionHandler<Kernel>
|
|
|
|
public static class LibraryHelpers
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Executes the action.
|
|
|
|
/// Adds the virtual folder.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <exception cref="System.NotImplementedException"></exception>
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
protected override Task ExecuteAction()
|
|
|
|
/// <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(string name, User user, IServerApplicationPaths appPaths)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return Task.Run(() =>
|
|
|
|
name = FileSystem.GetValidFilename(name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
|
|
|
|
|
|
|
|
var virtualFolderPath = Path.Combine(rootFolderPath, name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(virtualFolderPath))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var action = QueryString["action"];
|
|
|
|
throw new ArgumentException("There is already a media collection with the name " + name + ".");
|
|
|
|
|
|
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(action))
|
|
|
|
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(virtualFolderPath);
|
|
|
|
throw new ArgumentNullException();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
User user = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(QueryString["userId"]))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
user = ApiService.GetUserById(QueryString["userId"]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (action.Equals("AddVirtualFolder", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
AddVirtualFolder(Uri.UnescapeDataString(QueryString["name"]), user);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (action.Equals("RemoveVirtualFolder", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
RemoveVirtualFolder(QueryString["name"], user);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (action.Equals("RenameVirtualFolder", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
RenameVirtualFolder(QueryString["name"], QueryString["newName"], user);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (action.Equals("RemoveMediaPath", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
RemoveMediaPath(QueryString["virtualFolderName"], QueryString["mediaPath"], user);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (action.Equals("AddMediaPath", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
AddMediaPath(QueryString["virtualFolderName"], QueryString["mediaPath"], user);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a virtual folder to either the default view or a user view
|
|
|
|
/// Removes the virtual folder.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
private void AddVirtualFolder(string name, User user)
|
|
|
|
/// <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)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name = FileSystem.GetValidFilename(name);
|
|
|
|
var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
|
|
|
|
|
|
|
|
var path = Path.Combine(rootFolderPath, name);
|
|
|
|
|
|
|
|
|
|
|
|
var rootFolderPath = user != null ? user.RootFolderPath : Kernel.ApplicationPaths.DefaultUserViewsPath;
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
var virtualFolderPath = Path.Combine(rootFolderPath, name);
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new DirectoryNotFoundException("The media folder does not exist");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(virtualFolderPath))
|
|
|
|
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 ArgumentException("There is already a media collection with the name " + name + ".");
|
|
|
|
throw new DirectoryNotFoundException("The media collection does not exist");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(virtualFolderPath);
|
|
|
|
if (Directory.Exists(newPath))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new ArgumentException("There is already a media collection with the name " + newPath + ".");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Directory.Move(currentPath, newPath);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Deletes a shortcut from within a virtual folder, within either the default view or a user view
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="virtualFolderName">Name of the virtual folder.</param>
|
|
|
|
|
|
|
|
/// <param name="mediaPath">The media path.</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 RemoveMediaPath(string virtualFolderName, string mediaPath, User user, IServerApplicationPaths appPaths)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
|
|
|
|
|
|
|
|
var path = Path.Combine(rootFolderPath, virtualFolderName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new DirectoryNotFoundException("The media folder does not exist");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var shortcut = Directory.EnumerateFiles(path, "*.lnk", SearchOption.AllDirectories).FirstOrDefault(f => FileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(shortcut))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new DirectoryNotFoundException("The media folder does not exist");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
File.Delete(shortcut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
@ -92,7 +116,10 @@ namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
/// <param name="virtualFolderName">Name of the virtual folder.</param>
|
|
|
|
/// <param name="virtualFolderName">Name of the virtual folder.</param>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
private void AddMediaPath(string virtualFolderName, string path, User user)
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
|
|
|
/// <exception cref="System.ArgumentException">The path is not valid.</exception>
|
|
|
|
|
|
|
|
/// <exception cref="System.IO.DirectoryNotFoundException">The path does not exist.</exception>
|
|
|
|
|
|
|
|
public static void AddMediaPath(string virtualFolderName, string path, User user, IServerApplicationPaths appPaths)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!Path.IsPathRooted(path))
|
|
|
|
if (!Path.IsPathRooted(path))
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -111,10 +138,10 @@ namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
path += Path.DirectorySeparatorChar;
|
|
|
|
path += Path.DirectorySeparatorChar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var rootFolderPath = user != null ? user.RootFolderPath : Kernel.ApplicationPaths.DefaultUserViewsPath;
|
|
|
|
var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
|
|
|
|
var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
|
|
|
|
var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
|
|
|
|
|
|
|
|
|
|
|
|
ValidateNewMediaPath(rootFolderPath, path);
|
|
|
|
ValidateNewMediaPath(rootFolderPath, path, appPaths);
|
|
|
|
|
|
|
|
|
|
|
|
var shortcutFilename = Path.GetFileNameWithoutExtension(path);
|
|
|
|
var shortcutFilename = Path.GetFileNameWithoutExtension(path);
|
|
|
|
|
|
|
|
|
|
|
@ -134,9 +161,11 @@ namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="currentViewRootFolderPath">The current view root folder path.</param>
|
|
|
|
/// <param name="currentViewRootFolderPath">The current view root folder path.</param>
|
|
|
|
/// <param name="mediaPath">The media path.</param>
|
|
|
|
/// <param name="mediaPath">The media path.</param>
|
|
|
|
private void ValidateNewMediaPath(string currentViewRootFolderPath, string mediaPath)
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
|
|
|
/// <exception cref="System.ArgumentException"></exception>
|
|
|
|
|
|
|
|
private static void ValidateNewMediaPath(string currentViewRootFolderPath, string mediaPath, IServerApplicationPaths appPaths)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var duplicate = Directory.EnumerateFiles(Kernel.ApplicationPaths.RootFolderPath, "*.lnk", SearchOption.AllDirectories)
|
|
|
|
var duplicate = Directory.EnumerateFiles(appPaths.RootFolderPath, "*.lnk", SearchOption.AllDirectories)
|
|
|
|
.Select(FileSystem.ResolveShortcut)
|
|
|
|
.Select(FileSystem.ResolveShortcut)
|
|
|
|
.FirstOrDefault(p => !IsNewPathValid(mediaPath, p));
|
|
|
|
.FirstOrDefault(p => !IsNewPathValid(mediaPath, p));
|
|
|
|
|
|
|
|
|
|
|
@ -162,7 +191,7 @@ namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
/// <param name="newPath">The new path.</param>
|
|
|
|
/// <param name="newPath">The new path.</param>
|
|
|
|
/// <param name="existingPath">The existing path.</param>
|
|
|
|
/// <param name="existingPath">The existing path.</param>
|
|
|
|
/// <returns><c>true</c> if [is new path valid] [the specified new path]; otherwise, <c>false</c>.</returns>
|
|
|
|
/// <returns><c>true</c> if [is new path valid] [the specified new path]; otherwise, <c>false</c>.</returns>
|
|
|
|
private bool IsNewPathValid(string newPath, string existingPath)
|
|
|
|
private static bool IsNewPathValid(string newPath, string existingPath)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Example: D:\Movies is the existing path
|
|
|
|
// Example: D:\Movies is the existing path
|
|
|
|
// D:\ cannot be added
|
|
|
|
// D:\ cannot be added
|
|
|
@ -188,74 +217,5 @@ namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Renames a virtual folder within either the default view or a user view
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
|
|
|
|
/// <param name="newName">The new name.</param>
|
|
|
|
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
|
|
|
|
private void RenameVirtualFolder(string name, string newName, User user)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var rootFolderPath = user != null ? user.RootFolderPath : Kernel.ApplicationPaths.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 (Directory.Exists(newPath))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new ArgumentException("There is already a media collection with the name " + newPath + ".");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Directory.Move(currentPath, newPath);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Deletes a virtual folder from either the default view or a user view
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
|
|
|
|
private void RemoveVirtualFolder(string name, User user)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var rootFolderPath = user != null ? user.RootFolderPath : Kernel.ApplicationPaths.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>
|
|
|
|
|
|
|
|
/// Deletes a shortcut from within a virtual folder, within either the default view or a user view
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="virtualFolderName">Name of the virtual folder.</param>
|
|
|
|
|
|
|
|
/// <param name="mediaPath">The media path.</param>
|
|
|
|
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
|
|
|
|
private void RemoveMediaPath(string virtualFolderName, string mediaPath, User user)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var rootFolderPath = user != null ? user.RootFolderPath : Kernel.ApplicationPaths.DefaultUserViewsPath;
|
|
|
|
|
|
|
|
var path = Path.Combine(rootFolderPath, virtualFolderName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new DirectoryNotFoundException("The media folder does not exist");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var shortcut = Directory.EnumerateFiles(path, "*.lnk", SearchOption.AllDirectories).FirstOrDefault(f => FileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(shortcut))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new DirectoryNotFoundException("The media folder does not exist");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
File.Delete(shortcut);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|