1.1.19 • Published 12 months ago

@cimo/form-data_parser v1.1.19

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Form-data_parser

Parser for the form-data request. Light, fast and secure. Writed with native Typescript code and no dependencies are used.

Pack

  1. npm run pack
  2. Copy the file "package_name-x.x.x.tgz" in the project root folder.
  3. In the "package.json" file insert: "@cimo/package_name": "file:package_name-x.x.x.tgz"

Publish

  1. npm run build
  2. npm login --auth-type=legacy
  3. npm publish --auth-type=legacy --access public

Installation

  1. Link for npm package -> https://www.npmjs.com/package/@cimo/form-data_parser

Server - Example with "NodeJs Express"

  • Server.ts
...

import * as ControllerTest from "../Controller/Test";

...

server.listen(8080, () => {

    ...

    ControllerTest.api(app);
});

...
  • ControllerTest.ts
...

import * as ControllerUpload from "../Controller/Upload";

export const api = (app: Express.Express): void => {
    app.post("/upload", (request: Express.Request, response: Express.Response) => {
        void (async () => {
            await ControllerUpload.execute(request, false)
                .then((resultControllerUploadList) => {
                    let fileName = "";

                    for (const resultControllerUpload of resultControllerUploadList) {
                        if (resultControllerUpload.name === "file" && resultControllerUpload.filename) {
                            fileName = resultControllerUpload.filename;

                            break;
                        }
                    }

                    response.status(200).send({ fileName: `${fileName}` });
                })
                .catch((error: Error) => {
                    response.status(200).send({ Error: "Upload failed." });
                });
        })();
    });
};

...
  • ControllerUpload.ts
...

import { Cfdp, CfdpModel } from "@cimo/form-data_parser";

export const execute = (request: Express.Request, isFileExists: boolean): Promise<CfdpModel.Iinput[]> => {
    return new Promise((resolve, reject) => {
        const chunkList: Buffer[] = [];

        request.on("data", (data: Buffer) => {
            chunkList.push(data);
        });

        request.on("end", () => {
            const buffer = Buffer.concat(chunkList);
            const formDataList = Cfdp.readInput(buffer, request.headers["content-type"]);

            for (const formData of formDataList) {
                if (formData.name === "file" && formData.filename && formData.buffer) {
                    const input = `/home/root/file/input/${formData.filename}`;

                    if (isFileExists && Fs.existsSync(input)) {
                        reject("File exists.");
                    } else {
                        // Write the file "formData.buffer"

                        resolve(formDataList);
                    }

                    break;
                }
            }
        });

        request.on("error", (error: Error) => {
            reject(error);
        });
    });
};

...
1.1.19

12 months ago

1.1.18

12 months ago

1.1.17

12 months ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago