diff options
author | Minteck <contact@minteck.org> | 2022-08-21 17:31:56 +0200 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-08-21 17:31:56 +0200 |
commit | a2df9a69dcc14cb70118cda2ded499055e7ee358 (patch) | |
tree | 6dd283e4e9452d38bce81ddaaae49b5335755842 /together/node_modules/formidable/src/VolatileFile.js | |
parent | 84dd0735820b16b60f600284d35183d76547a71f (diff) | |
download | pluralconnect-a2df9a69dcc14cb70118cda2ded499055e7ee358.tar.gz pluralconnect-a2df9a69dcc14cb70118cda2ded499055e7ee358.tar.bz2 pluralconnect-a2df9a69dcc14cb70118cda2ded499055e7ee358.zip |
m. update
Diffstat (limited to 'together/node_modules/formidable/src/VolatileFile.js')
-rw-r--r-- | together/node_modules/formidable/src/VolatileFile.js | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/together/node_modules/formidable/src/VolatileFile.js b/together/node_modules/formidable/src/VolatileFile.js new file mode 100644 index 0000000..01af428 --- /dev/null +++ b/together/node_modules/formidable/src/VolatileFile.js @@ -0,0 +1,82 @@ +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +const crypto = require('crypto'); +const { EventEmitter } = require('events'); + +class VolatileFile extends EventEmitter { + constructor({ filepath, newFilename, originalFilename, mimetype, hashAlgorithm, createFileWriteStream }) { + super(); + + this.lastModifiedDate = null; + Object.assign(this, { filepath, newFilename, originalFilename, mimetype, hashAlgorithm, createFileWriteStream }); + + this.size = 0; + this._writeStream = null; + + if (typeof this.hashAlgorithm === 'string') { + this.hash = crypto.createHash(this.hashAlgorithm); + } else { + this.hash = null; + } + } + + open() { + this._writeStream = this.createFileWriteStream(this); + this._writeStream.on('error', (err) => { + this.emit('error', err); + }); + } + + destroy() { + this._writeStream.destroy(); + } + + toJSON() { + const json = { + size: this.size, + newFilename: this.newFilename, + length: this.length, + originalFilename: this.originalFilename, + mimetype: this.mimetype, + }; + if (this.hash && this.hash !== '') { + json.hash = this.hash; + } + return json; + } + + toString() { + return `VolatileFile: ${this.originalFilename}`; + } + + write(buffer, cb) { + if (this.hash) { + this.hash.update(buffer); + } + + if (this._writeStream.closed || this._writeStream.destroyed) { + cb(); + return; + } + + this._writeStream.write(buffer, () => { + this.size += buffer.length; + this.emit('progress', this.size); + cb(); + }); + } + + end(cb) { + if (this.hash) { + this.hash = this.hash.digest('hex'); + } + this._writeStream.end(() => { + this.emit('end'); + cb(); + }); + } +} + +module.exports = VolatileFile; |