feat(auth): implement user authentication system
This commit is contained in:
parent
4847ab793a
commit
25cea4fbe8
12051 changed files with 1462377 additions and 0 deletions
87
backend/node_modules/formidable/src/PersistentFile.js
generated
vendored
Normal file
87
backend/node_modules/formidable/src/PersistentFile.js
generated
vendored
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/* eslint-disable no-underscore-dangle */
|
||||
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const crypto = require('crypto');
|
||||
const { EventEmitter } = require('events');
|
||||
|
||||
class PersistentFile extends EventEmitter {
|
||||
constructor({ filepath, newFilename, originalFilename, mimetype, hashAlgorithm }) {
|
||||
super();
|
||||
|
||||
this.lastModifiedDate = null;
|
||||
Object.assign(this, { filepath, newFilename, originalFilename, mimetype, hashAlgorithm });
|
||||
|
||||
this.size = 0;
|
||||
this._writeStream = null;
|
||||
|
||||
if (typeof this.hashAlgorithm === 'string') {
|
||||
this.hash = crypto.createHash(this.hashAlgorithm);
|
||||
} else {
|
||||
this.hash = null;
|
||||
}
|
||||
}
|
||||
|
||||
open() {
|
||||
this._writeStream = new fs.WriteStream(this.filepath);
|
||||
this._writeStream.on('error', (err) => {
|
||||
this.emit('error', err);
|
||||
});
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
const json = {
|
||||
size: this.size,
|
||||
filepath: this.filepath,
|
||||
newFilename: this.newFilename,
|
||||
mimetype: this.mimetype,
|
||||
mtime: this.lastModifiedDate,
|
||||
length: this.length,
|
||||
originalFilename: this.originalFilename,
|
||||
};
|
||||
if (this.hash && this.hash !== '') {
|
||||
json.hash = this.hash;
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `PersistentFile: ${this.newFilename}, Original: ${this.originalFilename}, Path: ${this.filepath}`;
|
||||
}
|
||||
|
||||
write(buffer, cb) {
|
||||
if (this.hash) {
|
||||
this.hash.update(buffer);
|
||||
}
|
||||
|
||||
if (this._writeStream.closed) {
|
||||
cb();
|
||||
return;
|
||||
}
|
||||
|
||||
this._writeStream.write(buffer, () => {
|
||||
this.lastModifiedDate = new Date();
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this._writeStream.destroy();
|
||||
fs.unlink(this.filepath, () => {});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PersistentFile;
|
||||
Loading…
Add table
Add a link
Reference in a new issue