Merge commit from fork

This commit is contained in:
Yiling-J
2026-12-03 10:44:12 +08:00
committed by GitHub
parent 17ba2631c5
commit e420f03b35
3 changed files with 160 additions and 142 deletions
+39 -3
View File
@@ -1,3 +1,4 @@
import { basename, extname } from 'node:path'
import { Hono } from 'hono'
import {
buildContentDisposition,
@@ -6,6 +7,32 @@ import {
} from '@shumai/core/src/s3/s3'
import { serveStatic } from 'hono/bun'
// Only serve formats used by preview and media clients inline on the app origin.
const inlineFileTypes = new Map([
['.aac', 'audio/aac'],
['.avif', 'image/avif'],
['.bmp', 'image/bmp'],
['.gif', 'image/gif'],
['.glb', 'model/gltf-binary'],
['.gltf', 'model/gltf+json'],
['.ico', 'image/x-icon'],
['.jpeg', 'image/jpeg'],
['.jpg', 'image/jpeg'],
['.m3u8', 'application/vnd.apple.mpegurl'],
['.m4a', 'audio/mp4'],
['.m4s', 'video/iso.segment'],
['.mp3', 'audio/mpeg'],
['.mp4', 'video/mp4'],
['.ogg', 'audio/ogg'],
['.opus', 'audio/opus'],
['.pdf', 'application/pdf'],
['.png', 'image/png'],
['.ts', 'video/mp2t'],
['.wav', 'audio/wav'],
['.webm', 'video/webm'],
['.webp', 'image/webp'],
])
const route = new Hono()
.use('*', async (c, next) => {
if (getStorageBackend() !== 'local') {
@@ -36,9 +63,18 @@ const route = new Hono()
serveStatic({
root: './data',
rewriteRequestPath: (path) => path.replace(/^\/files\//, ''),
onFound: (_path, c) => {
if (c.req.query('download') === '1') {
c.header('Content-Disposition', buildContentDisposition(c.req.query('filename')))
onFound: (path, c) => {
const inlineType = inlineFileTypes.get(extname(path).toLowerCase())
c.header('Content-Type', inlineType ?? 'application/octet-stream')
c.header('X-Content-Type-Options', 'nosniff')
if (!inlineType) {
c.header('Content-Security-Policy', 'sandbox')
}
if (!inlineType || c.req.query('download') === '1') {
const filename = c.req.query('filename') ?? (inlineType ? undefined : basename(path))
c.header('Content-Disposition', buildContentDisposition(filename))
}
},
}),