droboticflow/internal/worker/capture_v4_source.go

90 lines
3.8 KiB
Go

package worker
import (
"fmt"
"path"
"regexp"
"strings"
"droboticflow/internal/domain"
)
func workflowUsesV4CaptureDelivery(workflow domain.WorkflowRecord) bool {
for _, node := range workflow.Nodes {
if node.Data.OperatorID != "operator-builtin-package" {
continue
}
// Match the package operator's boolean parser so alternate true values
// cannot select v4 while silently bypassing the source preflight.
switch strings.ToLower(strings.TrimSpace(nodeHyperparamsMap(node)["DELIVERY_JSON_V4"])) {
case "1", "true", "yes", "on":
return true
}
}
return false
}
// validateV4CaptureSourceSelection checks the exact frozen selection, without
// listing storage or reconstructing a part from a whole-capture input. Call it
// before claiming a new launch; already-created runs keep their own inputs.
func validateV4CaptureSourceSelection(workflow domain.WorkflowRecord, record *domain.CaptureRecord) error {
if !workflowUsesV4CaptureDelivery(workflow) {
return nil
}
if record == nil {
return fmt.Errorf("v4 original source requires a capture record")
}
root, prefix := record.SourcePrefix, record.Prefix
if root == "" || path.Clean(root) != root || strings.HasPrefix(root, "/") || strings.ContainsAny(root, "\\\x00") || root == ".." || strings.HasPrefix(root, "../") {
return fmt.Errorf("v4 original source requires a safe capture root")
}
part := path.Base(prefix)
capture := path.Base(root)
if !regexp.MustCompile(`^part[0-9]{4,}$`).MatchString(part) || prefix != root+"/"+part || record.CaptureID != capture+"/"+part {
return fmt.Errorf("v4 original source requires one explicit capture/part selection")
}
if strings.TrimSpace(record.Bucket) == "" || strings.TrimSpace(record.Fingerprint) == "" {
return fmt.Errorf("v4 original source requires a bucket and snapshot fingerprint")
}
capPattern, partPattern := regexp.QuoteMeta(capture), regexp.QuoteMeta(part)
// These paths match the eight original-source roles consumed by v4. In
// particular, metadata is rooted at the capture, never at the part directory.
requirements := []captureRequirement{
{"左相机视频", partPattern + `/` + capPattern + `_camera_left_` + partPattern + `(?:_[a-z0-9_]+)?\.(?:mp4|mov|mkv)`},
{"右相机视频", partPattern + `/` + capPattern + `_camera_right_` + partPattern + `(?:_[a-z0-9_]+)?\.(?:mp4|mov|mkv)`},
{"左相机 PTS", partPattern + `/` + capPattern + `_camera_left_` + partPattern + `(?:_[a-z0-9_]+)?_pts\.csv`},
{"右相机 PTS", partPattern + `/` + capPattern + `_camera_right_` + partPattern + `(?:_[a-z0-9_]+)?_pts\.csv`},
{"IMU", partPattern + `/` + capPattern + `_imu_` + partPattern + `\.csv`},
{"相机标定", capPattern + `_calibration_camera\.yaml`},
{"IMU 标定", capPattern + `_calibration_imu\.yaml`},
{"meta_" + capture + ".json", `meta_` + capPattern + `\.json`},
}
counts := make([]int, len(requirements))
patterns := make([]*regexp.Regexp, len(requirements))
for i, requirement := range requirements {
patterns[i] = regexp.MustCompile(`(?i)^` + requirement.Pattern + `$`)
}
seen := map[string]bool{}
for _, key := range record.SourceKeys {
if !strings.HasPrefix(key, root+"/") || path.Clean(key) != key || strings.ContainsAny(key, "\\\x00") || seen[key] {
return fmt.Errorf("v4 original source contains an unsafe or duplicate object selection")
}
seen[key] = true
relative := strings.TrimPrefix(key, root+"/")
if strings.Contains(relative, "/") && !strings.HasPrefix(key, prefix+"/") {
return fmt.Errorf("v4 original source selection includes another part")
}
for i, pattern := range patterns {
if pattern.MatchString(relative) {
counts[i]++
}
}
}
for i, count := range counts {
if count != 1 {
return fmt.Errorf("v4 original source requires exactly one %s (found %d)", requirements[i].Name, count)
}
}
return nil
}