Created
April 29, 2023 22:23
-
-
Save ahmed-alhelali/037f9acfadc078deccfe62bacf19da6f to your computer and use it in GitHub Desktop.
JavaScript code M&A
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
Object.defineProperty(exports, "__esModule", { value: true }); | |
exports.downloadVideoAndUploadToStorage = void 0; | |
const admin = require("firebase-admin"); | |
const functions = require("firebase-functions"); | |
const https = require("https"); | |
const fs = require("fs"); | |
const os = require("os"); | |
const path = require("path"); | |
admin.initializeApp(); | |
exports.downloadVideoAndUploadToStorage = functions.firestore | |
.document('one_piece/{Id}') | |
.onCreate(async (snap, context) => { | |
var _a; | |
console.log(`Video url new uploaded`); | |
const { url } = snap.data(); | |
if (!((_a = snap.data()) === null || _a === void 0 ? void 0 : _a.url)) { | |
console.log('No URL found in document'); | |
return null; | |
} | |
console.log(`Video url new uploaded`); | |
const bucket = admin.storage().bucket(); | |
const tempFilePath = path.join(os.tmpdir(), path.basename(url)); | |
const file = fs.createWriteStream(tempFilePath); | |
https.get(url, (response) => { | |
response.pipe(file); | |
file.on('finish', async () => { | |
await bucket.upload(tempFilePath, { | |
destination: `videos/${context.params.Id}.mp4`, | |
metadata: { | |
contentType: 'video/mp4', | |
}, | |
}); | |
console.log(`Video ${context.params.Id} uploaded to Firebase Storage`); | |
fs.unlinkSync(tempFilePath); | |
return null; | |
}); | |
file.on('error', (err) => { | |
console.error(`Error while downloading video: ${err.message}`); | |
fs.unlinkSync(tempFilePath); | |
return null; | |
}); | |
}); | |
return null; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment