mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 06:54:28 +00:00
fix: use renamed asset name in download Content-Disposition (#286)
* 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).
This commit is contained in:
@@ -4,6 +4,7 @@ import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
|
||||
import { AssetType, AssetStatus, Prisma } from '@shumai/db'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { AssetService } from './asset'
|
||||
|
||||
vi.mock('@shumai/core/src/s3/s3', () => ({
|
||||
@@ -2229,6 +2230,18 @@ describe('AssetService — natural sort by name', () => {
|
||||
for (const item of result) {
|
||||
expect(item.url).toBe('http://mock-s3-url')
|
||||
}
|
||||
|
||||
// Renamed file names must be passed to presign so the download
|
||||
// Content-Disposition header uses the asset name, not the storage key
|
||||
for (const name of ['file1.txt', 'file2.txt', 'version2.txt']) {
|
||||
expect(s3Service.presign).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
expect.any(String),
|
||||
'GET',
|
||||
true,
|
||||
name,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it('resolves top-level symlinks to their targets when passed as direct starting IDs', async () => {
|
||||
@@ -2539,6 +2552,40 @@ describe('AssetService — natural sort by name', () => {
|
||||
)
|
||||
|
||||
expect(url).toBe('http://mock-s3-url')
|
||||
expect(s3Service.presign).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
'files/01TESTULID000000000000000/test-720p.mp4',
|
||||
'GET',
|
||||
true,
|
||||
'test.mp4',
|
||||
)
|
||||
})
|
||||
|
||||
it('should use the renamed asset name as the download filename', async () => {
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'foo.png',
|
||||
type: AssetType.file,
|
||||
status: AssetStatus.uploaded,
|
||||
storageKey: {
|
||||
create: { key: 'files/01RENAMEDULID0000000000000/test.png' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const url = await assetService.getDownloadUrl(
|
||||
asset.id,
|
||||
'files/01RENAMEDULID0000000000000/test.png',
|
||||
)
|
||||
|
||||
expect(url).toBe('http://mock-s3-url')
|
||||
expect(s3Service.presign).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
'files/01RENAMEDULID0000000000000/test.png',
|
||||
'GET',
|
||||
true,
|
||||
'foo.png',
|
||||
)
|
||||
})
|
||||
|
||||
it('should return a presigned download URL for the original key', async () => {
|
||||
|
||||
@@ -1966,7 +1966,13 @@ export class AssetService {
|
||||
const key = file.storageKeyId ? storageKeyMap.get(file.storageKeyId) : null
|
||||
if (!key) return null
|
||||
|
||||
const url = await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET', true)
|
||||
const url = await s3Service.presign(
|
||||
process.env.S3_BUCKET || 'shumai',
|
||||
key,
|
||||
'GET',
|
||||
true,
|
||||
file.name,
|
||||
)
|
||||
|
||||
return {
|
||||
id: file.id,
|
||||
@@ -1988,7 +1994,7 @@ export class AssetService {
|
||||
const targetAssetId = await this.resolveLatestVersionId(assetId)
|
||||
const asset = await this.prismaClient.asset.findUnique({
|
||||
where: { id: targetAssetId },
|
||||
select: { id: true, parentId: true, storageKey: { select: { key: true } } },
|
||||
select: { id: true, name: true, parentId: true, storageKey: { select: { key: true } } },
|
||||
})
|
||||
|
||||
if (!asset?.storageKey?.key) {
|
||||
@@ -2022,7 +2028,13 @@ export class AssetService {
|
||||
throw new Error('Key does not belong to this asset')
|
||||
}
|
||||
|
||||
return s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET', true)
|
||||
return s3Service.presign(
|
||||
process.env.S3_BUCKET || 'shumai',
|
||||
key,
|
||||
'GET',
|
||||
true,
|
||||
asset.name ?? undefined,
|
||||
)
|
||||
}
|
||||
|
||||
private async toPreviewInfo(asset: Asset | AssetWithIncludes): Promise<PreviewInfo | null> {
|
||||
|
||||
Reference in New Issue
Block a user