80 lines
2.6 KiB
TypeScript
80 lines
2.6 KiB
TypeScript
import { json, error } from '@sveltejs/kit';
|
|
import type { RequestHandler } from './$types';
|
|
import { env } from '$env/dynamic/public';
|
|
|
|
// Main-page advertisement videos are wired into video/script1.ev by the same
|
|
// taobin_image service that handles menu images / promo catalogs.
|
|
const API_BASE = env.PUBLIC_POST_IMAGE;
|
|
|
|
export const POST: RequestHandler = async ({ request }) => {
|
|
try {
|
|
const formData = await request.formData();
|
|
|
|
const uid = formData.get('uid') as string;
|
|
const displayName = formData.get('displayName') as string;
|
|
const email = formData.get('email') as string;
|
|
const name = formData.get('name') as string;
|
|
const country = (formData.get('country') as string) || 'tha';
|
|
const start = formData.get('start') as string;
|
|
// 'NONE' means open-ended (no end date).
|
|
const end = (formData.get('end') as string) || 'NONE';
|
|
const machineNumbers = (formData.get('machine_numbers') as string) || '';
|
|
const brewingDuration = (formData.get('brewing_duration') as string) || '1';
|
|
const video = formData.get('video') as File;
|
|
const brewingVideo = formData.get('brewing_video') as File;
|
|
const brewingTxt = formData.get('brewing_txt') as File;
|
|
const brewingTxtEn = formData.get('brewing_txt_en') as File;
|
|
|
|
if (
|
|
!uid ||
|
|
!displayName ||
|
|
!email ||
|
|
!name ||
|
|
!start ||
|
|
!video ||
|
|
!brewingVideo ||
|
|
!brewingTxt ||
|
|
!brewingTxtEn
|
|
) {
|
|
throw error(400, 'Missing required fields');
|
|
}
|
|
|
|
const endpoint =
|
|
`${API_BASE}/video/mainpage/${encodeURIComponent(uid)}` +
|
|
`/${encodeURIComponent(displayName)}/${encodeURIComponent(email)}`;
|
|
|
|
logger.debug('[Video MainPage Proxy] Endpoint:', endpoint, 'name:', name);
|
|
|
|
const upstream = new FormData();
|
|
upstream.append('name', name);
|
|
upstream.append('country', country);
|
|
upstream.append('start', start);
|
|
upstream.append('end', end);
|
|
upstream.append('machine_numbers', machineNumbers);
|
|
upstream.append('brewing_duration', brewingDuration);
|
|
upstream.append('video', video);
|
|
upstream.append('brewing_video', brewingVideo);
|
|
upstream.append('brewing_txt', brewingTxt);
|
|
upstream.append('brewing_txt_en', brewingTxtEn);
|
|
|
|
const response = await fetch(endpoint, {
|
|
method: 'POST',
|
|
body: upstream
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({ detail: response.statusText }));
|
|
throw error(response.status, errorData.detail || 'Add main-page video failed');
|
|
}
|
|
|
|
return json(await response.json());
|
|
} catch (err) {
|
|
console.error('[Video MainPage Proxy] Error:', err);
|
|
|
|
if (err && typeof err === 'object' && 'status' in err) {
|
|
throw err;
|
|
}
|
|
|
|
throw error(500, err instanceof Error ? err.message : 'Internal server error');
|
|
}
|
|
};
|