(request: NextRequest)
| 21 | }); |
| 22 | |
| 23 | export async function POST(request: NextRequest) { |
| 24 | const { user, authFailedResponse } = await getUserFromAuth({ adminOnly: false }); |
| 25 | if (authFailedResponse) return authFailedResponse; |
| 26 | |
| 27 | let body: unknown; |
| 28 | try { |
| 29 | body = await request.json(); |
| 30 | } catch { |
| 31 | return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 }); |
| 32 | } |
| 33 | |
| 34 | const bodyResult = BodySchema.safeParse(body); |
| 35 | |
| 36 | if (!bodyResult.success) { |
| 37 | return NextResponse.json( |
| 38 | { |
| 39 | error: 'Invalid request body', |
| 40 | details: z.treeifyError(bodyResult.error), |
| 41 | }, |
| 42 | { status: 400 } |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | const { session_id, blob_type, content_length } = bodyResult.data; |
| 47 | |
| 48 | const [existingSession] = await db |
| 49 | .select() |
| 50 | .from(cliSessions) |
| 51 | .where(and(eq(cliSessions.session_id, session_id), eq(cliSessions.kilo_user_id, user.id))) |
| 52 | .limit(1); |
| 53 | |
| 54 | if (!existingSession) { |
| 55 | return NextResponse.json({ error: 'Session not found' }, { status: 404 }); |
| 56 | } |
| 57 | |
| 58 | const { signedUrl, key: blobKey } = await generateSignedUploadUrl( |
| 59 | session_id, |
| 60 | user.id, |
| 61 | 'sessions', |
| 62 | blob_type, |
| 63 | content_length |
| 64 | ); |
| 65 | |
| 66 | const [updatedSession] = await db |
| 67 | .update(cliSessions) |
| 68 | .set({ [`${blob_type}_blob_url`]: blobKey }) |
| 69 | .where(and(eq(cliSessions.session_id, session_id), eq(cliSessions.kilo_user_id, user.id))) |
| 70 | .returning({ |
| 71 | session_id: cliSessions.session_id, |
| 72 | updated_at: cliSessions.updated_at, |
| 73 | }); |
| 74 | |
| 75 | if (!updatedSession) { |
| 76 | return NextResponse.json({ error: 'Failed to update session' }, { status: 500 }); |
| 77 | } |
| 78 | |
| 79 | return NextResponse.json({ |
| 80 | signed_url: signedUrl, |
no test coverage detected