56 lines
1.9 KiB
TypeScript
56 lines
1.9 KiB
TypeScript
|
|
import { json, error } from '@sveltejs/kit';
|
||
|
|
import type { RequestHandler } from './$types';
|
||
|
|
import { env } from '$env/dynamic/public';
|
||
|
|
|
||
|
|
// Adv videos are served by the same taobin_image service as menu images.
|
||
|
|
const ADV_API_BASE = env.PUBLIC_POST_IMAGE;
|
||
|
|
|
||
|
|
export const POST: RequestHandler = async ({ request }) => {
|
||
|
|
try {
|
||
|
|
const formData = await request.formData();
|
||
|
|
|
||
|
|
const country = formData.get('country') as string;
|
||
|
|
const uid = formData.get('uid') as string;
|
||
|
|
const displayName = formData.get('displayName') as string;
|
||
|
|
const email = formData.get('email') as string;
|
||
|
|
const file = formData.get('file') as File;
|
||
|
|
// 'false' when the manifest is generated on a machine (method 2).
|
||
|
|
const regenerate = (formData.get('regenerate') as string) ?? 'true';
|
||
|
|
|
||
|
|
if (!country || !uid || !displayName || !email || !file) {
|
||
|
|
throw error(400, 'Missing required fields');
|
||
|
|
}
|
||
|
|
|
||
|
|
const endpoint =
|
||
|
|
`${ADV_API_BASE}/adv/upload/${encodeURIComponent(country)}/${encodeURIComponent(uid)}/${encodeURIComponent(displayName)}/${encodeURIComponent(email)}` +
|
||
|
|
`?regenerate=${encodeURIComponent(regenerate)}`;
|
||
|
|
|
||
|
|
console.log('[Adv Upload Proxy] Endpoint:', endpoint, 'file:', file.name);
|
||
|
|
|
||
|
|
// Upstream expects the multipart field name `files`.
|
||
|
|
const uploadFormData = new FormData();
|
||
|
|
uploadFormData.append('files', file);
|
||
|
|
|
||
|
|
const response = await fetch(endpoint, {
|
||
|
|
method: 'POST',
|
||
|
|
body: uploadFormData
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
const errorData = await response.json().catch(() => ({ detail: response.statusText }));
|
||
|
|
throw error(response.status, errorData.detail || 'Upload failed');
|
||
|
|
}
|
||
|
|
|
||
|
|
const result = await response.json();
|
||
|
|
return json(result);
|
||
|
|
} catch (err) {
|
||
|
|
console.error('[Adv Upload Proxy] Error:', err);
|
||
|
|
|
||
|
|
if (err && typeof err === 'object' && 'status' in err) {
|
||
|
|
throw err;
|
||
|
|
}
|
||
|
|
|
||
|
|
throw error(500, err instanceof Error ? err.message : 'Internal server error');
|
||
|
|
}
|
||
|
|
};
|