-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,49 @@ | ||
/** | ||
* Write file to OPFS recursively | ||
* @param filename Full path of the file | ||
* @param path Full path of the file | ||
* @param content File content | ||
*/ | ||
export async function writeFileOpfsRecursive(filename: string, content: Uint8Array) { | ||
let folder = await self.navigator.storage.getDirectory(); | ||
const parts = filename.split('/').filter(Boolean); | ||
for (let i = 0; i < parts.length - 1; i++) { | ||
folder = await folder.getDirectoryHandle(parts[i], { create: true }); | ||
} | ||
export async function writeFile( | ||
path: string, | ||
content: Uint8Array, | ||
opts?: { | ||
create?: boolean; | ||
root?: FileSystemDirectoryHandle; | ||
}, | ||
) { | ||
const parts = path.split('/').filter(Boolean); | ||
const folder = parts.slice(0, -1).join('/'); | ||
const folderHandle = await getDirectoryHandle(folder, { | ||
create: opts?.create, | ||
root: opts?.root, | ||
}); | ||
|
||
const basename = parts[parts.length - 1]; | ||
if (!basename) return; | ||
const file = await folder.getFileHandle(basename, { create: true }); | ||
const file = await folderHandle.getFileHandle(basename, { create: opts?.create }); | ||
const writable = await file.createWritable(); | ||
await writable.write(content); | ||
await writable.close(); | ||
} | ||
|
||
/** | ||
* Get directory handle from OPFS recursively | ||
* @param path Path to the directory | ||
* @returns Directory handle | ||
*/ | ||
export async function getDirectoryHandle( | ||
path: string, | ||
opts?: { | ||
create?: boolean; | ||
root?: FileSystemDirectoryHandle; | ||
}, | ||
): Promise<FileSystemDirectoryHandle> { | ||
let folder = opts?.root ?? (await self.navigator.storage.getDirectory()); | ||
const parts = path.split('/').filter(Boolean); | ||
for (const part of parts) { | ||
folder = await folder.getDirectoryHandle(part, { | ||
create: opts?.create ?? false, | ||
}); | ||
} | ||
return folder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters