-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
195 lines (177 loc) · 5.07 KB
/
Copy pathserver.js
File metadata and controls
195 lines (177 loc) · 5.07 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
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
import { createServer } from 'node:http';
import { readFileSync, existsSync, statSync } from 'node:fs';
import { join, extname, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { ReadOnlyDatabase } from './converter/source_reader.js';
import { listPlaylists } from './converter/playlist.js';
import { doConvert } from './convert.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const PORT = 8787;
const DATABASE = 'databases/m.db';
const CONTENT_TYPES = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.xml': 'application/xml',
};
let conversionState = {
status: 'idle',
progress: 0,
message: '',
output_path: '',
error: null,
};
function runConversion(selectedPlaylistNames, dbPath = DATABASE) {
conversionState = {
status: 'running',
progress: 10,
message: 'Converting...',
output_path: '',
error: null,
};
try {
const { outputPath, trackCount, playlistCount } = doConvert(
selectedPlaylistNames, undefined, {}, dbPath
);
conversionState = {
status: 'done',
progress: 100,
message: `Conversion complete! ${outputPath} (${trackCount} tracks, ${playlistCount} playlists)`,
output_path: outputPath,
error: null,
};
} catch (e) {
conversionState = {
status: 'error',
progress: 0,
message: e.message,
output_path: '',
error: e.message,
};
}
}
function jsonResponse(res, code, data) {
const body = JSON.stringify(data);
res.writeHead(code, {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
'Access-Control-Allow-Origin': '*',
});
res.end(body);
}
function sendFile(res, filePath) {
if (!existsSync(filePath) || !statSync(filePath).isFile()) {
res.writeHead(404);
res.end('Not found');
return;
}
const data = readFileSync(filePath);
res.writeHead(200, {
'Content-Type': CONTENT_TYPES[extname(filePath)] || 'application/octet-stream',
'Content-Length': data.length,
});
res.end(data);
}
function serveReactApp(res, path) {
const uiDir = join(__dirname, 'dist');
const reqPath = path === '/' || path === '' ? '/index.html' : path;
const filePath = join(uiDir, reqPath.replace(/^\//, ''));
if (existsSync(filePath) && statSync(filePath).isFile()) {
sendFile(res, filePath);
return;
}
const index = join(uiDir, 'index.html');
if (existsSync(index)) {
sendFile(res, index);
} else {
res.writeHead(404);
res.end('UI not built. Run: npm run ui:build');
}
}
function serveFile(res, path) {
const filePath = join(__dirname, path.replace(/^\//, ''));
if (existsSync(filePath) && statSync(filePath).isFile()) {
sendFile(res, filePath);
} else {
res.writeHead(404);
res.end('Not found');
}
}
function handlePlaylists(req, res) {
const url = new URL(req.url, `http://localhost:${PORT}`);
const dbPath = url.searchParams.get('db') || DATABASE;
try {
const db = new ReadOnlyDatabase(dbPath);
db.openSync();
try {
jsonResponse(res, 200, { playlists: listPlaylists(db) });
} finally {
db.close();
}
} catch (e) {
jsonResponse(res, 500, { error: e.message });
}
}
function handleConvert(req, res) {
let body = '';
req.on('data', chunk => { body += chunk; });
req.on('end', () => {
let data;
try {
data = JSON.parse(body);
} catch {
jsonResponse(res, 400, { error: 'Invalid JSON' });
return;
}
const selected = [...new Set(data.playlists || [])];
const dbPath = data.db || DATABASE;
if (!selected.length) {
jsonResponse(res, 400, { error: 'No playlists selected' });
return;
}
if (conversionState.status === 'running') {
jsonResponse(res, 409, { error: 'Conversion already in progress' });
return;
}
setImmediate(() => runConversion(selected, dbPath));
jsonResponse(res, 200, { message: 'Conversion started' });
});
}
const server = createServer((req, res) => {
const url = new URL(req.url, `http://localhost:${PORT}`);
const path = url.pathname;
if (req.method === 'OPTIONS') {
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
});
res.end();
return;
}
if (req.method === 'GET') {
if (path === '/api/playlists') handlePlaylists(req, res);
else if (path === '/api/status') jsonResponse(res, 200, conversionState);
else if (path.startsWith('/output/')) serveFile(res, path);
else serveReactApp(res, path);
} else if (req.method === 'POST' && path === '/api/convert') {
handleConvert(req, res);
} else {
res.writeHead(404);
res.end('Not found');
}
});
const isMain = process.argv[1] && (
process.argv[1].endsWith('server.js') ||
process.argv[1].endsWith('server')
);
if (isMain) {
server.listen(PORT, () => {
console.log(`EngineBox running at http://localhost:${PORT}`);
});
}
export { server };