-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
87 lines (76 loc) · 2.26 KB
/
upload.js
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
const fs = require('fs');
const {google} = require('googleapis');
const axios = require('axios');
const keyFile = 'credentials.json';
const drive = google.drive({
version: 'v3',
auth: new google.auth.JWT({
keyFile: keyFile,
scopes: ['https://www.googleapis.com/auth/drive'],
}),
});
const sendMsg = (msg) => {
const url = process.env.SLACK_WEBHOOK_URL;
const data = {
text: msg
};
axios.post(url, data)
.then(response => {
console.log('Slack response:', response.data);
})
.catch(error => {
console.error('Error:', error);
});
}
async function uploadFile(filePath) {
const fileName = filePath.split('/').pop();
const folderId = process.env.FOLDERID;
const fileMetadata = {
'name': fileName,
'parents': [folderId],
};
try {
// Check if the file already exists in the folder
let existingFileId;
const searchResponse = await drive.files.list({
q: `mimeType='image/png' and trashed = false and '${folderId}' in parents and name='${fileName}'`,
fields: 'nextPageToken, files(id, name)',
});
if (searchResponse.data.files.length > 0) {
existingFileId = searchResponse.data.files[0].id;
}
// Upload or replace the file
const media = {
mimeType: 'image/png',
body: fs.createReadStream(filePath),
};
if (existingFileId) {
console.log(`File ${fileName} already exists, replacing it.`);
const response = await drive.files.update({
fileId: existingFileId,
requestBody: fileMetadata,
media,
fields: 'id',
});
console.log(`[upload.js] File ID: ${response.data.id}, File: ${filePath} - Replaced`);
} else {
const response = await drive.files.create({
requestBody: fileMetadata,
media,
fields: 'id',
});
console.log(`[upload.js] File ID: ${response.data.id}, File: ${filePath} - Uploaded`);
}
} catch (err) {
console.error(`[upload.js] Error processing ${filePath}:`, err, fileMetadata);
// throw err;
sendMsg('Failed to upload, err:', err.message);
}
}
async function uploadFiles() {
const files = fs.readdirSync('.').filter((file) => file.startsWith('radar_frame_timestamp_'));
for (const file of files) {
await uploadFile(file);
}
}
uploadFiles();