fix(storage): COS 资源下载 - 公开 CDN 模式使用附件响应头 (#718)
This commit is contained in:
@@ -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, "\\", "_")
|
||||
|
||||
Reference in New Issue
Block a user