CoreFileSystemFileHandle.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CoreFileSystemFileHandle = void 0;
  4. const CoreFileSystemHandle_1 = require("./CoreFileSystemHandle");
  5. const CoreFileSystemSyncAccessHandle_1 = require("./CoreFileSystemSyncAccessHandle");
  6. const util_1 = require("./util");
  7. const CoreFileSystemWritableFileStream_1 = require("./CoreFileSystemWritableFileStream");
  8. class CoreFileSystemFileHandle extends CoreFileSystemHandle_1.CoreFileSystemHandle {
  9. constructor(_core, __path, ctx = {}) {
  10. const fullCtx = (0, util_1.ctx)(ctx);
  11. super('file', (0, util_1.basename)(__path, fullCtx.separator), fullCtx);
  12. this._core = _core;
  13. this.__path = __path;
  14. this.ctx = fullCtx;
  15. }
  16. /**
  17. * Returns a {@link Promise} which resolves to a {@link File} object
  18. * representing the state on disk of the entry represented by the handle.
  19. *
  20. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/getFile
  21. */
  22. async getFile() {
  23. try {
  24. const path = this.__path;
  25. const link = this._core.getResolvedLinkOrThrow(path);
  26. const node = link.getNode();
  27. if (!node.isFile()) {
  28. throw new Error('Not a file');
  29. }
  30. // Get file stats for lastModified
  31. const lastModified = node.mtime ? node.mtime.getTime() : Date.now();
  32. // Read file content
  33. const buffer = node.getBuffer();
  34. const data = new Uint8Array(buffer);
  35. const file = new File([data], this.name, { lastModified });
  36. return file;
  37. }
  38. catch (error) {
  39. if (error instanceof DOMException)
  40. throw error;
  41. if (error && typeof error === 'object') {
  42. switch (error.code) {
  43. case "EACCES" /* ERROR_CODE.EACCES */:
  44. throw (0, util_1.newNotAllowedError)();
  45. }
  46. }
  47. throw error;
  48. }
  49. }
  50. /**
  51. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createSyncAccessHandle
  52. */
  53. get createSyncAccessHandle() {
  54. if (!this.ctx.syncHandleAllowed)
  55. return undefined;
  56. return async () => new CoreFileSystemSyncAccessHandle_1.CoreFileSystemSyncAccessHandle(this._core, this.__path, this.ctx);
  57. }
  58. /**
  59. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createWritable
  60. */
  61. async createWritable({ keepExistingData = false } = { keepExistingData: false }) {
  62. (0, util_1.assertCanWrite)(this.ctx.mode);
  63. return new CoreFileSystemWritableFileStream_1.CoreFileSystemWritableFileStream(this._core, this.__path, keepExistingData);
  64. }
  65. }
  66. exports.CoreFileSystemFileHandle = CoreFileSystemFileHandle;
  67. //# sourceMappingURL=CoreFileSystemFileHandle.js.map