UNPKG

formflux

Version:

A package to upload files to a server and parsing multipart-formData requests

139 lines (138 loc) 5.8 kB
"use strict"; exports.__esModule = true; exports.default = void 0; var _fs = require("fs"); var _FormFluxError = _interopRequireDefault(require("./FormFluxError")); var _SetFileContentToReqFile = _interopRequireDefault(require("./SetFileContentToReqFile")); var _EventHandlers = _interopRequireDefault(require("./EventHandlers")); var _path = _interopRequireDefault(require("path")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } class writeFileContent { constructor(req, obj, options, forReason, storage) { this.obj = void 0; this.options = void 0; this.req = void 0; this.for = void 0; this.storage = void 0; this.obj = obj; this.options = options; this.req = req; this.for = forReason; this.storage = storage; } writeContent() { let flag = 0; if (this.obj.content.length > 0) { for (let i = 0; i < this.obj.metaData.length; i++) { var _this$obj$metaData$i, _this$obj$content$i, _this$obj$fileName$i, _this$obj$fieldNameFi; if (((_this$obj$metaData$i = this.obj.metaData[i]) == null ? void 0 : _this$obj$metaData$i.length) != 0 && ((_this$obj$content$i = this.obj.content[i]) == null ? void 0 : _this$obj$content$i.length) != 0, ((_this$obj$fileName$i = this.obj.fileName[i]) == null ? void 0 : _this$obj$fileName$i.length) != 0 && ((_this$obj$fieldNameFi = this.obj.fieldNameFile[i]) == null ? void 0 : _this$obj$fieldNameFi.length) != 0) { this.singleFile(i, this.obj.metaData[i], this.obj.content[i], this.obj.filesize[i], this.obj.fieldNameFile[i]); flag++; } } } if (flag == 0 || this.storage == "memory") { _EventHandlers.default.emitMessage("writeEnd", "write finish"); } } singleFile(count, metaData, content, filesize, fieldname) { let header = metaData.split(`filename="`)[1]; let fileName = header.substring(0, header.indexOf(`"`)); let access = true; if (this.options.fileFilter) this.options.fileFilter(this.req, { originalname: fileName, mimetype: metaData.split("Content-Type: ")[1], filesize, fieldname }, (error, bool) => { access = this.callBackFilter(error, bool); }); if (!access) throw new _FormFluxError.default("Invalid file", 400); this.options.filename(this.req, { originalname: fileName, mimetype: metaData.split("Content-Type: ")[1], filesize, fieldname }, (error, fileName) => { this.callBackFilename(error, fileName); }); this.obj.fileName.push(fileName); if (this.storage == "disk") { this.options["destination"](this.req, { originalname: fileName, mimetype: metaData.split("Content-Type: ")[1], filesize }, (error, filepath) => { this.callBackfilepath(error, filepath); }); } if (this.for == "any") { new _SetFileContentToReqFile.default(this.req, this.obj, "any", this.storage).setFileNames({ originalname: fileName, mimetype: metaData.split("Content-Type: ")[1], filepath: this.obj.filePath[count], filesize: Buffer.from(content).length, filename: `${this.obj.modifiedFileName[count]}`, fieldname, buffer: content }, null); } else if (this.for == "fields") { new _SetFileContentToReqFile.default(this.req, this.obj, "fields", this.storage).setFileNames({ originalname: fileName, mimetype: metaData.split("Content-Type: ")[1], filepath: this.obj.filePath[count], filesize: Buffer.from(content).length, filename: `${this.obj.modifiedFileName[count]}`, fieldname, buffer: content }, fieldname); } else if (this.for == "single") { new _SetFileContentToReqFile.default(this.req, this.obj, "single", this.storage).setFileNames({ originalname: fileName, mimetype: metaData.split("Content-Type: ")[1], filepath: this.obj.filePath[count], filesize: Buffer.from(content).length, filename: `${this.obj.modifiedFileName[count]}`, fieldname, buffer: content }, null); } if (!this.obj.modifiedFileName[count]) throw new _FormFluxError.default("Filename not found", 404); if (this.storage == "disk" && !this.obj.filePath[count]) throw new _FormFluxError.default("Destination path not found", 404); if (this.storage == "disk") { let writeFile = (0, _fs.createWriteStream)(this.obj.filePath[count]); this.obj.streams.push(writeFile); writeFile.write(content); writeFile.end(); writeFile.on("error", err => { for (let i = 0; i < this.obj.streams.length; i++) { this.obj.streams[i].destroy(err); if ((0, _fs.existsSync)(this.obj.filePath[i])) (0, _fs.unlinkSync)(this.obj.filePath[i]); } throw new Error(err.message); }); writeFile.on("close", () => { _EventHandlers.default.emitMessage("writeEnd", "write finish"); }); _EventHandlers.default.on("parseError", () => { this.obj.streams[0].destroy(new _FormFluxError.default("Error in parsing form data.Invalid Format!", 400)); throw new _FormFluxError.default("Error in parsing form data.Invalid Format!", 400); }); } } callBackFilename(error, fileName) { if (error) throw error; this.obj.modifiedFileName.push(fileName); } callBackfilepath(error, filepath) { if (error) throw error; let len = this.obj.modifiedFileName.length; this.obj.filePath.push(_path.default.resolve(filepath, `${this.obj.modifiedFileName[len - 1]}`)); } callBackFilter(error, bool) { if (error) throw error; return bool; } } var _default = exports.default = writeFileContent; //# sourceMappingURL=WriteFileContent.js.map