-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrclone.js
More file actions
108 lines (83 loc) · 2.18 KB
/
rclone.js
File metadata and controls
108 lines (83 loc) · 2.18 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const { spawn } = require('child_process');
const { parsePath, encodePath } = require('./utils.js');
async function listRemotes() {
const cmd = spawn('rclone', ['listremotes']);
let allData = '';
cmd.stdout.setEncoding('utf8');
cmd.stdout.on('data', (data) => {
allData += data;
});
return new Promise((resolve, reject) => {
cmd.stdout.on('end', () => {
const remotes = allData.split('\n')
.slice(0, -1)
.map(r => r.slice(0, -1));
resolve(remotes);
});
cmd.stdout.on('error', (e) => {
reject(e);
});
});
}
async function rcloneLs(reqPath) {
const pathParts = parsePath(reqPath);
const rclonePath = pathParts[0] + ':' + encodePath(pathParts.slice(1)).slice(1);
const ls = spawn('rclone', ['lsjson', rclonePath]);
ls.stdout.setEncoding('utf8');
let json = '';
return new Promise((resolve, reject) => {
ls.stdout.on('data', (data) => {
json += data;
});
ls.stdout.on('end', () => {
try {
const out = JSON.parse(json);
resolve(out);
}
catch (e) {
reject(e);
}
});
})
}
function rcloneCat(reqPath, offset, count) {
const pathParts = parsePath(reqPath);
const rclonePath = pathParts[0] + ':' + encodePath(pathParts.slice(1)).slice(1);
const args = ['cat'];
if (offset) {
args.push('--offset');
args.push(offset);
}
if (count) {
args.push('--count');
args.push(count);
}
args.push(rclonePath);
const cat = spawn('rclone', args);
return cat.stdout;
}
async function rcat(reqPath, inStream) {
const rclonePath = genRclonePath(reqPath);
const cmd = spawn('rclone', ['rcat', rclonePath]);
inStream.pipe(cmd.stdin);
return new Promise((resolve, reject) => {
cmd.stdin.on('close', () => {
resolve();
});
});
}
async function rcloneDelete(reqPath) {
const rclonePath = genRclonePath(reqPath);
const cmd = spawn('rclone', ['delete', rclonePath]);
}
function genRclonePath(reqPath) {
const pathParts = parsePath(reqPath);
const rclonePath = pathParts[0] + ':' + encodePath(pathParts.slice(1)).slice(1);
return rclonePath;
}
module.exports = {
listRemotes,
ls: rcloneLs,
cat: rcloneCat,
rcat,
};