Improve IO code

* Style changes
* Remove remnants of SMB support
* Use `GetInvalidFileNameChars` instead of rolling our own
* Remove possible unexpected behaviour with async file streams
* Remove some dead code
This commit is contained in:
Bond_009
2019-03-28 23:19:56 +01:00
parent 2dbc1153e8
commit 41df562419
5 changed files with 82 additions and 546 deletions

View File

@@ -19,8 +19,6 @@ namespace Emby.Server.Implementations.IO
{
protected ILogger Logger;
private readonly bool _supportsAsyncFileStreams;
private char[] _invalidFileNameChars;
private readonly List<IShortcutHandler> _shortcutHandlers = new List<IShortcutHandler>();
private readonly string _tempPath;
@@ -32,11 +30,8 @@ namespace Emby.Server.Implementations.IO
IApplicationPaths applicationPaths)
{
Logger = loggerFactory.CreateLogger("FileSystem");
_supportsAsyncFileStreams = true;
_tempPath = applicationPaths.TempDirectory;
SetInvalidFileNameChars(OperatingSystem.Id == OperatingSystemId.Windows);
_isEnvironmentCaseInsensitive = OperatingSystem.Id == OperatingSystemId.Windows;
}
@@ -45,20 +40,6 @@ namespace Emby.Server.Implementations.IO
_shortcutHandlers.Add(handler);
}
protected void SetInvalidFileNameChars(bool enableManagedInvalidFileNameChars)
{
if (enableManagedInvalidFileNameChars)
{
_invalidFileNameChars = Path.GetInvalidFileNameChars();
}
else
{
// Be consistent across platforms because the windows server will fail to query network shares that don't follow windows conventions
// https://referencesource.microsoft.com/#mscorlib/system/io/path.cs
_invalidFileNameChars = new char[] { '\"', '<', '>', '|', '\0', (char)1, (char)2, (char)3, (char)4, (char)5, (char)6, (char)7, (char)8, (char)9, (char)10, (char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20, (char)21, (char)22, (char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29, (char)30, (char)31, ':', '*', '?', '\\', '/' };
}
}
/// <summary>
/// Determines whether the specified filename is shortcut.
/// </summary>
@@ -92,20 +73,25 @@ namespace Emby.Server.Implementations.IO
var extension = Path.GetExtension(filename);
var handler = _shortcutHandlers.FirstOrDefault(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
if (handler != null)
{
return handler.Resolve(filename);
}
return null;
return handler?.Resolve(filename);
}
public virtual string MakeAbsolutePath(string folderPath, string filePath)
{
if (string.IsNullOrWhiteSpace(filePath)) return filePath;
if (string.IsNullOrWhiteSpace(filePath))
{
return filePath;
}
if (filePath.Contains(@"://")) return filePath; //stream
if (filePath.Length > 3 && filePath[1] == ':' && filePath[2] == '/') return filePath; //absolute local path
if (filePath.Contains("://"))
{
return filePath; // stream
}
if (filePath.Length > 3 && filePath[1] == ':' && filePath[2] == '/')
{
return filePath; // absolute local path
}
// unc path
if (filePath.StartsWith("\\\\"))
@@ -125,9 +111,7 @@ namespace Emby.Server.Implementations.IO
}
try
{
string path = System.IO.Path.Combine(folderPath, filePath);
path = System.IO.Path.GetFullPath(path);
return path;
return Path.Combine(Path.GetFullPath(folderPath), filePath);
}
catch (ArgumentException)
{
@@ -166,7 +150,7 @@ namespace Emby.Server.Implementations.IO
}
var extension = Path.GetExtension(shortcutPath);
var handler = _shortcutHandlers.FirstOrDefault(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
var handler = _shortcutHandlers.Find(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
if (handler != null)
{
@@ -244,12 +228,13 @@ namespace Emby.Server.Implementations.IO
private FileSystemMetadata GetFileSystemMetadata(FileSystemInfo info)
{
var result = new FileSystemMetadata();
result.Exists = info.Exists;
result.FullName = info.FullName;
result.Extension = info.Extension;
result.Name = info.Name;
var result = new FileSystemMetadata
{
Exists = info.Exists,
FullName = info.FullName,
Extension = info.Extension,
Name = info.Name
};
if (result.Exists)
{
@@ -260,8 +245,7 @@ namespace Emby.Server.Implementations.IO
// result.IsHidden = (info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
//}
var fileInfo = info as FileInfo;
if (fileInfo != null)
if (info is FileInfo fileInfo)
{
result.Length = fileInfo.Length;
result.DirectoryName = fileInfo.DirectoryName;
@@ -307,7 +291,7 @@ namespace Emby.Server.Implementations.IO
{
var builder = new StringBuilder(filename);
foreach (var c in _invalidFileNameChars)
foreach (var c in Path.GetInvalidFileNameChars())
{
builder = builder.Replace(c, ' ');
}
@@ -394,7 +378,7 @@ namespace Emby.Server.Implementations.IO
/// <returns>FileStream.</returns>
public virtual Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false)
{
if (_supportsAsyncFileStreams && isAsync)
if (isAsync)
{
return GetFileStream(path, mode, access, share, FileOpenOptions.Asynchronous);
}