fix(canvas): 资源引用 - 校验递归素材并修复断链

This commit is contained in:
ddCat committed 2026-09-24 00:12:53 +08:00
1 parent 5bbd208bef
commit 117fa7d70b
24 files changed
+697 -218

No files matched your search

+112 -35
View File
@@ -2,58 +2,90 @@ package assets
import (
"encoding/json"
"fmt"
"sort"
"strings"
)
// DocumentResourceReference describes one durable resource locator in a JSON document.
// Path is intentionally human-readable so it can be shown in save diagnostics.
type DocumentResourceReference struct {
ResourceID string `json:"resourceId"`
Path string `json:"path"`
NodeID string `json:"nodeId,omitempty"`
ReferenceType string `json:"referenceType"`
Source string `json:"source,omitempty"`
}
func DocumentReferences(raw string, resourceIDs map[string]struct{}) bool {
return len(DocumentReferencedIDs(raw, resourceIDs)) > 0
}
func DocumentReferencedIDs(raw string, resourceIDs map[string]struct{}) map[string]struct{} {
matched := map[string]struct{}{}
raw = strings.TrimSpace(raw)
if raw == "" || len(resourceIDs) == 0 {
if len(resourceIDs) == 0 {
return matched
}
found := map[string]struct{}{}
var value any
if err := json.Unmarshal([]byte(raw), &value); err == nil {
if scalar, ok := value.(string); ok {
if resourceID := ResourceID(scalar); resourceID != "" {
found[resourceID] = struct{}{}
references, err := CollectDocumentResourceReferences(raw)
if err != nil {
// A few read models store a URL directly rather than a JSON string.
if id := ResourceID(strings.TrimSpace(raw)); id != "" {
if _, exists := resourceIDs[id]; exists {
matched[id] = struct{}{}
}
} else {
walkReferenceDocument(value, "", found)
}
} else if resourceID := ResourceID(raw); resourceID != "" {
found[resourceID] = struct{}{}
return matched
}
for resourceID := range found {
if _, exists := resourceIDs[resourceID]; exists {
matched[resourceID] = struct{}{}
for _, reference := range references {
if _, exists := resourceIDs[reference.ResourceID]; exists {
matched[reference.ResourceID] = struct{}{}
}
}
return matched
}
// CollectOwnedDocumentReferences is kept as the compatibility API used by the
// history and deletion paths. The actual traversal lives in one collector so
// nested fields such as metadata.videoPreview.storageKey cannot be skipped by
// one of the callers.
func CollectOwnedDocumentReferences(raw string, resourceIDs map[string]struct{}) error {
references, err := CollectDocumentResourceReferences(raw)
if err != nil {
return err
}
for _, reference := range references {
resourceIDs[reference.ResourceID] = struct{}{}
}
return nil
}
func CollectDocumentResourceReferences(raw string) ([]DocumentResourceReference, error) {
raw = strings.TrimSpace(raw)
if raw == "" {
return nil
return nil, nil
}
var value any
if err := json.Unmarshal([]byte(raw), &value); err != nil {
return err
return nil, err
}
if scalar, ok := value.(string); ok {
if resourceID := ResourceID(scalar); resourceID != "" {
resourceIDs[resourceID] = struct{}{}
return []DocumentResourceReference{{ResourceID: resourceID, Path: "$", ReferenceType: "value"}}, nil
}
return nil
return nil, nil
}
walkReferenceDocument(value, "", resourceIDs)
return nil
references := make([]DocumentResourceReference, 0)
walkReferenceDocument(value, "", "", "", &references)
sort.Slice(references, func(i, j int) bool {
if references[i].Path != references[j].Path {
return references[i].Path < references[j].Path
}
if references[i].ResourceID != references[j].ResourceID {
return references[i].ResourceID < references[j].ResourceID
}
return references[i].ReferenceType < references[j].ReferenceType
})
return references, nil
}
func SortedIDs(values map[string]struct{}) []string {
@@ -65,30 +97,75 @@ func SortedIDs(values map[string]struct{}) []string {
return result
}
func walkReferenceDocument(value any, parentKey string, resourceIDs map[string]struct{}) {
func walkReferenceDocument(value any, parentKey, path, nodeID string, references *[]DocumentResourceReference) {
switch item := value.(type) {
case map[string]any:
for key, child := range item {
walkReferenceDocument(child, key, resourceIDs)
currentNodeID := nodeID
if candidate, ok := item["id"].(string); ok && parentKey == "nodes" {
currentNodeID = strings.TrimSpace(candidate)
}
keys := make([]string, 0, len(item))
for key := range item {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
child := item[key]
childPath := joinReferencePath(path, key)
if text, ok := child.(string); ok {
if resourceID := resourceIDForField(key, text); resourceID != "" {
*references = append(*references, DocumentResourceReference{
ResourceID: resourceID, Path: childPath, NodeID: currentNodeID, ReferenceType: key,
})
}
continue
}
walkReferenceDocument(child, key, childPath, currentNodeID, references)
}
case []any:
for _, child := range item {
walkReferenceDocument(child, parentKey, resourceIDs)
for index, child := range item {
childPath := arrayReferencePath(path, index, child)
walkReferenceDocument(child, parentKey, childPath, nodeID, references)
}
case string:
if isResourceLocatorField(parentKey) {
if resourceID := ResourceID(item); resourceID != "" {
resourceIDs[resourceID] = struct{}{}
}
}
if isBareResourceIDField(parentKey) {
if resourceID := ValidID(item); resourceID != "" {
resourceIDs[resourceID] = struct{}{}
}
if resourceID := resourceIDForField(parentKey, item); resourceID != "" {
*references = append(*references, DocumentResourceReference{
ResourceID: resourceID, Path: path, NodeID: nodeID, ReferenceType: parentKey,
})
}
}
}
func resourceIDForField(field, value string) string {
if isResourceLocatorField(field) {
return ResourceID(value)
}
if isBareResourceIDField(field) {
return ValidID(value)
}
return ""
}
func joinReferencePath(parent, child string) string {
if parent == "" {
return child
}
return parent + "." + child
}
func arrayReferencePath(parent string, index int, child any) string {
if item, ok := child.(map[string]any); ok {
if id, ok := item["id"].(string); ok && isCanvasCollectionPath(parent) && strings.TrimSpace(id) != "" {
return fmt.Sprintf("%s[%s]", parent, strings.TrimSpace(id))
}
}
return fmt.Sprintf("%s[%d]", parent, index)
}
func isCanvasCollectionPath(path string) bool {
return path == "nodes" || strings.HasSuffix(path, ".clips") || path == "clips"
}
func isBareResourceIDField(field string) bool {
switch field {
case "resourceId", "resourceIds", "sampleResourceId", "referenceResourceId", "referenceResourceIds":
@@ -0,0 +1,38 @@
package assets
import "testing"
func TestCollectDocumentResourceReferencesIncludesNestedVideoPreview(t *testing.T) {
raw := `{"nodes":[{"id":"node-video","metadata":{"videoPreview":{"storageKey":"resource:poster-1","source":{"id":"nested-not-node","url":"/api/resources/poster-2/file"}}}}]}`
refs, err := CollectDocumentResourceReferences(raw)
if err != nil {
t.Fatal(err)
}
got := map[string]DocumentResourceReference{}
for _, ref := range refs {
got[ref.ResourceID] = ref
}
if ref := got["poster-1"]; ref.Path != "nodes[node-video].metadata.videoPreview.storageKey" || ref.NodeID != "node-video" || ref.ReferenceType != "storageKey" {
t.Fatalf("nested storageKey reference = %#v", ref)
}
if ref := got["poster-2"]; ref.Path != "nodes[node-video].metadata.videoPreview.source.url" || ref.NodeID != "node-video" {
t.Fatalf("nested URL reference = %#v", ref)
}
}
func TestCollectDocumentResourceReferencesUsesExplicitFieldsOnly(t *testing.T) {
raw := `{"nodes":[{"id":"node-1","metadata":{"label":"resource:not-a-reference","videoPreview":{"resourceId":"poster-3"}}}],"log":"/api/resources/log-1/file"}`
refs, err := CollectDocumentResourceReferences(raw)
if err != nil {
t.Fatal(err)
}
if len(refs) != 1 || refs[0].ResourceID != "poster-3" || refs[0].NodeID != "node-1" || refs[0].ReferenceType != "resourceId" {
t.Fatalf("references = %#v", refs)
}
}
func TestCollectDocumentResourceReferencesReportsInvalidJSON(t *testing.T) {
if _, err := CollectDocumentResourceReferences(`{"nodes":`); err == nil {
t.Fatal("expected malformed document to be rejected")
}
}