|
1 | 1 | import { Hono } from "hono"; |
2 | 2 | import { getMimeType } from "@/utils/mime.js"; |
3 | | -import type { AppContext } from '@/types/app.js'; |
| 3 | +import type { AppContext } from "@/types/app.js"; |
4 | 4 |
|
5 | 5 | const app = new Hono<AppContext>(); |
6 | 6 |
|
7 | | -app.get('/storage/*', async (c) => { |
8 | | -try { |
9 | | - const storage = c.var.storage; |
10 | | - const path = c.req.path.replace('/storage/', ''); |
11 | | - const file = await storage.get(path); |
12 | | - |
13 | | - const contentType = getMimeType(path); |
14 | | - const filename = path.split('/').pop(); |
15 | | - |
16 | | - // For images and markdown, use inline display instead of attachment |
17 | | - const isInline = contentType.startsWith('image/') || contentType === 'text/markdown'; |
18 | | - |
19 | | - // Compute ETag from file buffer for cache validation |
20 | | - const crypto = await import('crypto'); |
21 | | - const hash = crypto.createHash('md5').update(file).digest('hex'); |
22 | | - const etag = `"${hash}"`; |
23 | | - |
24 | | - // Check if client has cached version |
25 | | - const ifNoneMatch = c.req.header('if-none-match'); |
26 | | - if (ifNoneMatch === etag) { |
27 | | - return new Response(null, { status: 304 }); |
28 | | - } |
29 | | - |
30 | | - return new Response(file, { |
31 | | - headers: { |
32 | | - 'Content-Type': contentType, |
33 | | - 'Content-Disposition': isInline |
34 | | - ? `inline; filename="${filename}"` |
35 | | - : `attachment; filename="${filename}"`, |
36 | | - // Cache for 1 year since files are content-addressed (path includes version) |
37 | | - 'Cache-Control': 'public, max-age=31536000, immutable', |
38 | | - 'ETag': etag, |
39 | | - }, |
40 | | - }); |
41 | | -} catch (error) { |
42 | | - return c.json({ error: 'File not found' }, 404); |
43 | | -} |
| 7 | +app.get("/storage/*", async (c) => { |
| 8 | + try { |
| 9 | + const storage = c.var.storage; |
| 10 | + const path = c.req.path.replace("/storage/", ""); |
| 11 | + const file = await storage.get(path); |
| 12 | + |
| 13 | + const contentType = getMimeType(path); |
| 14 | + const filename = path.split("/").pop(); |
| 15 | + |
| 16 | + // For images and markdown, use inline display instead of attachment |
| 17 | + const isInline = |
| 18 | + contentType.startsWith("image/") || contentType === "text/markdown"; |
| 19 | + |
| 20 | + // Compute ETag from file buffer for cache validation |
| 21 | + const crypto = await import("crypto"); |
| 22 | + const hash = crypto.createHash("md5").update(file).digest("hex"); |
| 23 | + const etag = `"${hash}"`; |
| 24 | + |
| 25 | + // Check if client has cached version |
| 26 | + const ifNoneMatch = c.req.header("if-none-match"); |
| 27 | + if (ifNoneMatch === etag) { |
| 28 | + return new Response(null, { status: 304 }); |
| 29 | + } |
| 30 | + |
| 31 | + return new Response(file, { |
| 32 | + headers: { |
| 33 | + "Content-Type": contentType, |
| 34 | + "Content-Disposition": isInline |
| 35 | + ? `inline; filename="${filename}"` |
| 36 | + : `attachment; filename="${filename}"`, |
| 37 | + // Cache for 1 year since files are content-addressed (path includes version) |
| 38 | + "Cache-Control": "public, max-age=31536000, immutable", |
| 39 | + ETag: etag, |
| 40 | + }, |
| 41 | + }); |
| 42 | + } catch (error) { |
| 43 | + return c.json({ error: "File not found" }, 404); |
| 44 | + } |
44 | 45 | }); |
45 | 46 |
|
46 | | - |
47 | 47 | export default app; |
0 commit comments