|
@@ -1,122 +1,42 @@
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.Linq;
|
|
|
+using MediaBrowser.Common.IO;
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
using System;
|
|
|
-using System.Collections.Specialized;
|
|
|
using System.IO;
|
|
|
using System.Text;
|
|
|
|
|
|
-namespace MediaBrowser.Controller.IO
|
|
|
+namespace MediaBrowser.Common.Implementations.IO
|
|
|
{
|
|
|
/// <summary>
|
|
|
- /// Class FileSystem
|
|
|
+ /// Class CommonFileSystem
|
|
|
/// </summary>
|
|
|
- public static class FileSystem
|
|
|
+ public class CommonFileSystem : IFileSystem
|
|
|
{
|
|
|
- /// <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;
|
|
|
- }
|
|
|
+ protected ILogger Logger;
|
|
|
|
|
|
- return new FileInfo(path);
|
|
|
- }
|
|
|
- }
|
|
|
+ private readonly bool _supportsAsyncFileStreams;
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Gets the creation time UTC.
|
|
|
- /// </summary>
|
|
|
- /// <param name="info">The info.</param>
|
|
|
- /// <param name="logger">The logger.</param>
|
|
|
- /// <returns>DateTime.</returns>
|
|
|
- public static DateTime GetLastWriteTimeUtc(FileSystemInfo info, ILogger logger)
|
|
|
+ public CommonFileSystem(ILogger logger, bool supportsAsyncFileStreams)
|
|
|
{
|
|
|
- // This could throw an error on some file systems that have dates out of range
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- return info.LastWriteTimeUtc;
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
|
|
|
- return DateTime.MinValue;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Gets the creation time UTC.
|
|
|
- /// </summary>
|
|
|
- /// <param name="info">The info.</param>
|
|
|
- /// <param name="logger">The logger.</param>
|
|
|
- /// <returns>DateTime.</returns>
|
|
|
- public static DateTime GetCreationTimeUtc(FileSystemInfo info, ILogger logger)
|
|
|
- {
|
|
|
- // This could throw an error on some file systems that have dates out of range
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- return info.CreationTimeUtc;
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
|
|
|
- return DateTime.MinValue;
|
|
|
- }
|
|
|
+ Logger = logger;
|
|
|
+ _supportsAsyncFileStreams = supportsAsyncFileStreams;
|
|
|
}
|
|
|
|
|
|
/// <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
|
|
|
+ /// Determines whether the specified filename is shortcut.
|
|
|
/// </summary>
|
|
|
/// <param name="filename">The filename.</param>
|
|
|
- /// <returns>System.String.</returns>
|
|
|
+ /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
|
|
|
/// <exception cref="System.ArgumentNullException">filename</exception>
|
|
|
- public static string GetValidFilename(string filename)
|
|
|
+ public virtual bool IsShortcut(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);
|
|
|
- }
|
|
|
+ var extension = Path.GetExtension(filename);
|
|
|
|
|
|
- return builder.ToString();
|
|
|
+ return string.Equals(extension, ".mblink", StringComparison.OrdinalIgnoreCase);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -125,7 +45,7 @@ namespace MediaBrowser.Controller.IO
|
|
|
/// <param name="filename">The filename.</param>
|
|
|
/// <returns>System.String.</returns>
|
|
|
/// <exception cref="System.ArgumentNullException">filename</exception>
|
|
|
- public static string ResolveShortcut(string filename)
|
|
|
+ public virtual string ResolveShortcut(string filename)
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(filename))
|
|
|
{
|
|
@@ -137,25 +57,20 @@ namespace MediaBrowser.Controller.IO
|
|
|
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();
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Creates a shortcut file pointing to a specified path
|
|
|
+ /// Creates the shortcut.
|
|
|
/// </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)
|
|
|
+ /// <exception cref="System.ArgumentNullException">
|
|
|
+ /// shortcutPath
|
|
|
+ /// or
|
|
|
+ /// target
|
|
|
+ /// </exception>
|
|
|
+ public void CreateShortcut(string shortcutPath, string target)
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(shortcutPath))
|
|
|
{
|
|
@@ -168,96 +83,138 @@ namespace MediaBrowser.Controller.IO
|
|
|
}
|
|
|
|
|
|
File.WriteAllText(shortcutPath, target);
|
|
|
+ }
|
|
|
|
|
|
- //var link = new ShellLink();
|
|
|
+ /// <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;
|
|
|
+ }
|
|
|
|
|
|
- //((IShellLinkW)link).SetPath(target);
|
|
|
+ return new DirectoryInfo(path);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var fileInfo = new DirectoryInfo(path);
|
|
|
+
|
|
|
+ if (fileInfo.Exists)
|
|
|
+ {
|
|
|
+ return fileInfo;
|
|
|
+ }
|
|
|
|
|
|
- //((IPersistFile)link).Save(shortcutPath, true);
|
|
|
+ return new FileInfo(path);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private static readonly Dictionary<string, string> ShortcutExtensionsDictionary = new[] { ".mblink", ".lnk" }
|
|
|
- .ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
|
|
|
+ /// <summary>
|
|
|
+ /// The space char
|
|
|
+ /// </summary>
|
|
|
+ private const char SpaceChar = ' ';
|
|
|
+ /// <summary>
|
|
|
+ /// The invalid file name chars
|
|
|
+ /// </summary>
|
|
|
+ private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Determines whether the specified filename is shortcut.
|
|
|
+ /// Takes a filename and removes invalid characters
|
|
|
/// </summary>
|
|
|
/// <param name="filename">The filename.</param>
|
|
|
- /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
|
|
|
+ /// <returns>System.String.</returns>
|
|
|
/// <exception cref="System.ArgumentNullException">filename</exception>
|
|
|
- public static bool IsShortcut(string filename)
|
|
|
+ public string GetValidFilename(string filename)
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(filename))
|
|
|
{
|
|
|
throw new ArgumentNullException("filename");
|
|
|
}
|
|
|
|
|
|
- var extension = Path.GetExtension(filename);
|
|
|
+ var builder = new StringBuilder(filename);
|
|
|
|
|
|
- return !string.IsNullOrEmpty(extension) && ShortcutExtensionsDictionary.ContainsKey(extension);
|
|
|
+ foreach (var c in InvalidFileNameChars)
|
|
|
+ {
|
|
|
+ builder = builder.Replace(c, SpaceChar);
|
|
|
+ }
|
|
|
+
|
|
|
+ return builder.ToString();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Copies all.
|
|
|
+ /// Gets the creation time UTC.
|
|
|
/// </summary>
|
|
|
- /// <param name="source">The source.</param>
|
|
|
- /// <param name="target">The target.</param>
|
|
|
- /// <exception cref="System.ArgumentNullException">source</exception>
|
|
|
- /// <exception cref="System.ArgumentException">The source and target directories are the same</exception>
|
|
|
- public static void CopyAll(string source, string target)
|
|
|
+ /// <param name="info">The info.</param>
|
|
|
+ /// <returns>DateTime.</returns>
|
|
|
+ public DateTime GetCreationTimeUtc(FileSystemInfo info)
|
|
|
{
|
|
|
- if (string.IsNullOrEmpty(source))
|
|
|
- {
|
|
|
- throw new ArgumentNullException("source");
|
|
|
- }
|
|
|
- if (string.IsNullOrEmpty(target))
|
|
|
+ // This could throw an error on some file systems that have dates out of range
|
|
|
+ try
|
|
|
{
|
|
|
- throw new ArgumentNullException("target");
|
|
|
+ return info.CreationTimeUtc;
|
|
|
}
|
|
|
-
|
|
|
- if (source.Equals(target, StringComparison.OrdinalIgnoreCase))
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- throw new ArgumentException("The source and target directories are the same");
|
|
|
+ Logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
|
|
|
+ return DateTime.MinValue;
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // Check if the target directory exists, if not, create it.
|
|
|
- Directory.CreateDirectory(target);
|
|
|
-
|
|
|
- foreach (var file in Directory.EnumerateFiles(source))
|
|
|
+ /// <summary>
|
|
|
+ /// Gets the creation time UTC.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="info">The info.</param>
|
|
|
+ /// <param name="logger">The logger.</param>
|
|
|
+ /// <returns>DateTime.</returns>
|
|
|
+ public DateTime GetLastWriteTimeUtc(FileSystemInfo info)
|
|
|
+ {
|
|
|
+ // This could throw an error on some file systems that have dates out of range
|
|
|
+ try
|
|
|
{
|
|
|
- File.Copy(file, Path.Combine(target, Path.GetFileName(file)), true);
|
|
|
+ return info.LastWriteTimeUtc;
|
|
|
}
|
|
|
-
|
|
|
- // Copy each subdirectory using recursion.
|
|
|
- foreach (var dir in Directory.EnumerateDirectories(source))
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- CopyAll(dir, Path.Combine(target, Path.GetFileName(dir)));
|
|
|
+ Logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
|
|
|
+ return DateTime.MinValue;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Parses the ini file.
|
|
|
+ /// Gets the last write time UTC.
|
|
|
/// </summary>
|
|
|
/// <param name="path">The path.</param>
|
|
|
- /// <returns>NameValueCollection.</returns>
|
|
|
- public static NameValueCollection ParseIniFile(string path)
|
|
|
+ /// <returns>DateTime.</returns>
|
|
|
+ public DateTime GetLastWriteTimeUtc(string path)
|
|
|
{
|
|
|
- var values = new NameValueCollection();
|
|
|
+ return GetLastWriteTimeUtc(GetFileSystemInfo(path));
|
|
|
+ }
|
|
|
|
|
|
- foreach (var line in File.ReadAllLines(path))
|
|
|
+ /// <summary>
|
|
|
+ /// Gets the file stream.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="path">The path.</param>
|
|
|
+ /// <param name="mode">The mode.</param>
|
|
|
+ /// <param name="access">The access.</param>
|
|
|
+ /// <param name="share">The share.</param>
|
|
|
+ /// <param name="isAsync">if set to <c>true</c> [is asynchronous].</param>
|
|
|
+ /// <returns>FileStream.</returns>
|
|
|
+ public FileStream GetFileStream(string path, FileMode mode, FileAccess access, FileShare share, bool isAsync = false)
|
|
|
+ {
|
|
|
+ if (_supportsAsyncFileStreams && isAsync)
|
|
|
{
|
|
|
- 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 new FileStream(path, mode, access, share, 4096, true);
|
|
|
}
|
|
|
|
|
|
- return values;
|
|
|
+ return new FileStream(path, mode, access, share);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -381,4 +338,5 @@ namespace MediaBrowser.Controller.IO
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
}
|