11import { BaseAdaptor , BaseAdaptorOptions } from '../../base' ;
22import { FileStat , PathMap } from '../../definitions' ;
33
4- const INDEXDB_VERSION = 1 ;
5- // const INDEXDB_DEFAULT_DB = 'files-multitool';
6- const INDEXDB_DEFAULT_STORE = 'files' ;
4+ const INDEXEDDB_VERSION = 1 ;
5+ // const INDEXEDDB_DEFAULT_DB = 'files-multitool';
6+ const INDEXEDDB_DEFAULT_STORE = 'files' ;
77
8- export interface IndexDBAdaptorOptions extends BaseAdaptorOptions {
8+ export interface IndexedDBAdaptorOptions extends BaseAdaptorOptions {
99 db ?: string ;
1010}
1111
12- interface IndexDBFileStat extends FileStat {
12+ interface IndexedDBFileStat extends FileStat {
1313 content ?: string ;
1414 deletedAt ?: Date ;
1515}
1616
17- export class IndexDBAdaptor extends BaseAdaptor {
18- declare options : IndexDBAdaptorOptions ;
17+ const joinPath = ( path : string , fileName : string ) => {
18+ return [
19+ path . replace ( / \/ $ / , '' ) ,
20+ fileName . replace ( / ^ \/ / , '' ) ,
21+ ] . filter ( v => v ) . join ( '/' ) ;
22+ }
23+
24+ export class IndexedDBAdaptor extends BaseAdaptor {
25+ declare options : IndexedDBAdaptorOptions ;
1926 db = null as IDBDatabase | null ;
2027 indexedDB = null as IDBFactory | null ;
2128
@@ -30,7 +37,7 @@ export class IndexDBAdaptor extends BaseAdaptor {
3037 async init ( ) : Promise < void > {
3138 this . indexedDB = window . indexedDB ;
3239 this . db = await new Promise ( ( resolve , reject ) => {
33- const request = this . indexedDB ! . open ( this . ref , INDEXDB_VERSION ) ;
40+ const request = this . indexedDB ! . open ( this . ref , INDEXEDDB_VERSION ) ;
3441 request . onerror = ( event ) => {
3542 reject ( event ) ;
3643 } ;
@@ -39,7 +46,7 @@ export class IndexDBAdaptor extends BaseAdaptor {
3946 } ;
4047 request . onupgradeneeded = ( event ) => {
4148 const db = ( event . target as any ) . result as IDBDatabase ;
42- const store = db . createObjectStore ( INDEXDB_DEFAULT_STORE , { keyPath : 'path' } ) ;
49+ const store = db . createObjectStore ( INDEXEDDB_DEFAULT_STORE , { keyPath : 'path' } ) ;
4350
4451 store . createIndex ( 'path' , 'path' , { unique : true } ) ;
4552 store . createIndex ( 'isDirectory' , 'isDirectory' , { unique : false } ) ;
@@ -55,24 +62,24 @@ export class IndexDBAdaptor extends BaseAdaptor {
5562 } ) as IDBDatabase ;
5663
5764 this . db . addEventListener ( 'close' , ( ) => {
65+ super . destroy ( ) ;
5866 this . db = null ;
5967 } ) ;
68+
69+ return super . init ( ) ;
6070 }
6171
6272 destroy ( ) : Promise < void > {
63- return new Promise ( ( resolve ) => {
64- if ( this . db ) {
65- this . db . addEventListener ( 'close' , ( ) => {
66- resolve ( ) ;
67- } ) ;
68- this . db . close ( ) ;
69- }
70- } ) ;
73+ if ( this . db ) {
74+ this . db . close ( ) ;
75+ this . db = null ;
76+ }
77+ return super . destroy ( ) ;
7178 }
7279
7380 // internal helper methods
7481
75- _convertFileStat ( stat : IndexDBFileStat ) : FileStat {
82+ _convertFileStat ( stat : IndexedDBFileStat ) : FileStat {
7683 const clonedStat = { ...stat } ;
7784 delete clonedStat . content ;
7885 delete clonedStat . deletedAt ;
@@ -84,19 +91,19 @@ export class IndexDBAdaptor extends BaseAdaptor {
8491 throw new Error ( 'Adaptor is not initialized.' ) ;
8592 }
8693 return this . db !
87- . transaction ( INDEXDB_DEFAULT_STORE , 'readwrite' )
88- . objectStore ( INDEXDB_DEFAULT_STORE ) ;
94+ . transaction ( INDEXEDDB_DEFAULT_STORE , 'readwrite' )
95+ . objectStore ( INDEXEDDB_DEFAULT_STORE ) ;
8996 }
9097
91- _getItem ( path : string ) : Promise < IndexDBFileStat | null > {
98+ _getItem ( path : string ) : Promise < IndexedDBFileStat | null > {
9299 const store = this . _getStore ( ) ;
93100 const request = store . get ( path ) ;
94101 return new Promise ( ( resolve , reject ) => {
95102 request . onerror = ( event ) => {
96103 reject ( request . error ) ;
97104 } ;
98105 request . onsuccess = ( event ) => {
99- resolve ( ( request . result || null ) as IndexDBFileStat | null ) ;
106+ resolve ( ( request . result || null ) as IndexedDBFileStat | null ) ;
100107 } ;
101108 } ) ;
102109 }
@@ -114,7 +121,7 @@ export class IndexDBAdaptor extends BaseAdaptor {
114121 } ) ;
115122 }
116123
117- async _putItem ( stat : IndexDBFileStat ) : Promise < IndexDBFileStat > {
124+ async _putItem ( stat : IndexedDBFileStat ) : Promise < IndexedDBFileStat > {
118125 let writeStat = stat ;
119126 const existing = await this . _getItem ( stat . path ) ;
120127 if ( existing && existing . deletedAt ) {
@@ -137,7 +144,7 @@ export class IndexDBAdaptor extends BaseAdaptor {
137144 } ) ;
138145 }
139146
140- _listItems ( path : string ) : Promise < IndexDBFileStat [ ] > {
147+ _listItems ( path : string ) : Promise < IndexedDBFileStat [ ] > {
141148 const store = this . _getStore ( ) ;
142149 const index = store . index ( 'parentPath' ) ;
143150 const request = index . getAll ( path ) ;
@@ -146,7 +153,7 @@ export class IndexDBAdaptor extends BaseAdaptor {
146153 reject ( request . error ) ;
147154 } ;
148155 request . onsuccess = ( event ) => {
149- resolve ( ( request . result || [ ] ) as IndexDBFileStat [ ] ) ;
156+ resolve ( ( request . result || [ ] ) as IndexedDBFileStat [ ] ) ;
150157 } ;
151158 } ) ;
152159 }
@@ -210,7 +217,6 @@ export class IndexDBAdaptor extends BaseAdaptor {
210217 for ( let i = 0 ; i < parts . length ; i ++ ) {
211218 const partPath = parts . slice ( 0 , i + 1 ) . join ( '/' ) ;
212219 const stat = await this . _getItem ( partPath ) ;
213- console . log ( 'mkdir' , path , stat ) ;
214220 if ( ! stat || stat . deletedAt ) {
215221 await this . _putItem ( {
216222 path : partPath ,
@@ -234,7 +240,7 @@ export class IndexDBAdaptor extends BaseAdaptor {
234240 const items = await this . _listItems ( path ) ;
235241 for ( const item of items ) {
236242 if ( item . deletedAt ) continue ;
237- const itemPath = ` ${ path } / ${ item . path . split ( '/' ) . pop ( ) } ` ;
243+ const itemPath = joinPath ( path , item . path . split ( '/' ) . pop ( ) as string ) ;
238244 if ( item . isDirectory ) {
239245 await this . rmdir ( itemPath ) ;
240246 } else {
@@ -255,7 +261,7 @@ export class IndexDBAdaptor extends BaseAdaptor {
255261 const fileName = newFileName || stat . path . split ( '/' ) . pop ( ) ! ;
256262 await this . _putItem ( {
257263 ...stat ,
258- path : [ targetPath , fileName ] . join ( '/' ) ,
264+ path : joinPath ( targetPath , fileName ) ,
259265 parentPath : targetPath ,
260266 } ) ;
261267 }
@@ -269,9 +275,10 @@ export class IndexDBAdaptor extends BaseAdaptor {
269275 await this . mkdir ( newPath ) ;
270276 for ( const item of items ) {
271277 if ( item . deletedAt ) continue ;
272- const itemPath = ` ${ path } / ${ item . path . split ( '/' ) . pop ( ) } ` ;
278+ const itemPath = joinPath ( path , item . path . split ( '/' ) . pop ( ) as string ) ;
273279 if ( item . isDirectory && recursive ) {
274- await this . _copyDirectory ( itemPath , `${ newPath } /${ item . path . split ( '/' ) . pop ( ) } ` , recursive ) ;
280+ const newItemPath = joinPath ( newPath , item . path . split ( '/' ) . pop ( ) as string ) ;
281+ await this . _copyDirectory ( itemPath , newItemPath , recursive ) ;
275282 } else if ( item . isFile ) {
276283 await this . _copyFile ( itemPath , newPath ) ;
277284 }
0 commit comments