import { getPresignedUrl } from '@/lib/aws'; import { withLogging } from '../../../lib/logger'; import { nanoid } from '@/lib/utils'; /** * @param req * @returns */ export const POST = withLogging( async ( session, json: { id?: string; fileName: string; fileType: string; }, ): Promise => { const user = session?.user?.email ?? 'anonymous'; // if (!email) { // return new Response('Unauthorized', { // status: 401, // }); // } try { const { fileName, fileType, id = nanoid() } = json; const signedFileName = `${user}/${id}/${fileName}`; const res = await getPresignedUrl(signedFileName, fileType); return Response.json({ id, signedUrl: res.url, publicUrl: `https://${process.env.AWS_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${signedFileName}`, fields: res.fields, }); } catch (error) { return new Response((error as Error).message, { status: 400, }); } }, );