Last active
June 1, 2021 15:45
-
-
Save Anshul0305/bbf5246366cb7f540e7b5d7154c62247 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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