file-system-access
Version:
File System Access API implementation (ponyfill) with pluggable storage adapters via IndexedDB, Cache API, in-memory etc.
41 lines • 2.55 kB
JavaScript
export const errors = {
INVALID: ['seeking position failed.', 'InvalidStateError'],
GONE: ['A requested file or directory could not be found at the time an operation was processed.', 'NotFoundError'],
MISMATCH: ['The path supplied exists, but was not an entry of requested type.', 'TypeMismatchError'],
MOD_ERR: ['The object can not be modified in this way.', 'InvalidModificationError'],
SYNTAX: (m) => [`Failed to execute 'write' on 'UnderlyingSinkBase': Invalid params passed. ${m}`, 'SyntaxError'],
ABORT: ['The operation was aborted', 'AbortError'],
SECURITY: ['It was determined that certain files are unsafe for access within a Web application, or that too many calls are being made on file resources.', 'SecurityError'],
DISALLOWED: ['The request is not allowed by the user agent or the platform in the current context.', 'NotAllowedError']
};
export const isChunkObject = (chunk) => {
return typeof chunk === 'object' && typeof chunk.type !== 'undefined';
};
export async function makeDirHandleFromFileList(fileList) {
var _a, _b, _c;
const { FolderHandle, FileHandle } = await import('./adapters/memory.js');
const { FileSystemDirectoryHandle } = await import('./FileSystemDirectoryHandle.js');
const rootName = (_b = (_a = fileList[0].webkitRelativePath) === null || _a === void 0 ? void 0 : _a.split('/', 1)[0]) !== null && _b !== void 0 ? _b : "";
const root = new FolderHandle(rootName, false);
for (let i = 0; i < fileList.length; i++) {
const file = fileList[i];
const path = ((_c = file.webkitRelativePath) === null || _c === void 0 ? void 0 : _c.length) ? file.webkitRelativePath.split('/') : ["", file.name];
// Remove the root folder part
path.shift();
const name = path.pop();
const dir = path.reduce((dir, path) => {
if (!dir._entries[path])
dir._entries[path] = new FolderHandle(path, false);
return dir._entries[path];
}, root);
dir._entries[name] = new FileHandle(file.name, file, false);
}
return new FileSystemDirectoryHandle(root);
}
export async function makeFileHandlesFromFileList(fileList) {
const { FileHandle } = await import('./adapters/memory.js');
const { FileSystemFileHandle } = await import('./FileSystemFileHandle.js');
const files = Array.from(fileList).map(file => new FileSystemFileHandle(new FileHandle(file.name, file, false)));
return files;
}
//# sourceMappingURL=util.js.map