fix(storage): COS 资源下载 - 公开 CDN 模式使用附件响应头 (#718)

This commit is contained in:
MasterChiefCN authored and GitHub committed 2026-10-10 09:10:59 +08:00
1 parent d77a7c2885
commit ee6b344671
5 files changed
+88 -7

No files matched your search

+22
View File
@@ -248,6 +248,28 @@ func SignedOriginObjectDownloadURL(setting Settings, objectKey string, expiresAt
return signedOriginObjectURL(setting, objectKey, objectURLSigning{ExpiresAt: expiresAt, ContentDisposition: disposition})
}
// PublicCDNObjectDownloadURL returns a public CDN URL that asks the object
// storage origin to emit an attachment response. The query parameter keeps
// ordinary display URLs inline while allowing a browser to download this one
// without routing object bytes through the application server.
//
// Public CDN deployments must preserve response-content-disposition when
// forwarding requests to the object store.
func PublicCDNObjectDownloadURL(setting Settings, objectKey string, fileName string) (string, error) {
value, err := OssCDNObjectURL(setting.CDNBaseURL, objectKey)
if err != nil {
return "", err
}
parsed, err := url.Parse(value)
if err != nil {
return "", err
}
query := parsed.Query()
query.Set("response-content-disposition", objectDownloadContentDisposition(fileName, objectKey))
parsed.RawQuery = query.Encode()
return parsed.String(), nil
}
func objectDownloadContentDisposition(fileName string, objectKey string) string {
name := strings.TrimSpace(fileName)
name = strings.ReplaceAll(name, "\\", "_")