feat: GET /images/<filename> direct download + download_url in response
This commit is contained in:
1 parent
f676f6c098
commit
08a26138f3
1 file changed
+31
-1
+31
-1
@@ -500,6 +500,7 @@ function registerHandlers(srv) {
|
||||
tool: name, model, provider, mime, aspect, transparent,
|
||||
refs_used: imageUrls.length, filepath, filename, bytes: size,
|
||||
latency_ms: Date.now() - t0,
|
||||
download_url: `https://img-gen-ai.dynv6.net/images/${filename}`,
|
||||
}, null, 2) },
|
||||
],
|
||||
};
|
||||
@@ -518,7 +519,7 @@ function registerHandlers(srv) {
|
||||
return {
|
||||
content: [
|
||||
{ type: 'image', mimeType: mime, data: buf.toString('base64') },
|
||||
{ type: 'text', text: JSON.stringify({ filename, filepath, bytes: buf.length, mime }, null, 2) },
|
||||
{ type: 'text', text: JSON.stringify({ filename, filepath, bytes: buf.length, mime, download_url: `https://img-gen-ai.dynv6.net/images/${filename}` }, null, 2) },
|
||||
],
|
||||
};
|
||||
}
|
||||
@@ -580,6 +581,35 @@ const httpServer = http.createServer(async (req, res) => {
|
||||
process.stderr.write(`[HTTP] ${req.method} ${url.pathname} -> incoming\n`);
|
||||
|
||||
try {
|
||||
// ---- Serve saved images directly (GET /images/<filename>) ----
|
||||
if (url.pathname.startsWith('/images/') && req.method === 'GET') {
|
||||
const filename = url.pathname.slice('/images/'.length);
|
||||
// Security: prevent path traversal
|
||||
if (!filename || filename.includes('/') || filename.includes('\\') || filename.includes('..')) {
|
||||
res.statusCode = 400;
|
||||
res.setHeader('content-type', 'application/json');
|
||||
res.end(JSON.stringify({ error: 'invalid filename' }));
|
||||
return;
|
||||
}
|
||||
const filepath = path.join(OUTPUT_DIR, filename);
|
||||
try {
|
||||
const buf = await fs.readFile(filepath);
|
||||
const ext = path.extname(filename).toLowerCase();
|
||||
const mime = ext === '.png' ? 'image/png' : ext === '.jpg' || ext === '.jpeg' ? 'image/jpeg' : ext === '.webp' ? 'image/webp' : 'application/octet-stream';
|
||||
res.statusCode = 200;
|
||||
res.setHeader('content-type', mime);
|
||||
res.setHeader('content-length', buf.length);
|
||||
res.setHeader('cache-control', 'public, max-age=3600');
|
||||
res.end(buf);
|
||||
} catch (e) {
|
||||
res.statusCode = 404;
|
||||
res.setHeader('content-type', 'application/json');
|
||||
res.end(JSON.stringify({ error: 'file not found' }));
|
||||
}
|
||||
process.stderr.write(`[image] GET ${filename} -> ${res.statusCode}\n`);
|
||||
return;
|
||||
}
|
||||
|
||||
// ---- Traditional SSE transport (for Figma) ----
|
||||
if (url.pathname === '/sse' && req.method === 'GET') {
|
||||
const sseTransport = new SSEServerTransport('/messages', res);
|
||||
|
||||
Reference in new issue
Block a user