1
1
// Copyright (c) 2024 Files Community
2
2
// Licensed under the MIT License. See the LICENSE.
3
3
4
- using LiteDB ;
5
4
using Microsoft . Win32 ;
6
- using System . IO ;
7
5
using System . Runtime . CompilerServices ;
8
- using System . Text ;
9
6
using Windows . ApplicationModel ;
10
- using Windows . Storage ;
11
7
using JsonSerializer = System . Text . Json . JsonSerializer ;
12
8
using static Files . App . Helpers . RegistryHelpers ;
13
9
using static Files . App . Utils . FileTags . TaggedFileRegistry ;
@@ -18,63 +14,6 @@ public sealed class FileTagsDatabase
18
14
{
19
15
private readonly static string FileTagsKey = @$ "Software\Files Community\{ Package . Current . Id . FullName } \v1\FileTags";
20
16
21
- private readonly static string FileTagsDbPath = Path . Combine ( ApplicationData . Current . LocalFolder . Path , "filetags.db" ) ;
22
- private const string FileTagsCollectionName = "taggedfiles" ;
23
-
24
- static FileTagsDatabase ( )
25
- {
26
- if ( File . Exists ( FileTagsDbPath ) )
27
- {
28
- SafetyExtensions . IgnoreExceptions ( ( ) => CheckDbVersion ( FileTagsDbPath ) ) ;
29
-
30
- using ( var database = new LiteDatabase ( new ConnectionString ( FileTagsDbPath )
31
- {
32
- Connection = ConnectionType . Direct ,
33
- Upgrade = true
34
- } ) )
35
- {
36
- UpdateDb ( database ) ;
37
- ImportCore ( database . GetCollection < TaggedFile > ( FileTagsCollectionName ) . FindAll ( ) . ToArray ( ) ) ;
38
- }
39
-
40
- File . Delete ( FileTagsDbPath ) ;
41
- }
42
- }
43
-
44
- private static void UpdateDb ( LiteDatabase database )
45
- {
46
- if ( database . UserVersion == 0 )
47
- {
48
- var col = database . GetCollection ( FileTagsCollectionName ) ;
49
- foreach ( var doc in col . FindAll ( ) )
50
- {
51
- doc [ "Tags" ] = new BsonValue ( new [ ] { doc [ "Tag" ] . AsString } ) ;
52
- doc . Remove ( "Tags" ) ;
53
- col . Update ( doc ) ;
54
- }
55
- database . UserVersion = 1 ;
56
- }
57
- }
58
-
59
- // https://github.com/mbdavid/LiteDB/blob/master/LiteDB/Engine/Engine/Upgrade.cs
60
- private static void CheckDbVersion ( string filename )
61
- {
62
- var buffer = new byte [ 8192 * 2 ] ;
63
- using ( var stream = new FileStream ( filename , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
64
- {
65
- // read first 16k
66
- stream . Read ( buffer , 0 , buffer . Length ) ;
67
-
68
- // checks if v7 (plain or encrypted)
69
- if ( Encoding . UTF8 . GetString ( buffer , 25 , "** This is a LiteDB file **" . Length ) == "** This is a LiteDB file **" &&
70
- buffer [ 52 ] == 7 )
71
- {
72
- return ; // version 4.1.4
73
- }
74
- }
75
- File . Delete ( filename ) ; // recreate DB with correct version
76
- }
77
-
78
17
public void SetTags ( string filePath , ulong ? frn , string [ ] tags )
79
18
{
80
19
using var filePathKey = Registry . CurrentUser . CreateSubKey ( CombineKeys ( FileTagsKey , filePath ) ) ;
0 commit comments