52 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

2024-02-14 23:48:46 -05:00
import { getAccessToken, withApiAuthRequired } from '@auth0/nextjs-auth0';
import { IncomingForm } from 'formidable'
import fs from 'fs/promises';
2024-02-14 23:48:46 -05:00
export const config = {
api: {
bodyParser: false,
},
};
async function createBlobFromFile(path: string): Promise<Blob> {
const file = await fs.readFile(path);
return new Blob([file]);
}
2024-02-14 23:48:46 -05:00
export default withApiAuthRequired(async function handler(req, res) {
const { accessToken } = await getAccessToken(req, res);
const form = new IncomingForm();
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'
2024-02-14 23:48:46 -05:00
try {
const response = await fetch(process.env.NEXT_PUBLIC_API_URL + '/api/Artist/Portfolio', {
2024-02-14 23:48:46 -05:00
method: 'POST',
headers: {
"Authorization": `Bearer ${accessToken}`,
"Content-Type": " application/octet-stream"
2024-02-14 23:48:46 -05:00
},
body: await createBlobFromFile(file[0].filepath) // Don't set Content-Type, FormData will handle it
2024-02-14 23:48:46 -05:00
});
2024-02-15 21:49:12 -05:00
(response)
2024-02-14 23:48:46 -05:00
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' });
}
});
});