Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```
- import { env } from '@/lib/config/env';
- import { NextRequest, NextResponse } from 'next/server';
- const ALLOWED_DOMAINS = ['domain1', 'domain2'];
- function getDomainFromOrigin(origin: string): string | null {
- try {
- const url = new URL(origin);
- return url.hostname;
- } catch {
- return null;
- }
- }
- export async function POST(request: NextRequest) {
- try {
- const domain = getDomainFromOrigin(request.headers.get('origin') || '');
- if (!domain || !ALLOWED_DOMAINS.includes(domain)) {
- console.log('domain not allowed', domain);
- return new Response(JSON.stringify({ error: 'Origin not allowed' }), {
- status: 403,
- headers: {
- 'Content-Type': 'application/json',
- 'Access-Control-Allow-Origin': 'null',
- },
- });
- }
- const formData = await request.formData();
- const video = formData.get('video') as File;
- if (!video) {
- console.warn('[VideoUpload] Missing required fields');
- return NextResponse.json(
- { error: 'Missing required fields' },
- { status: 400 },
- );
- }
- const upstreamForm = new FormData();
- upstreamForm.append('video', video);
- const response = await fetch(
- `${BACKEND_ENDPOINT}/api/tasks/verify-video-task`,
- {
- method: 'POST',
- headers: {
- 'x-twitter-api-key': env.API_KEY,
- },
- body: upstreamForm,
- },
- );
- const text = await response.text();
- console.log('[VideoUpload] BE response status:', response.status);
- console.log('[VideoUpload] BE response body:', text);
- try {
- const data = JSON.parse(text);
- return NextResponse.json(
- {
- success: data.success ?? false,
- verified: data.verified ?? false,
- message:
- data.message ||
- 'Video verification failed. Please try again with a clearer recording.',
- },
- { status: response.status },
- );
- } catch (err) {
- return NextResponse.json(
- {
- success: false,
- error: 'Invalid response from backend',
- },
- { status: 500 },
- );
- }
- } catch (err) {
- console.error('[VideoUpload] Unexpected error:', err);
- return NextResponse.json(
- { error: 'Internal server error' },
- { status: 500 },
- );
- }
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment