|
1 | | -import { NextRequest } from 'next/server'; |
2 | | -import { jsonResponse } from '@/lib/apiHandler'; |
3 | | -import { createAPIRoute } from '@/lib/routeHelper'; |
4 | | -import { validateFileSize, validateUnlockTime } from '@/lib/validation'; |
5 | | -import { ErrorCode, createErrorResponse } from '@/lib/errors'; |
6 | | - |
| 1 | +import { NextRequest } from "next/server"; |
| 2 | +import { jsonResponse } from "@/lib/apiHandler"; |
| 3 | +import { createAPIRoute } from "@/lib/routeHelper"; |
| 4 | +import { |
| 5 | + validateFileSize, |
| 6 | + validateUnlockTime, |
| 7 | + validateRequestSize, |
| 8 | + validateKey, |
| 9 | + validateTimestamp, |
| 10 | +} from "@/lib/validation"; |
| 11 | +import { ErrorCode, createErrorResponse } from "@/lib/errors"; |
7 | 12 |
|
8 | 13 | export async function POST(request: NextRequest) { |
9 | | - const contentLength = request.headers.get('content-length'); |
10 | | - const maxSize = parseInt(process.env.MAX_FILE_SIZE_MB || '10') * 1024 * 1024; |
11 | | - |
12 | | - if (contentLength && parseInt(contentLength) > maxSize) { |
13 | | - return jsonResponse({ |
14 | | - error: `Request body exceeds maximum size of ${maxSize / 1024 / 1024}MB` |
15 | | - }, 413); |
| 14 | + const contentLength = parseInt(request.headers.get("content-length") || "0"); |
| 15 | + |
| 16 | + const sizeValidation = validateRequestSize(contentLength); |
| 17 | + if (!sizeValidation.valid) { |
| 18 | + return jsonResponse({ error: sizeValidation.error }, 413); |
16 | 19 | } |
17 | | - |
18 | | - return createAPIRoute(async ({ container, request: ctx, ip }) => { |
19 | | - const formData = await ctx.formData(); |
20 | | - const encryptedBlob = formData.get('encryptedBlob') as File; |
21 | | - const keyB = formData.get('keyB') as string; |
22 | | - const iv = formData.get('iv') as string; |
23 | | - const unlockTime = parseInt(formData.get('unlockTime') as string); |
24 | | - const isDMS = formData.get('isDMS') === 'true'; |
25 | | - const pulseInterval = formData.get('pulseInterval') ? |
26 | | - parseInt(formData.get('pulseInterval') as string) : undefined; |
27 | | - |
28 | | - if (!encryptedBlob || !keyB || !iv || !unlockTime || isNaN(unlockTime)) { |
29 | | - return createErrorResponse(ErrorCode.INVALID_UNLOCK_TIME, 'Missing required fields'); |
30 | | - } |
31 | | - |
32 | | - const sizeValidation = validateFileSize(encryptedBlob.size); |
33 | | - if (!sizeValidation.valid) { |
34 | | - return jsonResponse({ error: sizeValidation.error }, 400); |
35 | | - } |
36 | | - |
37 | | - const timeValidation = validateUnlockTime(unlockTime); |
38 | | - if (!timeValidation.valid) { |
39 | | - return createErrorResponse(ErrorCode.INVALID_UNLOCK_TIME, timeValidation.error); |
40 | | - } |
41 | | - |
42 | | - const sealService = container.sealService; |
43 | | - const blobBuffer = await encryptedBlob.arrayBuffer(); |
44 | | - const result = await sealService.createSeal({ |
45 | | - encryptedBlob: blobBuffer, |
46 | | - keyB, |
47 | | - iv, |
48 | | - unlockTime, |
49 | | - isDMS, |
50 | | - pulseInterval, |
51 | | - }, ip); |
52 | | - |
53 | | - return jsonResponse({ |
54 | | - success: true, |
55 | | - sealId: result.sealId, |
56 | | - iv: result.iv, |
57 | | - publicUrl: `/v/${result.sealId}`, |
58 | | - pulseUrl: result.pulseToken ? `/pulse/${result.pulseToken}` : undefined, |
59 | | - }); |
60 | | - }, { rateLimit: { limit: 10, window: 60000 } })(request); |
| 20 | + |
| 21 | + return createAPIRoute( |
| 22 | + async ({ container, request: ctx, ip }) => { |
| 23 | + const formData = await ctx.formData(); |
| 24 | + const encryptedBlob = formData.get("encryptedBlob") as File; |
| 25 | + const keyB = formData.get("keyB") as string; |
| 26 | + const iv = formData.get("iv") as string; |
| 27 | + const unlockTime = parseInt(formData.get("unlockTime") as string); |
| 28 | + const isDMS = formData.get("isDMS") === "true"; |
| 29 | + const pulseInterval = formData.get("pulseInterval") |
| 30 | + ? parseInt(formData.get("pulseInterval") as string) |
| 31 | + : undefined; |
| 32 | + |
| 33 | + if (!encryptedBlob || !keyB || !iv || !unlockTime || isNaN(unlockTime)) { |
| 34 | + return createErrorResponse( |
| 35 | + ErrorCode.INVALID_UNLOCK_TIME, |
| 36 | + "Missing required fields", |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + const keyBValidation = validateKey(keyB, "Key B"); |
| 41 | + if (!keyBValidation.valid) { |
| 42 | + return jsonResponse({ error: keyBValidation.error }, 400); |
| 43 | + } |
| 44 | + |
| 45 | + const ivValidation = validateKey(iv, "IV"); |
| 46 | + if (!ivValidation.valid) { |
| 47 | + return jsonResponse({ error: ivValidation.error }, 400); |
| 48 | + } |
| 49 | + |
| 50 | + const timestampValidation = validateTimestamp(unlockTime); |
| 51 | + if (!timestampValidation.valid) { |
| 52 | + return jsonResponse({ error: timestampValidation.error }, 400); |
| 53 | + } |
| 54 | + |
| 55 | + const fileSizeValidation = validateFileSize(encryptedBlob.size); |
| 56 | + if (!fileSizeValidation.valid) { |
| 57 | + return jsonResponse({ error: fileSizeValidation.error }, 400); |
| 58 | + } |
| 59 | + |
| 60 | + const timeValidation = validateUnlockTime(unlockTime); |
| 61 | + if (!timeValidation.valid) { |
| 62 | + return createErrorResponse( |
| 63 | + ErrorCode.INVALID_UNLOCK_TIME, |
| 64 | + timeValidation.error, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + const sealService = container.sealService; |
| 69 | + const blobBuffer = await encryptedBlob.arrayBuffer(); |
| 70 | + const result = await sealService.createSeal( |
| 71 | + { |
| 72 | + encryptedBlob: blobBuffer, |
| 73 | + keyB, |
| 74 | + iv, |
| 75 | + unlockTime, |
| 76 | + isDMS, |
| 77 | + pulseInterval, |
| 78 | + }, |
| 79 | + ip, |
| 80 | + ); |
| 81 | + |
| 82 | + return jsonResponse({ |
| 83 | + success: true, |
| 84 | + sealId: result.sealId, |
| 85 | + iv: result.iv, |
| 86 | + publicUrl: `/v/${result.sealId}`, |
| 87 | + pulseUrl: result.pulseToken ? `/pulse/${result.pulseToken}` : undefined, |
| 88 | + }); |
| 89 | + }, |
| 90 | + { rateLimit: { limit: 10, window: 60000 } }, |
| 91 | + )(request); |
61 | 92 | } |
0 commit comments