fix(media): 资源交付 - 按内容识别上传类型并统一重试与附件交付 (#678)
Co-authored-by: ddcat666 <26564566+ddcat666@users.noreply.github.com>
This commit is contained in:
@@ -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
|
||||
|
||||
24
backend/internal/assets/content_delivery_test.go
Normal file
24
backend/internal/assets/content_delivery_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
16
backend/internal/assets/content_type.go
Normal file
16
backend/internal/assets/content_type.go
Normal 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/")
|
||||
}
|
||||
Reference in New Issue
Block a user