File size: 1,001 Bytes
6b8f69a
 
 
 
 
 
 
 
 
8e3dbd3
 
 
 
 
 
 
6b8f69a
8e3dbd3
5d7d435
 
8e3dbd3
 
 
6b8f69a
5d7d435
8e3dbd3
 
 
 
 
 
 
 
 
 
 
 
6b8f69a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { auth } from '@/auth';
import { getPresignedUrl } from '@/lib/aws';
import { nanoid } from '@/lib/utils';

/**
 * @param req
 * @returns
 */
export async function POST(req: Request): Promise<Response> {
  const session = await auth();
  const user = session?.user?.email ?? 'anonymous';
  // if (!email) {
  //   return new Response('Unauthorized', {
  //     status: 401,
  //   });
  // }

  try {
    const { fileName, fileType, id } = (await req.json()) as {
      id?: string;
      fileName: string;
      fileType: string;
    };

    const signedFileName = `${user}/${id ?? nanoid()}/${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,
    });
  }
}