Skip to content

Commit

Permalink
feat: 🎸 implement .getFile() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 14, 2023
1 parent d51f853 commit 17015a3
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/node-to-fsa/NodeFileSystemFileHandle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NodeFileSystemHandle } from './NodeFileSystemHandle';
import { NodeFileSystemSyncAccessHandle } from './NodeFileSystemSyncAccessHandle';
import { basename, ctx as createCtx } from './util';
import { basename, ctx as createCtx, newNotAllowedError } from './util';
import type { NodeFsaContext, NodeFsaFs } from './types';

export class NodeFileSystemFileHandle extends NodeFileSystemHandle {
Expand All @@ -13,10 +13,30 @@ export class NodeFileSystemFileHandle extends NodeFileSystemHandle {
}

/**
* Returns a {@link Promise} which resolves to a {@link File} object
* representing the state on disk of the entry represented by the handle.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/getFile
*/
public async getFile(): Promise<File> {
throw new Error('Not implemented');
try {
const path = this.__path;
const promises = this.fs.promises;
const stats = await promises.stat(path);
const data = await promises.readFile(path);
const file = new File([data], this.name, {lastModified: stats.mtime.getTime()});
return file;
} catch (error) {
if (error instanceof DOMException) throw error;
if (error && typeof error === 'object') {
switch (error.code) {
case 'EPERM':
case 'EACCES':
throw newNotAllowedError();
}
}
throw error;
}
}

/**
Expand Down

0 comments on commit 17015a3

Please sign in to comment.