formflux
Version:
A package to upload files to a server and parsing multipart-formData requests
134 lines • 5.4 kB
JavaScript
import { createWriteStream, existsSync, unlinkSync } from "fs";
import FormfluxError from "./FormFluxError";
import setFileContentToReq from "./SetFileContentToReqFile";
import EventHandlers from "./EventHandlers";
import path from "path";
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.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("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 setFileContentToReq(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 setFileContentToReq(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 setFileContentToReq(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("Filename not found", 404);
if (this.storage == "disk" && !this.obj.filePath[count]) throw new FormfluxError("Destination path not found", 404);
if (this.storage == "disk") {
let writeFile = 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 (existsSync(this.obj.filePath[i])) unlinkSync(this.obj.filePath[i]);
}
throw new Error(err.message);
});
writeFile.on("close", () => {
EventHandlers.emitMessage("writeEnd", "write finish");
});
EventHandlers.on("parseError", () => {
this.obj.streams[0].destroy(new FormfluxError("Error in parsing form data.Invalid Format!", 400));
throw new FormfluxError("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.resolve(filepath, `${this.obj.modifiedFileName[len - 1]}`));
}
callBackFilter(error, bool) {
if (error) throw error;
return bool;
}
}
export default writeFileContent;
//# sourceMappingURL=WriteFileContent.js.map