Skip to content

Commit

Permalink
fix: upload need token
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Mar 11, 2024
1 parent acdd7f6 commit f0453db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/app/api/s3/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { NextRequest } from 'next/server'

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

import { apiClient } from '~/lib/request'

const config = {
accountId: 'de7ecb0eaa0a328071255d557a6adb66',
accessKeyId: process.env.S3_ACCESS_KEY as string,
Expand Down Expand Up @@ -34,6 +36,17 @@ async function uploadToS3(path: string, body: Buffer, contentType: string) {
export const POST = async (req: NextRequest) => {
const formData = await req.formData()
const file = formData.get('file')
const token = formData.get('token') as string

if (!token) {
return NextResponse.json({ error: 'No token received.' }, { status: 400 })
}
const { isGuest } = await apiClient.user.checkTokenValid(token)

if (isGuest) {
return NextResponse.json({ error: 'Invalid token.' }, { status: 401 })
}

if (!file) {
return NextResponse.json({ error: 'No files received.' }, { status: 400 })
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/ui/editor/Milkdown/plugins/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { FloatPopover } from '~/components/ui/float-popover'
import { Form, FormInput } from '~/components/ui/form'
import { FixedZoomedImage } from '~/components/ui/image'
import { useCurrentModal, useModalStack } from '~/components/ui/modal'
import { getToken } from '~/lib/cookie'
import { toast } from '~/lib/toast'

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

fetch('/api/s3', {
method: 'POST',
Expand Down

0 comments on commit f0453db

Please sign in to comment.