Skip to content

Instantly share code, notes, and snippets.

@Anshul0305
Last active June 1, 2021 15:45
Show Gist options
  • Save Anshul0305/bbf5246366cb7f540e7b5d7154c62247 to your computer and use it in GitHub Desktop.
Save Anshul0305/bbf5246366cb7f540e7b5d7154c62247 to your computer and use it in GitHub Desktop.
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault(),
storageBucket: "YOUR STORAGE BUCKET URL HERE"
});
const settings = { timestampsInSnapshots: true};
const db = admin.firestore();
db.settings(settings);
const bucket = admin.storage().bucket();
function uploadFile(file, token){
return new Promise((resolve, reject)=> {
const metadata = {
metadata: {
firebaseStorageDownloadTokens: token
},
contentType: 'image/png',
cacheControl: 'public, max-age=31536000',
};
bucket.upload(file, {
gzip: true,
metadata: metadata,
destination: `images/${token}`
}, function(err, file) {
if(err){
console.log(err);
reject(err)
} else {
resolve({
fileName: file.name,
token: token
});
}
});
});
}
function handleImageReceived(agent){
const fileUrl = agent.parameters.url;
const token = Date.now()
return uploadFile(fileUrl, token)
.then(res => {
agent.add("Image uploaded");
})
.catch(error => console.log(error));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment