2013-10-31 14:03:23 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Controller;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2013-02-25 03:56:00 +00:00
|
|
|
|
namespace MediaBrowser.Api.Library
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2013-02-25 03:56:00 +00:00
|
|
|
|
/// Class LibraryHelpers
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2013-02-25 03:56:00 +00:00
|
|
|
|
public static class LibraryHelpers
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-10-30 14:40:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The shortcut file extension
|
|
|
|
|
/// </summary>
|
2013-10-01 20:20:48 +00:00
|
|
|
|
private const string ShortcutFileExtension = ".mblink";
|
2013-10-30 14:40:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The shortcut file search
|
|
|
|
|
/// </summary>
|
2013-10-01 20:20:48 +00:00
|
|
|
|
private const string ShortcutFileSearch = "*" + ShortcutFileExtension;
|
2013-10-01 19:25:12 +00:00
|
|
|
|
|
2013-02-25 03:56:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes a shortcut from within a virtual folder, within either the default view or a user view
|
|
|
|
|
/// </summary>
|
2013-10-30 14:40:14 +00:00
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
2013-02-25 03:56:00 +00:00
|
|
|
|
/// <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>
|
2013-10-30 14:40:14 +00:00
|
|
|
|
public static void RemoveMediaPath(IFileSystem fileSystem, string virtualFolderName, string mediaPath, User user, IServerApplicationPaths appPaths)
|
2013-02-25 03:56:00 +00:00
|
|
|
|
{
|
|
|
|
|
var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
|
|
|
|
|
var path = Path.Combine(rootFolderPath, virtualFolderName);
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
{
|
2013-03-04 16:42:19 +00:00
|
|
|
|
throw new DirectoryNotFoundException(string.Format("The media collection {0} does not exist", virtualFolderName));
|
2013-02-25 03:56:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-30 14:40:14 +00:00
|
|
|
|
var shortcut = Directory.EnumerateFiles(path, ShortcutFileSearch, SearchOption.AllDirectories).FirstOrDefault(f => fileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase));
|
2013-02-25 03:56:00 +00:00
|
|
|
|
|
2013-03-04 16:42:19 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(shortcut))
|
2013-02-25 03:56:00 +00:00
|
|
|
|
{
|
2013-03-04 16:42:19 +00:00
|
|
|
|
File.Delete(shortcut);
|
2013-02-25 03:56:00 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
|
|
|
|
|
/// </summary>
|
2013-10-30 14:40:14 +00:00
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <param name="virtualFolderName">Name of the virtual folder.</param>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <param name="user">The user.</param>
|
2013-02-25 03:56:00 +00:00
|
|
|
|
/// <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>
|
2013-10-30 14:40:14 +00:00
|
|
|
|
public static void AddMediaPath(IFileSystem fileSystem, string virtualFolderName, string path, User user, IServerApplicationPaths appPaths)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
throw new DirectoryNotFoundException("The path does not exist.");
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-25 03:56:00 +00:00
|
|
|
|
var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
|
|
|
|
|
|
2014-01-01 18:26:31 +00:00
|
|
|
|
ValidateNewMediaPath(fileSystem, rootFolderPath, path);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
var shortcutFilename = Path.GetFileNameWithoutExtension(path);
|
|
|
|
|
|
2013-10-01 19:25:12 +00:00
|
|
|
|
var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
while (File.Exists(lnk))
|
|
|
|
|
{
|
|
|
|
|
shortcutFilename += "1";
|
2013-10-01 19:25:12 +00:00
|
|
|
|
lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-30 14:40:14 +00:00
|
|
|
|
fileSystem.CreateShortcut(lnk, path);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Validates that a new media path can be added
|
|
|
|
|
/// </summary>
|
2013-10-30 14:40:14 +00:00
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <param name="currentViewRootFolderPath">The current view root folder path.</param>
|
|
|
|
|
/// <param name="mediaPath">The media path.</param>
|
2013-10-30 14:40:14 +00:00
|
|
|
|
/// <exception cref="System.ArgumentException">
|
|
|
|
|
/// </exception>
|
2014-01-01 18:26:31 +00:00
|
|
|
|
private static void ValidateNewMediaPath(IFileSystem fileSystem, string currentViewRootFolderPath, string mediaPath)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2014-01-01 18:26:31 +00:00
|
|
|
|
var pathsInCurrentVIew = Directory.EnumerateFiles(currentViewRootFolderPath, ShortcutFileSearch, SearchOption.AllDirectories)
|
|
|
|
|
.Select(fileSystem.ResolveShortcut)
|
|
|
|
|
.ToList();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-25 01:51:17 +00:00
|
|
|
|
// Don't allow duplicate sub-paths within the same user library, or it will result in duplicate items
|
|
|
|
|
// See comments in IsNewPathValid
|
2014-01-01 18:26:31 +00:00
|
|
|
|
var duplicate = pathsInCurrentVIew
|
|
|
|
|
.FirstOrDefault(p => !IsNewPathValid(fileSystem, mediaPath, p));
|
2013-05-24 19:52:41 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(duplicate))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(string.Format("The path cannot be added to the library because {0} already exists.", duplicate));
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
// Make sure the current root folder doesn't already have a shortcut to the same path
|
2014-01-01 18:26:31 +00:00
|
|
|
|
duplicate = pathsInCurrentVIew
|
|
|
|
|
.FirstOrDefault(p => string.Equals(mediaPath, p, StringComparison.OrdinalIgnoreCase));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(duplicate))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(string.Format("The path {0} already exists in the library", mediaPath));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Validates that a new path can be added based on an existing path
|
|
|
|
|
/// </summary>
|
2014-01-01 18:26:31 +00:00
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <param name="newPath">The new 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>
|
2014-01-01 18:26:31 +00:00
|
|
|
|
private static bool IsNewPathValid(IFileSystem fileSystem, string newPath, string existingPath)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
// Example: D:\Movies is the existing path
|
|
|
|
|
// D:\ cannot be added
|
2013-05-24 19:52:41 +00:00
|
|
|
|
// Neither can D:\Movies\Kids
|
2013-02-21 01:33:05 +00:00
|
|
|
|
// A D:\Movies duplicate is ok here since that will be caught later
|
|
|
|
|
|
2014-01-01 18:26:31 +00:00
|
|
|
|
if (string.Equals(newPath, existingPath, StringComparison.OrdinalIgnoreCase))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 01:51:17 +00:00
|
|
|
|
// If enforceSubPathRestriction is true, validate the D:\Movies\Kids scenario
|
2014-01-01 18:26:31 +00:00
|
|
|
|
if (fileSystem.ContainsSubPath(existingPath, newPath))
|
2013-05-24 19:52:41 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
// Validate the D:\ scenario
|
2014-01-01 18:26:31 +00:00
|
|
|
|
if (fileSystem.ContainsSubPath(newPath, existingPath))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|