-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
224 lines (200 loc) · 5.79 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const fs = require("fs");
const { unlink, writeFile } = require("fs/promises");
const { auth } = require("./auth.js");
const {
GOOGLE_DOC_TYPE,
WORD_DOC_TYPE,
GOOGLE_SHEET_TYPE,
EXCEL_SHEET_TYPE,
DRIVE_FOLDER_TYPE,
DRIVE_SHORTCUT_TYPE,
} = require("./constants.js");
const { bytesToMB, isFolder, shutDown } = require("./utils.js");
const { google } = require("googleapis");
let HOST_DRIVE;
let TARGET_DRIVE;
const INITIAL_NODE = {
kind: "drive#file",
mimeType: DRIVE_FOLDER_TYPE,
id: "root",
name: "Root",
parent: "root",
};
const TRUTH_PATTERN = [];
let TP_ITERATOR = 0;
const SKIPPED = [];
async function authInit() {
const host_auth_client = await auth(true);
const target_auth_client = await auth(false);
HOST_DRIVE = google.drive({ version: "v3", auth: host_auth_client });
TARGET_DRIVE = google.drive({
version: "v3",
auth: target_auth_client,
});
}
const listFiles = async (hostParentID, targetParentID) => {
let res = await HOST_DRIVE.files.list({
q: `"${hostParentID}" in parents and trashed=false and mimeType!="${DRIVE_SHORTCUT_TYPE}"`,
fields:
"nextPageToken, kind, incompleteSearch, files(size, kind, mimeType, id, name)",
});
const files = res.data.files;
while (res.data.nextPageToken) {
res = await HOST_DRIVE.files.list({
pageToken: res.data.nextPageToken,
q: `"${hostParentID}" in parents and trashed=false and mimeType!="${DRIVE_SHORTCUT_TYPE}"`,
fields:
"nextPageToken, kind, incompleteSearch, files(size, kind, mimeType, id, name)",
});
for (const file of res.data.files) files.push(file);
if (files.length === 0) break;
}
for (const item of files) {
bytesToMB(item);
item.parent = targetParentID;
}
return files;
};
async function buildTruthPattern(path, currentNode) {
console.log(path);
const { parent, children, ...restOfCurrentNode } = currentNode;
TRUTH_PATTERN.push(restOfCurrentNode);
if (isFolder(currentNode)) {
currentNode.children = await listFiles(currentNode.id, null);
for (const child of currentNode.children)
await buildTruthPattern(path + child.name + "/", child);
}
}
const createNewFolder = async (name, parent, mimeType) => {
// if (name !== TRUTH_PATTERN[TP_ITERATOR].name) {
// console.log("MISMATCH FOUND DURING FOLDER CREATION:", name);
// shutDown();
// }
const res = await TARGET_DRIVE.files
.create({
requestBody: {
name: name,
parents: [parent],
mimeType: mimeType,
},
})
.then((res) => {
TP_ITERATOR++;
return res;
});
return res.data.id;
};
const fileTransfer = async (currentNode, path) => {
// if (currentNode.name !== TRUTH_PATTERN[TP_ITERATOR].name) {
// console.log("MISMATCH FOUND DURING FILE TRANSFER:");
// console.log(currentNode);
// shutDown();
// }
if (parseFloat(currentNode.size) > 5) {
SKIPPED.push(path);
console.log("====> SKIPPING FILE: (TOO LARGE)");
TP_ITERATOR++;
return;
}
let exportType;
let convertBeforeUpload = false;
let fileNameOnDisk = "";
if (currentNode.mimeType === GOOGLE_DOC_TYPE) {
exportType = WORD_DOC_TYPE;
convertBeforeUpload = true;
fileNameOnDisk = currentNode.name + ".docx";
} else if (currentNode.mimeType === GOOGLE_SHEET_TYPE) {
exportType = EXCEL_SHEET_TYPE;
convertBeforeUpload = true;
fileNameOnDisk = currentNode.name + ".xlsx";
} else {
convertBeforeUpload = false;
fileNameOnDisk = currentNode.name;
}
let res = convertBeforeUpload
? await HOST_DRIVE.files.export(
{
fileId: currentNode.id,
mimeType: exportType,
},
{
responseType: "stream",
},
)
: await HOST_DRIVE.files.get(
{
fileId: currentNode.id,
alt: "media",
},
{
responseType: "stream",
},
);
const saveToDisk = new Promise((resolve) => {
let streamToSave = fs.createWriteStream(fileNameOnDisk);
res.data.pipe(streamToSave);
streamToSave.on("finish", function () {
streamToSave.close();
});
streamToSave.on("close", async () => {
resolve();
});
});
await saveToDisk;
console.log("====> File Downloaded: ", fileNameOnDisk);
convertBeforeUpload
? await TARGET_DRIVE.files.create({
requestBody: {
name: currentNode.name,
parents: [currentNode.parent],
mimeType: currentNode.mimeType,
},
media: {
mimeType: exportType,
body: fs.createReadStream(fileNameOnDisk),
},
})
: await TARGET_DRIVE.files.create({
requestBody: {
name: currentNode.name,
parents: [currentNode.parent],
mimeType: currentNode.mimeType,
},
media: {
body: fs.createReadStream(fileNameOnDisk),
},
});
await unlink(fileNameOnDisk);
console.log("====> File Uploaded: ", fileNameOnDisk);
TP_ITERATOR++;
};
const sync = async (path, currentNode) => {
console.log(path);
if (isFolder(currentNode)) {
currentNode.newID = await createNewFolder(
currentNode.name,
currentNode.parent,
currentNode.mimeType,
);
currentNode.children = await listFiles(currentNode.id, currentNode.newID);
for (const child of currentNode.children)
await sync(path + child.name + "/", child);
} else {
await fileTransfer(currentNode, path);
}
};
async function main() {
await authInit();
// console.log("Building Truth Pattern...");
// console.log();
// await buildTruthPattern("./", { ...INITIAL_NODE });
console.log();
console.log("Starting sync now...");
console.log();
await sync("./", { ...INITIAL_NODE });
console.log();
const skippedData = SKIPPED.join("\n");
await writeFile("skipped.txt", skippedData);
console.log("Sync Complete.");
}
main();