-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve-zip.js
More file actions
49 lines (44 loc) · 1.5 KB
/
Copy pathserve-zip.js
File metadata and controls
49 lines (44 loc) · 1.5 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
// Temporary HTTP server to serve the zip for the media server
const http = require('http');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// Create tar-like archive using a simple approach: serve individual files
const BASE = __dirname;
const PORT = 9999;
// Collect all files
function getFiles(dir, base = '') {
const entries = fs.readdirSync(dir, { withFileTypes: true });
let files = [];
for (const e of entries) {
const rel = base ? `${base}/${e.name}` : e.name;
if (e.name === 'node_modules' || e.name === '.git') continue;
if (e.isDirectory()) {
files.push(...getFiles(path.join(dir, e.name), rel));
} else {
files.push(rel);
}
}
return files;
}
const server = http.createServer((req, res) => {
if (req.url === '/files.json') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(getFiles(BASE)));
return;
}
const filePath = path.join(BASE, decodeURIComponent(req.url));
if (fs.existsSync(filePath) && !filePath.includes('node_modules')) {
res.writeHead(200);
fs.createReadStream(filePath).pipe(res);
} else {
res.writeHead(404);
res.end('Not found');
}
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`Serving aya-expo-tools on http://0.0.0.0:${PORT}`);
console.log('Media server can download from: http://192.168.15.146:' + PORT);
console.log('Or via WireGuard: http://10.253.0.1:' + PORT);
console.log('\nPress Ctrl+C to stop');
});