Repository navigation
Expand file tree
/
Copy pathfirebase.js
More file actions
87 lines (76 loc) · 2.73 KB
/
Copy pathfirebase.js
File metadata and controls
87 lines (76 loc) · 2.73 KB
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
// Import the Firebase Admin SDK
const admin = require("firebase-admin");
const fs = require("fs");
const path = require("path");
require("dotenv").config();
// Initialize Firebase Admin SDK
const initializeFirebase = async () => {
return new Promise((resolve, reject) => {
try {
// Initialize Firebase Admin SDK
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FIREBASE_PROJECT_ID,
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
}),
storageBucket: "asterin041.appspot.com", // Your Firebase Storage bucket
});
console.log("Firebase initialized successfully.");
const bucket = admin.storage().bucket(); // Now, initialize bucket after Firebase is initialized
resolve(bucket); // Resolve with bucket so it can be used later
} catch (err) {
console.error("Error initializing Firebase:", err);
reject(err); // Reject the promise if initialization fails
}
});
};
//
const uploadFolder = async (folderPath, bucket) => {
try {
const files = fs.readdirSync(folderPath); // Get all files in the folder
files.forEach((file) => {
const filePath = path.join(folderPath, file); // Full path to the file
// Upload each file
bucket.upload(
filePath,
{
destination: file, // Path in Firebase Storage, just the file name
public: true,
},
(err, uploadedFile) => {
if (err) {
console.log("Error uploading file:", filePath, err);
} else {
console.log("File uploaded successfully:", uploadedFile.name);
}
}
);
});
} catch (error) {
console.log("Error uploading files:", error);
}
};
// async function upload() {
// bucket = await initializeFirebase();
// uploadFolder('./auth_info_baileys',bucket);
// }
// upload();
const downloadFolder = async (localFolder, bucket) => {
try {
const [files] = await bucket.getFiles(); // Get all files
files.forEach((file) => {
const localFilePath = path.join(localFolder, path.basename(file.name)); // Local path to save the file
file.download({ destination: localFilePath }, (err) => {
if (err) {
console.log("Error downloading file:", file.name, err);
} else {
console.log("File downloaded successfully:", localFilePath);
}
});
});
} catch (error) {
console.log("Error fetching files:", error);
}
};
module.exports = { uploadFolder,downloadFolder,initializeFirebase}