fix(media): 资源交付 - 按内容识别上传类型并统一重试与附件交付 (#678)

Co-authored-by: ddcat666 <26564566+ddcat666@users.noreply.github.com>
This commit is contained in:
Catlaxy AI
2026-10-06 09:35:50 +08:00
committed by GitHub
parent 3750fada7a
commit 398f8e36f5
10 changed files with 322 additions and 39 deletions

View File

@@ -140,6 +140,10 @@ func ResolveAccess(resource *model.Resource, setting storage.Settings, options A
}
if resource.Provider == "local" {
access.Delivery = DeliveryLocal
} else if !InlineMediaType(resource.MimeType) {
// A redirect cannot enforce our attachment and sandbox response headers.
access.Delivery = DeliveryProxy
access.FallbackReason = "attachment_required"
} else if options.Purpose == PurposeDownload {
// Cross-origin `a[download]` is only advisory. A real browser download must
// therefore be enforced by the object store response itself. Downloads use

View File

@@ -0,0 +1,24 @@
package assets
import (
"testing"
"time"
"infinite-canvas/backend/internal/storage"
)
func TestNonMediaDeliveryUsesPlatform(t *testing.T) {
for _, mimeType := range []string{"text/html", "image/svg+xml", "application/pdf", "application/octet-stream"} {
for _, purpose := range []AccessPurpose{PurposeDisplay, PurposeCopy, PurposeProcess, PurposeDownload, PurposeProvider} {
t.Run(mimeType+"/"+string(purpose), func(t *testing.T) {
r := testReadyResource("aliyun")
r.MimeType = mimeType
var variants []ResourceVariant
got, err := ResolveAccess(r, storage.Settings{Provider: "aliyun", CDNBaseURL: "https://cdn.example", Delivery: storage.DeliverySettings{CDNAuthMode: "public"}}, AccessOptions{Purpose: purpose}, time.Now(), testPlatformURL(&variants))
if err != nil || got.Delivery != DeliveryProxy || len(variants) != 1 {
t.Fatalf("non-media escaped controlled delivery: access=%+v err=%v", got, err)
}
})
}
}
}

View File

@@ -0,0 +1,16 @@
package assets
import (
"mime"
"strings"
)
// InlineMediaType excludes active XML images such as SVG. Other documents and
// unknown formats remain uploadable, but must use controlled attachment delivery.
func InlineMediaType(value string) bool {
mediaType, _, err := mime.ParseMediaType(value)
if err != nil || strings.HasSuffix(mediaType, "+xml") || mediaType == "image/svg" {
return false
}
return strings.HasPrefix(mediaType, "image/") || strings.HasPrefix(mediaType, "audio/") || strings.HasPrefix(mediaType, "video/")
}