|
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { StorageOperations } from "@/lib/storage-operations"; |
| 4 | +import { FILE_LIMITS } from "@/lib/constants"; |
| 5 | +import { AppError } from "@/types/errors"; |
| 6 | +import { generateSalt, hashPin } from "@/lib/auth"; |
| 7 | +import { errorResponse, ApiErrors, validationError } from "@/lib/api-errors"; |
| 8 | +import type { CreateGistResponse } from "@/types/api"; |
| 9 | +import type { GistMetadata } from "@/types/models"; |
| 10 | + |
| 11 | +export const runtime = "edge"; |
| 12 | + |
| 13 | +// Validation schema for gist metadata |
| 14 | +const metadataSchema = z.object({ |
| 15 | + expires_at: z.string().datetime().nullable().optional(), |
| 16 | + one_time_view: z.boolean().optional(), |
| 17 | + file_count: z.number().int().positive().optional(), |
| 18 | + blob_count: z.number().int().positive().optional(), |
| 19 | +}); |
| 20 | + |
| 21 | +/** |
| 22 | + * Parse multipart form data from request |
| 23 | + */ |
| 24 | +async function parseMultipartFormData(request: NextRequest): Promise<{ |
| 25 | + metadata: Record<string, unknown>; |
| 26 | + blob: Uint8Array; |
| 27 | + password?: string; |
| 28 | +}> { |
| 29 | + const formData = await request.formData(); |
| 30 | + |
| 31 | + // Get required parts |
| 32 | + const metadataFile = formData.get("metadata") as File | null; |
| 33 | + const blobFile = formData.get("blob") as File | null; |
| 34 | + const passwordValue = formData.get("password") as string | null; |
| 35 | + |
| 36 | + if (!metadataFile || !blobFile) { |
| 37 | + throw ApiErrors.badRequest( |
| 38 | + "Missing required form data parts: metadata and blob" |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + // Parse metadata JSON |
| 43 | + let metadata: Record<string, unknown>; |
| 44 | + try { |
| 45 | + const metadataText = await metadataFile.text(); |
| 46 | + metadata = JSON.parse(metadataText) as Record<string, unknown>; |
| 47 | + } catch { |
| 48 | + throw ApiErrors.badRequest("Invalid metadata JSON"); |
| 49 | + } |
| 50 | + |
| 51 | + // Read blob data |
| 52 | + const blobBuffer = await blobFile.arrayBuffer(); |
| 53 | + const blob = new Uint8Array(blobBuffer); |
| 54 | + |
| 55 | + return { |
| 56 | + metadata, |
| 57 | + blob, |
| 58 | + password: passwordValue || undefined, |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * POST /api/gists |
| 64 | + * Creates a new encrypted gist |
| 65 | + */ |
| 66 | +export async function POST(request: NextRequest) { |
| 67 | + try { |
| 68 | + // Check content type |
| 69 | + const contentType = request.headers.get("content-type"); |
| 70 | + if (!contentType?.includes("multipart/form-data")) { |
| 71 | + return errorResponse( |
| 72 | + ApiErrors.badRequest("Content-Type must be multipart/form-data") |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + // Parse multipart form data |
| 77 | + let formParts: { |
| 78 | + metadata: Record<string, unknown>; |
| 79 | + blob: Uint8Array; |
| 80 | + password?: string; |
| 81 | + }; |
| 82 | + try { |
| 83 | + formParts = await parseMultipartFormData(request); |
| 84 | + } catch (error) { |
| 85 | + if (error instanceof AppError) { |
| 86 | + return errorResponse(error); |
| 87 | + } |
| 88 | + return errorResponse( |
| 89 | + ApiErrors.badRequest("Failed to parse multipart form data") |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + const { metadata: rawMetadata, blob, password } = formParts; |
| 94 | + |
| 95 | + // Validate metadata |
| 96 | + const validationResult = metadataSchema.safeParse(rawMetadata); |
| 97 | + if (!validationResult.success) { |
| 98 | + const errors = validationResult.error.flatten(); |
| 99 | + return errorResponse( |
| 100 | + validationError("Invalid metadata", errors.fieldErrors) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + const validatedMetadata = validationResult.data; |
| 105 | + |
| 106 | + // Check size limits |
| 107 | + if (blob.length > FILE_LIMITS.MAX_TOTAL_SIZE) { |
| 108 | + return errorResponse( |
| 109 | + ApiErrors.payloadTooLarge( |
| 110 | + `Total size exceeds ${FILE_LIMITS.MAX_TOTAL_SIZE / 1024 / 1024}MB limit` |
| 111 | + ) |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + // Hash password if provided |
| 116 | + let editPinHash: string | undefined; |
| 117 | + let editPinSalt: string | undefined; |
| 118 | + if (password) { |
| 119 | + editPinSalt = await generateSalt(); |
| 120 | + editPinHash = await hashPin(password, editPinSalt); |
| 121 | + } |
| 122 | + |
| 123 | + // Prepare metadata for storage |
| 124 | + const metadata: Omit< |
| 125 | + GistMetadata, |
| 126 | + "id" | "created_at" | "updated_at" | "version" | "current_version" |
| 127 | + > = { |
| 128 | + expires_at: validatedMetadata.expires_at ?? undefined, |
| 129 | + one_time_view: validatedMetadata.one_time_view, |
| 130 | + edit_pin_hash: editPinHash, |
| 131 | + edit_pin_salt: editPinSalt, |
| 132 | + total_size: blob.length, |
| 133 | + blob_count: validatedMetadata.blob_count || 1, |
| 134 | + // We'll need to set encrypted_metadata in the actual implementation |
| 135 | + encrypted_metadata: { iv: "", data: "" }, // Placeholder for now |
| 136 | + }; |
| 137 | + |
| 138 | + // Create gist using storage operations |
| 139 | + try { |
| 140 | + const { id } = await StorageOperations.createGist(metadata, blob); |
| 141 | + |
| 142 | + // Build response |
| 143 | + const response: CreateGistResponse = { |
| 144 | + id, |
| 145 | + url: `${process.env.NEXT_PUBLIC_APP_URL || "https://ghostpaste.dev"}/g/${id}`, |
| 146 | + createdAt: new Date().toISOString(), |
| 147 | + expiresAt: metadata.expires_at ?? null, |
| 148 | + isOneTimeView: metadata.one_time_view ?? false, |
| 149 | + }; |
| 150 | + |
| 151 | + return NextResponse.json<CreateGistResponse>(response, { |
| 152 | + status: 201, |
| 153 | + headers: { |
| 154 | + Location: response.url, |
| 155 | + "Cache-Control": "no-store", |
| 156 | + }, |
| 157 | + }); |
| 158 | + } catch (error) { |
| 159 | + // Handle storage errors |
| 160 | + if (error instanceof AppError) { |
| 161 | + return errorResponse(error); |
| 162 | + } |
| 163 | + |
| 164 | + // Log unexpected errors |
| 165 | + console.error("Storage error:", error); |
| 166 | + return errorResponse(ApiErrors.storageError("Failed to store gist data")); |
| 167 | + } |
| 168 | + } catch (error) { |
| 169 | + // Handle unexpected errors |
| 170 | + console.error("Unexpected error in POST /api/gists:", error); |
| 171 | + return errorResponse( |
| 172 | + error instanceof Error ? error : new Error("Unknown error") |
| 173 | + ); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +/** |
| 178 | + * OPTIONS /api/gists |
| 179 | + * Handle preflight requests |
| 180 | + */ |
| 181 | +export async function OPTIONS() { |
| 182 | + return new NextResponse(null, { |
| 183 | + status: 200, |
| 184 | + headers: { |
| 185 | + "Access-Control-Allow-Origin": |
| 186 | + process.env.NEXT_PUBLIC_APP_URL || "https://ghostpaste.dev", |
| 187 | + "Access-Control-Allow-Methods": "POST, OPTIONS", |
| 188 | + "Access-Control-Allow-Headers": "Content-Type", |
| 189 | + "Access-Control-Max-Age": "86400", |
| 190 | + }, |
| 191 | + }); |
| 192 | +} |
0 commit comments