node-formidable/formidable

Buffer usage

R0N1n-dev opened this issue · 2 comments

Support plan

  • Which support plan is this issue covered by? (Community, Sponsor, Enterprise):Community
  • Currently blocking your project/work? (yes/no):Yes
  • Affecting a production system? (yes/no):Yes

Context

  • Node.js version: v18.16.0
  • Release Line of Formidable (Legacy, Current, Next):Current
  • Formidable exact version: 3.5.1
  • Environment (node, browser, native, OS): v18.16.0, Brave, Linux, PopOS
  • Used with (popular names of modules): @prisma/client, @cosmicjs/sdk

What are you trying to achieve or the steps to reproduce?

import cosmic from "~~/lib/cosmic.mjs";
import formidable from "formidable";

export default defineEventHandler(async (event) => {
  if ((event.node.req.method = "POST")) {
    const form = formidable({});
    await form.parse(event.node.req, (err, fields, files) => {
      if (err) {
        console.log(err);
        return;
      }
      console.table(files?.file[0]);
      const media = /*files?.file[0]*/ {
        originalname: files?.file[0].originalFilename,
        buffer: files?.file[0].buffer,
      };
      //console.log(media);
      return { media };
    });
    /*console.log(media);
    try {
      const data = await cosmic.media.insertOne({
        media: media,
        folder: "nuxt-test",
      });
      return { Success: true, data };
    } catch (error) {
      console.log(error.message);
    }*/
  }
});

What was the result you got?

None. Coz I cannot get a buffer

What result did you expect?

A buffer Ican use to upload the image coz the cosmic API need a buffer

Search for similar issues and examples

@R0N1n-dev

I think you can check this example https://github.com/node-formidable/formidable/blob/master/examples/forceBuffer.js

Or you can try formidable-mini which is trimmed down version of formidable - just the parser and exposes a FormData/File API, thus meaning the "files" there are regular Web API Files, so you can file.arrayBuffer() (arraybuffer/uint8array) to Cosmic.