univ-fs-webnfs
Version:
The universal Filesystem for Web File System Access API (Native File System API)
99 lines • 2.9 kB
JavaScript
import { AbstractFileSystem, NotFoundError, NotSupportedError, SyntaxError, TypeMismatchError, } from "univ-fs";
import { WnfsDirectory } from "./WnfsDirectory";
import { WnfsFile } from "./WnfsFile";
export class WnfsFileSystem extends AbstractFileSystem {
constructor(options) {
super("", options);
}
_doGetDirectory(path) {
return new WnfsDirectory(this, path);
}
_doGetFile(path) {
return new WnfsFile(this, path);
}
async _doGetURL(path, isDirectory, options) {
options = { method: "GET", ...options };
if (options.method !== "GET") {
throw this._createError({
...NotSupportedError,
path,
e: { message: `"${options.method}" is not supported` },
});
}
if (isDirectory) {
throw this._createError({
...TypeMismatchError,
path,
e: { message: `"${path}" is not a directory` },
});
}
const file = this.getFile(path);
const blob = await file.read("blob");
return URL.createObjectURL(blob);
}
async _doHead(path) {
if (path === "/") {
return {};
}
const { parent, name } = await this._getParent(path);
try {
const fileHandle = await parent.getFileHandle(name);
const file = await fileHandle.getFile();
return {
modified: file.lastModified,
size: file.size,
};
}
catch (e) {
if (e.code === NotFoundError.code) {
throw e;
}
}
await parent.getDirectoryHandle(name);
return {};
}
_doPatch(path) {
throw this._createError({
...NotSupportedError,
path,
e: { message: "patch is not supported" },
});
}
async _getParent(path) {
const parts = path.split("/").filter((part) => !!part);
if (parts.length === 0) {
throw this._createError({
...SyntaxError,
path,
});
}
let parent = await this._getRoot();
const end = parts.length - 1;
for (let i = 0; i < end; i++) {
const part = parts[i];
parent = await parent.getDirectoryHandle(part);
}
return { parent, name: parts[end] };
}
async _getRoot() {
if (this.root) {
return this.root;
}
const root = await window.showDirectoryPicker();
this.root = root;
return root;
}
canPatchAccessed() {
return false;
}
canPatchCreated() {
return false;
}
canPatchModified() {
return false;
}
supportDirectory() {
return true;
}
}
//# sourceMappingURL=WnfsFileSystem.js.map