mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 10:56:52 +00:00
* fix(core): use renamed asset name in download Content-Disposition
Downloads of renamed assets (e.g. test.png renamed to foo.png) previously
saved under the storage-key basename because the presigned URLs / local
file handler only set Content-Disposition: attachment without a filename,
and the HTML download attribute is ignored for cross-origin URLs.
- Add buildContentDisposition() helper that emits a sanitized
'attachment; filename=...; filename*=UTF-8...' header (RFC 5987) so
non-ASCII names survive.
- Thread an optional filename through S3Service.presign():
* S3 backend passes it via contentDisposition (response-content-disposition)
* Local backend carries it as a ?filename= query param consumed by the
/files serveStatic onFound handler
- getDownloadLinks / getDownloadUrl now pass the renamed asset name from
the DB, so all download flows (file browser, viewers, breadcrumb, share)
save files under the current asset name.
* fix(core): keep Content-Disposition header ASCII for non-ASCII names
Bun's Headers API rejects header values containing non-ASCII bytes, so a
download of a renamed file with a non-ASCII name (e.g. 幻境边界.pptx) threw
'TypeError: Header Content-Disposition has invalid value' on the local
storage backend.
Per RFC 5987, non-ASCII names belong only in the percent-encoded
filename* parameter. buildContentDisposition now emits the quoted
filename="..." fallback only when the name is pure ASCII; otherwise the
header carries just 'attachment; filename*=UTF-8''...' (all-ASCII, accepted
by Bun/Node Headers and all modern browsers).
130 lines
4.2 KiB
TypeScript
130 lines
4.2 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { Hono } from 'hono'
|
|
|
|
const { mockServeStatic } = vi.hoisted(() => ({
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
mockServeStatic: vi.fn<(args: any) => any>(() => (c: any) => c.text('static-file')),
|
|
}))
|
|
|
|
vi.mock('hono/bun', () => ({
|
|
serveStatic: mockServeStatic,
|
|
}))
|
|
|
|
describe('S3 API', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
vi.resetModules()
|
|
})
|
|
|
|
it('GET /files/* calls serveStatic with correct options', async () => {
|
|
const { default: route } = await import('./s3')
|
|
const app = new Hono().route('/files', route)
|
|
|
|
const res = await app.request('/files/b1/test.webp')
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(await res.text()).toBe('static-file')
|
|
expect(mockServeStatic).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
root: './data',
|
|
rewriteRequestPath: expect.any(Function),
|
|
}),
|
|
)
|
|
|
|
// Verify rewriteRequestPath logic
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const options = vi.mocked(mockServeStatic).mock.calls[0][0] as any
|
|
if (options && options.rewriteRequestPath) {
|
|
expect(options.rewriteRequestPath('/files/b1/test.webp')).toBe('b1/test.webp')
|
|
}
|
|
})
|
|
|
|
it('GET /files/* sets Content-Disposition with filename when download=1 & filename query params are present', async () => {
|
|
// We need to capture the onFound callback from serveStatic options
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
let onFoundCallback: ((path: string, c: any) => void) | undefined
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
mockServeStatic.mockImplementationOnce((options: any) => {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
onFoundCallback = (options as any).onFound
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
return (c: any) => c.text('static-file')
|
|
})
|
|
|
|
const { default: route } = await import('./s3')
|
|
const app = new Hono().route('/files', route)
|
|
|
|
await app.request('/files/b1/test.webp?download=1&filename=foo.png')
|
|
|
|
expect(onFoundCallback).toBeDefined()
|
|
|
|
// Test with download=1 and filename
|
|
const mockContextWithDownload = {
|
|
req: {
|
|
query: (key: string) =>
|
|
key === 'download' ? '1' : key === 'filename' ? 'foo.png' : undefined,
|
|
},
|
|
header: vi.fn(),
|
|
}
|
|
|
|
if (onFoundCallback) {
|
|
onFoundCallback('/files/b1/test.webp', mockContextWithDownload)
|
|
expect(mockContextWithDownload.header).toHaveBeenCalledWith(
|
|
'Content-Disposition',
|
|
'attachment; filename="foo.png"; filename*=UTF-8\'\'foo.png',
|
|
)
|
|
}
|
|
|
|
// Test with download=1 but no filename
|
|
const mockContextDownloadNoFilename = {
|
|
req: { query: (key: string) => (key === 'download' ? '1' : undefined) },
|
|
header: vi.fn(),
|
|
}
|
|
|
|
if (onFoundCallback) {
|
|
onFoundCallback('/files/b1/test.webp', mockContextDownloadNoFilename)
|
|
expect(mockContextDownloadNoFilename.header).toHaveBeenCalledWith(
|
|
'Content-Disposition',
|
|
'attachment',
|
|
)
|
|
}
|
|
|
|
// Test without download param
|
|
const mockContextWithout = {
|
|
req: { query: () => undefined },
|
|
header: vi.fn(),
|
|
}
|
|
|
|
if (onFoundCallback) {
|
|
onFoundCallback('/files/b1/test.webp', mockContextWithout)
|
|
expect(mockContextWithout.header).not.toHaveBeenCalled()
|
|
}
|
|
})
|
|
|
|
it('PUT /files/:bucket/:key calls s3Service.putObject', async () => {
|
|
const { s3Service: service } = await import('@shumai/core/src/s3/s3')
|
|
const mockPut = vi.spyOn(service, 'putObject').mockResolvedValue(undefined)
|
|
|
|
const { default: route } = await import('./s3')
|
|
const app = new Hono().route('/files', route)
|
|
const body = new TextEncoder().encode('test-data')
|
|
|
|
const res = await app.request('/files/b1/test.webp', {
|
|
method: 'PUT',
|
|
headers: {
|
|
'content-length': body.byteLength.toString(),
|
|
},
|
|
body,
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(await res.text()).toBe('OK')
|
|
expect(mockPut).toHaveBeenCalledWith(
|
|
'b1',
|
|
'test.webp',
|
|
expect.any(ReadableStream),
|
|
body.byteLength,
|
|
)
|
|
})
|
|
})
|