-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
app.js
229 lines (187 loc) · 7.38 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
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
225
226
227
228
229
#!/usr/bin/env node
'use strict';
const express = require('express');
const formidable = require('formidable');
const { join, basename } = require('path');
const fs = require('fs-extra');
const morgan = require('morgan');
const asyncHandler = require('express-async-handler');
const archiver = require('archiver');
const pMap = require('p-map');
const os = require('os');
const flatMap = require('lodash/flatMap');
const contentDisposition = require('content-disposition');
const { createProxyMiddleware } = require('http-proxy-middleware');
const qrcode = require('qrcode-terminal');
const clipboardy = require('clipboardy');
const bodyParser = require('body-parser');
const filenamify = require('filenamify');
const util = require('util');
const stream = require('stream');
const parseRange = require('range-parser');
const pipeline = util.promisify(stream.pipeline);
const maxFields = 1000;
const debug = false;
const isDirectory = async (filePath) => (await fs.lstat(filePath)).isDirectory();
module.exports = ({ sharedPath: sharedPathIn, port, maxUploadSize, zipCompressionLevel, devMode }) => {
// console.log({ sharedPath: sharedPathIn, port, maxUploadSize, zipCompressionLevel });
const sharedPath = sharedPathIn || process.cwd();
function getFilePath(relPath) {
if (relPath == null) return sharedPath;
return join(sharedPath, join('/', relPath));
}
const app = express();
if (debug) app.use(morgan('dev'));
// NOTE: Must support non latin characters
app.post('/api/upload', asyncHandler(async (req, res) => {
// console.log(req.headers)
// parse a file upload
const form = new formidable({
multiples: true,
keepExtensions: true,
uploadDir: sharedPath,
maxFileSize: maxUploadSize,
maxFields,
});
form.parse(req, async (err, fields, { files: filesIn }) => {
if (err) {
console.error('Upload failed', err);
res.status(400).send({ error: { message: err.message } });
return;
}
if (filesIn) {
const files = Array.isArray(filesIn) ? filesIn : [filesIn];
// console.log(JSON.stringify({ fields, files }, null, 2));
console.log('Uploaded files:');
files.forEach((f) => console.log(f.name, `(${f.size} bytes)`));
await pMap(files, async (file) => {
try {
const targetPath = join(sharedPath, filenamify(file.name, { maxLength: 255 }));
if (!(await fs.pathExists(targetPath))) await fs.rename(file.path, targetPath);
} catch (err) {
console.error(`Failed to rename ${file.name}`, err);
}
}, { concurrency: 10 });
}
res.end();
});
}));
// NOTE: Must support non latin characters
app.post('/api/paste', bodyParser.urlencoded({ extended: false }), asyncHandler(async (req, res) => {
if (req.body.saveAsFile === 'true') {
await fs.writeFile(getFilePath(`client-clipboard-${new Date().getTime()}.txt`), req.body.clipboard);
} else {
await clipboardy.write(req.body.clipboard);
}
res.end();
}));
// NOTE: Must support non latin characters
app.post('/api/copy', asyncHandler(async (req, res) => {
res.send(await clipboardy.read());
}));
async function serveDirZip(filePath, res) {
const archive = archiver('zip', {
zlib: { level: zipCompressionLevel },
});
res.writeHead(200, {
'Content-Type': 'application/zip',
// NOTE: Must support non latin characters
'Content-disposition': contentDisposition(`${basename(filePath)}.zip`),
});
const promise = pipeline(archive, res);
archive.directory(filePath, basename(filePath));
archive.finalize();
await promise;
}
async function serveResumableFileDownload({ filePath, range, res, forceDownload }) {
if (forceDownload) {
// Set the filename in the Content-disposition header
res.set('Content-disposition', contentDisposition(basename(filePath)));
}
const { size: fileSize } = await fs.stat(filePath);
if (range) {
const subranges = parseRange(fileSize, range);
if (subranges.type !== 'bytes') throw new Error(`Invalid range type ${subranges.type}`);
if (subranges.length !== 1) throw new Error('Only a single range is supported');
const [{ start, end }] = subranges;
const contentLength = (end - start) + 1;
// Set headers for resumable download
res.status(206).set({
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': contentLength,
'Content-Type': 'application/octet-stream',
});
await pipeline(fs.createReadStream(filePath, { start, end }), res);
} else {
// Standard download without resuming
res.set({
// 'Content-Type': 'application/octet-stream',
'Content-Length': fileSize,
});
await pipeline(fs.createReadStream(filePath), res);
}
}
app.get('/api/download', asyncHandler(async (req, res) => {
const filePath = getFilePath(req.query.f);
const forceDownload = req.query.forceDownload === 'true';
const isDir = await isDirectory(filePath);
if (isDir) {
await serveDirZip(filePath, res);
} else {
const { range } = req.headers;
await serveResumableFileDownload({ filePath, range, res, forceDownload });
}
}));
app.get('/api/browse', asyncHandler(async (req, res) => {
const curRelPath = req.query.p || '/';
const curAbsPath = getFilePath(curRelPath);
let readdirEntries = await fs.readdir(curAbsPath);
readdirEntries = readdirEntries.sort(new Intl.Collator(undefined, {numeric: true}).compare);
const entries = (await pMap(readdirEntries, async (entry) => {
try {
const fileAbsPath = join(curAbsPath, entry); // TODO what if a file called ".."
const fileRelPath = join(curRelPath, entry);
const isDir = await isDirectory(fileAbsPath);
return {
path: fileRelPath,
isDir,
fileName: entry,
};
} catch (err) {
console.warn(err.message);
// https://github.com/mifi/ezshare/issues/29
return undefined;
}
}, { concurrency: 10 })).filter((f) => f);
res.send({
files: [
{ path: join(curRelPath, '..'), fileName: '..', isDir: true },
...entries
],
curRelPath,
sharedPath,
});
}));
console.log(`Sharing path ${sharedPath}`);
app.listen(port, () => {
const interfaces = os.networkInterfaces();
const urls = flatMap(Object.values(interfaces), (addresses) => addresses).filter(({ family, address }) => family === 'IPv4' && address !== '127.0.0.1').map(({ address }) => `http://${address}:${port}/`);
if (urls.length === 0) return;
console.log('Server listening:');
urls.forEach((url) => {
console.log();
console.log(`Scan this QR code on your phone or enter ${url}`);
console.log();
qrcode.generate(url);
});
if (urls.length > 1) {
console.log('Note that there are multiple QR codes above, one for each network interface. (scroll up)');
}
});
// Serving the frontend depending on dev/production
if (devMode) app.use('/', createProxyMiddleware({ target: 'http://localhost:3000', ws: true }));
else app.use('/', express.static(join(__dirname, 'ezshare-frontend/dist')));
// Default to index because SPA
app.use('*', (req, res) => res.sendFile(join(__dirname, 'ezshare-frontend/dist/index.html')));
};