Files
shumai/packages/api/src/s3.ts
T
yiling b7367bb129 refactor(storage): use original filenames and fix download UX
- Store uploaded files as files/{ulid}/{original-name} instead of files/{ulid}/raw
- Store transcoded videos as {stem}-{resolution}.mp4 (e.g. foo-720p.mp4)
- Store transcoded images as {stem}-{width}p.webp (e.g. foo-1920p.webp)
- Add download?: boolean param to presign - only sets Content-Disposition: attachment when true
- Local storage handler only sets Content-Disposition when ?download=1 query param is present
- Add POST /api/files/download-url endpoint for key-based single file downloads
  - Uses ResourceType.Asset for permission check
  - Validates key belongs to the asset's storage directory
- Fix existing download-links API to pass download: true to presign
- Fix breadcrumb navbar download: video shows resolution submenu, others download directly
- Fix video player download: calls download-url API, shows correct file type label
- Remove filename query param hack from presign URLs
- Add sanitizeFilename() and stemFromKey() utility helpers
- Add key field to VideoTranscode DTO interface
2026-07-02 17:39:23 +08:00

33 lines
873 B
TypeScript

import { Hono } from 'hono'
import { s3Service } from '@shumai/core/src/s3/s3'
import { serveStatic } from 'hono/bun'
const route = new Hono()
.get(
'/*',
serveStatic({
root: './data',
rewriteRequestPath: (path) => path.replace(/^\/files\//, ''),
onFound: (_path, c) => {
if (c.req.query('download') === '1') {
c.header('Content-Disposition', 'attachment')
}
},
}),
)
.put('/:bucket/:key{.+}', async (c) => {
const bucket = c.req.param('bucket')
const key = c.req.param('key')
const body = await c.req.arrayBuffer()
try {
await s3Service.putObject(bucket, key, Buffer.from(body), body.byteLength)
return c.text('OK')
} catch (e: unknown) {
const error = e as Error
return c.text(error.message || 'Internal Server Error', 500)
}
})
export default route