| 1 | import type { NextRequest } from "next/server"; |
| 2 | import { getAuth } |
| 3 | import { getObject, headObject |
| 4 | |
| 5 | const EXT_TO_MIME: Record |
| 6 | jpg: "image/jpeg", |
| 7 | jpeg: "image/jpeg", |
| 8 | png: "image/png", |
| 9 | webp: "image/webp", |
| 10 | gif: "image/gif", |
| 11 | svg: "image/svg+xml", |
| 12 | }; |
| 13 | |
| 14 | const CACHE_PRIVATE = " |
| 15 | const CACHE_PUBLIC = " |
| 16 | |
| 17 | const PUBLIC_PREFIXES = [ |
| 18 | |
| 19 | type Params |
| 20 | |
| 21 | function normalizeETag(etag: |
| 22 | return etag.replace |
| 23 | } |
| 24 | |
| 25 | export async function GET |
| 26 | const { path |
| 27 | const key = |
| 28 | |
| 29 | const isPublic = |
| 30 | if (!isPublic) |
| 31 | const auth = |
| 32 | const session = |
| 33 | if (!session) |
| 34 | } |
| 35 | |
| 36 | const cacheControl = |
| 37 | |
| 38 | const lastSegment = |
| 39 | const dotIdx = |
| 40 | const ext = |
| 41 | const contentType = |
| 42 | |
| 43 | const ifNoneMatch = |
| 44 | |
| 45 | // Cheap ETag check — no body download needed |
| 46 | if (ifNoneMatch) { |
| 47 | try { |
| 48 | const { etag |
| 49 | if (etag && |
| 50 | return new Response |
| 51 | status: 304, |
| 52 | headers: { "Cache-Control": |
| 53 | }); |
| 54 | } |
| 55 | } catch (err: |
| 56 | const e = |
| 57 | const code = |
| 58 | const httpStatus = |
| 59 | if (code !== |
| 60 | console.error(`[files] headObject( |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | let stream: |
| 66 | let etag: |
| 67 | try { |
| 68 | ({ stream, etag |
| 69 | } catch (err: |
| 70 | const e = |
| 71 | const code = |
| 72 | const httpStatus = |
| 73 | if (code === |
| 74 | return new Response |
| 75 | } |
| 76 | console.error(`[files] getObject( |
| 77 | throw err; |
| 78 | } |
| 79 | |
| 80 | const headers: |
| 81 | "Content-Type": contentType, |
| 82 | "Cache-Control": cacheControl, |
| 83 | }; |
| 84 | if (etag) headers |
| 85 | |
| 86 | return new Response |
| 87 | } |