diff --git a/src/actions/upload-to-cdn.ts b/src/actions/upload-to-cdn.ts new file mode 100644 index 00000000..935d0754 --- /dev/null +++ b/src/actions/upload-to-cdn.ts @@ -0,0 +1,42 @@ +'use server'; + +import { v4 as uuidv4 } from 'uuid'; + +export async function uploadFileAction(formData: FormData) { + const CDN_BASE_UPLOAD_URL = process.env.CDN_BASE_UPLOAD_URL!; + const CDN_BASE_ACCESS_URL = process.env.CDN_BASE_ACCESS_URL!; + const CDN_API_KEY = process.env.CDN_API_KEY!; + + try { + const file = formData.get('file') as File; + const uniqueFileName = formData.get('uniqueFileName') || uuidv4(); // Generate unique key if not provided + + if (!file) { + return { error: 'File is required', status: 400 }; + } + + const uploadUrl = `${CDN_BASE_UPLOAD_URL}/${uniqueFileName}`; + const fileBuffer = Buffer.from(await file.arrayBuffer()); + + const response = await fetch(uploadUrl, { + method: 'PUT', + headers: { + AccessKey: CDN_API_KEY, + 'Content-Type': 'application/octet-stream', + }, + body: fileBuffer, + }); + + if (response.ok) { + return { + message: 'File uploaded successfully', + url: `${CDN_BASE_ACCESS_URL}/${uniqueFileName}`, + }; + } else { + return { error: 'Failed to upload file', status: response.status }; + } + } catch (error) { + console.error('Error uploading file:', error); + return { error: 'Internal server error', status: 500 }; + } +} diff --git a/src/app/api/upload-to-cdn/route.ts b/src/app/api/upload-to-cdn/route.ts deleted file mode 100644 index 9f34d64a..00000000 --- a/src/app/api/upload-to-cdn/route.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { serverEnv } from '@/env/server'; -import { NextResponse } from 'next/server'; -import { v4 as uuidv4 } from 'uuid'; - -const CDN_BASE_UPLOAD_URL = serverEnv.CDN_BASE_UPLOAD_URL; -const CDN_BASE_ACCESS_URL = serverEnv.CDN_BASE_ACCESS_URL; -const CDN_API_KEY = serverEnv.CDN_API_KEY!; - -export async function POST(req: Request): Promise { - try { - const formData = await req.formData(); - const file = formData.get('file') as File; - const uniqueFileName = formData.get('uniqueFileName') || uuidv4(); // Generate unique key if not provided - - if (!file) { - return NextResponse.json({ error: 'File is required' }, { status: 400 }); - } - - const uploadUrl = `${CDN_BASE_UPLOAD_URL}/${uniqueFileName}`; - - const fileBuffer = Buffer.from(await file.arrayBuffer()); - - const response = await fetch(uploadUrl, { - method: 'PUT', - headers: { - AccessKey: CDN_API_KEY, - 'Content-Type': 'application/octet-stream', - }, - body: fileBuffer, - }); - - if (response.ok) { - return NextResponse.json({ - message: 'File uploaded successfully', - url: `${CDN_BASE_ACCESS_URL}/${uniqueFileName}`, - }); - } else { - return NextResponse.json( - { error: 'Failed to upload file' }, - { status: response.status } - ); - } - } catch (error) { - console.error('Error uploading file to BunnyCDN:', error); - return NextResponse.json( - { error: 'Internal server error' }, - { status: 500 } - ); - } -} diff --git a/src/components/job-form.tsx b/src/components/job-form.tsx index 7e152626..06bfb1d6 100644 --- a/src/components/job-form.tsx +++ b/src/components/job-form.tsx @@ -32,6 +32,8 @@ import { FaFileUpload } from 'react-icons/fa'; import { Switch } from './ui/switch'; import { Label } from './ui/label'; import dynamic from 'next/dynamic'; +import { uploadFileAction } from '@/actions/upload-to-cdn'; + const DynamicLineDrawingAnimation = dynamic( () => import('./gmaps-autosuggest'), @@ -86,16 +88,12 @@ const PostJobForm = () => { const uniqueFileName = `${Date.now()}-${file.name}`; formData.append('uniqueFileName', uniqueFileName); - const res = await fetch(`/api/upload-to-cdn`, { - method: 'POST', - body: formData, - }); - - if (!res.ok) { + const res = await uploadFileAction(formData); + if (!res) { throw new Error('Failed to upload image'); } - const uploadRes = await res.json(); + const uploadRes = res; return uploadRes.url; } catch (error) { console.error('Image upload failed:', error);