-
Notifications
You must be signed in to change notification settings - Fork 0
/
concat.js
79 lines (62 loc) · 1.71 KB
/
concat.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
const fs = require('fs');
const path = require('path');
const { parseToken, parsePath, encodePath, buildRemfsDir } = require('./utils.js');
async function handleConcat(req, res, params, fsRoot, reqPath, pauth) {
console.log(reqPath, params);
const tokenName = 'remfs-token';
const token = parseToken(req, tokenName);
const perms = await pauth.getPerms(token);
const dstAbsPath = getAbsPath(params.dstFile, reqPath);
const dstFsPath = fsRoot + dstAbsPath;
const writeStream = fs.createWriteStream(dstFsPath);
if (!perms.canRead(dstAbsPath)) {
res.statusCode = 403;
res.write(`No permission to write '${dstAbsPath}'`);
res.end();
return;
}
// check perms
for (const filePath of params.srcFiles) {
const absPath = getAbsPath(filePath, reqPath);
if (!perms.canRead(absPath)) {
res.statusCode = 403;
res.write(`No permission to write '${absPath}'`);
res.end();
return;
}
const fsPath = fsRoot + absPath;
try {
await fs.promises.stat(fsPath);
}
catch (e) {
res.statusCode = 404;
res.write(`Not found: '${absPath}'`);
res.end();
return;
}
// pipe the src files one at a time
await new Promise((resolve, reject) => {
const readStream = fs.createReadStream(fsPath);
readStream.on('error', (e) => {
reject(e);
});
readStream.pipe(writeStream, { end: false });
readStream.on('end', resolve);
});
}
writeStream.end();
res.end();
}
function getAbsPath(filePath, reqPath) {
let absPath;
if (filePath.startsWith('/')) {
absPath = filePath;
}
else {
absPath = '/' + reqPath + '/' + filePath;
}
return absPath;
}
module.exports = {
handleConcat,
};