Skip to content

Commit

Permalink
Fix: Fixed System.TypeInitializationException in FileTagsDatabase (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
hishitetsu authored Jun 23, 2024
1 parent ccf4c36 commit 30f67b9
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/Files.App/Utils/FileTags/FileTagsDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ namespace Files.App.Utils.FileTags
{
public sealed class FileTagsDatabase
{
private readonly static string FileTagsKey = @$"Software\Files Community\{Package.Current.Id.FullName}\v1\FileTags";
private static string? _FileTagsKey;
private string? FileTagsKey => _FileTagsKey ??= SafetyExtensions.IgnoreExceptions(() => @$"Software\Files Community\{Package.Current.Id.FullName}\v1\FileTags");

public void SetTags(string filePath, ulong? frn, string[] tags)
{
if (FileTagsKey is null)
return;

using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, filePath));

if (tags is [])
Expand Down Expand Up @@ -47,6 +51,9 @@ public void SetTags(string filePath, ulong? frn, string[] tags)

private TaggedFile? FindTag(string? filePath, ulong? frn)
{
if (FileTagsKey is null)
return null;

if (filePath is not null)
{
using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, filePath));
Expand Down Expand Up @@ -87,6 +94,9 @@ public void SetTags(string filePath, ulong? frn, string[] tags)

public void UpdateTag(string oldFilePath, ulong? frn, string? newFilePath)
{
if (FileTagsKey is null)
return;

var tag = FindTag(oldFilePath, null);
using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, oldFilePath));
SaveValues(filePathKey, null);
Expand All @@ -112,6 +122,9 @@ public void UpdateTag(string oldFilePath, ulong? frn, string? newFilePath)

public void UpdateTag(ulong oldFrn, ulong? frn, string? newFilePath)
{
if (FileTagsKey is null)
return;

var tag = FindTag(null, oldFrn);
using var frnKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, "FRN", oldFrn.ToString()));
SaveValues(frnKey, null);
Expand Down Expand Up @@ -143,26 +156,31 @@ public string[] GetTags(string? filePath, ulong? frn)
public IEnumerable<TaggedFile> GetAll()
{
var list = new List<TaggedFile>();
IterateKeys(list, FileTagsKey, 0);

if (FileTagsKey is not null)
IterateKeys(list, FileTagsKey, 0);

return list;
}

public IEnumerable<TaggedFile> GetAllUnderPath(string folderPath)
{
folderPath = folderPath.Replace('/', '\\').TrimStart('\\');
var list = new List<TaggedFile>();
IterateKeys(list, CombineKeys(FileTagsKey, folderPath), 0);

if (FileTagsKey is not null)
IterateKeys(list, CombineKeys(FileTagsKey, folderPath), 0);

return list;
}

public void Import(string json)
{
if (FileTagsKey is null)
return;

var tags = JsonSerializer.Deserialize<TaggedFile[]>(json);
ImportCore(tags);
}

private static void ImportCore(TaggedFile[]? tags)
{
Registry.CurrentUser.DeleteSubKeyTree(FileTagsKey, false);
if (tags is null)
{
Expand All @@ -183,7 +201,10 @@ private static void ImportCore(TaggedFile[]? tags)
public string Export()
{
var list = new List<TaggedFile>();
IterateKeys(list, FileTagsKey, 0);

if (FileTagsKey is not null)
IterateKeys(list, FileTagsKey, 0);

return JsonSerializer.Serialize(list);
}

Expand Down

0 comments on commit 30f67b9

Please sign in to comment.