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.