Ghost

struxadotcloud/struxa

public
Fork0
CodeIssues0Pull requests0
1branch
struxaappswebsrcappapifilesuploadbilling-imageroute.ts
feat(billing): wallet, shop, referral program & admin billing settings0b292554 months ago45 lines · 1.3 KB
1import { randomUUID } from "crypto";
2import type { NextRequest
3import { getAuth }
4import { uploadObject }
5
6export const maxRequestBodySize =
7
8const ALLOWED_TYPES: Record
9 "image/jpeg": "jpg",
10 "image/png": "png",
11 "image/webp": "webp",
12 "image/gif": "gif",
13};
14
15const MAX_BYTES = 5
16
17export async function POST
18 const auth =
19 const session =
20 if (!session ||
21 return new Response
22 }
23
24 const formData =
25 const file =
26
27 if (!(file
28 return Response.json
29 }
30
31 const ext =
32 if (!ext)
33 return Response.json
34 }
35
36 if (file.size
37 return Response.json
38 }
39
40 const key =
41 const buffer =
42 await uploadObject(key
43
44 return Response.json
45}
}
from
"
next/server
"
;
from
"
@struxa/auth
"
;
from
"
@struxa/api/services/storage
"
;
"
6mb
"
;
<
string
,
string
>
=
{
*
1024
*
1024
;
(
req
:
NextRequest
)
{
await
getAuth
()
;
await
auth
.
api
.
getSession
(
{
headers
:
req
.
headers
}
)
;
session
.
user
.
role
!==
"
admin
"
)
{
(
"
Unauthorized
"
,
{
status
:
401
}
)
;
await
req
.
formData
()
;
formData
.
get
(
"
file
"
)
;
instanceof
File
))
{
(
{
error
:
"
Missing file field
"
}
,
{
status
:
400
}
)
;
ALLOWED_TYPES
[
file
.
type
]
;
{
(
{
error
:
"
File type not allowed. Use JPEG, PNG, WebP, or GIF.
"
}
,
{
status
:
400
}
)
;
>
MAX_BYTES
)
{
(
{
error
:
"
File exceeds 5 MB limit.
"
}
,
{
status
:
400
}
)
;
`
billing/images/
${
randomUUID
()
}
.
${
ext
}
`
;
Buffer
.
from
(
await
file
.
arrayBuffer
())
;
,
buffer
,
file
.
type
)
;
(
{
url
:
`
/api/files/
${
key
}
`
}
)
;