Pepguy commited on
Commit
e510c35
·
verified ·
1 Parent(s): 31389fd

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +19 -17
app.js CHANGED
@@ -9,7 +9,7 @@ const redis = createClient();
9
  app.use(express.json());
10
 
11
  const SCOPES = ["https://www.googleapis.com/auth/drive.file"];
12
- const TOKEN_PATH = "token.json";
13
  const CREDENTIALS_PATH = "credentials.json";
14
 
15
 
@@ -43,18 +43,15 @@ async function getAllRedisData() {
43
  }
44
 
45
  async function uploadToDrive(filename) {
46
- const auth = await getAuthClient();
47
- const drive = google.drive({ version: "v3", auth });
48
-
49
- const fileMetadata = { name: path.basename(filename) };
50
- const media = {
51
- mimeType: "application/json",
52
- body: fs.createReadStream(filename),
53
- };
54
-
55
  const res = await drive.files.create({
56
- resource: fileMetadata,
57
- media,
 
 
 
 
 
 
58
  fields: "id",
59
  });
60
 
@@ -92,11 +89,16 @@ app.get("/do-all", async (_, res) => {
92
  });
93
 
94
  app.get("/do-backup", async (_, res) => {
95
- const data = await getAllRedisData();
96
- const filename = `redis_backup_${Date.now()}.json`;
97
- fs.writeFileSync(filename, JSON.stringify(data, null, 2));
98
- const fileId = await uploadToDrive(filename);
99
- res.send("Uploaded. File ID: " + fileId);
 
 
 
 
 
100
  });
101
 
102
  (async () => {
 
9
  app.use(express.json());
10
 
11
  const SCOPES = ["https://www.googleapis.com/auth/drive.file"];
12
+
13
  const CREDENTIALS_PATH = "credentials.json";
14
 
15
 
 
43
  }
44
 
45
  async function uploadToDrive(filename) {
 
 
 
 
 
 
 
 
 
46
  const res = await drive.files.create({
47
+ requestBody: {
48
+ name: path.basename(filename),
49
+ parents: ['1AZaP0NKXcGkU_lpKa-Qkxl5FXvaiqJfW'], // ← Your folder ID
50
+ },
51
+ media: {
52
+ mimeType: 'application/json',
53
+ body: fs.createReadStream(filename),
54
+ },
55
  fields: "id",
56
  });
57
 
 
89
  });
90
 
91
  app.get("/do-backup", async (_, res) => {
92
+ try {
93
+ const data = await getAllRedisData();
94
+ const filename = `redis_backup_${Date.now()}.json`;
95
+ fs.writeFileSync(filename, JSON.stringify(data, null, 2));
96
+ const fileId = await uploadToDrive(filename);
97
+ res.send("Uploaded. File ID: " + fileId);
98
+ } catch (err) {
99
+ console.error(err);
100
+ res.status(500).send("Backup failed");
101
+ }
102
  });
103
 
104
  (async () => {