oss请求处理

This commit is contained in:
andy committed 2026-08-19 10:52:55 +08:00
1 parent 18cb51670c
commit a9a4cdf125
18 files changed
+450 -29

No files matched your search

+26 -2
View File
@@ -271,6 +271,20 @@ func (h *assetsHandler) download(w http.ResponseWriter, r *http.Request, public
}
return
}
inline := r.URL.Query().Get("inline") == "1"
if inline {
signedURL, signed, signErr := h.service.SignedDownloadURL(r.Context(), scope, id, assets.DefaultSignedURLTTL)
if signErr != nil {
writeAssetError(w, signErr, public, "")
return
}
if signed {
w.Header().Set("Cache-Control", "private, no-store")
w.Header().Set("Location", signedURL)
w.WriteHeader(http.StatusTemporaryRedirect)
return
}
}
blob, err := h.service.Download(r.Context(), scope, id)
if err != nil {
if public {
@@ -281,7 +295,11 @@ func (h *assetsHandler) download(w http.ResponseWriter, r *http.Request, public
return
}
defer blob.Body.Close()
writeBlob(w, blob, "private, no-store", contentDisposition(a.Name))
disposition := contentDisposition(a.Name)
if inline {
disposition = inlineContentDisposition(a.Name)
}
writeBlob(w, blob, "private, no-store", disposition)
}
func (h *assetsHandler) serveStored(w http.ResponseWriter, r *http.Request, key string) {
@@ -383,6 +401,12 @@ func writeBlob(w http.ResponseWriter, blob assets.Blob, cache, disposition strin
_, _ = io.Copy(w, blob.Body)
}
func contentDisposition(name string) string {
return namedContentDisposition("attachment", name)
}
func inlineContentDisposition(name string) string {
return namedContentDisposition("inline", name)
}
func namedContentDisposition(kind, name string) string {
clean := strings.TrimSpace(strings.NewReplacer("\r", "_", "\n", "_", "/", "_", "\\", "_").Replace(name))
if clean == "" {
clean = "download"
@@ -395,7 +419,7 @@ func contentDisposition(name string) string {
ascii.WriteByte('_')
}
}
return `attachment; filename="` + ascii.String() + `"; filename*=UTF-8''` + url.PathEscape(clean)
return kind + `; filename="` + ascii.String() + `"; filename*=UTF-8''` + url.PathEscape(clean)
}
func requestOrigin(r *http.Request) string {
scheme := "http"