Skip to content

Commit f0453db

Browse files
committed
fix: upload need token
Signed-off-by: Innei <[email protected]>
1 parent acdd7f6 commit f0453db

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/app/api/s3/route.ts

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type { NextRequest } from 'next/server'
33

44
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3'
55

6+
import { apiClient } from '~/lib/request'
7+
68
const config = {
79
accountId: 'de7ecb0eaa0a328071255d557a6adb66',
810
accessKeyId: process.env.S3_ACCESS_KEY as string,
@@ -34,6 +36,17 @@ async function uploadToS3(path: string, body: Buffer, contentType: string) {
3436
export const POST = async (req: NextRequest) => {
3537
const formData = await req.formData()
3638
const file = formData.get('file')
39+
const token = formData.get('token') as string
40+
41+
if (!token) {
42+
return NextResponse.json({ error: 'No token received.' }, { status: 400 })
43+
}
44+
const { isGuest } = await apiClient.user.checkTokenValid(token)
45+
46+
if (isGuest) {
47+
return NextResponse.json({ error: 'Invalid token.' }, { status: 401 })
48+
}
49+
3750
if (!file) {
3851
return NextResponse.json({ error: 'No files received.' }, { status: 400 })
3952
}

src/components/ui/editor/Milkdown/plugins/Image.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { FloatPopover } from '~/components/ui/float-popover'
1313
import { Form, FormInput } from '~/components/ui/form'
1414
import { FixedZoomedImage } from '~/components/ui/image'
1515
import { useCurrentModal, useModalStack } from '~/components/ui/modal'
16+
import { getToken } from '~/lib/cookie'
1617
import { toast } from '~/lib/toast'
1718

1819
const base64ToFile = (base64: string) => {
@@ -37,6 +38,7 @@ const Image = () => {
3738
if (src.startsWith('http')) return
3839
const formData = new FormData()
3940
formData.append('file', base64ToFile(src))
41+
formData.append('token', getToken()!)
4042

4143
fetch('/api/s3', {
4244
method: 'POST',

0 commit comments

Comments
 (0)