fix: upload works in both Docker and dev mode

Falls back to absolute path on host when /app/screenshots doesn't exist.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Yusuf Suleman
2026-04-01 21:56:26 -05:00
parent f94987ac1b
commit e1f18cfb18
3 changed files with 33 additions and 17 deletions

View File

@@ -59,10 +59,13 @@
const fd = new FormData(); const fd = new FormData();
fd.append('file', file); fd.append('file', file);
try { try {
await fetch('/upload', { method: 'POST', body: fd, credentials: 'include' }); const res = await fetch('/upload', { method: 'POST', body: fd, credentials: 'include' });
uploadStatus = 'done'; const data = await res.json();
setTimeout(() => uploadStatus = '', 2000); console.log('Upload result:', data);
} catch { uploadStatus = data.ok ? 'done' : '';
if (data.ok) setTimeout(() => uploadStatus = '', 2000);
} catch (e) {
console.error('Upload error:', e);
uploadStatus = ''; uploadStatus = '';
} }
} }

View File

@@ -3,23 +3,36 @@ import type { RequestHandler } from './$types';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
const UPLOAD_DIR = '/app/screenshots'; function getUploadDir(): string {
// Docker: /app/screenshots (mounted volume)
if (fs.existsSync('/app/screenshots')) return '/app/screenshots';
// Dev: platform/screenshots (relative to project)
const devPath = '/media/yusiboyz/Media/Scripts/platform/screenshots';
if (!fs.existsSync(devPath)) fs.mkdirSync(devPath, { recursive: true });
return devPath;
}
export const POST: RequestHandler = async ({ request }) => { export const POST: RequestHandler = async ({ request }) => {
const formData = await request.formData(); try {
const file = formData.get('file') as File; const formData = await request.formData();
if (!file) return json({ error: 'No file' }, { status: 400 }); const file = formData.get('file') as File;
if (!file) return json({ error: 'No file provided' }, { status: 400 });
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19); const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
const ext = file.name.split('.').pop() || 'png'; const ext = file.name?.split('.').pop() || 'png';
const filename = `${timestamp}.${ext}`; const filename = `${timestamp}.${ext}`;
const buffer = Buffer.from(await file.arrayBuffer()); const arrayBuffer = await file.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
// Ensure directory exists const uploadDir = getUploadDir();
if (!fs.existsSync(UPLOAD_DIR)) fs.mkdirSync(UPLOAD_DIR, { recursive: true }); const fullPath = path.join(uploadDir, filename);
fs.writeFileSync(fullPath, buffer);
fs.writeFileSync(path.join(UPLOAD_DIR, filename), buffer); console.log(`Screenshot saved: ${fullPath} (${buffer.length} bytes)`);
return json({ ok: true, filename, size: buffer.length });
return json({ ok: true, filename, path: `/app/screenshots/${filename}` }); } catch (err: any) {
console.error('Upload failed:', err);
return json({ error: err.message || 'Upload failed' }, { status: 500 });
}
}; };

0
screenshots/test.txt Normal file
View File