fileFromPath.js 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __exportStar = (this && this.__exportStar) || function(m, exports) {
  10. for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
  11. };
  12. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  13. if (kind === "m") throw new TypeError("Private method is not writable");
  14. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  15. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  16. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  17. };
  18. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  19. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  20. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  21. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  22. };
  23. var __importDefault = (this && this.__importDefault) || function (mod) {
  24. return (mod && mod.__esModule) ? mod : { "default": mod };
  25. };
  26. var _FileFromPath_path, _FileFromPath_start;
  27. Object.defineProperty(exports, "__esModule", { value: true });
  28. exports.fileFromPath = exports.fileFromPathSync = void 0;
  29. const fs_1 = require("fs");
  30. const path_1 = require("path");
  31. const node_domexception_1 = __importDefault(require("node-domexception"));
  32. const File_1 = require("./File");
  33. const isPlainObject_1 = __importDefault(require("./isPlainObject"));
  34. __exportStar(require("./isFile"), exports);
  35. const MESSAGE = "The requested file could not be read, "
  36. + "typically due to permission problems that have occurred after a reference "
  37. + "to a file was acquired.";
  38. class FileFromPath {
  39. constructor(input) {
  40. _FileFromPath_path.set(this, void 0);
  41. _FileFromPath_start.set(this, void 0);
  42. __classPrivateFieldSet(this, _FileFromPath_path, input.path, "f");
  43. __classPrivateFieldSet(this, _FileFromPath_start, input.start || 0, "f");
  44. this.name = (0, path_1.basename)(__classPrivateFieldGet(this, _FileFromPath_path, "f"));
  45. this.size = input.size;
  46. this.lastModified = input.lastModified;
  47. }
  48. slice(start, end) {
  49. return new FileFromPath({
  50. path: __classPrivateFieldGet(this, _FileFromPath_path, "f"),
  51. lastModified: this.lastModified,
  52. size: end - start,
  53. start
  54. });
  55. }
  56. async *stream() {
  57. const { mtimeMs } = await fs_1.promises.stat(__classPrivateFieldGet(this, _FileFromPath_path, "f"));
  58. if (mtimeMs > this.lastModified) {
  59. throw new node_domexception_1.default(MESSAGE, "NotReadableError");
  60. }
  61. if (this.size) {
  62. yield* (0, fs_1.createReadStream)(__classPrivateFieldGet(this, _FileFromPath_path, "f"), {
  63. start: __classPrivateFieldGet(this, _FileFromPath_start, "f"),
  64. end: __classPrivateFieldGet(this, _FileFromPath_start, "f") + this.size - 1
  65. });
  66. }
  67. }
  68. get [(_FileFromPath_path = new WeakMap(), _FileFromPath_start = new WeakMap(), Symbol.toStringTag)]() {
  69. return "File";
  70. }
  71. }
  72. function createFileFromPath(path, { mtimeMs, size }, filenameOrOptions, options = {}) {
  73. let filename;
  74. if ((0, isPlainObject_1.default)(filenameOrOptions)) {
  75. [options, filename] = [filenameOrOptions, undefined];
  76. }
  77. else {
  78. filename = filenameOrOptions;
  79. }
  80. const file = new FileFromPath({ path, size, lastModified: mtimeMs });
  81. if (!filename) {
  82. filename = file.name;
  83. }
  84. return new File_1.File([file], filename, {
  85. ...options, lastModified: file.lastModified
  86. });
  87. }
  88. function fileFromPathSync(path, filenameOrOptions, options = {}) {
  89. const stats = (0, fs_1.statSync)(path);
  90. return createFileFromPath(path, stats, filenameOrOptions, options);
  91. }
  92. exports.fileFromPathSync = fileFromPathSync;
  93. async function fileFromPath(path, filenameOrOptions, options) {
  94. const stats = await fs_1.promises.stat(path);
  95. return createFileFromPath(path, stats, filenameOrOptions, options);
  96. }
  97. exports.fileFromPath = fileFromPath;