Skip to content

Commit c5f6e8c

Browse files
committed
fix fsa adaptor to actually work
1 parent e0c6623 commit c5f6e8c

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/adaptors/fsa-api/index.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import { BaseAdaptor, BaseAdaptorOptions } from '../../base';
88
import { FileStat, PathMap } from '../../definitions';
99

10-
const INDEXEDDB_VERSION = 0;
10+
const INDEXEDDB_VERSION = 1;
1111
const INDEXEDDB_DEFAULT_DB = 'files-multitool';
12-
const INDEXEDDB_DEFAULT_STORE = '~~fsa-api~~';
12+
const INDEXEDDB_DEFAULT_STORE = 'files-fsa-api';
1313

1414
export interface FSAAdaptorOptions extends BaseAdaptorOptions {
1515
db?: string;
@@ -138,19 +138,17 @@ export class FSAAdaptor extends BaseAdaptor {
138138
};
139139
request.onupgradeneeded = (event) => {
140140
const db = (event.target as any).result as IDBDatabase;
141-
const store = db.createObjectStore(this.options.store || INDEXEDDB_DEFAULT_STORE, { keyPath: 'path' });
142-
141+
const store = db.createObjectStore(this.options.store || INDEXEDDB_DEFAULT_STORE, { keyPath: 'ref' });
143142
store.createIndex('ref', 'ref', { unique: true });
144-
145-
resolve(db);
143+
store.createIndex('handle', 'handle', { unique: false });
146144
};
147145
}) as IDBDatabase;
148146

149-
const store = db
147+
const getStore = () => db
150148
.transaction(this.options.store || INDEXEDDB_DEFAULT_STORE, 'readwrite')
151149
.objectStore(this.options.store || INDEXEDDB_DEFAULT_STORE);
152150

153-
let handle = await this._getHandleFromDB(store);
151+
let handle = await this._getHandleFromDB(getStore());
154152

155153
const startIn = this.options.startIn || 'documents';
156154
if (!handle) {
@@ -159,18 +157,18 @@ export class FSAAdaptor extends BaseAdaptor {
159157
mode: 'readwrite',
160158
startIn,
161159
});
162-
await this._saveHandleToDB(store, handle);
160+
await this._saveHandleToDB(getStore(), handle);
163161
} else {
164162
try {
165163
await this._verifyHandle(handle);
166164
} catch (err) {
167-
await this._dropHandleFromDB(store);
165+
await this._dropHandleFromDB(getStore());
168166
handle = await window.showDirectoryPicker({
169167
id: this.ref,
170168
mode: 'readwrite',
171169
startIn,
172170
});
173-
await this._saveHandleToDB(store, handle);
171+
await this._saveHandleToDB(getStore(), handle);
174172
}
175173
}
176174

@@ -189,13 +187,15 @@ export class FSAAdaptor extends BaseAdaptor {
189187
async _getParentHandle(path: string, create?: boolean): Promise<FileSystemDirectoryHandle> {
190188
if (!this.isInitialized) throw new Error('Adaptor not initialized');
191189
if (path === '') return this.root!;
192-
if (this.pathCache[path]) return this.pathCache[path] as FileSystemDirectoryHandle;
193190
const parts = path.split('/');
191+
parts.pop();
192+
const parentPath = parts.join('/');
193+
if (this.pathCache[parentPath]) return this.pathCache[parentPath] as FileSystemDirectoryHandle;
194194
let handle = this.root!;
195-
for (let i = 0; i < parts.length - 1; i++) {
195+
for (let i = 0; i < parts.length; i++) {
196196
handle = await handle.getDirectoryHandle(parts[i], { create });
197197
}
198-
this.pathCache[path] = handle;
198+
this.pathCache[parentPath] = handle;
199199
return handle;
200200
}
201201

@@ -278,6 +278,7 @@ export class FSAAdaptor extends BaseAdaptor {
278278
await writable.write(data as ArrayBuffer);
279279
await writable.close();
280280
this._purgeCache(file);
281+
this._purgeCache(path);
281282
}
282283

283284
async deleteFile(path: string): Promise<void> {
@@ -301,16 +302,18 @@ export class FSAAdaptor extends BaseAdaptor {
301302

302303
const map: PathMap = {};
303304
for await (const [key, value] of handle.entries()) {
304-
const fullPath = [path, key].join('/');
305+
const fullPath = path !== '' ? [path, key].join('/') : key;
305306
map[fullPath] = await this._getHandleStat(fullPath, value) ;
306307
}
307308

308309
return map;
309310
}
310311

311312
async mkdir(path: string): Promise<void> {
312-
const parent = await this._getParentHandle(path);
313+
if (path === '') return;
313314
const dirName = path.split('/').pop()!;
315+
if (dirName === '') return;
316+
const parent = await this._getParentHandle(path);
314317
await parent.getDirectoryHandle(dirName, { create: true });
315318
}
316319

0 commit comments

Comments
 (0)