Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build env fix #398

Merged
merged 8 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/actions/upload-to-cdn.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
}
50 changes: 0 additions & 50 deletions src/app/api/upload-to-cdn/route.ts

This file was deleted.

12 changes: 5 additions & 7 deletions src/components/job-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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);
Expand Down
Loading