package api import ( "context" "crypto/sha256" "droboticflow/internal/domain" "encoding/hex" "encoding/json" "fmt" "os" "path/filepath" "regexp" "strings" ) var sourceFingerprintPattern = regexp.MustCompile(`^[0-9a-f]{64}$`) func (s *server) sourceIndexManifestDocument(ctx context.Context, run *domain.RunRecord, artifact *domain.ExecutionArtifact) runArtifactManifestDocument { d := runArtifactManifestDocument{SchemaVersion: sourceIndexSchema, Recognized: true, Summary: map[string]any{"sourceIndex": true, "contentCount": 0}, Members: []runArtifactManifestMember{}} manifest, e := s.readSourceRevisionManifest(run, artifact) if e != nil { d.Message = "源引用修订校验失败" return d } fingerprint := stringValue(manifest["sourceFingerprint"]) if !sourceFingerprintPattern.MatchString(fingerprint) || stringValue(manifest["runId"]) != run.ID { d.Message = "源引用归属不一致" return d } var sources map[string]annotationPinnedSource b, _ := json.Marshal(manifest["sources"]) if e = json.Unmarshal(b, &sources); e != nil || len(sources) != 5 { d.Message = "源引用不完整" return d } for kind, source := range sources { ext := ".csv" if kind == "leftVideo" || kind == "rightVideo" { ext = ".mp4" } if kind != "leftVideo" && kind != "rightVideo" && kind != "leftPts" && kind != "rightPts" && kind != "imu" { d.Message = "源引用类型无效" return d } expected := filepath.Join(s.workspaceMountPath, "system", "runs", run.ID, "annotation-sources", fingerprint, kind+ext) if source.Path != expected || source.Version == "" || !sourceFingerprintPattern.MatchString(source.SHA256) || source.Bytes <= 0 { d.Message = "源引用路径无效" return d } version, e := s.annotationFileVersion(ctx, run.OrganizationID, source.Path) if e != nil || version != source.Version { d.Message = "固定源文件版本不可用" return d } } for _, raw := range listValue(manifest["items"]) { item := mapValue(raw) dir := stringValue(item["directory"]) if !regexp.MustCompile(`^slices/slice-[0-9]{6}$`).MatchString(dir) { d.Message = "源引用切片路径无效" return d } for _, kind := range []string{"leftVideo", "rightVideo"} { source := sources[kind] name := "left.mp4" if kind == "rightVideo" { name = "right.mp4" } member := s.newRunArtifactManifestMember(run, artifact, sourceIndexSchema, "source-video", dir, name, dir+"/"+name, source.Path, "video/mp4", source.Bytes, map[string]any{"sourceIndex": true, "sha256": source.SHA256}) member.pinnedSource = &source d.Members = append(d.Members, member) } files := mapValue(item["files"]) for _, kind := range []string{"annotation", "synchronization"} { file := mapValue(files[kind]) name := stringValue(file["name"]) expected := "annotation.json" if kind == "synchronization" { expected = "synchronized-slice.json" } if name != expected { d.Message = "源引用元数据缺失" return d } candidate := filepath.Join(artifact.Path, dir, name) member := s.newRunArtifactManifestMember(run, artifact, sourceIndexSchema, kind, dir, name, dir+"/"+name, candidate, "application/json", int64Value(file["bytes"]), map[string]any{"sha256": file["sha256"], "sourceIndex": true}) info, err := os.Stat(candidate) if err != nil || info.Size() > maxRunArtifactInlineMemberBytes { d.Message = "源引用元数据不可用" return d } content, err := os.ReadFile(candidate) digest := sha256.Sum256(content) if err != nil || hex.EncodeToString(digest[:]) != stringValue(file["sha256"]) { d.Message = "源引用元数据校验失败" return d } member.inlineContent = content member.ContentAvailable = true d.Members = append(d.Members, member) } member := s.newRunArtifactManifestMember(run, artifact, sourceIndexSchema, "source-index", dir, "source-index.json", dir+"/source-index.json", "", "application/json", 1, map[string]any{"sourceIndex": true}) member.inlineContent, _ = json.Marshal(map[string]any{"schemaVersion": sourceIndexSchema, "sourceFrames": item["sourceFrames"], "sourceFingerprint": fingerprint, "semantics": "half-open"}) member.SizeBytes = int64(len(member.inlineContent)) member.ContentAvailable = true d.Members = append(d.Members, member) } d.Summary["contentCount"] = len(d.Members) d.Summary["contentAvailableCount"] = countRunArtifactManifestMembersWithContent(d.Members) d.Summary["groups"] = runArtifactManifestMemberGroups(d.Members) return d } func (s *server) readSourceRevisionManifest(run *domain.RunRecord, artifact *domain.ExecutionArtifact) (map[string]any, error) { if artifact.Metadata == nil || artifact.Metadata.SHA256 == "" { return nil, fmt.Errorf("missing source revision checksum") } path := filepath.Join(artifact.Path, "revision-manifest.json") if _, ok := s.safeRunArtifactManifestLocalFile(run, path); !ok { return nil, fmt.Errorf("unsafe source revision path") } info, e := os.Stat(path) if e != nil { return nil, e } if info.Size() > maxRunArtifactManifestBytes { return nil, fmt.Errorf("source revision too large") } m, e := readVerifiedAnnotationJSON(path, artifact.Metadata.SHA256) if e != nil { return nil, e } if stringValue(m["schemaVersion"]) != sourceIndexSchema || !strings.HasSuffix(artifact.Path, fmt.Sprintf("revision-%06d", intValue(m["revision"]))) { return nil, fmt.Errorf("source revision identity mismatch") } return m, nil }