From d3afa531913d94e541969f008d8065603579d9af Mon Sep 17 00:00:00 2001 From: Erwin de Haan Date: Sat, 26 Jan 2019 23:24:28 +0100 Subject: [PATCH] Final refactored `IFileSystem` and `ManagedFileSystem` --- .../IO/ManagedFileSystem.cs | 197 ++++-------------- MediaBrowser.Model/IO/IFileSystem.cs | 142 ++----------- 2 files changed, 63 insertions(+), 276 deletions(-) diff --git a/Emby.Server.Implementations/IO/ManagedFileSystem.cs b/Emby.Server.Implementations/IO/ManagedFileSystem.cs index 3e7774abf..7c44878ec 100644 --- a/Emby.Server.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Server.Implementations/IO/ManagedFileSystem.cs @@ -50,7 +50,7 @@ namespace Emby.Server.Implementations.IO _isEnvironmentCaseInsensitive = environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows; } - public string DefaultDirectory + public virtual string DefaultDirectory { get { @@ -60,7 +60,7 @@ namespace Emby.Server.Implementations.IO { try { - if (DirectoryExists(value)) + if (Directory.Exists(value)) { return value; } @@ -75,7 +75,7 @@ namespace Emby.Server.Implementations.IO } } - public void AddShortcutHandler(IShortcutHandler handler) + public virtual void AddShortcutHandler(IShortcutHandler handler) { _shortcutHandlers.Add(handler); } @@ -94,13 +94,6 @@ namespace Emby.Server.Implementations.IO } } - public char DirectorySeparatorChar => Path.DirectorySeparatorChar; - - public string GetFullPath(string path) - { - return Path.GetFullPath(path); - } - /// /// Determines whether the specified filename is shortcut. /// @@ -142,7 +135,7 @@ namespace Emby.Server.Implementations.IO return null; } - public string MakeAbsolutePath(string folderPath, string filePath) + public virtual string MakeAbsolutePath(string folderPath, string filePath) { if (string.IsNullOrWhiteSpace(filePath)) return filePath; @@ -195,7 +188,7 @@ namespace Emby.Server.Implementations.IO /// or /// target /// - public void CreateShortcut(string shortcutPath, string target) + public virtual void CreateShortcut(string shortcutPath, string target) { if (string.IsNullOrEmpty(shortcutPath)) { @@ -227,7 +220,7 @@ namespace Emby.Server.Implementations.IO /// A object. /// If the specified path points to a directory, the returned object's /// property will be set to true and all other properties will reflect the properties of the directory. - public FileSystemMetadata GetFileSystemInfo(string path) + public virtual FileSystemMetadata 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)) @@ -262,7 +255,7 @@ namespace Emby.Server.Implementations.IO /// If the specified path points to a directory, the returned object's /// property and the property will both be set to false. /// For automatic handling of files and directories, use . - public FileSystemMetadata GetFileInfo(string path) + public virtual FileSystemMetadata GetFileInfo(string path) { var fileInfo = new FileInfo(path); @@ -277,7 +270,7 @@ namespace Emby.Server.Implementations.IO /// If the specified path points to a file, the returned object's /// property will be set to true and the property will be set to false. /// For automatic handling of files and directories, use . - public FileSystemMetadata GetDirectoryInfo(string path) + public virtual FileSystemMetadata GetDirectoryInfo(string path) { var fileInfo = new DirectoryInfo(path); @@ -339,24 +332,19 @@ namespace Emby.Server.Implementations.IO return result; } - /// - /// The space char - /// - private const char SpaceChar = ' '; - /// /// Takes a filename and removes invalid characters /// /// The filename. /// System.String. /// filename - public string GetValidFilename(string filename) + public virtual string GetValidFilename(string filename) { var builder = new StringBuilder(filename); foreach (var c in _invalidFileNameChars) { - builder = builder.Replace(c, SpaceChar); + builder = builder.Replace(c, ' '); } return builder.ToString(); @@ -386,17 +374,17 @@ namespace Emby.Server.Implementations.IO /// /// The path. /// DateTime. - public DateTime GetCreationTimeUtc(string path) + public virtual DateTime GetCreationTimeUtc(string path) { return GetCreationTimeUtc(GetFileSystemInfo(path)); } - public DateTime GetCreationTimeUtc(FileSystemMetadata info) + public virtual DateTime GetCreationTimeUtc(FileSystemMetadata info) { return info.CreationTimeUtc; } - public DateTime GetLastWriteTimeUtc(FileSystemMetadata info) + public virtual DateTime GetLastWriteTimeUtc(FileSystemMetadata info) { return info.LastWriteTimeUtc; } @@ -425,7 +413,7 @@ namespace Emby.Server.Implementations.IO /// /// The path. /// DateTime. - public DateTime GetLastWriteTimeUtc(string path) + public virtual DateTime GetLastWriteTimeUtc(string path) { return GetLastWriteTimeUtc(GetFileSystemInfo(path)); } @@ -439,7 +427,7 @@ namespace Emby.Server.Implementations.IO /// The share. /// if set to true [is asynchronous]. /// FileStream. - public Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false) + public virtual Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false) { if (_supportsAsyncFileStreams && isAsync) { @@ -449,7 +437,7 @@ namespace Emby.Server.Implementations.IO return GetFileStream(path, mode, access, share, FileOpenOptions.None); } - public Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, FileOpenOptions fileOpenOptions) + public virtual Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, FileOpenOptions fileOpenOptions) => new FileStream(path, GetFileMode(mode), GetFileAccess(access), GetFileShare(share), 4096, GetFileOptions(fileOpenOptions)); private static FileOptions GetFileOptions(FileOpenOptions mode) @@ -511,7 +499,7 @@ namespace Emby.Server.Implementations.IO } } - public void SetHidden(string path, bool isHidden) + public virtual void SetHidden(string path, bool isHidden) { if (_environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows) { @@ -535,7 +523,7 @@ namespace Emby.Server.Implementations.IO } } - public void SetReadOnly(string path, bool isReadOnly) + public virtual void SetReadOnly(string path, bool isReadOnly) { if (_environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows) { @@ -559,7 +547,7 @@ namespace Emby.Server.Implementations.IO } } - public void SetAttributes(string path, bool isHidden, bool isReadOnly) + public virtual void SetAttributes(string path, bool isHidden, bool isReadOnly) { if (_environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows) { @@ -611,7 +599,7 @@ namespace Emby.Server.Implementations.IO /// /// The file1. /// The file2. - public void SwapFiles(string file1, string file2) + public virtual void SwapFiles(string file1, string file2) { if (string.IsNullOrEmpty(file1)) { @@ -630,18 +618,13 @@ namespace Emby.Server.Implementations.IO SetHidden(file2, false); Directory.CreateDirectory(_tempPath); - CopyFile(file1, temp1, true); + File.Copy(file1, temp1, true); - CopyFile(file2, file1, true); - CopyFile(temp1, file2, true); + File.Copy(file2, file1, true); + File.Copy(temp1, file2, true); } - private static char GetDirectorySeparatorChar(string path) - { - return Path.DirectorySeparatorChar; - } - - public bool ContainsSubPath(string parentPath, string path) + public virtual bool ContainsSubPath(string parentPath, string path) { if (string.IsNullOrEmpty(parentPath)) { @@ -653,19 +636,19 @@ namespace Emby.Server.Implementations.IO throw new ArgumentNullException(nameof(path)); } - var separatorChar = GetDirectorySeparatorChar(parentPath); + var separatorChar = Path.DirectorySeparatorChar; return path.IndexOf(parentPath.TrimEnd(separatorChar) + separatorChar, StringComparison.OrdinalIgnoreCase) != -1; } - public bool IsRootPath(string path) + public virtual bool IsRootPath(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } - var parent = GetDirectoryName(path); + var parent = Path.GetDirectoryName(path); if (!string.IsNullOrEmpty(parent)) { @@ -675,12 +658,7 @@ namespace Emby.Server.Implementations.IO return true; } - public string GetDirectoryName(string path) - { - return Path.GetDirectoryName(path); - } - - public string NormalizePath(string path) + public virtual string NormalizePath(string path) { if (string.IsNullOrEmpty(path)) { @@ -692,10 +670,10 @@ namespace Emby.Server.Implementations.IO return path; } - return path.TrimEnd(GetDirectorySeparatorChar(path)); + return path.TrimEnd(Path.DirectorySeparatorChar); } - public bool AreEqual(string path1, string path2) + public virtual bool AreEqual(string path1, string path2) { if (path1 == null && path2 == null) { @@ -710,7 +688,7 @@ namespace Emby.Server.Implementations.IO return string.Equals(NormalizePath(path1), NormalizePath(path2), StringComparison.OrdinalIgnoreCase); } - public string GetFileNameWithoutExtension(FileSystemMetadata info) + public virtual string GetFileNameWithoutExtension(FileSystemMetadata info) { if (info.IsDirectory) { @@ -720,12 +698,7 @@ namespace Emby.Server.Implementations.IO return Path.GetFileNameWithoutExtension(info.FullName); } - public string GetFileNameWithoutExtension(string path) - { - return Path.GetFileNameWithoutExtension(path); - } - - public bool IsPathFile(string path) + public virtual bool IsPathFile(string path) { // Cannot use Path.IsPathRooted because it returns false under mono when using windows-based paths, e.g. C:\\ @@ -740,23 +713,13 @@ namespace Emby.Server.Implementations.IO //return Path.IsPathRooted(path); } - public void DeleteFile(string path) + public virtual void DeleteFile(string path) { SetAttributes(path, false, false); File.Delete(path); } - - public void DeleteDirectory(string path, bool recursive) - { - Directory.Delete(path, recursive); - } - - public void CreateDirectory(string path) - { - Directory.CreateDirectory(path); - } - - public List GetDrives() + + public virtual List GetDrives() { // Only include drives in the ready state or this method could end up being very slow, waiting for drives to timeout return DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => new FileSystemMetadata @@ -768,19 +731,19 @@ namespace Emby.Server.Implementations.IO }).ToList(); } - public IEnumerable GetDirectories(string path, bool recursive = false) + public virtual IEnumerable GetDirectories(string path, bool recursive = false) { var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; return ToMetadata(new DirectoryInfo(path).EnumerateDirectories("*", searchOption)); } - public IEnumerable GetFiles(string path, bool recursive = false) + public virtual IEnumerable GetFiles(string path, bool recursive = false) { return GetFiles(path, null, false, recursive); } - public IEnumerable GetFiles(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false) + public virtual IEnumerable GetFiles(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false) { var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; @@ -809,7 +772,7 @@ namespace Emby.Server.Implementations.IO return ToMetadata(files); } - public IEnumerable GetFileSystemEntries(string path, bool recursive = false) + public virtual IEnumerable GetFileSystemEntries(string path, bool recursive = false) { var directoryInfo = new DirectoryInfo(path); var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; @@ -827,89 +790,19 @@ namespace Emby.Server.Implementations.IO { return infos.Select(GetFileSystemMetadata); } - - public string[] ReadAllLines(string path) - { - return File.ReadAllLines(path); - } - - public void WriteAllLines(string path, IEnumerable lines) - { - File.WriteAllLines(path, lines); - } - - public Stream OpenRead(string path) - { - return File.OpenRead(path); - } - - public void CopyFile(string source, string target, bool overwrite) - { - File.Copy(source, target, overwrite); - } - - public void MoveFile(string source, string target) - { - File.Move(source, target); - } - - public void MoveDirectory(string source, string target) - { - Directory.Move(source, target); - } - - public bool DirectoryExists(string path) - { - return Directory.Exists(path); - } - - public bool FileExists(string path) - { - return File.Exists(path); - } - - public string ReadAllText(string path) - { - return File.ReadAllText(path); - } - - public byte[] ReadAllBytes(string path) - { - return File.ReadAllBytes(path); - } - - public void WriteAllText(string path, string text, Encoding encoding) - { - File.WriteAllText(path, text, encoding); - } - - public void WriteAllText(string path, string text) - { - File.WriteAllText(path, text); - } - - public void WriteAllBytes(string path, byte[] bytes) - { - File.WriteAllBytes(path, bytes); - } - - public string ReadAllText(string path, Encoding encoding) - { - return File.ReadAllText(path, encoding); - } - - public IEnumerable GetDirectoryPaths(string path, bool recursive = false) + + public virtual IEnumerable GetDirectoryPaths(string path, bool recursive = false) { var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; return Directory.EnumerateDirectories(path, "*", searchOption); } - public IEnumerable GetFilePaths(string path, bool recursive = false) + public virtual IEnumerable GetFilePaths(string path, bool recursive = false) { return GetFilePaths(path, null, false, recursive); } - public IEnumerable GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false) + public virtual IEnumerable GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false) { var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; @@ -938,7 +831,7 @@ namespace Emby.Server.Implementations.IO return files; } - public IEnumerable GetFileSystemEntryPaths(string path, bool recursive = false) + public virtual IEnumerable GetFileSystemEntryPaths(string path, bool recursive = false) { var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; return Directory.EnumerateFileSystemEntries(path, "*", searchOption); @@ -948,7 +841,7 @@ namespace Emby.Server.Implementations.IO { if (_environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.OSX) { - RunProcess("chmod", "+x \"" + path + "\"", GetDirectoryName(path)); + RunProcess("chmod", "+x \"" + path + "\"", Path.GetDirectoryName(path)); } } diff --git a/MediaBrowser.Model/IO/IFileSystem.cs b/MediaBrowser.Model/IO/IFileSystem.cs index b8a315ccc..6c9b2bd88 100644 --- a/MediaBrowser.Model/IO/IFileSystem.cs +++ b/MediaBrowser.Model/IO/IFileSystem.cs @@ -36,32 +36,32 @@ namespace MediaBrowser.Model.IO string MakeAbsolutePath(string folderPath, string filePath); /// - /// Returns a object for the specified file or directory path. + /// Returns a object for the specified file or directory path. /// /// A path to a file or directory. - /// A object. - /// If the specified path points to a directory, the returned object's - /// property will be set to true and all other properties will reflect the properties of the directory. + /// A object. + /// If the specified path points to a directory, the returned object's + /// property will be set to true and all other properties will reflect the properties of the directory. FileSystemMetadata GetFileSystemInfo(string path); /// - /// Returns a object for the specified file path. + /// Returns a object for the specified file path. /// /// A path to a file. - /// A object. - /// If the specified path points to a directory, the returned object's - /// property and the property will both be set to false. - /// For automatic handling of files and directories, use . + /// A object. + /// If the specified path points to a directory, the returned object's + /// property and the property will both be set to false. + /// For automatic handling of files and directories, use . FileSystemMetadata GetFileInfo(string path); /// - /// Returns a object for the specified directory path. + /// Returns a object for the specified directory path. /// /// A path to a directory. - /// A object. - /// If the specified path points to a file, the returned object's - /// property will be set to true and the property will be set to false. - /// For automatic handling of files and directories, use . + /// A object. + /// If the specified path points to a file, the returned object's + /// property will be set to true and the property will be set to false. + /// For automatic handling of files and directories, use . FileSystemMetadata GetDirectoryInfo(string path); /// @@ -110,14 +110,8 @@ namespace MediaBrowser.Model.IO /// FileStream. Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false); - Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, FileOpenOptions fileOpenOptions); - - /// - /// Opens the read. - /// - /// The path. - /// Stream. - Stream OpenRead(string path); + Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, + FileOpenOptions fileOpenOptions); string DefaultDirectory { get; } @@ -152,8 +146,6 @@ namespace MediaBrowser.Model.IO /// System.String. string NormalizePath(string path); - string GetDirectoryName(string path); - /// /// Gets the file name without extension. /// @@ -161,13 +153,6 @@ namespace MediaBrowser.Model.IO /// System.String. string GetFileNameWithoutExtension(FileSystemMetadata info); - /// - /// Gets the file name without extension. - /// - /// The path. - /// System.String. - string GetFileNameWithoutExtension(string path); - /// /// Determines whether [is path file] [the specified path]. /// @@ -181,13 +166,6 @@ namespace MediaBrowser.Model.IO /// The path. void DeleteFile(string path); - /// - /// Deletes the directory. - /// - /// The path. - /// if set to true [recursive]. - void DeleteDirectory(string path, bool recursive); - /// /// Gets the directories. /// @@ -211,86 +189,6 @@ namespace MediaBrowser.Model.IO /// IEnumerable<FileSystemMetadata>. IEnumerable GetFileSystemEntries(string path, bool recursive = false); - /// - /// Creates the directory. - /// - /// The path. - void CreateDirectory(string path); - - /// - /// Copies the file. - /// - /// The source. - /// The target. - /// if set to true [overwrite]. - void CopyFile(string source, string target, bool overwrite); - - /// - /// Moves the file. - /// - /// The source. - /// The target. - void MoveFile(string source, string target); - - /// - /// Moves the directory. - /// - /// The source. - /// The target. - void MoveDirectory(string source, string target); - - /// - /// Directories the exists. - /// - /// The path. - /// true if XXXX, false otherwise. - bool DirectoryExists(string path); - - /// - /// Files the exists. - /// - /// The path. - /// true if XXXX, false otherwise. - bool FileExists(string path); - - /// - /// Reads all text. - /// - /// The path. - /// System.String. - string ReadAllText(string path); - - byte[] ReadAllBytes(string path); - - void WriteAllBytes(string path, byte[] bytes); - - /// - /// Writes all text. - /// - /// The path. - /// The text. - void WriteAllText(string path, string text); - - /// - /// Writes all text. - /// - /// The path. - /// The text. - /// The encoding. - void WriteAllText(string path, string text, Encoding encoding); - - /// - /// Reads all text. - /// - /// The path. - /// The encoding. - /// System.String. - string ReadAllText(string path, Encoding encoding); - - string[] ReadAllLines(string path); - - void WriteAllLines(string path, IEnumerable lines); - /// /// Gets the directory paths. /// @@ -306,6 +204,7 @@ namespace MediaBrowser.Model.IO /// if set to true [recursive]. /// IEnumerable<System.String>. IEnumerable GetFilePaths(string path, bool recursive = false); + IEnumerable GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive); /// @@ -319,15 +218,10 @@ namespace MediaBrowser.Model.IO void SetHidden(string path, bool isHidden); void SetReadOnly(string path, bool readOnly); void SetAttributes(string path, bool isHidden, bool readOnly); - - char DirectorySeparatorChar { get; } - - string GetFullPath(string path); - List GetDrives(); - void SetExecutable(string path); } + //TODO Investigate if can be replaced by the one from System.IO ? public enum FileOpenMode {