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