0

https://i.stack.imgur.com/3dWQb.png

Every time I try to upload an image file it comes through corrupted at 1.83kb and no actual image attached. Like in the above screenshot.

I don't know if this is the problem, but this is the tool I'm using and copying to parse Multipart Form Data

https://github.com/mscdex/busboy

and this is my code:

app.post("/createPost", (request, response) => {
  response.set("Access-Control-Allow-Origin", "*");
  let token_id = uuidv4();

  const bb = busboy({ headers: request.headers });

  bb.on("file", (name, file, info) => {
    const { filename, encoding, mimeType } = info;
    let filepath = path.join(os.tmpdir(), filename);
    file.pipe(fs.createWriteStream(filepath));
    fileData = { filepath, mimeType };
    console.log(
      `File [${name}]: filename: %j, encoding: %j, mimeType: %j`,
      filename,
      encoding,
      mimeType
    );
    file
      .on("data", (data) => {
        console.log(`File [${name}] got ${data.length} bytes`);
      })
      .on("close", () => {
        console.log(`File [${name}] done`);
      });
  });

  bb.on("field", (name, val, info) => {
    console.log(`Field [${name}]: value: %j`, val);
    fields[name] = val;
  });

  bb.on("close", () => {
    console.log("Done parsing form!");
    bucket.upload(
      fileData.filepath,
      {
        uploadType: "media",
        metadata: {
          metadata: {
            contentType: fileData.mimeType,
            firebaseStorageDownloadTokens: token_id,
          },
        },
      },
      (err, uploadedFile) => {
        if (!err) {
          createDocument(uploadedFile);
        }
      }
    );
    // response.writeHead(303, { Connection: "close", Location: "/" });
    function createDocument(uploadedFile) {
      db.collection("posts")
        .doc(fields.id)
        .set({
          id: fields.id,
          caption: fields.caption,
          location: fields.location,
          date: parseInt(fields.date),
          imgUrl: `https://firebasestorage.googleapis.com/v0/b/${bucket.name}/o/${uploadedFile.name}?alt=media&token=${token_id}`,
          name: fields.name,
        })
        .then((response) => {
          console.log("response=", response);
        });
    }
    response.end();
  });
  request.pipe(bb);
});

Does anybody see anything I did wrong here? Anything in this code that could be corrupting my image files?

It works when the image is coming from the browsers tmp folder, like when I use the webcam and take a picture inside the browser. However uploading files results in corrupted data. Anybody know why? Is there something wrong with my code? Could it be a Firestore rules issue blocking image uploads?

This is my rule set:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    allow read, write: if true;
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}

It used to work, so I don't think its the rules. I didn't change them. I don't understand what happened here and why its not working anymore. I've tried 4 different browsers and none of them work. Help?

edit:

The uploads are correctly being converted to BLOB and displaying property before uploading.

https://i.stack.imgur.com/O2ZzY.jpg

Eric Andre
  • 204
  • 2
  • 9
  • Are you getting corrupted images on console preview? Can you try downloading the file locally to verify that it's the same as what you uploaded? Meanwhile check this [StackOverflow Answer](https://stackoverflow.com/a/60160119/18265570) – Roopa M Oct 14 '22 at 06:43
  • @Roopa M I don't know exactly what you mean by console preview, but the image loads fine in the browser and correctly gets converted into BLOB format. So in my opinion the problem HAS to be server side. [https://i.imgur.com/EbberFY.jpg](https://i.imgur.com/EbberFY.jpg) – Eric Andre Oct 15 '22 at 12:37
  • In the first screenshot it's what Firestorage is showing, a blank white 1.83kb image file. When I go to the link it's blank, when I download that file it's just a 1.83kb blank file. Somewhere between uploading and reaching Firebase it's getting corrupted. Is there any other code I can post? The post in OP is all the backend I have. It used to work too I didn't change anything. – Eric Andre Oct 15 '22 at 12:44
  • Seems like you need to configure Multer (Or alternative middleware) in your request to make the file available for you. Have a look at this [Article](https://javascript.plainenglish.io/uploading-an-image-to-firebase-cloud-storage-and-returning-url-with-express-nodejs-713daac7a5d4#:~:text=configure%20Multer%20as%20a%20middleware%20in%20your%20request%20to%20make%20the%20file%20available%20for%20you) – Roopa M Oct 17 '22 at 09:09

0 Answers0