fixes #567 - Deprecate native shortcut code

This commit is contained in:
Luke Pulverenti
2013-10-30 10:40:14 -04:00
parent cbd6c47af4
commit be7918e5f6
19 changed files with 452 additions and 342 deletions

View File

@@ -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);

View 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);
}
}
}

View 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();
}
}
}

View File

@@ -0,0 +1,413 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
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>
[SuppressUnmanagedCodeSecurity]
public static class NativeMethods
{
/// <summary>
/// The MA x_ PATH
/// </summary>
public const int MAX_PATH = 260;
/// <summary>
/// The MA x_ ALTERNATE
/// </summary>
public const int MAX_ALTERNATE = 14;
/// <summary>
/// The INVALI d_ HANDL e_ VALUE
/// </summary>
public static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
/// <summary>
/// The STG m_ READ
/// </summary>
public const uint STGM_READ = 0;
}
/// <summary>
/// Struct FILETIME
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct FILETIME
{
/// <summary>
/// The dw low date time
/// </summary>
public uint dwLowDateTime;
/// <summary>
/// The dw high date time
/// </summary>
public uint dwHighDateTime;
}
/// <summary>
/// Struct WIN32_FIND_DATA
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WIN32_FIND_DATA
{
/// <summary>
/// The dw file attributes
/// </summary>
public FileAttributes dwFileAttributes;
/// <summary>
/// The ft creation time
/// </summary>
public FILETIME ftCreationTime;
/// <summary>
/// The ft last access time
/// </summary>
public FILETIME ftLastAccessTime;
/// <summary>
/// The ft last write time
/// </summary>
public FILETIME ftLastWriteTime;
/// <summary>
/// The n file size high
/// </summary>
public int nFileSizeHigh;
/// <summary>
/// The n file size low
/// </summary>
public int nFileSizeLow;
/// <summary>
/// The dw reserved0
/// </summary>
public int dwReserved0;
/// <summary>
/// The dw reserved1
/// </summary>
public int dwReserved1;
/// <summary>
/// The c file name
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_PATH)]
public string cFileName;
/// <summary>
/// This will always be null when FINDEX_INFO_LEVELS = basic
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_ALTERNATE)]
public string cAlternate;
/// <summary>
/// Gets or sets the path.
/// </summary>
/// <value>The path.</value>
public string Path { get; set; }
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
public override string ToString()
{
return Path ?? string.Empty;
}
}
/// <summary>
/// Enum SLGP_FLAGS
/// </summary>
[Flags]
public enum SLGP_FLAGS
{
/// <summary>
/// Retrieves the standard short (8.3 format) file name
/// </summary>
SLGP_SHORTPATH = 0x1,
/// <summary>
/// Retrieves the Universal Naming Convention (UNC) path name of the file
/// </summary>
SLGP_UNCPRIORITY = 0x2,
/// <summary>
/// Retrieves the raw path name. A raw path is something that might not exist and may include environment variables that need to be expanded
/// </summary>
SLGP_RAWPATH = 0x4
}
/// <summary>
/// Enum SLR_FLAGS
/// </summary>
[Flags]
public enum SLR_FLAGS
{
/// <summary>
/// Do not display a dialog box if the link cannot be resolved. When SLR_NO_UI is set,
/// the high-order word of fFlags can be set to a time-out value that specifies the
/// maximum amount of time to be spent resolving the link. The function returns if the
/// link cannot be resolved within the time-out duration. If the high-order word is set
/// to zero, the time-out duration will be set to the default value of 3,000 milliseconds
/// (3 seconds). To specify a value, set the high word of fFlags to the desired time-out
/// duration, in milliseconds.
/// </summary>
SLR_NO_UI = 0x1,
/// <summary>
/// Obsolete and no longer used
/// </summary>
SLR_ANY_MATCH = 0x2,
/// <summary>
/// If the link object has changed, update its path and list of identifiers.
/// If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine
/// whether or not the link object has changed.
/// </summary>
SLR_UPDATE = 0x4,
/// <summary>
/// Do not update the link information
/// </summary>
SLR_NOUPDATE = 0x8,
/// <summary>
/// Do not execute the search heuristics
/// </summary>
SLR_NOSEARCH = 0x10,
/// <summary>
/// Do not use distributed link tracking
/// </summary>
SLR_NOTRACK = 0x20,
/// <summary>
/// Disable distributed link tracking. By default, distributed link tracking tracks
/// removable media across multiple devices based on the volume name. It also uses the
/// Universal Naming Convention (UNC) path to track remote file systems whose drive letter
/// has changed. Setting SLR_NOLINKINFO disables both types of tracking.
/// </summary>
SLR_NOLINKINFO = 0x40,
/// <summary>
/// Call the Microsoft Windows Installer
/// </summary>
SLR_INVOKE_MSI = 0x80
}
/// <summary>
/// The IShellLink interface allows Shell links to be created, modified, and resolved
/// </summary>
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")]
public interface IShellLinkW
{
/// <summary>
/// Retrieves the path and file name of a Shell link object
/// </summary>
/// <param name="pszFile">The PSZ file.</param>
/// <param name="cchMaxPath">The CCH max path.</param>
/// <param name="pfd">The PFD.</param>
/// <param name="fFlags">The f flags.</param>
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATA pfd, SLGP_FLAGS fFlags);
/// <summary>
/// Retrieves the list of item identifiers for a Shell link object
/// </summary>
/// <param name="ppidl">The ppidl.</param>
void GetIDList(out IntPtr ppidl);
/// <summary>
/// Sets the pointer to an item identifier list (PIDL) for a Shell link object.
/// </summary>
/// <param name="pidl">The pidl.</param>
void SetIDList(IntPtr pidl);
/// <summary>
/// Retrieves the description string for a Shell link object
/// </summary>
/// <param name="pszName">Name of the PSZ.</param>
/// <param name="cchMaxName">Name of the CCH max.</param>
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
/// <summary>
/// Sets the description for a Shell link object. The description can be any application-defined string
/// </summary>
/// <param name="pszName">Name of the PSZ.</param>
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
/// <summary>
/// Retrieves the name of the working directory for a Shell link object
/// </summary>
/// <param name="pszDir">The PSZ dir.</param>
/// <param name="cchMaxPath">The CCH max path.</param>
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
/// <summary>
/// Sets the name of the working directory for a Shell link object
/// </summary>
/// <param name="pszDir">The PSZ dir.</param>
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
/// <summary>
/// Retrieves the command-line arguments associated with a Shell link object
/// </summary>
/// <param name="pszArgs">The PSZ args.</param>
/// <param name="cchMaxPath">The CCH max path.</param>
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
/// <summary>
/// Sets the command-line arguments for a Shell link object
/// </summary>
/// <param name="pszArgs">The PSZ args.</param>
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
/// <summary>
/// Retrieves the hot key for a Shell link object
/// </summary>
/// <param name="pwHotkey">The pw hotkey.</param>
void GetHotkey(out short pwHotkey);
/// <summary>
/// Sets a hot key for a Shell link object
/// </summary>
/// <param name="wHotkey">The w hotkey.</param>
void SetHotkey(short wHotkey);
/// <summary>
/// Retrieves the show command for a Shell link object
/// </summary>
/// <param name="piShowCmd">The pi show CMD.</param>
void GetShowCmd(out int piShowCmd);
/// <summary>
/// Sets the show command for a Shell link object. The show command sets the initial show state of the window.
/// </summary>
/// <param name="iShowCmd">The i show CMD.</param>
void SetShowCmd(int iShowCmd);
/// <summary>
/// Retrieves the location (path and index) of the icon for a Shell link object
/// </summary>
/// <param name="pszIconPath">The PSZ icon path.</param>
/// <param name="cchIconPath">The CCH icon path.</param>
/// <param name="piIcon">The pi icon.</param>
void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
int cchIconPath, out int piIcon);
/// <summary>
/// Sets the location (path and index) of the icon for a Shell link object
/// </summary>
/// <param name="pszIconPath">The PSZ icon path.</param>
/// <param name="iIcon">The i icon.</param>
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
/// <summary>
/// Sets the relative path to the Shell link object
/// </summary>
/// <param name="pszPathRel">The PSZ path rel.</param>
/// <param name="dwReserved">The dw reserved.</param>
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
/// <summary>
/// Attempts to find the target of a Shell link, even if it has been moved or renamed
/// </summary>
/// <param name="hwnd">The HWND.</param>
/// <param name="fFlags">The f flags.</param>
void Resolve(IntPtr hwnd, SLR_FLAGS fFlags);
/// <summary>
/// Sets the path and file name of a Shell link object
/// </summary>
/// <param name="pszFile">The PSZ file.</param>
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
/// <summary>
/// Interface IPersist
/// </summary>
[ComImport, Guid("0000010c-0000-0000-c000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPersist
{
/// <summary>
/// Gets the class ID.
/// </summary>
/// <param name="pClassID">The p class ID.</param>
[PreserveSig]
void GetClassID(out Guid pClassID);
}
/// <summary>
/// Interface IPersistFile
/// </summary>
[ComImport, Guid("0000010b-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPersistFile : IPersist
{
/// <summary>
/// Gets the class ID.
/// </summary>
/// <param name="pClassID">The p class ID.</param>
new void GetClassID(out Guid pClassID);
/// <summary>
/// Determines whether this instance is dirty.
/// </summary>
[PreserveSig]
int IsDirty();
/// <summary>
/// Loads the specified PSZ file name.
/// </summary>
/// <param name="pszFileName">Name of the PSZ file.</param>
/// <param name="dwMode">The dw mode.</param>
[PreserveSig]
void Load([In, MarshalAs(UnmanagedType.LPWStr)]
string pszFileName, uint dwMode);
/// <summary>
/// Saves the specified PSZ file name.
/// </summary>
/// <param name="pszFileName">Name of the PSZ file.</param>
/// <param name="remember">if set to <c>true</c> [remember].</param>
[PreserveSig]
void Save([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
[In, MarshalAs(UnmanagedType.Bool)] bool remember);
/// <summary>
/// Saves the completed.
/// </summary>
/// <param name="pszFileName">Name of the PSZ file.</param>
[PreserveSig]
void SaveCompleted([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
/// <summary>
/// Gets the cur file.
/// </summary>
/// <param name="ppszFileName">Name of the PPSZ file.</param>
[PreserveSig]
void GetCurFile([In, MarshalAs(UnmanagedType.LPWStr)] string ppszFileName);
}
// CLSID_ShellLink from ShlGuid.h
/// <summary>
/// Class ShellLink
/// </summary>
[
ComImport,
Guid("00021401-0000-0000-C000-000000000046")
]
public class ShellLink
{
}
}

View File

@@ -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" />