-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters