mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-11-15 18:08:53 -07:00
fixes #567 - Deprecate native shortcut code
This commit is contained in:
parent
cbd6c47af4
commit
be7918e5f6
@ -12,20 +12,27 @@ namespace MediaBrowser.Api.Library
|
||||
/// </summary>
|
||||
public static class LibraryHelpers
|
||||
{
|
||||
/// <summary>
|
||||
/// The shortcut file extension
|
||||
/// </summary>
|
||||
private const string ShortcutFileExtension = ".mblink";
|
||||
/// <summary>
|
||||
/// The shortcut file search
|
||||
/// </summary>
|
||||
private const string ShortcutFileSearch = "*" + ShortcutFileExtension;
|
||||
|
||||
/// <summary>
|
||||
/// Adds the virtual folder.
|
||||
/// </summary>
|
||||
/// <param name="fileSystem">The file system.</param>
|
||||
/// <param name="name">The name.</param>
|
||||
/// <param name="collectionType">Type of the collection.</param>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="appPaths">The app paths.</param>
|
||||
/// <exception cref="System.ArgumentException">There is already a media collection with the name + name + .</exception>
|
||||
public static void AddVirtualFolder(string name, string collectionType, User user, IServerApplicationPaths appPaths)
|
||||
public static void AddVirtualFolder(IFileSystem fileSystem, string name, string collectionType, User user, IServerApplicationPaths appPaths)
|
||||
{
|
||||
name = FileSystem.GetValidFilename(name);
|
||||
name = fileSystem.GetValidFilename(name);
|
||||
|
||||
var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
|
||||
var virtualFolderPath = Path.Combine(rootFolderPath, name);
|
||||
@ -106,12 +113,13 @@ namespace MediaBrowser.Api.Library
|
||||
/// <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>
|
||||
/// <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>
|
||||
public static void RemoveMediaPath(string virtualFolderName, string mediaPath, User user, IServerApplicationPaths appPaths)
|
||||
public static void RemoveMediaPath(IFileSystem fileSystem, string virtualFolderName, string mediaPath, User user, IServerApplicationPaths appPaths)
|
||||
{
|
||||
var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
|
||||
var path = Path.Combine(rootFolderPath, virtualFolderName);
|
||||
@ -121,7 +129,7 @@ namespace MediaBrowser.Api.Library
|
||||
throw new DirectoryNotFoundException(string.Format("The media collection {0} does not exist", virtualFolderName));
|
||||
}
|
||||
|
||||
var shortcut = Directory.EnumerateFiles(path, ShortcutFileSearch, SearchOption.AllDirectories).FirstOrDefault(f => FileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase));
|
||||
var shortcut = Directory.EnumerateFiles(path, ShortcutFileSearch, SearchOption.AllDirectories).FirstOrDefault(f => fileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (!string.IsNullOrEmpty(shortcut))
|
||||
{
|
||||
@ -132,13 +140,14 @@ namespace MediaBrowser.Api.Library
|
||||
/// <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>
|
||||
/// <param name="virtualFolderName">Name of the virtual folder.</param>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <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>
|
||||
public static void AddMediaPath(string virtualFolderName, string path, User user, IServerApplicationPaths appPaths)
|
||||
public static void AddMediaPath(IFileSystem fileSystem, string virtualFolderName, string path, User user, IServerApplicationPaths appPaths)
|
||||
{
|
||||
if (!Path.IsPathRooted(path))
|
||||
{
|
||||
@ -160,7 +169,7 @@ namespace MediaBrowser.Api.Library
|
||||
var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
|
||||
var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
|
||||
|
||||
ValidateNewMediaPath(rootFolderPath, path, appPaths);
|
||||
ValidateNewMediaPath(fileSystem, rootFolderPath, path, appPaths);
|
||||
|
||||
var shortcutFilename = Path.GetFileNameWithoutExtension(path);
|
||||
|
||||
@ -172,20 +181,22 @@ namespace MediaBrowser.Api.Library
|
||||
lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
|
||||
}
|
||||
|
||||
FileSystem.CreateShortcut(lnk, path);
|
||||
fileSystem.CreateShortcut(lnk, path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates that a new media path can be added
|
||||
/// </summary>
|
||||
/// <param name="fileSystem">The file system.</param>
|
||||
/// <param name="currentViewRootFolderPath">The current view root folder path.</param>
|
||||
/// <param name="mediaPath">The media path.</param>
|
||||
/// <param name="appPaths">The app paths.</param>
|
||||
/// <exception cref="System.ArgumentException"></exception>
|
||||
private static void ValidateNewMediaPath(string currentViewRootFolderPath, string mediaPath, IServerApplicationPaths appPaths)
|
||||
/// <exception cref="System.ArgumentException">
|
||||
/// </exception>
|
||||
private static void ValidateNewMediaPath(IFileSystem fileSystem, string currentViewRootFolderPath, string mediaPath, IServerApplicationPaths appPaths)
|
||||
{
|
||||
var duplicate = Directory.EnumerateFiles(appPaths.RootFolderPath, ShortcutFileSearch, SearchOption.AllDirectories)
|
||||
.Select(FileSystem.ResolveShortcut)
|
||||
.Select(fileSystem.ResolveShortcut)
|
||||
.FirstOrDefault(p => !IsNewPathValid(mediaPath, p, false));
|
||||
|
||||
if (!string.IsNullOrEmpty(duplicate))
|
||||
@ -196,7 +207,7 @@ namespace MediaBrowser.Api.Library
|
||||
// Don't allow duplicate sub-paths within the same user library, or it will result in duplicate items
|
||||
// See comments in IsNewPathValid
|
||||
duplicate = Directory.EnumerateFiles(currentViewRootFolderPath, ShortcutFileSearch, SearchOption.AllDirectories)
|
||||
.Select(FileSystem.ResolveShortcut)
|
||||
.Select(fileSystem.ResolveShortcut)
|
||||
.FirstOrDefault(p => !IsNewPathValid(mediaPath, p, true));
|
||||
|
||||
if (!string.IsNullOrEmpty(duplicate))
|
||||
@ -206,7 +217,7 @@ namespace MediaBrowser.Api.Library
|
||||
|
||||
// Make sure the current root folder doesn't already have a shortcut to the same path
|
||||
duplicate = Directory.EnumerateFiles(currentViewRootFolderPath, ShortcutFileSearch, SearchOption.AllDirectories)
|
||||
.Select(FileSystem.ResolveShortcut)
|
||||
.Select(fileSystem.ResolveShortcut)
|
||||
.FirstOrDefault(p => mediaPath.Equals(p, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (!string.IsNullOrEmpty(duplicate))
|
||||
|
@ -186,6 +186,8 @@ namespace MediaBrowser.Api.Library
|
||||
|
||||
private readonly IDirectoryWatchers _directoryWatchers;
|
||||
|
||||
private readonly IFileSystem _fileSystem;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LibraryStructureService"/> class.
|
||||
/// </summary>
|
||||
@ -193,7 +195,7 @@ namespace MediaBrowser.Api.Library
|
||||
/// <param name="userManager">The user manager.</param>
|
||||
/// <param name="libraryManager">The library manager.</param>
|
||||
/// <exception cref="System.ArgumentNullException">appPaths</exception>
|
||||
public LibraryStructureService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IDirectoryWatchers directoryWatchers)
|
||||
public LibraryStructureService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IDirectoryWatchers directoryWatchers, IFileSystem fileSystem)
|
||||
{
|
||||
if (appPaths == null)
|
||||
{
|
||||
@ -204,6 +206,7 @@ namespace MediaBrowser.Api.Library
|
||||
_appPaths = appPaths;
|
||||
_libraryManager = libraryManager;
|
||||
_directoryWatchers = directoryWatchers;
|
||||
_fileSystem = fileSystem;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -241,13 +244,13 @@ namespace MediaBrowser.Api.Library
|
||||
{
|
||||
if (string.IsNullOrEmpty(request.UserId))
|
||||
{
|
||||
LibraryHelpers.AddVirtualFolder(request.Name, request.CollectionType, null, _appPaths);
|
||||
LibraryHelpers.AddVirtualFolder(_fileSystem, request.Name, request.CollectionType, null, _appPaths);
|
||||
}
|
||||
else
|
||||
{
|
||||
var user = _userManager.GetUserById(new Guid(request.UserId));
|
||||
|
||||
LibraryHelpers.AddVirtualFolder(request.Name, request.CollectionType, user, _appPaths);
|
||||
LibraryHelpers.AddVirtualFolder(_fileSystem, request.Name, request.CollectionType, user, _appPaths);
|
||||
}
|
||||
|
||||
// Need to add a delay here or directory watchers may still pick up the changes
|
||||
@ -352,13 +355,13 @@ namespace MediaBrowser.Api.Library
|
||||
{
|
||||
if (string.IsNullOrEmpty(request.UserId))
|
||||
{
|
||||
LibraryHelpers.AddMediaPath(request.Name, request.Path, null, _appPaths);
|
||||
LibraryHelpers.AddMediaPath(_fileSystem, request.Name, request.Path, null, _appPaths);
|
||||
}
|
||||
else
|
||||
{
|
||||
var user = _userManager.GetUserById(new Guid(request.UserId));
|
||||
|
||||
LibraryHelpers.AddMediaPath(request.Name, request.Path, user, _appPaths);
|
||||
LibraryHelpers.AddMediaPath(_fileSystem, request.Name, request.Path, user, _appPaths);
|
||||
}
|
||||
|
||||
// Need to add a delay here or directory watchers may still pick up the changes
|
||||
@ -389,13 +392,13 @@ namespace MediaBrowser.Api.Library
|
||||
{
|
||||
if (string.IsNullOrEmpty(request.UserId))
|
||||
{
|
||||
LibraryHelpers.RemoveMediaPath(request.Name, request.Path, null, _appPaths);
|
||||
LibraryHelpers.RemoveMediaPath(_fileSystem, request.Name, request.Path, null, _appPaths);
|
||||
}
|
||||
else
|
||||
{
|
||||
var user = _userManager.GetUserById(new Guid(request.UserId));
|
||||
|
||||
LibraryHelpers.RemoveMediaPath(request.Name, request.Path, user, _appPaths);
|
||||
LibraryHelpers.RemoveMediaPath(_fileSystem, request.Name, request.Path, user, _appPaths);
|
||||
}
|
||||
|
||||
// Need to add a delay here or directory watchers may still pick up the changes
|
||||
|
@ -212,6 +212,7 @@ namespace MediaBrowser.Controller.Entities
|
||||
public static IProviderManager ProviderManager { get; set; }
|
||||
public static ILocalizationManager LocalizationManager { get; set; }
|
||||
public static IItemRepository ItemRepository { get; set; }
|
||||
public static IFileSystem FileSystem { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="System.String" /> that represents this instance.
|
||||
@ -395,7 +396,7 @@ namespace MediaBrowser.Controller.Entities
|
||||
// When resolving the root, we need it's grandchildren (children of user views)
|
||||
var flattenFolderDepth = isPhysicalRoot ? 2 : 0;
|
||||
|
||||
args.FileSystemDictionary = FileData.GetFilteredFileSystemEntries(args.Path, Logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
|
||||
args.FileSystemDictionary = FileData.GetFilteredFileSystemEntries(args.Path, FileSystem, Logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
|
||||
|
||||
// Need to remove subpaths that may have been resolved from shortcuts
|
||||
// Example: if \\server\movies exists, then strip out \\server\movies\action
|
||||
@ -413,7 +414,7 @@ namespace MediaBrowser.Controller.Entities
|
||||
}
|
||||
|
||||
//update our dates
|
||||
EntityResolutionHelper.EnsureDates(this, args, false);
|
||||
EntityResolutionHelper.EnsureDates(FileSystem, this, args, false);
|
||||
|
||||
IsOffline = false;
|
||||
|
||||
|
@ -693,7 +693,7 @@ namespace MediaBrowser.Controller.Entities
|
||||
//existing item - check if it has changed
|
||||
if (currentChild.HasChanged(child))
|
||||
{
|
||||
EntityResolutionHelper.EnsureDates(currentChild, child.ResolveArgs, false);
|
||||
EntityResolutionHelper.EnsureDates(FileSystem, currentChild, child.ResolveArgs, false);
|
||||
|
||||
validChildren.Add(new Tuple<BaseItem, bool>(currentChild, true));
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ namespace MediaBrowser.Controller.IO
|
||||
/// Gets the filtered file system entries.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <param name="fileSystem">The file system.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <param name="args">The args.</param>
|
||||
/// <param name="searchPattern">The search pattern.</param>
|
||||
@ -22,7 +23,7 @@ namespace MediaBrowser.Controller.IO
|
||||
/// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param>
|
||||
/// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">path</exception>
|
||||
public static Dictionary<string, FileSystemInfo> GetFilteredFileSystemEntries(string path, ILogger logger, ItemResolveArgs args, string searchPattern = "*", int flattenFolderDepth = 0, bool resolveShortcuts = true)
|
||||
public static Dictionary<string, FileSystemInfo> GetFilteredFileSystemEntries(string path, IFileSystem fileSystem, ILogger logger, ItemResolveArgs args, string searchPattern = "*", int flattenFolderDepth = 0, bool resolveShortcuts = true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
@ -56,9 +57,9 @@ namespace MediaBrowser.Controller.IO
|
||||
|
||||
var fullName = entry.FullName;
|
||||
|
||||
if (resolveShortcuts && FileSystem.IsShortcut(fullName))
|
||||
if (resolveShortcuts && fileSystem.IsShortcut(fullName))
|
||||
{
|
||||
var newPath = FileSystem.ResolveShortcut(fullName);
|
||||
var newPath = fileSystem.ResolveShortcut(fullName);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(newPath))
|
||||
{
|
||||
@ -77,7 +78,7 @@ namespace MediaBrowser.Controller.IO
|
||||
}
|
||||
else if (flattenFolderDepth > 0 && isDirectory)
|
||||
{
|
||||
foreach (var child in GetFilteredFileSystemEntries(fullName, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
|
||||
foreach (var child in GetFilteredFileSystemEntries(fullName, fileSystem, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
|
||||
{
|
||||
dict[child.Key] = child.Value;
|
||||
}
|
||||
|
@ -1,10 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace MediaBrowser.Controller.IO
|
||||
{
|
||||
@ -13,38 +9,6 @@ namespace MediaBrowser.Controller.IO
|
||||
/// </summary>
|
||||
public static class FileSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the file system info.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns>FileSystemInfo.</returns>
|
||||
public static FileSystemInfo GetFileSystemInfo(string path)
|
||||
{
|
||||
// Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists
|
||||
if (Path.HasExtension(path))
|
||||
{
|
||||
var fileInfo = new FileInfo(path);
|
||||
|
||||
if (fileInfo.Exists)
|
||||
{
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
return new DirectoryInfo(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
var fileInfo = new DirectoryInfo(path);
|
||||
|
||||
if (fileInfo.Exists)
|
||||
{
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
return new FileInfo(path);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the creation time UTC.
|
||||
/// </summary>
|
||||
@ -87,116 +51,6 @@ namespace MediaBrowser.Controller.IO
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The space char
|
||||
/// </summary>
|
||||
private const char SpaceChar = ' ';
|
||||
/// <summary>
|
||||
/// The invalid file name chars
|
||||
/// </summary>
|
||||
private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
|
||||
|
||||
/// <summary>
|
||||
/// Takes a filename and removes invalid characters
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">filename</exception>
|
||||
public static string GetValidFilename(string filename)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filename))
|
||||
{
|
||||
throw new ArgumentNullException("filename");
|
||||
}
|
||||
|
||||
var builder = new StringBuilder(filename);
|
||||
|
||||
foreach (var c in InvalidFileNameChars)
|
||||
{
|
||||
builder = builder.Replace(c, SpaceChar);
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the shortcut.
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">filename</exception>
|
||||
public static string ResolveShortcut(string filename)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filename))
|
||||
{
|
||||
throw new ArgumentNullException("filename");
|
||||
}
|
||||
|
||||
if (string.Equals(Path.GetExtension(filename), ".mblink", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return File.ReadAllText(filename);
|
||||
}
|
||||
|
||||
//return new WindowsShortcut(filename).ResolvedPath;
|
||||
|
||||
var link = new ShellLink();
|
||||
((IPersistFile)link).Load(filename, NativeMethods.STGM_READ);
|
||||
// TODO: if I can get hold of the hwnd call resolve first. This handles moved and renamed files.
|
||||
// ((IShellLinkW)link).Resolve(hwnd, 0)
|
||||
var sb = new StringBuilder(NativeMethods.MAX_PATH);
|
||||
WIN32_FIND_DATA data;
|
||||
((IShellLinkW)link).GetPath(sb, sb.Capacity, out data, 0);
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a shortcut file pointing to a specified path
|
||||
/// </summary>
|
||||
/// <param name="shortcutPath">The shortcut path.</param>
|
||||
/// <param name="target">The target.</param>
|
||||
/// <exception cref="System.ArgumentNullException">shortcutPath</exception>
|
||||
public static void CreateShortcut(string shortcutPath, string target)
|
||||
{
|
||||
if (string.IsNullOrEmpty(shortcutPath))
|
||||
{
|
||||
throw new ArgumentNullException("shortcutPath");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(target))
|
||||
{
|
||||
throw new ArgumentNullException("target");
|
||||
}
|
||||
|
||||
File.WriteAllText(shortcutPath, target);
|
||||
|
||||
//var link = new ShellLink();
|
||||
|
||||
//((IShellLinkW)link).SetPath(target);
|
||||
|
||||
//((IPersistFile)link).Save(shortcutPath, true);
|
||||
}
|
||||
|
||||
private static readonly Dictionary<string, string> ShortcutExtensionsDictionary = new[] { ".mblink", ".lnk" }
|
||||
.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the specified filename is shortcut.
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename.</param>
|
||||
/// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">filename</exception>
|
||||
public static bool IsShortcut(string filename)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filename))
|
||||
{
|
||||
throw new ArgumentNullException("filename");
|
||||
}
|
||||
|
||||
var extension = Path.GetExtension(filename);
|
||||
|
||||
return !string.IsNullOrEmpty(extension) && ShortcutExtensionsDictionary.ContainsKey(extension);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies all.
|
||||
/// </summary>
|
||||
@ -234,151 +88,5 @@ namespace MediaBrowser.Controller.IO
|
||||
CopyAll(dir, Path.Combine(target, Path.GetFileName(dir)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the ini file.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns>NameValueCollection.</returns>
|
||||
public static NameValueCollection ParseIniFile(string path)
|
||||
{
|
||||
var values = new NameValueCollection();
|
||||
|
||||
foreach (var line in File.ReadAllLines(path))
|
||||
{
|
||||
var data = line.Split('=');
|
||||
|
||||
if (data.Length < 2) continue;
|
||||
|
||||
var key = data[0];
|
||||
|
||||
var value = data.Length == 2 ? data[1] : string.Join(string.Empty, data, 1, data.Length - 1);
|
||||
|
||||
values[key] = value;
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adapted from http://stackoverflow.com/questions/309495/windows-shortcut-lnk-parser-in-java
|
||||
/// </summary>
|
||||
internal class WindowsShortcut
|
||||
{
|
||||
public bool IsDirectory { get; private set; }
|
||||
public bool IsLocal { get; private set; }
|
||||
public string ResolvedPath { get; private set; }
|
||||
|
||||
public WindowsShortcut(string file)
|
||||
{
|
||||
ParseLink(File.ReadAllBytes(file), Encoding.UTF8);
|
||||
}
|
||||
|
||||
private static bool isMagicPresent(byte[] link)
|
||||
{
|
||||
|
||||
const int magic = 0x0000004C;
|
||||
const int magic_offset = 0x00;
|
||||
|
||||
return link.Length >= 32 && bytesToDword(link, magic_offset) == magic;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gobbles up link data by parsing it and storing info in member fields
|
||||
* @param link all the bytes from the .lnk file
|
||||
*/
|
||||
private void ParseLink(byte[] link, Encoding encoding)
|
||||
{
|
||||
if (!isMagicPresent(link))
|
||||
throw new IOException("Invalid shortcut; magic is missing", 0);
|
||||
|
||||
// get the flags byte
|
||||
byte flags = link[0x14];
|
||||
|
||||
// get the file attributes byte
|
||||
const int file_atts_offset = 0x18;
|
||||
byte file_atts = link[file_atts_offset];
|
||||
byte is_dir_mask = (byte)0x10;
|
||||
if ((file_atts & is_dir_mask) > 0)
|
||||
{
|
||||
IsDirectory = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
IsDirectory = false;
|
||||
}
|
||||
|
||||
// if the shell settings are present, skip them
|
||||
const int shell_offset = 0x4c;
|
||||
const byte has_shell_mask = (byte)0x01;
|
||||
int shell_len = 0;
|
||||
if ((flags & has_shell_mask) > 0)
|
||||
{
|
||||
// the plus 2 accounts for the length marker itself
|
||||
shell_len = bytesToWord(link, shell_offset) + 2;
|
||||
}
|
||||
|
||||
// get to the file settings
|
||||
int file_start = 0x4c + shell_len;
|
||||
|
||||
const int file_location_info_flag_offset_offset = 0x08;
|
||||
int file_location_info_flag = link[file_start + file_location_info_flag_offset_offset];
|
||||
IsLocal = (file_location_info_flag & 2) == 0;
|
||||
// get the local volume and local system values
|
||||
//final int localVolumeTable_offset_offset = 0x0C;
|
||||
const int basename_offset_offset = 0x10;
|
||||
const int networkVolumeTable_offset_offset = 0x14;
|
||||
const int finalname_offset_offset = 0x18;
|
||||
int finalname_offset = link[file_start + finalname_offset_offset] + file_start;
|
||||
String finalname = getNullDelimitedString(link, finalname_offset, encoding);
|
||||
if (IsLocal)
|
||||
{
|
||||
int basename_offset = link[file_start + basename_offset_offset] + file_start;
|
||||
String basename = getNullDelimitedString(link, basename_offset, encoding);
|
||||
ResolvedPath = basename + finalname;
|
||||
}
|
||||
else
|
||||
{
|
||||
int networkVolumeTable_offset = link[file_start + networkVolumeTable_offset_offset] + file_start;
|
||||
int shareName_offset_offset = 0x08;
|
||||
int shareName_offset = link[networkVolumeTable_offset + shareName_offset_offset]
|
||||
+ networkVolumeTable_offset;
|
||||
String shareName = getNullDelimitedString(link, shareName_offset, encoding);
|
||||
ResolvedPath = shareName + "\\" + finalname;
|
||||
}
|
||||
}
|
||||
|
||||
private static string getNullDelimitedString(byte[] bytes, int off, Encoding encoding)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
// count bytes until the null character (0)
|
||||
while (true)
|
||||
{
|
||||
if (bytes[off + len] == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
len++;
|
||||
}
|
||||
|
||||
return encoding.GetString(bytes, off, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* convert two bytes into a short note, this is little endian because it's
|
||||
* for an Intel only OS.
|
||||
*/
|
||||
private static int bytesToWord(byte[] bytes, int off)
|
||||
{
|
||||
return ((bytes[off + 1] & 0xff) << 8) | (bytes[off] & 0xff);
|
||||
}
|
||||
|
||||
private static int bytesToDword(byte[] bytes, int off)
|
||||
{
|
||||
return (bytesToWord(bytes, off + 2) << 16) | bytesToWord(bytes, off);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
45
MediaBrowser.Controller/IO/IFileSystem.cs
Normal file
45
MediaBrowser.Controller/IO/IFileSystem.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System.IO;
|
||||
|
||||
namespace MediaBrowser.Controller.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface IFileSystem
|
||||
/// </summary>
|
||||
public interface IFileSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines whether the specified filename is shortcut.
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename.</param>
|
||||
/// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
|
||||
bool IsShortcut(string filename);
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the shortcut.
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
string ResolveShortcut(string filename);
|
||||
|
||||
/// <summary>
|
||||
/// Creates the shortcut.
|
||||
/// </summary>
|
||||
/// <param name="shortcutPath">The shortcut path.</param>
|
||||
/// <param name="target">The target.</param>
|
||||
void CreateShortcut(string shortcutPath, string target);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the file system info.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns>FileSystemInfo.</returns>
|
||||
FileSystemInfo GetFileSystemInfo(string path);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the valid filename.
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
string GetValidFilename(string filename);
|
||||
}
|
||||
}
|
@ -94,6 +94,7 @@
|
||||
<Compile Include="Entities\ImageSourceInfo.cs" />
|
||||
<Compile Include="Entities\LinkedChild.cs" />
|
||||
<Compile Include="Entities\MusicVideo.cs" />
|
||||
<Compile Include="IO\IFileSystem.cs" />
|
||||
<Compile Include="Library\ILibraryPostScanTask.cs" />
|
||||
<Compile Include="Library\ILibraryPrescanTask.cs" />
|
||||
<Compile Include="Library\IMetadataSaver.cs" />
|
||||
@ -141,7 +142,6 @@
|
||||
<Compile Include="Entities\Year.cs" />
|
||||
<Compile Include="IO\FileSystem.cs" />
|
||||
<Compile Include="IO\IDirectoryWatchers.cs" />
|
||||
<Compile Include="IO\NativeMethods.cs" />
|
||||
<Compile Include="IServerApplicationHost.cs" />
|
||||
<Compile Include="IServerApplicationPaths.cs" />
|
||||
<Compile Include="Library\SearchHintInfo.cs" />
|
||||
|
@ -126,10 +126,11 @@ namespace MediaBrowser.Controller.Resolvers
|
||||
/// <summary>
|
||||
/// Ensures DateCreated and DateModified have values
|
||||
/// </summary>
|
||||
/// <param name="fileSystem">The file system.</param>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="args">The args.</param>
|
||||
/// <param name="includeCreationTime">if set to <c>true</c> [include creation time].</param>
|
||||
public static void EnsureDates(BaseItem item, ItemResolveArgs args, bool includeCreationTime)
|
||||
public static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args, bool includeCreationTime)
|
||||
{
|
||||
if (!Path.IsPathRooted(item.Path))
|
||||
{
|
||||
@ -152,7 +153,7 @@ namespace MediaBrowser.Controller.Resolvers
|
||||
}
|
||||
else
|
||||
{
|
||||
var fileData = FileSystem.GetFileSystemInfo(item.Path);
|
||||
var fileData = fileSystem.GetFileSystemInfo(item.Path);
|
||||
|
||||
if (fileData.Exists)
|
||||
{
|
||||
|
@ -18,9 +18,12 @@ namespace MediaBrowser.Providers
|
||||
/// </summary>
|
||||
public class ImagesByNameProvider : ImageFromMediaLocationProvider
|
||||
{
|
||||
public ImagesByNameProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
|
||||
private readonly IFileSystem _fileSystem;
|
||||
|
||||
public ImagesByNameProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IFileSystem fileSystem)
|
||||
: base(logManager, configurationManager)
|
||||
{
|
||||
_fileSystem = fileSystem;
|
||||
}
|
||||
|
||||
public override ItemUpdateType ItemUpdateType
|
||||
@ -150,7 +153,7 @@ namespace MediaBrowser.Providers
|
||||
/// <returns>System.String.</returns>
|
||||
protected string GetLocation(BaseItem item)
|
||||
{
|
||||
var name = FileSystem.GetValidFilename(item.Name);
|
||||
var name = _fileSystem.GetValidFilename(item.Name);
|
||||
|
||||
return Path.Combine(ConfigurationManager.ApplicationPaths.GeneralPath, name);
|
||||
}
|
||||
|
@ -22,15 +22,18 @@ namespace MediaBrowser.Providers
|
||||
/// </summary>
|
||||
private readonly ILogger _logger;
|
||||
|
||||
private readonly IFileSystem _fileSystem;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RefreshIntrosTask"/> class.
|
||||
/// </summary>
|
||||
/// <param name="libraryManager">The library manager.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
public RefreshIntrosTask(ILibraryManager libraryManager, ILogger logger)
|
||||
public RefreshIntrosTask(ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem)
|
||||
{
|
||||
_libraryManager = libraryManager;
|
||||
_logger = logger;
|
||||
_fileSystem = fileSystem;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -77,7 +80,7 @@ namespace MediaBrowser.Providers
|
||||
/// <returns>Task.</returns>
|
||||
private async Task RefreshIntro(string path, CancellationToken cancellationToken)
|
||||
{
|
||||
var item = _libraryManager.ResolvePath(FileSystem.GetFileSystemInfo(path));
|
||||
var item = _libraryManager.ResolvePath(_fileSystem.GetFileSystemInfo(path));
|
||||
|
||||
if (item == null)
|
||||
{
|
||||
|
@ -87,10 +87,12 @@ namespace MediaBrowser.Server.Implementations.IO
|
||||
private ILibraryManager LibraryManager { get; set; }
|
||||
private IServerConfigurationManager ConfigurationManager { get; set; }
|
||||
|
||||
private readonly IFileSystem _fileSystem;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DirectoryWatchers" /> class.
|
||||
/// </summary>
|
||||
public DirectoryWatchers(ILogManager logManager, ITaskManager taskManager, ILibraryManager libraryManager, IServerConfigurationManager configurationManager)
|
||||
public DirectoryWatchers(ILogManager logManager, ITaskManager taskManager, ILibraryManager libraryManager, IServerConfigurationManager configurationManager, IFileSystem fileSystem)
|
||||
{
|
||||
if (taskManager == null)
|
||||
{
|
||||
@ -101,6 +103,7 @@ namespace MediaBrowser.Server.Implementations.IO
|
||||
TaskManager = taskManager;
|
||||
Logger = logManager.GetLogger("DirectoryWatchers");
|
||||
ConfigurationManager = configurationManager;
|
||||
_fileSystem = fileSystem;
|
||||
|
||||
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
|
||||
}
|
||||
@ -418,7 +421,7 @@ namespace MediaBrowser.Server.Implementations.IO
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = FileSystem.GetFileSystemInfo(path);
|
||||
var data = _fileSystem.GetFileSystemInfo(path);
|
||||
|
||||
if (!data.Exists
|
||||
|| data.Attributes.HasFlag(FileAttributes.Directory)
|
||||
|
@ -169,6 +169,8 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
private readonly ConcurrentDictionary<string, UserRootFolder> _userRootFolders =
|
||||
new ConcurrentDictionary<string, UserRootFolder>();
|
||||
|
||||
private readonly IFileSystem _fileSystem;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LibraryManager" /> class.
|
||||
/// </summary>
|
||||
@ -177,7 +179,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
/// <param name="userManager">The user manager.</param>
|
||||
/// <param name="configurationManager">The configuration manager.</param>
|
||||
/// <param name="userDataRepository">The user data repository.</param>
|
||||
public LibraryManager(ILogger logger, ITaskManager taskManager, IUserManager userManager, IServerConfigurationManager configurationManager, IUserDataManager userDataRepository, Func<IDirectoryWatchers> directoryWatchersFactory)
|
||||
public LibraryManager(ILogger logger, ITaskManager taskManager, IUserManager userManager, IServerConfigurationManager configurationManager, IUserDataManager userDataRepository, Func<IDirectoryWatchers> directoryWatchersFactory, IFileSystem fileSystem)
|
||||
{
|
||||
_logger = logger;
|
||||
_taskManager = taskManager;
|
||||
@ -185,6 +187,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
ConfigurationManager = configurationManager;
|
||||
_userDataRepository = userDataRepository;
|
||||
_directoryWatchersFactory = directoryWatchersFactory;
|
||||
_fileSystem = fileSystem;
|
||||
ByReferenceItems = new ConcurrentDictionary<Guid, BaseItem>();
|
||||
|
||||
ConfigurationManager.ConfigurationUpdated += ConfigurationUpdated;
|
||||
@ -417,7 +420,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
ResolverHelper.SetInitialItemValues(item, args);
|
||||
ResolverHelper.SetInitialItemValues(item, args, _fileSystem);
|
||||
|
||||
// Now handle the issue with posibly having the same item referenced from multiple physical
|
||||
// places within the library. Be sure we always end up with just one instance.
|
||||
@ -482,7 +485,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
// When resolving the root, we need it's grandchildren (children of user views)
|
||||
var flattenFolderDepth = isPhysicalRoot ? 2 : 0;
|
||||
|
||||
args.FileSystemDictionary = FileData.GetFilteredFileSystemEntries(args.Path, _logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
|
||||
args.FileSystemDictionary = FileData.GetFilteredFileSystemEntries(args.Path, _fileSystem, _logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
|
||||
|
||||
// Need to remove subpaths that may have been resolved from shortcuts
|
||||
// Example: if \\server\movies exists, then strip out \\server\movies\action
|
||||
@ -701,7 +704,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
var validFilename = FileSystem.GetValidFilename(name).Trim();
|
||||
var validFilename = _fileSystem.GetValidFilename(name).Trim();
|
||||
|
||||
string subFolderPrefix = null;
|
||||
|
||||
@ -1066,7 +1069,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
Name = Path.GetFileName(dir),
|
||||
|
||||
Locations = Directory.EnumerateFiles(dir, "*.mblink", SearchOption.TopDirectoryOnly)
|
||||
.Select(FileSystem.ResolveShortcut)
|
||||
.Select(_fileSystem.ResolveShortcut)
|
||||
.OrderBy(i => i)
|
||||
.ToList(),
|
||||
|
||||
@ -1150,7 +1153,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
try
|
||||
{
|
||||
// Try to resolve the path into a video
|
||||
video = ResolvePath(FileSystem.GetFileSystemInfo(info.Path)) as Video;
|
||||
video = ResolvePath(_fileSystem.GetFileSystemInfo(info.Path)) as Video;
|
||||
|
||||
if (video == null)
|
||||
{
|
||||
|
@ -1,5 +1,6 @@
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.IO;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Resolvers;
|
||||
using System;
|
||||
@ -18,7 +19,8 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="args">The args.</param>
|
||||
public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args)
|
||||
/// <param name="fileSystem">The file system.</param>
|
||||
public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem)
|
||||
{
|
||||
item.ResetResolveArgs(args);
|
||||
|
||||
@ -48,7 +50,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
item.DontFetchMeta = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1;
|
||||
|
||||
// Make sure DateCreated and DateModified have values
|
||||
EntityResolutionHelper.EnsureDates(item, args, true);
|
||||
EntityResolutionHelper.EnsureDates(fileSystem, item, args, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -47,6 +47,7 @@ using MediaBrowser.Server.Implementations.ServerManager;
|
||||
using MediaBrowser.Server.Implementations.Session;
|
||||
using MediaBrowser.Server.Implementations.WebSocket;
|
||||
using MediaBrowser.ServerApplication.FFMpeg;
|
||||
using MediaBrowser.ServerApplication.IO;
|
||||
using MediaBrowser.ServerApplication.Native;
|
||||
using MediaBrowser.ServerApplication.Networking;
|
||||
using MediaBrowser.WebDashboard.Api;
|
||||
@ -246,6 +247,9 @@ namespace MediaBrowser.ServerApplication
|
||||
|
||||
RegisterSingleInstance<IBlurayExaminer>(() => new BdInfoExaminer());
|
||||
|
||||
var fileSystemManager = FileSystemFactory.CreateFileSystemManager();
|
||||
RegisterSingleInstance(fileSystemManager);
|
||||
|
||||
var mediaEncoderTask = RegisterMediaEncoder();
|
||||
|
||||
UserDataManager = new UserDataManager(LogManager);
|
||||
@ -263,10 +267,10 @@ namespace MediaBrowser.ServerApplication
|
||||
UserManager = new UserManager(Logger, ServerConfigurationManager, UserRepository);
|
||||
RegisterSingleInstance(UserManager);
|
||||
|
||||
LibraryManager = new LibraryManager(Logger, TaskManager, UserManager, ServerConfigurationManager, UserDataManager, () => DirectoryWatchers);
|
||||
LibraryManager = new LibraryManager(Logger, TaskManager, UserManager, ServerConfigurationManager, UserDataManager, () => DirectoryWatchers, fileSystemManager);
|
||||
RegisterSingleInstance(LibraryManager);
|
||||
|
||||
DirectoryWatchers = new DirectoryWatchers(LogManager, TaskManager, LibraryManager, ServerConfigurationManager);
|
||||
DirectoryWatchers = new DirectoryWatchers(LogManager, TaskManager, LibraryManager, ServerConfigurationManager, fileSystemManager);
|
||||
RegisterSingleInstance(DirectoryWatchers);
|
||||
|
||||
ProviderManager = new ProviderManager(HttpClient, ServerConfigurationManager, DirectoryWatchers, LogManager, LibraryManager);
|
||||
|
264
MediaBrowser.ServerApplication/IO/CommonFileSystem.cs
Normal file
264
MediaBrowser.ServerApplication/IO/CommonFileSystem.cs
Normal file
@ -0,0 +1,264 @@
|
||||
using MediaBrowser.Controller.IO;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace MediaBrowser.ServerApplication.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// Class CommonFileSystem
|
||||
/// </summary>
|
||||
public class CommonFileSystem : IFileSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines whether the specified filename is shortcut.
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename.</param>
|
||||
/// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">filename</exception>
|
||||
public virtual bool IsShortcut(string filename)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filename))
|
||||
{
|
||||
throw new ArgumentNullException("filename");
|
||||
}
|
||||
|
||||
var extension = Path.GetExtension(filename);
|
||||
|
||||
return string.Equals(extension, ".mblink", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the shortcut.
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">filename</exception>
|
||||
public virtual string ResolveShortcut(string filename)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filename))
|
||||
{
|
||||
throw new ArgumentNullException("filename");
|
||||
}
|
||||
|
||||
if (string.Equals(Path.GetExtension(filename), ".mblink", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return File.ReadAllText(filename);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the shortcut.
|
||||
/// </summary>
|
||||
/// <param name="shortcutPath">The shortcut path.</param>
|
||||
/// <param name="target">The target.</param>
|
||||
/// <exception cref="System.ArgumentNullException">
|
||||
/// shortcutPath
|
||||
/// or
|
||||
/// target
|
||||
/// </exception>
|
||||
public void CreateShortcut(string shortcutPath, string target)
|
||||
{
|
||||
if (string.IsNullOrEmpty(shortcutPath))
|
||||
{
|
||||
throw new ArgumentNullException("shortcutPath");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(target))
|
||||
{
|
||||
throw new ArgumentNullException("target");
|
||||
}
|
||||
|
||||
File.WriteAllText(shortcutPath, target);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the file system info.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns>FileSystemInfo.</returns>
|
||||
public FileSystemInfo GetFileSystemInfo(string path)
|
||||
{
|
||||
// Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists
|
||||
if (Path.HasExtension(path))
|
||||
{
|
||||
var fileInfo = new FileInfo(path);
|
||||
|
||||
if (fileInfo.Exists)
|
||||
{
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
return new DirectoryInfo(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
var fileInfo = new DirectoryInfo(path);
|
||||
|
||||
if (fileInfo.Exists)
|
||||
{
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
return new FileInfo(path);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The space char
|
||||
/// </summary>
|
||||
private const char SpaceChar = ' ';
|
||||
/// <summary>
|
||||
/// The invalid file name chars
|
||||
/// </summary>
|
||||
private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
|
||||
|
||||
/// <summary>
|
||||
/// Takes a filename and removes invalid characters
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">filename</exception>
|
||||
public string GetValidFilename(string filename)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filename))
|
||||
{
|
||||
throw new ArgumentNullException("filename");
|
||||
}
|
||||
|
||||
var builder = new StringBuilder(filename);
|
||||
|
||||
foreach (var c in InvalidFileNameChars)
|
||||
{
|
||||
builder = builder.Replace(c, SpaceChar);
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Adapted from http://stackoverflow.com/questions/309495/windows-shortcut-lnk-parser-in-java
|
||||
/// </summary>
|
||||
internal class WindowsShortcut
|
||||
{
|
||||
public bool IsDirectory { get; private set; }
|
||||
public bool IsLocal { get; private set; }
|
||||
public string ResolvedPath { get; private set; }
|
||||
|
||||
public WindowsShortcut(string file)
|
||||
{
|
||||
ParseLink(File.ReadAllBytes(file), Encoding.UTF8);
|
||||
}
|
||||
|
||||
private static bool isMagicPresent(byte[] link)
|
||||
{
|
||||
|
||||
const int magic = 0x0000004C;
|
||||
const int magic_offset = 0x00;
|
||||
|
||||
return link.Length >= 32 && bytesToDword(link, magic_offset) == magic;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gobbles up link data by parsing it and storing info in member fields
|
||||
* @param link all the bytes from the .lnk file
|
||||
*/
|
||||
private void ParseLink(byte[] link, Encoding encoding)
|
||||
{
|
||||
if (!isMagicPresent(link))
|
||||
throw new IOException("Invalid shortcut; magic is missing", 0);
|
||||
|
||||
// get the flags byte
|
||||
byte flags = link[0x14];
|
||||
|
||||
// get the file attributes byte
|
||||
const int file_atts_offset = 0x18;
|
||||
byte file_atts = link[file_atts_offset];
|
||||
byte is_dir_mask = (byte)0x10;
|
||||
if ((file_atts & is_dir_mask) > 0)
|
||||
{
|
||||
IsDirectory = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
IsDirectory = false;
|
||||
}
|
||||
|
||||
// if the shell settings are present, skip them
|
||||
const int shell_offset = 0x4c;
|
||||
const byte has_shell_mask = (byte)0x01;
|
||||
int shell_len = 0;
|
||||
if ((flags & has_shell_mask) > 0)
|
||||
{
|
||||
// the plus 2 accounts for the length marker itself
|
||||
shell_len = bytesToWord(link, shell_offset) + 2;
|
||||
}
|
||||
|
||||
// get to the file settings
|
||||
int file_start = 0x4c + shell_len;
|
||||
|
||||
const int file_location_info_flag_offset_offset = 0x08;
|
||||
int file_location_info_flag = link[file_start + file_location_info_flag_offset_offset];
|
||||
IsLocal = (file_location_info_flag & 2) == 0;
|
||||
// get the local volume and local system values
|
||||
//final int localVolumeTable_offset_offset = 0x0C;
|
||||
const int basename_offset_offset = 0x10;
|
||||
const int networkVolumeTable_offset_offset = 0x14;
|
||||
const int finalname_offset_offset = 0x18;
|
||||
int finalname_offset = link[file_start + finalname_offset_offset] + file_start;
|
||||
String finalname = getNullDelimitedString(link, finalname_offset, encoding);
|
||||
if (IsLocal)
|
||||
{
|
||||
int basename_offset = link[file_start + basename_offset_offset] + file_start;
|
||||
String basename = getNullDelimitedString(link, basename_offset, encoding);
|
||||
ResolvedPath = basename + finalname;
|
||||
}
|
||||
else
|
||||
{
|
||||
int networkVolumeTable_offset = link[file_start + networkVolumeTable_offset_offset] + file_start;
|
||||
int shareName_offset_offset = 0x08;
|
||||
int shareName_offset = link[networkVolumeTable_offset + shareName_offset_offset]
|
||||
+ networkVolumeTable_offset;
|
||||
String shareName = getNullDelimitedString(link, shareName_offset, encoding);
|
||||
ResolvedPath = shareName + "\\" + finalname;
|
||||
}
|
||||
}
|
||||
|
||||
private static string getNullDelimitedString(byte[] bytes, int off, Encoding encoding)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
// count bytes until the null character (0)
|
||||
while (true)
|
||||
{
|
||||
if (bytes[off + len] == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
len++;
|
||||
}
|
||||
|
||||
return encoding.GetString(bytes, off, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* convert two bytes into a short note, this is little endian because it's
|
||||
* for an Intel only OS.
|
||||
*/
|
||||
private static int bytesToWord(byte[] bytes, int off)
|
||||
{
|
||||
return ((bytes[off + 1] & 0xff) << 8) | (bytes[off] & 0xff);
|
||||
}
|
||||
|
||||
private static int bytesToDword(byte[] bytes, int off)
|
||||
{
|
||||
return (bytesToWord(bytes, off + 2) << 16) | bytesToWord(bytes, off);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
19
MediaBrowser.ServerApplication/IO/FileSystemFactory.cs
Normal file
19
MediaBrowser.ServerApplication/IO/FileSystemFactory.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using MediaBrowser.Controller.IO;
|
||||
|
||||
namespace MediaBrowser.ServerApplication.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// Class FileSystemFactory
|
||||
/// </summary>
|
||||
public static class FileSystemFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates the file system manager.
|
||||
/// </summary>
|
||||
/// <returns>IFileSystem.</returns>
|
||||
public static IFileSystem CreateFileSystemManager()
|
||||
{
|
||||
return new NativeFileSystem();
|
||||
}
|
||||
}
|
||||
}
|
@ -4,8 +4,46 @@ using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
using System.Text;
|
||||
|
||||
namespace MediaBrowser.Controller.IO
|
||||
namespace MediaBrowser.ServerApplication.IO
|
||||
{
|
||||
public class NativeFileSystem : CommonFileSystem
|
||||
{
|
||||
public override bool IsShortcut(string filename)
|
||||
{
|
||||
return base.IsShortcut(filename) ||
|
||||
string.Equals(Path.GetExtension(filename), ".lnk", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public override string ResolveShortcut(string filename)
|
||||
{
|
||||
var path = base.ResolveShortcut(filename);
|
||||
|
||||
if (!string.IsNullOrEmpty(path))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
if (string.Equals(Path.GetExtension(filename), ".lnk", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return ResolveLnk(filename);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private string ResolveLnk(string filename)
|
||||
{
|
||||
var link = new ShellLink();
|
||||
((IPersistFile)link).Load(filename, NativeMethods.STGM_READ);
|
||||
// TODO: if I can get hold of the hwnd call resolve first. This handles moved and renamed files.
|
||||
// ((IShellLinkW)link).Resolve(hwnd, 0)
|
||||
var sb = new StringBuilder(NativeMethods.MAX_PATH);
|
||||
WIN32_FIND_DATA data;
|
||||
((IShellLinkW)link).GetPath(sb, sb.Capacity, out data, 0);
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Class NativeMethods
|
||||
/// </summary>
|
||||
@ -46,7 +84,6 @@ namespace MediaBrowser.Controller.IO
|
||||
public uint dwHighDateTime;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Struct WIN32_FIND_DATA
|
||||
/// </summary>
|
||||
@ -184,7 +221,6 @@ namespace MediaBrowser.Controller.IO
|
||||
SLR_INVOKE_MSI = 0x80
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The IShellLink interface allows Shell links to be created, modified, and resolved
|
||||
/// </summary>
|
||||
@ -311,7 +347,6 @@ namespace MediaBrowser.Controller.IO
|
||||
void GetClassID(out Guid pClassID);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Interface IPersistFile
|
||||
/// </summary>
|
||||
@ -374,4 +409,5 @@ namespace MediaBrowser.Controller.IO
|
||||
public class ShellLink
|
||||
{
|
||||
}
|
||||
|
||||
}
|
@ -188,9 +188,12 @@
|
||||
<Compile Include="EntryPoints\StartupWizard.cs" />
|
||||
<Compile Include="FFMpeg\FFMpegDownloadInfo.cs" />
|
||||
<Compile Include="FFMpeg\FFMpegInfo.cs" />
|
||||
<Compile Include="IO\FileSystemFactory.cs" />
|
||||
<Compile Include="Native\Assemblies.cs" />
|
||||
<Compile Include="IO\CommonFileSystem.cs" />
|
||||
<Compile Include="Native\HttpClientFactory.cs" />
|
||||
<Compile Include="Native\NativeApp.cs" />
|
||||
<Compile Include="IO\NativeFileSystem.cs" />
|
||||
<Compile Include="Native\ServerAuthorization.cs" />
|
||||
<Compile Include="Native\Autorun.cs" />
|
||||
<Compile Include="Native\BrowserLauncher.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user