57 lines
2.4 KiB
TypeScript
57 lines
2.4 KiB
TypeScript
|
|
import { json, error } from '@sveltejs/kit';
|
||
|
|
import type { RequestHandler } from './$types';
|
||
|
|
import { env } from '$env/dynamic/public';
|
||
|
|
|
||
|
|
const API_BASE = env.PUBLIC_POST_IMAGE;
|
||
|
|
|
||
|
|
// Edit a web-managed main-page video: change date range and/or replace the .mp4.
|
||
|
|
export const POST: RequestHandler = async ({ request }) => {
|
||
|
|
try {
|
||
|
|
const formData = await request.formData();
|
||
|
|
|
||
|
|
const slug = formData.get('slug') as string;
|
||
|
|
const uid = formData.get('uid') as string;
|
||
|
|
const displayName = formData.get('displayName') as string;
|
||
|
|
const email = formData.get('email') as string;
|
||
|
|
const country = (formData.get('country') as string) || 'tha';
|
||
|
|
const start = formData.get('start') as string;
|
||
|
|
const end = (formData.get('end') as string) || 'NONE';
|
||
|
|
const name = formData.get('name') as string | null;
|
||
|
|
const brewingDuration = formData.get('brewing_duration') as string | null;
|
||
|
|
const video = formData.get('video') as File | null;
|
||
|
|
const brewingVideo = formData.get('brewing_video') as File | null;
|
||
|
|
const brewingTxt = formData.get('brewing_txt') as File | null;
|
||
|
|
const brewingTxtEn = formData.get('brewing_txt_en') as File | null;
|
||
|
|
|
||
|
|
if (!slug || !uid || !displayName || !email || !start) {
|
||
|
|
throw error(400, 'Missing required fields');
|
||
|
|
}
|
||
|
|
|
||
|
|
const endpoint =
|
||
|
|
`${API_BASE}/video/mainpage/update/${encodeURIComponent(slug)}/${encodeURIComponent(uid)}` +
|
||
|
|
`/${encodeURIComponent(displayName)}/${encodeURIComponent(email)}`;
|
||
|
|
|
||
|
|
const upstream = new FormData();
|
||
|
|
upstream.append('country', country);
|
||
|
|
upstream.append('start', start);
|
||
|
|
upstream.append('end', end);
|
||
|
|
if (name) upstream.append('name', name);
|
||
|
|
if (brewingDuration) upstream.append('brewing_duration', brewingDuration);
|
||
|
|
if (video) upstream.append('video', video);
|
||
|
|
if (brewingVideo) upstream.append('brewing_video', brewingVideo);
|
||
|
|
if (brewingTxt) upstream.append('brewing_txt', brewingTxt);
|
||
|
|
if (brewingTxtEn) upstream.append('brewing_txt_en', brewingTxtEn);
|
||
|
|
|
||
|
|
const response = await fetch(endpoint, { method: 'POST', body: upstream });
|
||
|
|
if (!response.ok) {
|
||
|
|
const e = await response.json().catch(() => ({ detail: response.statusText }));
|
||
|
|
throw error(response.status, e.detail || 'Update main-page video failed');
|
||
|
|
}
|
||
|
|
return json(await response.json());
|
||
|
|
} catch (err) {
|
||
|
|
console.error('[Video MainPage Update Proxy] Error:', err);
|
||
|
|
if (err && typeof err === 'object' && 'status' in err) throw err;
|
||
|
|
throw error(500, err instanceof Error ? err.message : 'Internal server error');
|
||
|
|
}
|
||
|
|
};
|