Io abstraction (#7293)

Co-authored-by: p-storm <paul.de.man@gmail.com>
This commit is contained in:
P-Storm
2020-11-02 18:33:43 +01:00
committed by GitHub
parent 5c3eef0112
commit 0d4017fe1a
109 changed files with 700 additions and 678 deletions

View File

@@ -4,6 +4,7 @@
using System;
using System.IO;
using System.IO.Abstractions;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
@@ -19,18 +20,28 @@ namespace Wox.Infrastructure.Storage
/// </summary>
public class BinaryStorage<T> : IStorage<T>
{
private readonly IFileSystem _fileSystem;
// This storage helper returns whether or not to delete the binary storage items
private const int _binaryStorage = 0;
private StoragePowerToysVersionInfo _storageHelper;
public BinaryStorage(string filename)
: this(filename, new FileSystem())
{
}
public BinaryStorage(string filename, IFileSystem fileSystem)
{
_fileSystem = fileSystem;
const string directoryName = "Cache";
var directoryPath = Path.Combine(Constant.DataDirectory, directoryName);
var path = _fileSystem.Path;
var directoryPath = path.Combine(Constant.DataDirectory, directoryName);
Helper.ValidateDirectory(directoryPath);
const string fileSuffix = ".cache";
FilePath = Path.Combine(directoryPath, $"{filename}{fileSuffix}");
FilePath = path.Combine(directoryPath, $"{filename}{fileSuffix}");
}
public string FilePath { get; }
@@ -42,17 +53,17 @@ namespace Wox.Infrastructure.Storage
// Depending on the version number of the previously installed PT Run, delete the cache if it is found to be incompatible
if (_storageHelper.ClearCache)
{
if (File.Exists(FilePath))
if (_fileSystem.File.Exists(FilePath))
{
File.Delete(FilePath);
_fileSystem.File.Delete(FilePath);
Log.Info($"Deleting cached data at <{FilePath}>", GetType());
}
}
if (File.Exists(FilePath))
if (_fileSystem.File.Exists(FilePath))
{
if (new FileInfo(FilePath).Length == 0)
if (_fileSystem.FileInfo.FromFileName(FilePath).Length == 0)
{
Log.Error($"Zero length cache file <{FilePath}>", GetType());
@@ -60,7 +71,7 @@ namespace Wox.Infrastructure.Storage
return defaultData;
}
using (var stream = new FileStream(FilePath, FileMode.Open))
using (var stream = _fileSystem.FileStream.Create(FilePath, FileMode.Open))
{
var d = Deserialize(stream, defaultData);
return d;
@@ -76,7 +87,7 @@ namespace Wox.Infrastructure.Storage
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
private T Deserialize(FileStream stream, T defaultData)
private T Deserialize(Stream stream, T defaultData)
{
// http://stackoverflow.com/questions/2120055/binaryformatter-deserialize-gives-serializationexception
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

View File

@@ -5,6 +5,7 @@
using System;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using Newtonsoft.Json;
using Wox.Plugin.Logger;
@@ -15,6 +16,10 @@ namespace Wox.Infrastructure.Storage
/// </summary>
public class JsonStorage<T>
{
private static readonly IFileSystem FileSystem = new FileSystem();
private static readonly IPath Path = FileSystem.Path;
private static readonly IFile File = FileSystem.File;
private readonly JsonSerializerSettings _serializerSettings;
private T _data;

View File

@@ -2,7 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.IO.Abstractions;
using Wox.Plugin;
namespace Wox.Infrastructure.Storage
@@ -10,6 +10,9 @@ namespace Wox.Infrastructure.Storage
public class PluginJsonStorage<T> : JsonStorage<T>
where T : new()
{
private static readonly IFileSystem FileSystem = new FileSystem();
private static readonly IPath Path = FileSystem.Path;
public PluginJsonStorage()
{
// C# related, add python related below

View File

@@ -3,12 +3,15 @@
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.IO.Abstractions;
namespace Wox.Infrastructure.Storage
{
public class StoragePowerToysVersionInfo
{
private static readonly IFileSystem FileSystem = new FileSystem();
private static readonly IFile File = FileSystem.File;
// This detail is accessed by the storage items and is used to decide if the cache must be deleted or not
public bool ClearCache { get; set; }

View File

@@ -2,7 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.IO.Abstractions;
using Wox.Plugin;
namespace Wox.Infrastructure.Storage
@@ -10,6 +10,9 @@ namespace Wox.Infrastructure.Storage
public class WoxJsonStorage<T> : JsonStorage<T>
where T : new()
{
private static readonly IFileSystem FileSystem = new FileSystem();
private static readonly IPath Path = FileSystem.Path;
public WoxJsonStorage()
{
var directoryPath = Path.Combine(Constant.DataDirectory, DirectoryName);