-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.js
More file actions
93 lines (83 loc) · 2.63 KB
/
upload.js
File metadata and controls
93 lines (83 loc) · 2.63 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
88
89
90
91
92
93
const { initializeApp } = require("firebase/app");
const {
getStorage,
ref,
uploadBytes,
deleteObject,
listAll,
} = require("firebase/storage");
const path = require("path");
const fs = require("fs");
require("dotenv").config();
const firebaseConfig = {
apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
projectId: process.env.FIREBASE_PROJECT_ID,
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.FIREBASE_APP_ID,
measurementId: process.env.FIREBASE_MEASUREMENT_ID,
};
const app = initializeApp(firebaseConfig);
const storage = getStorage(app);
const dirPath = path.join(__dirname, "backups_db");
async function deleteOldFilesIfNecessary() {
const listRef = ref(storage, `${process.env.BACKUP_PATH}/`);
try {
console.log(
`Deleting old files on firebase on dir ${process.env.BACKUP_PATH}/...`
);
const fileList = await listAll(listRef);
if (!fileList || !fileList.items || fileList.items.length === 0) {
console.error("No files available for deletion.");
return;
}
const maxFiles = Number(process.env.MAX_FILES) || 3;
if (fileList.items.length >= maxFiles) {
fileList.items.sort((a, b) => a.name.localeCompare(b.name));
const oldestFile = fileList.items[0];
await deleteObject(oldestFile);
console.log(`Old file ${oldestFile.name} deleted.`);
}
} catch (err) {
console.error("Error deleting old files:", err);
}
}
fs.readdir(dirPath, (err, files) => {
console.log(`Upload files on date: ${new Date().toISOString()}...`);
if (err) {
console.error("Error reading directory:", err);
return;
}
deleteOldFilesIfNecessary().then(() => {
if (files.length === 0) {
console.log("No files found in the directory.");
return;
}
files.forEach((file) => {
const filePath = path.join(dirPath, file);
const destFileName = `${process.env.BACKUP_PATH}/${file}`;
const fileRef = ref(storage, destFileName);
if (file !== ".gitignore") {
fs.readFile(filePath, (err, data) => {
if (err) {
console.error(`Error reading file ${file}:`, err);
return;
}
uploadBytes(fileRef, data)
.then(() => {
console.log(
`File ${file} uploaded successfully to Firebase Storage`
);
})
.catch((err) => {
console.error(
`Error uploading file ${file} to Firebase Storage:`,
err
);
});
});
}
});
});
});