jellyfin/MediaBrowser.Api/Library/LibraryHelpers.cs

91 lines
3.5 KiB
C#
Raw Normal View History

using MediaBrowser.Common.IO;
using MediaBrowser.Controller;
2013-02-21 01:33:05 +00:00
using System;
using System.IO;
using System.Linq;
2015-10-04 04:23:11 +00:00
using CommonIO;
2013-02-21 01:33:05 +00:00
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
{
/// <summary>
/// The shortcut file extension
/// </summary>
2013-10-01 20:20:48 +00:00
private const string ShortcutFileExtension = ".mblink";
/// <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>
/// <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="appPaths">The app paths.</param>
/// <exception cref="System.IO.DirectoryNotFoundException">The media folder does not exist</exception>
2014-02-21 05:04:11 +00:00
public static void RemoveMediaPath(IFileSystem fileSystem, string virtualFolderName, string mediaPath, IServerApplicationPaths appPaths)
2013-02-25 03:56:00 +00:00
{
2015-12-06 01:40:49 +00:00
if (string.IsNullOrWhiteSpace(mediaPath))
{
throw new ArgumentNullException("mediaPath");
}
2014-02-21 05:04:11 +00:00
var rootFolderPath = appPaths.DefaultUserViewsPath;
2013-02-25 03:56:00 +00:00
var path = Path.Combine(rootFolderPath, virtualFolderName);
2015-12-06 01:40:49 +00:00
if (!fileSystem.DirectoryExists(path))
2013-02-25 03:56:00 +00:00
{
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
}
2015-09-24 17:50:49 +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
{
fileSystem.DeleteFile(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>
/// <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>
2013-02-25 03:56:00 +00:00
/// <param name="appPaths">The app paths.</param>
2014-02-21 05:04:11 +00:00
public static void AddMediaPath(IFileSystem fileSystem, string virtualFolderName, string path, IServerApplicationPaths appPaths)
2013-02-21 01:33:05 +00:00
{
2015-12-06 01:40:49 +00:00
if (string.IsNullOrWhiteSpace(path))
2015-12-03 16:45:14 +00:00
{
throw new ArgumentNullException("path");
}
2015-09-13 21:32:02 +00:00
if (!fileSystem.DirectoryExists(path))
2013-02-21 01:33:05 +00:00
{
throw new DirectoryNotFoundException("The path does not exist.");
}
2014-02-21 05:04:11 +00:00
var rootFolderPath = appPaths.DefaultUserViewsPath;
2013-02-21 01:33:05 +00:00
var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
2014-07-26 17:30:15 +00:00
var shortcutFilename = fileSystem.GetFileNameWithoutExtension(path);
2013-02-21 01:33:05 +00:00
2013-10-01 19:25:12 +00:00
var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
2013-02-21 01:33:05 +00:00
2015-09-13 21:32:02 +00:00
while (fileSystem.FileExists(lnk))
2013-02-21 01:33:05 +00:00
{
shortcutFilename += "1";
2013-10-01 19:25:12 +00:00
lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
2013-02-21 01:33:05 +00:00
}
fileSystem.CreateShortcut(lnk, path);
2013-02-21 01:33:05 +00:00
}
}
}