jellyfin/MediaBrowser.Api/Library/LibraryHelpers.cs

82 lines
3.4 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;
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
{
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);
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
}
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>
/// <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>
/// <exception cref="System.IO.DirectoryNotFoundException">The path does not exist.</exception>
2014-02-21 05:04:11 +00:00
/// <exception cref="System.ArgumentException">The path is not valid.</exception>
public static void AddMediaPath(IFileSystem fileSystem, string virtualFolderName, string path, IServerApplicationPaths appPaths)
2013-02-21 01:33:05 +00:00
{
if (!Directory.Exists(path))
{
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);
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
}
fileSystem.CreateShortcut(lnk, path);
2013-02-21 01:33:05 +00:00
}
}
}