Skip to content

Instantly share code, notes, and snippets.

@ahmed-alhelali
Created April 29, 2023 22:23
Show Gist options
  • Save ahmed-alhelali/037f9acfadc078deccfe62bacf19da6f to your computer and use it in GitHub Desktop.
Save ahmed-alhelali/037f9acfadc078deccfe62bacf19da6f to your computer and use it in GitHub Desktop.
JavaScript code M&A
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