0x0x230x

Untitled

Jul 14th, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. ```
  2. import { env } from '@/lib/config/env';
  3. import { NextRequest, NextResponse } from 'next/server';
  4.  
  5. const ALLOWED_DOMAINS = ['domain1', 'domain2'];
  6.  
  7. function getDomainFromOrigin(origin: string): string | null {
  8. try {
  9. const url = new URL(origin);
  10. return url.hostname;
  11. } catch {
  12. return null;
  13. }
  14. }
  15.  
  16. export async function POST(request: NextRequest) {
  17. try {
  18. const domain = getDomainFromOrigin(request.headers.get('origin') || '');
  19.  
  20. if (!domain || !ALLOWED_DOMAINS.includes(domain)) {
  21. console.log('domain not allowed', domain);
  22. return new Response(JSON.stringify({ error: 'Origin not allowed' }), {
  23. status: 403,
  24. headers: {
  25. 'Content-Type': 'application/json',
  26. 'Access-Control-Allow-Origin': 'null',
  27. },
  28. });
  29. }
  30. const formData = await request.formData();
  31.  
  32. const video = formData.get('video') as File;
  33.  
  34.  
  35. if (!video) {
  36. console.warn('[VideoUpload] Missing required fields');
  37. return NextResponse.json(
  38. { error: 'Missing required fields' },
  39. { status: 400 },
  40. );
  41. }
  42.  
  43. const upstreamForm = new FormData();
  44. upstreamForm.append('video', video);
  45.  
  46. const response = await fetch(
  47. `${BACKEND_ENDPOINT}/api/tasks/verify-video-task`,
  48. {
  49. method: 'POST',
  50. headers: {
  51. 'x-twitter-api-key': env.API_KEY,
  52. },
  53. body: upstreamForm,
  54. },
  55. );
  56.  
  57. const text = await response.text();
  58. console.log('[VideoUpload] BE response status:', response.status);
  59. console.log('[VideoUpload] BE response body:', text);
  60.  
  61. try {
  62. const data = JSON.parse(text);
  63.  
  64. return NextResponse.json(
  65. {
  66. success: data.success ?? false,
  67. verified: data.verified ?? false,
  68. message:
  69. data.message ||
  70. 'Video verification failed. Please try again with a clearer recording.',
  71. },
  72. { status: response.status },
  73. );
  74. } catch (err) {
  75. return NextResponse.json(
  76. {
  77. success: false,
  78. error: 'Invalid response from backend',
  79. },
  80. { status: 500 },
  81. );
  82. }
  83. } catch (err) {
  84. console.error('[VideoUpload] Unexpected error:', err);
  85. return NextResponse.json(
  86. { error: 'Internal server error' },
  87. { status: 500 },
  88. );
  89. }
  90. }
  91.  
  92. ```
Advertisement
Add Comment
Please, Sign In to add comment