-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
115 lines (107 loc) · 3.79 KB
/
app.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
'use strict';
const fs = require("fs");
const { spawn } = require('child_process');
const { EOL } = require('os');
const pendingDL = "list.txt"
const original_config = "rclone.conf";
const modified_config = "rclone_modified.conf";
const rclonePath = "rclone"; // Change the rclonePath if rclone is not in the system path
const retriesBeforeExit = 1; // Retries of the progress
var token = null;
var tokenExpTime = null;
function readTokenFromOriginalConfig() {
try {
let configContent = fs.readFileSync(original_config, { encoding: "utf-8" }).split(/\r?\n/);
for (let i = 0; i < configContent.length; i++) {
if (configContent[i].startsWith("token = ")) {
token = configContent[i].replace("token = ", "");
tokenExpTime = Date.parse(JSON.parse(token).expiry)
}
}
} catch (e) {
console.log(">>>------ Encountered error while trying to read the token:\n>>>------ " + e);
}
if (token != null) {
readpendingDL();
if (!errorParsingPendingDL && folderIDList.length != 0) {
console.log(">>>------ Start");
downloadFile(0);
}
}
}
var folderIDList = [];
var destinationList = [];
var errorParsingPendingDL = false;
function readpendingDL() {
try {
let pendingDLContent = fs.readFileSync(pendingDL, { encoding: "utf-8" }).split(/\r?\n/);
for (let i = 0; i < pendingDLContent.length; i++) {
if (pendingDLContent[i] != "") {
let array = pendingDLContent[i].split("===");
if (array.length != 2) {
console.log(">>>------ Encountered error while parsing this line:\n>>>------ " + pendingDLContent[i]);
errorParsingPendingDL = true;
} else {
folderIDList.push(array[0]);
destinationList.push(array[1]);
}
}
}
} catch (e) {
console.log(">>>------ Encountered error while trying to read the file list:\n>>>------ " + e);
errorParsingPendingDL = true;
}
}
var downloadRetries = 0;
function downloadFile(index) {
console.log(">>>------ Copying Folder from " + folderIDList[index] + " to " + destinationList[index]);
if (fs.existsSync(modified_config))
fs.unlinkSync(modified_config)
fs.copyFileSync(original_config, modified_config);
fs.appendFileSync(modified_config, EOL +
"[tmp]" + EOL +
"type = drive" + EOL +
"scope = drive" + EOL +
"root_folder_id = " + folderIDList[index] + EOL +
"token = " + token + EOL
);
//create rclone process
const rclone = spawn(rclonePath, ["--drive-server-side-across-configs", "--config", modified_config, "-P", "copy", "tmp:", destinationList[index]], { stdio: 'inherit' });
rclone.on('close', (code) => {
readTokensFromModifiedConfig();
console.log(">>>------ Child process exited with code " + code);
if (code != 0) {
if (downloadRetries < retriesBeforeExit) {
downloadRetries++;
downloadFile(index);
} else {
console.log(">>>------ Encountered an error.");
}
} else if (index + 1 == folderIDList.length) {
console.log(">>>------ Finished.");
} else {
downloadRetries = 0;
downloadFile(index + 1);
}
});
}
function readTokensFromModifiedConfig() {
try {
let modifiedConfigContent = fs.readFileSync(modified_config, { encoding: "utf-8" }).split(/\r?\n/);
for (let i = 0; i < modifiedConfigContent.length; i++) {
if (modifiedConfigContent[i].startsWith("token = ")) {
let tmp_token = modifiedConfigContent[i].replace("token = ", "");
let tmp_tokenExpTime = Date.parse(JSON.parse(tmp_token).expiry)
if (token != tmp_token && tmp_tokenExpTime > tokenExpTime) {
let originalConfigFileContent = fs.readFileSync(original_config, { encoding: "utf-8" });
originalConfigFileContent = originalConfigFileContent.replace(token, tmp_token);
fs.writeFileSync(original_config, originalConfigFileContent);
token = tmp_token;
}
}
}
} catch (e) {
console.log(">>>------ Encountered error trying to read token:\n>>>------ " + e);
}
}
readTokenFromOriginalConfig();