1+ // Copyright (c) 2023 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
2+ // Licensed under MIT license. See License.txt in the project root for license information.
3+
4+ using Net . DistributedFileStoreCache . SupportCode ;
5+ using System . Text . Json ;
6+ using AuthPermissions . AspNetCore . ShardingServices ;
7+ using Net . DistributedFileStoreCache ;
8+ using Microsoft . Extensions . DependencyInjection ;
9+ using System . Text . Encodings . Web ;
10+
11+ namespace ConsoleApp . AuthP6Upgrade ;
12+ class Program
13+ {
14+ static void Main ( string [ ] args )
15+ {
16+ //The arguments needed are
17+ //1. The name of the json file holding the AuthP version 5 sharding entries, e.g. shardingsettings.Production.json
18+ //2. The name for the new FileStore Cache file, e.g. FileStoreCacheFile.Production.json
19+ //3. The filepath to the the json file. This can be a relative or absolute
20+
21+
22+ if ( args . Length != 3 )
23+ {
24+ Console . WriteLine ( "This app expects three arguments: " ) ;
25+ Console . WriteLine ( " 1. The filepath to the the json file. This can be a relative or absolute." ) ;
26+ Console . WriteLine ( " 2. The name of the json file holding the AuthP version 5 sharding entries." ) ;
27+ Console . WriteLine ( " e.g. shardingsettings.Production.json" ) ;
28+ Console . WriteLine ( " 3. The name for the new FileStore Cache file used by AuthP version 6." ) ;
29+ Console . WriteLine ( " e.g. FileStoreCacheFile.Production.json" ) ;
30+ return ;
31+ }
32+
33+ var filePathToJsonFilePath = Path . Combine ( args [ 0 ] , args [ 1 ] ) ;
34+
35+ if ( ! File . Exists ( filePathToJsonFilePath ) )
36+ {
37+ Console . WriteLine ( "No json file was found using the filepath and the json file name you provided." ) ;
38+ Console . WriteLine ( "The full filePath you provided is" ) ;
39+ Console . WriteLine ( filePathToJsonFilePath ) ;
40+ return ;
41+ }
42+
43+ var jsonString = File . ReadAllText ( filePathToJsonFilePath ) ;
44+ Console . WriteLine ( jsonString ) ;
45+ var shardingData = JsonSerializer . Deserialize < JsonFileFormat > ( jsonString ) ? . ShardingDatabases ;
46+
47+ if ( shardingData == null || ! shardingData . Any ( ) )
48+ {
49+ Console . WriteLine ( "There aren't any sharding entries in your json file to place the the FileStore cache." ) ;
50+ Console . WriteLine ( "In this case the FileStore cache will start empty, which is what you want." ) ;
51+ }
52+
53+ var breakDownCacheName = args [ 2 ] . Split ( '.' ) ;
54+ if ( ! ( ( breakDownCacheName . Length == 3 && breakDownCacheName [ 2 ] == "json" )
55+ || ( breakDownCacheName . Length == 2 && breakDownCacheName [ 1 ] != "json" ) ) )
56+ {
57+ Console . WriteLine ( "The name of the FileStore Cache name doesn't have the correct format." ) ;
58+ Console . WriteLine ( "The FileStore Cache should have three parts, e.g. FileStoreCacheFile.Production.json" ) ;
59+ Console . WriteLine ( args [ 2 ] ) ;
60+ return ;
61+ }
62+ //Console.WriteLine(breakDownCacheName.ToString());
63+
64+ //Now use Net.DistributedFileStoreCache to add the
65+ string fileStoreName = null ;
66+ var services = new ServiceCollection ( ) ;
67+ services . AddDistributedFileStoreCache ( options =>
68+ {
69+ options . WhichVersion = FileStoreCacheVersions . Class ;
70+ options . PathToCacheFileDirectory = Path . GetFullPath ( args [ 0 ] ) ;
71+ options . FirstPartOfCacheFileName = breakDownCacheName [ 0 ] ;
72+ options . SecondPartOfCacheFileName = breakDownCacheName [ 1 ] ;
73+
74+ options . JsonSerializerForCacheFile = new JsonSerializerOptions
75+ {
76+ //This will make the json in the FileStore json file will be easier to read
77+ //BUT it will be a bit slower and take up more characters
78+ WriteIndented = true ,
79+ //This makes unicode chars smaller - especially useful for FileStoreCacheVersions.Class
80+ //see https://github.com/JonPSmith/Net.DistributedFileStoreCache/wiki/Tips-on-making-your-cache-fast#class-version---already-has-unsaferelaxedjsonescaping
81+ Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping
82+ } ;
83+ fileStoreName = options . FormCacheFileName ( ) ;
84+ } ) ;
85+ var serviceProvider = services . BuildServiceProvider ( ) ;
86+ var readWriteService = serviceProvider . GetRequiredService < IDistributedFileStoreCacheClass > ( ) ;
87+
88+ foreach ( var shardingEntry in shardingData )
89+ {
90+ var key = GetSetShardingEntriesFileStoreCache . ShardingEntryPrefix + shardingEntry . Name ;
91+ readWriteService . SetClass ( key , shardingEntry ) ;
92+ Console . WriteLine ( $ "Added the sharding entry with the name of { shardingEntry . Name } added to the FileStore") ;
93+ }
94+
95+ Console . WriteLine ( $ "Successfully copied { shardingData . Count } sharding entry to the FileStore Cache called '{ fileStoreName } '.") ;
96+ }
97+ }
98+
99+ public class JsonFileFormat
100+ {
101+ public List < ShardingEntry > ShardingDatabases { get ; set ; }
102+ }
0 commit comments