2026-05-19 16:05:09 +08:00
|
|
|
import { Hono } from 'hono'
|
2026-06-01 23:16:22 +08:00
|
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
2026-05-26 19:38:57 +08:00
|
|
|
import { serveStatic } from 'hono/bun'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
const route = new Hono()
|
2026-05-26 19:38:57 +08:00
|
|
|
.get(
|
|
|
|
|
'/*',
|
|
|
|
|
serveStatic({
|
|
|
|
|
root: './data',
|
|
|
|
|
rewriteRequestPath: (path) => path.replace(/^\/files\//, ''),
|
2026-06-08 19:35:46 +08:00
|
|
|
onFound: (_path, c) => {
|
2026-07-02 15:26:10 +08:00
|
|
|
if (c.req.query('download') === '1') {
|
|
|
|
|
c.header('Content-Disposition', 'attachment')
|
2026-06-08 19:35:46 +08:00
|
|
|
}
|
|
|
|
|
},
|
2026-05-26 19:38:57 +08:00
|
|
|
}),
|
|
|
|
|
)
|
2026-05-19 16:05:09 +08:00
|
|
|
.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
|