11// Copyright (c) 2024 Files Community
22// Licensed under the MIT License. See the LICENSE.
33
4- using LiteDB ;
54using Microsoft . Win32 ;
6- using System . IO ;
75using System . Runtime . CompilerServices ;
8- using System . Text ;
96using Windows . ApplicationModel ;
10- using Windows . Storage ;
117using JsonSerializer = System . Text . Json . JsonSerializer ;
128using static Files . App . Helpers . RegistryHelpers ;
139using static Files . App . Utils . FileTags . TaggedFileRegistry ;
@@ -18,63 +14,6 @@ public sealed class FileTagsDatabase
1814 {
1915 private readonly static string FileTagsKey = @$ "Software\Files Community\{ Package . Current . Id . FullName } \v1\FileTags";
2016
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-
7817 public void SetTags ( string filePath , ulong ? frn , string [ ] tags )
7918 {
8019 using var filePathKey = Registry . CurrentUser . CreateSubKey ( CombineKeys ( FileTagsKey , filePath ) ) ;
0 commit comments