I created a Chrome Extension that takes a screenshot of the current tab. I'd like to then upload the image to Firebase.
chrome.tabs.captureVisibleTab
returns at dataUrl string [docs], and I attempt to include it as formData to a POST request as follows:
screenshotBtn.onclick = function(){
// generate the screenshot
chrome.tabs.captureVisibleTab(null, { format: 'png', quality: 80 }, function(dataUrl){
// console.log(dataUrl);
// create imageBlob from data
let imgBlob = b64toBlob(dataUrl.replace('data:image/png;base64,', ''), "image/png");
console.log('imgBlob: ', imgBlob);
let formData = new FormData();
formData.append('image', imgBlob);
// upload to Firebase Storage
let url = endpoint + '/uploadImage';
fetch(url, {
method: 'POST',
headers: {
'Content-Type': false
},
body: formData
})
.then((response) => response.json())
.then(data => {
console.log(data);
});
});
};
(the function b64toBlob
is taken from this Stackoverflow Post)
I try to process the image and upload it to Firebase on my server as follows:
app.post("/api/uploadImage", (req, res) => {
(async () => {
try {
console.log(req.body.image);
// req.body.image = image in base64 format
await uploadFile(req.body.image);
return res.status(200).send();
} catch(error){
console.log(error);
return res.status(500).send(error);
}
})();
});
async function uploadFile(imageBlob){
const metadata = {
metadata: {
firebaseStorageDownloadTokens: uuid()
},
contentType: 'image/jpeg',
cacheControl: 'public, max-age=31536000'
};
await bucket.upload(imageBlob, {
gzip: true,
metadata: metadata,
});
}
I'm having trouble telling whether the issue is
- with the way the POST request is formatted
- with the way the file is received on the server
I am currently getting a 500 error, and this is what the request looks like: