mirror of
https://github.com/D4M13N-D3V/comissions-app-ui.git
synced 2025-03-14 08:15:08 +00:00
53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
![]() |
import { getAccessToken, withApiAuthRequired } from '@auth0/nextjs-auth0';
|
||
|
import { IncomingForm } from 'formidable'
|
||
|
import fs from 'fs/promises';
|
||
|
|
||
|
export const config = {
|
||
|
api: {
|
||
|
bodyParser: false,
|
||
|
},
|
||
|
};
|
||
|
async function createBlobFromFile(path: string): Promise<Blob> {
|
||
|
const file = await fs.readFile(path);
|
||
|
return new Blob([file]);
|
||
|
}
|
||
|
|
||
|
export default withApiAuthRequired(async function handler(req, res) {
|
||
|
const { accessToken } = await getAccessToken(req, res);
|
||
|
|
||
|
const form = new IncomingForm();
|
||
|
const requestId = req.query.requestId;
|
||
|
|
||
|
form.parse(req, async (err, fields, files) => {
|
||
|
if (err) {
|
||
|
console.error('Error parsing form:', err);
|
||
|
res.status(500).json({ error: 'Error parsing form' });
|
||
|
return;
|
||
|
}
|
||
|
const file = files["newImage"]; // Assuming your file input field name is 'file'
|
||
|
try {
|
||
|
|
||
|
const response = await fetch(process.env.NEXT_PUBLIC_API_URL+'/api/Requests/Artist/'+requestId+'/Assets', {
|
||
|
method: 'POST',
|
||
|
headers: {
|
||
|
"Authorization": `Bearer ${accessToken}`,
|
||
|
"Content-Type": " application/octet-stream"
|
||
|
},
|
||
|
body: await createBlobFromFile(file[0].filepath) // Don't set Content-Type, FormData will handle it
|
||
|
});
|
||
|
|
||
|
(response)
|
||
|
if (!response.ok) {
|
||
|
const errorData = await response.json();
|
||
|
throw new Error(errorData.message || 'Failed to upload file');
|
||
|
}
|
||
|
|
||
|
const responseData = await response.json();
|
||
|
res.status(200).json(responseData);
|
||
|
} catch (error) {
|
||
|
console.error('Error uploading file:', error);
|
||
|
res.status(500).json({ error: 'Error uploading file' });
|
||
|
}
|
||
|
});
|
||
|
});
|