@nasriya/hypercloud
Version:
Nasriya HyperCloud is a lightweight Node.js HTTP2 framework.
52 lines (51 loc) • 1.55 kB
JavaScript
import fs from 'fs';
import path from '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.join(options.tempPath, `${Date.now()}_temp_${options.fileName}`);
this.#_stream = fs.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.statSync(this.path).size;
resolve();
});
});
}
}
export default UploadedStorageFile;