fix(canvas): 资源引用 - 校验递归素材并修复断链
This commit is contained in:
1 parent
5bbd208bef
commit
117fa7d70b
24 files changed
+697
-218
No files matched your search
@@ -4,14 +4,19 @@ import (
|
||||
"encoding/json"
|
||||
"infinite-canvas/backend/internal/assets"
|
||||
"infinite-canvas/backend/internal/kernel"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
)
|
||||
|
||||
type MediaAssetReference struct {
|
||||
AssetID string
|
||||
ResourceID string
|
||||
AssetID string
|
||||
ResourceID string
|
||||
NodeID string
|
||||
Path string
|
||||
ReferenceType string
|
||||
}
|
||||
|
||||
// validateCanvasMediaAssets is the final server-side invariant for canvas sync:
|
||||
@@ -23,12 +28,16 @@ func (s *Service) ValidateCanvasMediaAssets(userID string, raw json.RawMessage)
|
||||
if err != nil {
|
||||
return kernel.BadAuthRequest("画布媒体数据格式错误")
|
||||
}
|
||||
if len(references) == 0 {
|
||||
allResourceReferences, err := assets.CollectDocumentResourceReferences(string(raw))
|
||||
if err != nil {
|
||||
return kernel.BadAuthRequest("画布媒体数据格式错误")
|
||||
}
|
||||
if len(references) == 0 && len(allResourceReferences) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
assetIDSet := make(map[string]struct{}, len(references))
|
||||
resourceIDSet := make(map[string]struct{}, len(references))
|
||||
resourceIDSet := make(map[string]struct{}, len(references)+len(allResourceReferences))
|
||||
for _, reference := range references {
|
||||
if reference.AssetID == "" {
|
||||
return kernel.BadAuthRequest("画布媒体尚未进入素材库,请等待同步完成后重试")
|
||||
@@ -36,6 +45,9 @@ func (s *Service) ValidateCanvasMediaAssets(userID string, raw json.RawMessage)
|
||||
assetIDSet[reference.AssetID] = struct{}{}
|
||||
resourceIDSet[reference.ResourceID] = struct{}{}
|
||||
}
|
||||
for _, reference := range allResourceReferences {
|
||||
resourceIDSet[reference.ResourceID] = struct{}{}
|
||||
}
|
||||
|
||||
ownedAssets, err := s.repo.AssetsForUserIDs(userID, assets.SortedIDs(assetIDSet))
|
||||
if err != nil {
|
||||
@@ -56,11 +68,18 @@ func (s *Service) ValidateCanvasMediaAssets(userID string, raw json.RawMessage)
|
||||
readyResources[resource.ID] = struct{}{}
|
||||
}
|
||||
}
|
||||
missing := make([]assets.DocumentResourceReference, 0)
|
||||
for _, reference := range allResourceReferences {
|
||||
if _, exists := readyResources[reference.ResourceID]; !exists {
|
||||
reference.Source = "current"
|
||||
missing = append(missing, reference)
|
||||
}
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return canvasResourcesMissingError(missing)
|
||||
}
|
||||
|
||||
for _, reference := range references {
|
||||
if _, exists := readyResources[reference.ResourceID]; !exists {
|
||||
return kernel.BadAuthRequest("画布媒体对应的云端资源不存在或尚未就绪,请重新上传")
|
||||
}
|
||||
resourceIDs, assetExists := assetResources[reference.AssetID]
|
||||
if !assetExists {
|
||||
return kernel.BadAuthRequest("画布媒体尚未进入素材库,请等待同步完成后重试")
|
||||
@@ -72,6 +91,18 @@ func (s *Service) ValidateCanvasMediaAssets(userID string, raw json.RawMessage)
|
||||
return nil
|
||||
}
|
||||
|
||||
func canvasResourcesMissingError(references []assets.DocumentResourceReference) *kernel.AppError {
|
||||
ids := make(map[string]struct{}, len(references))
|
||||
for _, reference := range references {
|
||||
ids[reference.ResourceID] = struct{}{}
|
||||
}
|
||||
err := kernel.NewAppError(http.StatusConflict, "画布引用的素材已变化,当前内容未被覆盖,请修复缺失素材后重试")
|
||||
err.Code = kernel.CodeCanvasResourcesMissing
|
||||
err.Reason = kernel.ReasonCanvasResourcesMissing
|
||||
err.Details = map[string]any{"resourceIds": assets.SortedIDs(ids), "missingResources": references}
|
||||
return err
|
||||
}
|
||||
|
||||
// validateAssetCanvasReferences prevents an Asset update from changing the
|
||||
// resource behind a canvas that already points at that Asset.
|
||||
func (s *Service) ValidateAssetCanvasReferences(userID string, asset model.Asset) error {
|
||||
@@ -131,6 +162,7 @@ func (s *Service) ValidateAssetReplacementCanvasReferences(userID string, replac
|
||||
func MediaAssetReferences(raw json.RawMessage) ([]MediaAssetReference, error) {
|
||||
var payload struct {
|
||||
Nodes []struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Metadata struct {
|
||||
AssetID string `json:"assetId"`
|
||||
@@ -160,15 +192,16 @@ func MediaAssetReferences(raw json.RawMessage) ([]MediaAssetReference, error) {
|
||||
if !isCanvasMediaKind(node.Type) {
|
||||
continue
|
||||
}
|
||||
assetID := strings.TrimSpace(node.Metadata.AssetID)
|
||||
resourceID := firstCanvasResourceID(node.Metadata.StorageKey, node.Metadata.Content)
|
||||
if resourceID == "" {
|
||||
continue
|
||||
if resourceID != "" {
|
||||
references = append(references, MediaAssetReference{
|
||||
AssetID: assetID, ResourceID: resourceID, NodeID: node.ID,
|
||||
Path: "nodes[" + node.ID + "].metadata.storageKey", ReferenceType: "storageKey",
|
||||
})
|
||||
}
|
||||
references = append(references, MediaAssetReference{
|
||||
AssetID: strings.TrimSpace(node.Metadata.AssetID), ResourceID: resourceID,
|
||||
})
|
||||
}
|
||||
for _, clip := range payload.Timeline.Clips {
|
||||
for index, clip := range payload.Timeline.Clips {
|
||||
media := clip.DirectMedia
|
||||
if media == nil || !isCanvasMediaKind(media.Kind) {
|
||||
continue
|
||||
@@ -179,6 +212,7 @@ func MediaAssetReferences(raw json.RawMessage) ([]MediaAssetReference, error) {
|
||||
}
|
||||
references = append(references, MediaAssetReference{
|
||||
AssetID: strings.TrimSpace(media.AssetID), ResourceID: resourceID,
|
||||
Path: "timeline.clips[" + strconv.Itoa(index) + "].directMedia", ReferenceType: "directMedia",
|
||||
})
|
||||
}
|
||||
return references, nil
|
||||
|
||||
@@ -25,15 +25,17 @@ func SaveDocumentWithHistory(repo *repository.Repository, before *model.CanvasPr
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var restoredIDs []string
|
||||
if reason == "before_restore" {
|
||||
refs := map[string]struct{}{}
|
||||
if err := assets.CollectOwnedDocumentReferences(after.PayloadJSON, refs); err != nil {
|
||||
return err
|
||||
}
|
||||
restoredIDs = assets.SortedIDs(refs)
|
||||
refs := map[string]struct{}{}
|
||||
if err := assets.CollectOwnedDocumentReferences(after.PayloadJSON, refs); err != nil {
|
||||
return err
|
||||
}
|
||||
return repo.SaveCanvasWithSnapshot(after, snapshot, resourceIDs, restoredIDs, after.UpdatedAt.Add(-canvasHistoryInterval), canvasHistoryLimit, reason == "before_restore")
|
||||
return repo.SaveCanvasWithSnapshot(after, snapshot, resourceIDs, assets.SortedIDs(refs), after.UpdatedAt.Add(-canvasHistoryInterval), canvasHistoryLimit, reason == "before_restore" || reason == "before_resource_repair")
|
||||
}
|
||||
|
||||
// Explicit repair keeps the damaged preimage for audit/undo, but only protects
|
||||
// resources that still exist. The incoming document remains strictly validated.
|
||||
func (s *Service) RepairUserCanvasProject(userID string, raw json.RawMessage) (UserDataSummary, error) {
|
||||
return s.upsertUserCanvasProjectWithHistory(userID, raw, "before_resource_repair")
|
||||
}
|
||||
|
||||
func canvasRevisionConflict() error {
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
package canvas
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"infinite-canvas/backend/internal/assets"
|
||||
"infinite-canvas/backend/internal/model"
|
||||
"infinite-canvas/backend/internal/repository"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
type CanvasResourceRepairIssue struct {
|
||||
CanvasID string `json:"canvasId"`
|
||||
References []assets.DocumentResourceReference `json:"missingResources"`
|
||||
Repaired bool `json:"repaired"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
type CanvasResourceRepairReport struct {
|
||||
Apply bool `json:"apply"`
|
||||
Scanned int `json:"scanned"`
|
||||
Repairable int `json:"repairable"`
|
||||
Repaired int `json:"repaired"`
|
||||
Unresolved int `json:"unresolved"`
|
||||
Issues []CanvasResourceRepairIssue `json:"issues"`
|
||||
}
|
||||
|
||||
// RepairCanvasResources is an explicit maintenance operation. It only removes
|
||||
// invalid derived video previews, never original media, and retains the preimage.
|
||||
// Each canvas commits independently with the normal ownership/revision checks.
|
||||
func RepairCanvasResources(db *gorm.DB, apply bool) (CanvasResourceRepairReport, error) {
|
||||
report := CanvasResourceRepairReport{Apply: apply, Issues: []CanvasResourceRepairIssue{}}
|
||||
db = db.Session(&gorm.Session{Logger: logger.Default.LogMode(logger.Silent)})
|
||||
repo := repository.New(db)
|
||||
svc := New(repo, nil)
|
||||
after := ""
|
||||
for {
|
||||
var rows []model.CanvasProject
|
||||
if err := db.Where("id > ?", after).Order("id ASC").Limit(100).Find(&rows).Error; err != nil {
|
||||
return report, errors.New("读取画布失败;此前已提交的修复保留,请按报告重试")
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
return report, nil
|
||||
}
|
||||
for _, project := range rows {
|
||||
after = project.ID
|
||||
report.Scanned++
|
||||
issue := CanvasResourceRepairIssue{CanvasID: project.ID}
|
||||
raw, err := canvasProjectPayload(project)
|
||||
if err != nil {
|
||||
issue.Reason = "画布 JSON 无效,需人工核查"
|
||||
report.Issues = append(report.Issues, issue)
|
||||
report.Unresolved++
|
||||
continue
|
||||
}
|
||||
refs, err := assets.CollectDocumentResourceReferences(string(raw))
|
||||
if err != nil {
|
||||
return report, errors.New("收集资源引用失败")
|
||||
}
|
||||
ids := map[string]struct{}{}
|
||||
for _, ref := range refs {
|
||||
ids[ref.ResourceID] = struct{}{}
|
||||
}
|
||||
resources, err := repo.ResourcesForUserIDs(project.UserID, assets.SortedIDs(ids))
|
||||
if err != nil {
|
||||
return report, errors.New("读取资源状态失败;未把查询失败视为缺失")
|
||||
}
|
||||
for _, resource := range resources {
|
||||
if resource.Status == model.ResourceStatusReady {
|
||||
delete(ids, resource.ID)
|
||||
}
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
continue
|
||||
}
|
||||
for _, ref := range refs {
|
||||
if _, missing := ids[ref.ResourceID]; missing {
|
||||
ref.Source = "current"
|
||||
issue.References = append(issue.References, ref)
|
||||
}
|
||||
}
|
||||
var payload map[string]json.RawMessage
|
||||
var nodes []map[string]json.RawMessage
|
||||
if json.Unmarshal(raw, &payload) != nil || json.Unmarshal(payload["nodes"], &nodes) != nil {
|
||||
issue.Reason = "节点数据无效,需人工核查"
|
||||
} else {
|
||||
for _, node := range nodes {
|
||||
var metadata map[string]json.RawMessage
|
||||
if json.Unmarshal(node["metadata"], &metadata) != nil || metadata["videoPreview"] == nil {
|
||||
continue
|
||||
}
|
||||
previewRefs, parseErr := assets.CollectDocumentResourceReferences(string(metadata["videoPreview"]))
|
||||
if parseErr != nil {
|
||||
continue
|
||||
}
|
||||
for _, ref := range previewRefs {
|
||||
if _, missing := ids[ref.ResourceID]; missing {
|
||||
delete(metadata, "videoPreview")
|
||||
node["metadata"], _ = json.Marshal(metadata)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
payload["nodes"], _ = json.Marshal(nodes)
|
||||
repaired, marshalErr := json.Marshal(payload)
|
||||
if marshalErr != nil {
|
||||
return report, marshalErr
|
||||
}
|
||||
if err := svc.ValidateCanvasMediaAssets(project.UserID, repaired); err != nil {
|
||||
issue.Reason = "仍有主媒体缺失或素材绑定错误;未修改画布,请恢复有效历史或重新上传"
|
||||
} else {
|
||||
report.Repairable++
|
||||
if apply {
|
||||
if _, err := svc.RepairUserCanvasProject(project.UserID, repaired); err != nil {
|
||||
issue.Reason = "修复保存失败(版本冲突或资源再次变化);未覆盖,请重新扫描"
|
||||
} else {
|
||||
issue.Repaired = true
|
||||
report.Repaired++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if issue.Reason != "" {
|
||||
report.Unresolved++
|
||||
}
|
||||
report.Issues = append(report.Issues, issue)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -293,8 +293,9 @@ func (s *Service) upsertUserCanvasProjectWithHistory(userID string, raw json.Raw
|
||||
if errors.Is(err, repository.ErrCanvasRevisionConflict) {
|
||||
return canvasRevisionConflict()
|
||||
}
|
||||
if errors.Is(err, repository.ErrCanvasHistoryResourceMissing) {
|
||||
return kernel.NewAppError(http.StatusConflict, "画布引用的素材已变化,当前内容未被覆盖,请保留草稿并重新加载")
|
||||
var missing *repository.CanvasHistoryResourceMissingError
|
||||
if errors.As(err, &missing) {
|
||||
return canvasResourcesMissingError(missing.References)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user