Spaces:
Sleeping
Sleeping
File size: 2,540 Bytes
97f53b4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
var admin = require("firebase-admin");
var serviceAccount = require("../serviceAccountKey.json")
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "gs://flutter-hayat.appspot.com/"
});
var bucket = admin.storage().bucket();
const d = new Date()
const date = new Date(d.setFullYear(d.getFullYear() + 200)).toString()
async function uploadFile(filepath, filename) {
console.log("File path =>" + filepath)
console.log("File name =>" + filename)
try {
const file = bucket.file(filename);
const [exists] = await file.exists();
if (exists) {
console.log(`File with name ${filename} already exists. Deleting it...`);
await file.delete();
console.log(`File ${filename} deleted successfully.`);
}
await bucket.upload(filepath, {
gzip: true,
destination: filename,
metadata: {
cacheControl: 'public, max-age=31536000'
}
});
console.log(`${filename} uploaded to bucket.`);
} catch (e) {
console.log("Error: ", e);
}
}
async function generateSignedUrl(filename) {
const options = {
version: 'v2',
action: 'read',
expires: date
};
const [url] = await bucket.file(filename).getSignedUrl(options);
imageUrl = url + ''
console.log(url);
return url
};
async function uploadAvatar(filepath, userId) {
const imageName = `${userId}_profile.jpg`;
// Check file exists
const file = bucket.file(`ProfilePictures/${imageName}`);
const [exists] = await file.exists();
if (exists) {
console.log(`File with name ${file.name} already exists. Deleting it...`);
await file.delete();
console.log(`File ${file.name} deleted successfully.`);
}
// Upload file to Firebase Storage
await bucket.upload(filepath, {
gzip: true,
destination: `ProfilePictures/${imageName}`,
metadata: {
cacheControl: 'public, max-age=31536000',
},
});
console.log(`${imageName} uploaded successfully.`);
if (!exists) {
throw new Error(`File does not exist after upload: ${imageName}`);
}
// Generate signed URL
const [url] = await file.getSignedUrl({
action: 'read',
expires: '03-01-2030',
});
console.log('Generated URL:', url);
return url;
}
module.exports = {
admin,
bucket,
uploadFile,
generateSignedUrl,
uploadAvatar
} |