mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-01-18 06:29:44 +08:00
[PT Run] Clear binary and json storage files on version upgrade (#4479)
* Clean termination of powertoys process. * Fixed issue with run not responding to WM_CLOSE * Fixed serialization error in pinyin and image cache * Fixed merge conflict * Fixed nit wrt to master * Basic framework of clearing up of cache is working * formatting * removed the default argument of load * fixed nit comment * rewriting the PowerToys version * Each storage file has an associated version file which helps decide whether or not to delete that file on loading * removed unnecessary reference * renamed file to StoragePowerToysVersionInfo * adding log files * Checking whether the version strings are null, if so, we would clear the cache * Added filepath to log files to make it more informative * fixed nit naming * using lesser than to compare instead of portable version Co-authored-by: Divyansh Srivastava <somm14divi@gmail.com>
This commit is contained in:
parent
0b391584d4
commit
d17fc86fa4
@ -14,6 +14,10 @@ namespace Wox.Infrastructure.Storage
|
||||
/// </summary>
|
||||
public class BinaryStorage<T>
|
||||
{
|
||||
// This storage helper returns whether or not to delete the binary storage items
|
||||
private static readonly int BINARY_STORAGE = 0;
|
||||
private StoragePowerToysVersionInfo _storageHelper;
|
||||
|
||||
public BinaryStorage(string filename)
|
||||
{
|
||||
const string directoryName = "Cache";
|
||||
@ -28,6 +32,17 @@ namespace Wox.Infrastructure.Storage
|
||||
|
||||
public T TryLoad(T defaultData)
|
||||
{
|
||||
_storageHelper = new StoragePowerToysVersionInfo(FilePath, BINARY_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))
|
||||
{
|
||||
File.Delete(FilePath);
|
||||
Log.Info($"|BinaryStorage.TryLoad|Deleting cached data| <{FilePath}>");
|
||||
}
|
||||
}
|
||||
|
||||
if (File.Exists(FilePath))
|
||||
{
|
||||
if (new FileInfo(FilePath).Length == 0)
|
||||
@ -110,6 +125,8 @@ namespace Wox.Infrastructure.Storage
|
||||
Log.Exception($"|BinaryStorage.Save|serialize error for file <{FilePath}>", e);
|
||||
}
|
||||
}
|
||||
_storageHelper.Close();
|
||||
Log.Info($"|BinaryStorage.Save|Saving cached data| <{FilePath}>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,9 @@ namespace Wox.Infrastructure.Storage
|
||||
public string FilePath { get; set; }
|
||||
public string DirectoryPath { get; set; }
|
||||
|
||||
// This storage helper returns whether or not to delete the json storage items
|
||||
private static readonly int JSON_STORAGE = 1;
|
||||
private StoragePowerToysVersionInfo _storageHelper;
|
||||
|
||||
internal JsonStorage()
|
||||
{
|
||||
@ -33,6 +36,17 @@ namespace Wox.Infrastructure.Storage
|
||||
|
||||
public T Load()
|
||||
{
|
||||
_storageHelper = new StoragePowerToysVersionInfo(FilePath, JSON_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))
|
||||
{
|
||||
File.Delete(FilePath);
|
||||
Log.Info($"|JsonStorage.TryLoad|Deleting cached data|<{FilePath}>");
|
||||
}
|
||||
}
|
||||
|
||||
if (File.Exists(FilePath))
|
||||
{
|
||||
var serialized = File.ReadAllText(FilePath);
|
||||
@ -96,6 +110,8 @@ namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
string serialized = JsonConvert.SerializeObject(_data, Formatting.Indented);
|
||||
File.WriteAllText(FilePath, serialized);
|
||||
_storageHelper.Close();
|
||||
Log.Info($"|JsonStorage.Save|Saving cached data| <{FilePath}>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
public class StoragePowerToysVersionInfo
|
||||
{
|
||||
// This detail is accessed by the storage items and is used to decide if the cache must be deleted or not
|
||||
public bool clearCache = false;
|
||||
|
||||
|
||||
private String currentPowerToysVersion = String.Empty;
|
||||
private String FilePath { get; set; } = String.Empty;
|
||||
|
||||
// As of now this information is not pertinent but may be in the future
|
||||
// There may be cases when we want to delete only the .cache files and not the .json storage files
|
||||
private enum StorageType
|
||||
{
|
||||
BINARY_STORAGE = 0,
|
||||
JSON_STORAGE = 1
|
||||
}
|
||||
|
||||
// To compare the version numbers
|
||||
public static bool Lessthan(string version1, string version2)
|
||||
{
|
||||
string version = "v";
|
||||
string period = ".";
|
||||
const int versionLength = 3;
|
||||
|
||||
// If there is some error in populating/retrieving the version numbers, then the cache must be deleted
|
||||
// This case will not be hit, but is present as a fail safe
|
||||
if(String.IsNullOrEmpty(version1) || String.IsNullOrEmpty(version2))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
string[] split1 = version1.Split( new string[] { version, period }, StringSplitOptions.RemoveEmptyEntries);
|
||||
string[] split2 = version2.Split( new string[] { version, period }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
for(int i=0; i<versionLength; i++)
|
||||
{
|
||||
if(int.Parse(split1[i]) < int.Parse(split2[i]))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public string GetPreviousVersion()
|
||||
{
|
||||
if (File.Exists(FilePath))
|
||||
{
|
||||
return File.ReadAllText(FilePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
// which means it's an old version of PowerToys
|
||||
string oldVersion = "v0.0.0";
|
||||
return oldVersion;
|
||||
}
|
||||
}
|
||||
|
||||
private string GetFilePath(String AssociatedFilePath, int type)
|
||||
{
|
||||
string suffix = string.Empty;
|
||||
string cacheSuffix = ".cache";
|
||||
string jsonSuffix = ".json";
|
||||
|
||||
if(type == (uint)StorageType.BINARY_STORAGE)
|
||||
{
|
||||
suffix = cacheSuffix;
|
||||
}
|
||||
else if(type == (uint)StorageType.JSON_STORAGE)
|
||||
{
|
||||
suffix = jsonSuffix;
|
||||
}
|
||||
|
||||
string filePath = AssociatedFilePath.Substring(0, AssociatedFilePath.Length - suffix.Length) + "_version.txt";
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public StoragePowerToysVersionInfo(String AssociatedFilePath, int type)
|
||||
{
|
||||
FilePath = GetFilePath(AssociatedFilePath, type);
|
||||
// Get the previous version of PowerToys and cache Storage details from the CacheDetails.json storage file
|
||||
String previousVersion = GetPreviousVersion();
|
||||
currentPowerToysVersion = Microsoft.PowerToys.Settings.UI.Lib.Utilities.Helper.GetProductVersion();
|
||||
|
||||
// If the previous version is below a set threshold, then we want to delete the file
|
||||
// However, we do not want to delete the cache if the same version of powerToys is being launched
|
||||
if (Lessthan(previousVersion, currentPowerToysVersion))
|
||||
{
|
||||
clearCache = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
// Update the Version file to the current version of powertoys
|
||||
File.WriteAllText(FilePath, currentPowerToysVersion);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user