This commit is contained in:
parent
7a9bf3199a
commit
c294e2e9ae
5702 changed files with 465039 additions and 34 deletions
80
node_modules/formidable/src/VolatileFile.js
generated
vendored
Normal file
80
node_modules/formidable/src/VolatileFile.js
generated
vendored
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/* eslint-disable no-underscore-dangle */
|
||||
|
||||
import { createHash } from 'node:crypto';
|
||||
import { EventEmitter } from 'node: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 = 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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default VolatileFile;
|
||||
Loading…
Add table
Add a link
Reference in a new issue