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 02:33:05 +01:00
using System;
using System.IO;
using System.Linq;
2015-10-04 06:23:11 +02:00
using CommonIO;
2013-02-21 02:33:05 +01:00
2013-02-25 04:56:00 +01:00
namespace MediaBrowser.Api.Library
2013-02-21 02:33:05 +01:00
{
/// <summary>
2013-02-25 04:56:00 +01:00
/// Class LibraryHelpers
2013-02-21 02:33:05 +01:00
/// </summary>
2013-02-25 04:56:00 +01:00
public static class LibraryHelpers
2013-02-21 02:33:05 +01:00
{
/// <summary>
/// The shortcut file extension
/// </summary>
2013-10-01 22:20:48 +02:00
private const string ShortcutFileExtension = ".mblink";
/// <summary>
/// The shortcut file search
/// </summary>
2013-10-01 22:20:48 +02:00
private const string ShortcutFileSearch = "*" + ShortcutFileExtension;
2013-10-01 21:25:12 +02:00
2013-02-25 04:56:00 +01: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 04:56:00 +01: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 06:04:11 +01:00
public static void RemoveMediaPath(IFileSystem fileSystem, string virtualFolderName, string mediaPath, IServerApplicationPaths appPaths)
2013-02-25 04:56:00 +01:00
{
2015-12-06 02:40:49 +01:00
if (string.IsNullOrWhiteSpace(mediaPath))
{
throw new ArgumentNullException("mediaPath");
}
2014-02-21 06:04:11 +01:00
var rootFolderPath = appPaths.DefaultUserViewsPath;
2013-02-25 04:56:00 +01:00
var path = Path.Combine(rootFolderPath, virtualFolderName);
2015-12-06 02:40:49 +01:00
if (!fileSystem.DirectoryExists(path))
2013-02-25 04:56:00 +01:00
{
2013-03-04 17:42:19 +01:00
throw new DirectoryNotFoundException(string.Format("The media collection {0} does not exist", virtualFolderName));
2013-02-25 04:56:00 +01:00
}
2015-09-24 19:50:49 +02:00
var shortcut = Directory.EnumerateFiles(path, ShortcutFileSearch, SearchOption.AllDirectories).FirstOrDefault(f => fileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase));
2013-02-25 04:56:00 +01:00
2013-03-04 17:42:19 +01:00
if (!string.IsNullOrEmpty(shortcut))
2013-02-25 04:56:00 +01:00
{
fileSystem.DeleteFile(shortcut);
2013-02-25 04:56:00 +01:00
}
2013-02-21 02:33:05 +01: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 02:33:05 +01:00
/// <param name="virtualFolderName">Name of the virtual folder.</param>
/// <param name="path">The path.</param>
2013-02-25 04:56:00 +01:00
/// <param name="appPaths">The app paths.</param>
2014-02-21 06:04:11 +01:00
public static void AddMediaPath(IFileSystem fileSystem, string virtualFolderName, string path, IServerApplicationPaths appPaths)
2013-02-21 02:33:05 +01:00
{
2015-12-06 02:40:49 +01:00
if (string.IsNullOrWhiteSpace(path))
2015-12-03 17:45:14 +01:00
{
throw new ArgumentNullException("path");
}
2015-09-13 23:32:02 +02:00
if (!fileSystem.DirectoryExists(path))
2013-02-21 02:33:05 +01:00
{
throw new DirectoryNotFoundException("The path does not exist.");
}
2014-02-21 06:04:11 +01:00
var rootFolderPath = appPaths.DefaultUserViewsPath;
2013-02-21 02:33:05 +01:00
var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
2014-07-26 19:30:15 +02:00
var shortcutFilename = fileSystem.GetFileNameWithoutExtension(path);
2013-02-21 02:33:05 +01:00
2013-10-01 21:25:12 +02:00
var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
2013-02-21 02:33:05 +01:00
2015-09-13 23:32:02 +02:00
while (fileSystem.FileExists(lnk))
2013-02-21 02:33:05 +01:00
{
shortcutFilename += "1";
2013-10-01 21:25:12 +02:00
lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
2013-02-21 02:33:05 +01:00
}
fileSystem.CreateShortcut(lnk, path);
2013-02-21 02:33:05 +01:00
}
}
}