@@ -19,38 +19,49 @@ import {
19
19
UploadJobPersistInfo ,
20
20
} from "./types" ;
21
21
22
- const configPath = path . join ( os . homedir ( ) , ".kodo-browser" ) ;
22
+ const oldConfigPath = path . join ( os . homedir ( ) , ".kodo-browser" ) ;
23
+ const newConfigPath = path . join ( os . homedir ( ) , ".kodo-browser-v2" ) ;
23
24
24
25
export default async function ( ) {
26
+ try {
27
+ await fsPromises . access ( newConfigPath ) ;
28
+ } catch ( err : any ) {
29
+ if ( err . code !== "ENOENT" ) {
30
+ throw err ;
31
+ }
32
+ await fsPromises . mkdir ( newConfigPath , { recursive : true } ) ;
33
+ }
34
+
25
35
await migratePreferences ( ) ;
26
36
27
37
// below migrate date in config path.
28
38
// if config path isn't existing, no data need to migrate
29
39
try {
30
- await fsPromises . access ( configPath ) ;
40
+ await fsPromises . access ( oldConfigPath ) ;
31
41
} catch ( err : any ) {
32
42
if ( err . code === "ENOENT" ) {
33
43
return ;
34
44
}
35
45
throw err ;
36
46
}
37
47
48
+ await migrateEndpointConfig ( ) ;
38
49
await migrateAkHistory ( ) ;
39
50
40
- const filenames = await fsPromises . readdir ( configPath )
51
+ const filenames = await fsPromises . readdir ( oldConfigPath )
41
52
const ex = / (?< type > b o o k m a r k s | e x t e r n a l _ p a t h s | u p p r o g | d o w n p r o g ) _ (?< ak > .+ ) .j s o n / ;
42
53
for ( const fName of filenames ) {
43
54
const matchRes = fName . match ( ex ) ;
44
55
if ( ! matchRes || ! matchRes . groups ?. [ "ak" ] ) {
45
56
continue ;
46
57
}
47
58
const ak = matchRes . groups [ "ak" ] ;
48
- const filePath = path . join ( configPath , fName ) ;
59
+ const filePath = path . join ( oldConfigPath , fName ) ;
49
60
if ( ak === "undefined" ) {
50
61
await fsPromises . unlink ( filePath ) ;
51
62
continue ;
52
63
}
53
- const profileDirPath = path . join ( configPath , `profile_${ ak } ` ) ;
64
+ const profileDirPath = path . join ( newConfigPath , `profile_${ ak } ` ) ;
54
65
try {
55
66
await fsPromises . access ( profileDirPath ) ;
56
67
} catch {
@@ -166,35 +177,51 @@ async function migratePreferences() {
166
177
return ;
167
178
}
168
179
try {
169
- await fsPromises . access ( configPath ) ;
180
+ await fsPromises . access ( newConfigPath ) ;
170
181
} catch {
171
- await fsPromises . mkdir ( configPath ) ;
182
+ await fsPromises . mkdir ( newConfigPath ) ;
172
183
}
173
- const confFilePath = path . join ( configPath , "app_preferences.json" ) ;
184
+ const confFilePath = path . join ( newConfigPath , "app_preferences.json" ) ;
174
185
await fsPromises . writeFile ( confFilePath , JSON . stringify ( preferences ) ) ;
175
186
}
176
187
188
+ async function migrateEndpointConfig ( ) {
189
+ const oldFilePath = path . join ( oldConfigPath , "config.json" ) ;
190
+ const newFilePath = path . join ( newConfigPath , "config.json" ) ;
191
+ try {
192
+ await fsPromises . copyFile ( oldFilePath , newFilePath ) ;
193
+ } catch ( err : any ) {
194
+ if ( err . code !== "ENOENT" ) {
195
+ throw err ;
196
+ }
197
+ }
198
+ }
199
+
177
200
async function migrateAkHistory ( ) {
178
- const filePath = path . join ( configPath , "ak_histories.json" ) ;
179
- try {
180
- await fsPromises . access ( filePath ) ;
201
+ const oldFilePath = path . join ( oldConfigPath , "ak_histories.json" ) ;
202
+ const newFilePath = path . join ( newConfigPath , "ak_histories.json" ) ;
203
+ try {
204
+ await fsPromises . access ( oldFilePath ) ;
181
205
} catch {
182
206
return ;
183
207
}
184
- const oldContentBuf = await fsPromises . readFile ( filePath ) ;
208
+ const oldContentBuf = await fsPromises . readFile ( oldFilePath ) ;
185
209
const oldContent : OldAkHistory = JSON . parse ( oldContentBuf . toString ( ) ) ;
186
210
const content : AkHistory = {
187
211
historyItems : [ ] ,
188
212
} ;
189
213
for ( const i of oldContent . historyItems ) {
214
+ if ( ! i . accessKeyId || ! i . accessKeySecret ) {
215
+ continue ;
216
+ }
190
217
content . historyItems . push ( {
191
218
endpointType : i . isPublicCloud ? "public" : "private" ,
192
219
accessKey : i . accessKeyId ,
193
220
accessSecret : i . accessKeySecret ,
194
221
description : i . description ,
195
- } )
222
+ } ) ;
196
223
}
197
- await fsPromises . writeFile ( filePath , JSON . stringify ( content ) ) ;
224
+ await fsPromises . writeFile ( newFilePath , JSON . stringify ( content ) ) ;
198
225
}
199
226
200
227
const pathEx = / (?< protocol > [ a - z A - Z 0 - 9 \- _ ] + : \/ \/ ) (?< path > .+ ) / ;
@@ -206,19 +233,21 @@ async function migrateBookmarks(ak: string, filePath: string) {
206
233
homeAddress : oldContent . homeAddress ,
207
234
list : [ ] ,
208
235
} ;
209
- for ( const b of oldContent . bookmarks ) {
210
- const exRes = b . fullPath . match ( pathEx ) ;
211
- if ( ! exRes || ! exRes . groups ) {
212
- continue ;
236
+ if ( Array . isArray ( oldContent . bookmarks ) ) {
237
+ for ( const b of oldContent . bookmarks ) {
238
+ const exRes = b . fullPath . match ( pathEx ) ;
239
+ if ( ! exRes || ! exRes . groups ) {
240
+ continue ;
241
+ }
242
+ content . list . push ( {
243
+ protocol : exRes . groups [ "protocol" ] ,
244
+ path : exRes . groups [ "path" ] ,
245
+ timestamp : b . timestamp ,
246
+ } ) ;
213
247
}
214
- content . list . push ( {
215
- protocol : exRes . groups [ "protocol" ] ,
216
- path : exRes . groups [ "path" ] ,
217
- timestamp : b . timestamp ,
218
- } ) ;
219
248
}
220
249
await fsPromises . writeFile (
221
- path . join ( configPath , `profile_${ ak } ` , "bookmarks.json" ) ,
250
+ path . join ( newConfigPath , `profile_${ ak } ` , "bookmarks.json" ) ,
222
251
JSON . stringify ( content ) ,
223
252
) ;
224
253
await fsPromises . unlink ( filePath ) ;
@@ -242,7 +271,7 @@ async function migrateExternalPaths(ak: string, filePath: string) {
242
271
} ) ;
243
272
}
244
273
await fsPromises . writeFile (
245
- path . join ( configPath , `profile_${ ak } ` , "external_paths.json" ) ,
274
+ path . join ( newConfigPath , `profile_${ ak } ` , "external_paths.json" ) ,
246
275
JSON . stringify ( content ) ,
247
276
) ;
248
277
await fsPromises . unlink ( filePath ) ;
@@ -270,7 +299,7 @@ function migrateJobInfo(
270
299
if ( isUploadJob ( oldInfo ) ) {
271
300
info = {
272
301
...oldInfo ,
273
- uploadedParts : oldInfo . uploadedParts . map ( p => ( {
302
+ uploadedParts : ( oldInfo . uploadedParts || [ ] ) . map ( p => ( {
274
303
partNumber : p . PartNumber ,
275
304
etag : p . ETag ,
276
305
} ) ) ,
@@ -286,7 +315,7 @@ function migrateJobInfo(
286
315
info . status = [ "waiting" , "running" ] . includes ( oldInfo . status )
287
316
? "stopped"
288
317
: oldInfo . status ;
289
- info . storageClasses = oldInfo . storageClasses . map ( c => ( {
318
+ info . storageClasses = ( oldInfo . storageClasses || [ ] ) . map ( c => ( {
290
319
kodoName : c . kodoName ,
291
320
fileType : c . fileType ,
292
321
s3Name : c . s3Name ,
@@ -299,15 +328,19 @@ async function migrateTransferJobs(
299
328
filePath : string ,
300
329
type : "upload_prog" | "download_prog" ,
301
330
) {
302
- const basedir = path . join ( configPath , `profile_${ ak } ` , type ) ;
331
+ const basedir = path . join ( newConfigPath , `profile_${ ak } ` , type ) ;
303
332
const contentBuf = await fsPromises . readFile ( filePath ) ;
304
333
const oldContent = JSON . parse ( contentBuf . toString ( ) ) as Record < string , OldUploadJobPersistInfo | OldDownloadJobPersistInfo > ;
305
334
const dataStore = await getDataStoreOrCreate < UploadJobPersistInfo | DownloadJobPersistInfo > ( {
306
335
workingDirectory : basedir ,
307
336
} ) ;
308
337
for ( const [ oldId , oldInfo ] of Object . entries ( oldContent ) ) {
309
- const [ id , info ] = migrateJobInfo ( oldId , oldInfo ) ;
310
- await dataStore . set ( id , info ) ;
338
+ try {
339
+ const [ id , info ] = migrateJobInfo ( oldId , oldInfo ) ;
340
+ await dataStore . set ( id , info ) ;
341
+ } catch {
342
+ // skip error data
343
+ }
311
344
}
312
345
await dataStore . compact ( true ) ;
313
346
await dataStore . close ( ) ;
0 commit comments