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:
@@ -59,10 +59,13 @@
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
try {
|
||||
await fetch('/upload', { method: 'POST', body: fd, credentials: 'include' });
|
||||
uploadStatus = 'done';
|
||||
setTimeout(() => uploadStatus = '', 2000);
|
||||
} catch {
|
||||
const res = await fetch('/upload', { method: 'POST', body: fd, credentials: 'include' });
|
||||
const data = await res.json();
|
||||
console.log('Upload result:', data);
|
||||
uploadStatus = data.ok ? 'done' : '';
|
||||
if (data.ok) setTimeout(() => uploadStatus = '', 2000);
|
||||
} catch (e) {
|
||||
console.error('Upload error:', e);
|
||||
uploadStatus = '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,23 +3,36 @@ import type { RequestHandler } from './$types';
|
||||
import fs from 'fs';
|
||||
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 }) => {
|
||||
const formData = await request.formData();
|
||||
const file = formData.get('file') as File;
|
||||
if (!file) return json({ error: 'No file' }, { status: 400 });
|
||||
try {
|
||||
const formData = await request.formData();
|
||||
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 ext = file.name.split('.').pop() || 'png';
|
||||
const filename = `${timestamp}.${ext}`;
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
|
||||
const ext = file.name?.split('.').pop() || 'png';
|
||||
const filename = `${timestamp}.${ext}`;
|
||||
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const buffer = Buffer.from(arrayBuffer);
|
||||
|
||||
// Ensure directory exists
|
||||
if (!fs.existsSync(UPLOAD_DIR)) fs.mkdirSync(UPLOAD_DIR, { recursive: true });
|
||||
const uploadDir = getUploadDir();
|
||||
const fullPath = path.join(uploadDir, filename);
|
||||
fs.writeFileSync(fullPath, buffer);
|
||||
|
||||
fs.writeFileSync(path.join(UPLOAD_DIR, filename), buffer);
|
||||
|
||||
return json({ ok: true, filename, path: `/app/screenshots/${filename}` });
|
||||
console.log(`Screenshot saved: ${fullPath} (${buffer.length} bytes)`);
|
||||
return json({ ok: true, filename, size: buffer.length });
|
||||
} catch (err: any) {
|
||||
console.error('Upload failed:', err);
|
||||
return json({ error: err.message || 'Upload failed' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user