UNPKG

@nasriya/hypercloud

Version:

Nasriya HyperCloud is a lightweight Node.js HTTP2 framework.

57 lines (56 loc) 1.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); class UploadedStorageFile { #_closed = false; #_stream; #_data = { fieldName: '', fileName: '', mime: '', path: '', size: 0 }; constructor(options) { this.#_data.fieldName = options.fieldName; this.#_data.fileName = options.fileName; this.#_data.mime = options.mime; this.#_data.path = path_1.default.join(options.tempPath, `${Date.now()}_temp_${options.fileName}`); this.#_stream = fs_1.default.createWriteStream(this.path, { encoding: 'binary' }); } get fieldName() { return this.#_data.fieldName; } get fileName() { return this.#_data.fileName; } get mime() { return this.#_data.mime; } get path() { return this.#_data.path; } get size() { return this.#_data.size; } get closed() { return this.#_closed; } write(chunk) { return new Promise((resolve, reject) => { this.#_stream.write(chunk, (err) => { if (err) { reject(err); } else { resolve(); } }); }); } finish() { return new Promise((resolve, reject) => { if (this.#_closed) { return resolve(); } this.#_stream.end(() => { this.#_closed = true; this.#_data.size = fs_1.default.statSync(this.path).size; resolve(); }); }); } } exports.default = UploadedStorageFile;