package app import ( "context" "fmt" "io" "path/filepath" "strings" "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" "gitea.maximumdirect.net/eric/narratio/internal/audio" "gitea.maximumdirect.net/eric/narratio/internal/config" "gitea.maximumdirect.net/eric/narratio/internal/fileops" "gitea.maximumdirect.net/eric/narratio/internal/manifest" ) // RestoreExecutionResult captures concrete file-install results for one restore execution. type RestoreExecutionResult struct { DownloadedCount int } func executeRestorePlan( ctx context.Context, cfg *config.Config, current *RemoteCurrentState, plan *RestorePlan, report *RestoreReport, store storage.ObjectStore, ) (*RestoreExecutionResult, error) { if cfg == nil || cfg.Pipeline == nil || cfg.Session == nil { return nil, fmt.Errorf("resolved config with pipeline/session is required") } if current == nil { return nil, fmt.Errorf("remote current state is required") } if plan == nil { return nil, fmt.Errorf("restore plan is required") } if store == nil { return nil, fmt.Errorf("remote object store is required") } sessionRoot := artifacts.SessionWorkDirForCampaign(cfg.Pipeline.Workspace.Root, cfg.Session.Campaign, cfg.Session.SessionID) manifestActions := make([]RestoreAction, 0, 1) actions := make([]RestoreAction, 0, len(plan.Actions)) for _, action := range plan.Actions { if action.Kind != RestoreActionDownload { continue } if action.LocalRelativePath == config.PathManifestFile { manifestActions = append(manifestActions, action) continue } actions = append(actions, action) } if len(manifestActions) > 1 { return nil, fmt.Errorf("restore plan includes multiple manifest download actions") } if len(manifestActions) == 1 { actions = append(actions, manifestActions[0]) } result := &RestoreExecutionResult{} for _, action := range actions { if err := executeRestoreDownloadAction(ctx, cfg, sessionRoot, current, action, store); err != nil { if report != nil { report.markFailed(action, err) } return nil, fmt.Errorf("install %q from %q: %w", action.LocalRelativePath, action.RemoteKey, err) } if report != nil { report.markDownloaded(action) } result.DownloadedCount++ } return result, nil } func executeRestoreDownloadAction( ctx context.Context, cfg *config.Config, sessionRoot string, current *RemoteCurrentState, action RestoreAction, store storage.ObjectStore, ) error { safeLocalPath, err := joinWithinSessionRoot(sessionRoot, action.LocalRelativePath) if err != nil { return fmt.Errorf("resolve safe local path: %w", err) } if strings.TrimSpace(action.LocalPath) != "" && filepath.Clean(action.LocalPath) != safeLocalPath { return fmt.Errorf("restore plan local path mismatch for %q", action.LocalRelativePath) } if restoreActionIsAudio(action) { return executeRestoreAudioAction(ctx, cfg, safeLocalPath, action, store) } if err := fileops.EnsureWorkspaceDirectory(filepath.Dir(safeLocalPath)); err != nil { return fmt.Errorf("create destination directory: %w", err) } temporary, err := fileops.DownloadToSiblingTemp(safeLocalPath, func(destination io.Writer) error { return storage.DownloadTo(ctx, store, action.RemoteKey, destination) }) if err != nil { return fmt.Errorf("download to temp file: %w", err) } defer func() { _ = temporary.Cleanup() }() if action.LocalRelativePath == config.PathManifestFile { file, err := temporary.Open() if err != nil { return fmt.Errorf("open restored manifest: %w", err) } err = validateRestoredManifest(ctx, cfg, current, file) closeErr := file.Close() if err != nil { return err } if closeErr != nil { return fmt.Errorf("close restored manifest: %w", closeErr) } } if err := temporary.Install(filepath.Base(safeLocalPath), fileops.WorkspaceFileMode); err != nil { return fmt.Errorf("install file atomically: %w", err) } return nil } func executeRestoreAudioAction( ctx context.Context, cfg *config.Config, safeLocalPath string, action RestoreAction, store storage.ObjectStore, ) error { if cfg == nil || cfg.Pipeline == nil || cfg.Pipeline.Storage.S3 == nil || cfg.Session == nil { return fmt.Errorf("resolved s3 config and session are required") } spoolDir := artifacts.SessionSpoolRestoreAudioDir(cfg.Pipeline.Spool.Root, cfg.Session.Campaign, cfg.Session.SessionID) spoolPath := filepath.Join(spoolDir, filepath.Base(safeLocalPath)) cacheEnabled := cfg.Pipeline.Cache.S3Audio == nil || *cfg.Pipeline.Cache.S3Audio _, err := audio.MaterializeS3Audio(ctx, audio.S3MaterializeRequest{ Store: store, Object: storage.ObjectInfo{ Key: action.RemoteKey, Size: action.Size, ETag: action.ETag, }, Bucket: strings.TrimSpace(cfg.Pipeline.Storage.S3.Bucket), CacheRoot: strings.TrimSpace(cfg.Pipeline.Cache.Root), CacheEnabled: cacheEnabled, SpoolPath: spoolPath, DestPath: safeLocalPath, }) if err != nil { return fmt.Errorf("materialize audio: %w", err) } return nil } func validateRestoredManifest(ctx context.Context, cfg *config.Config, current *RemoteCurrentState, source io.Reader) error { manifestStore := &manifest.LocalStore{} m, err := manifestStore.LoadReader(ctx, source) if err != nil { return fmt.Errorf("validate manifest decode: %w", err) } requestedSession := strings.TrimSpace(cfg.Session.SessionID) requestedCampaign := strings.TrimSpace(cfg.Session.Campaign) manifestSession := strings.TrimSpace(m.SessionID) manifestCampaign := strings.TrimSpace(m.Campaign) if manifestSession != requestedSession { return fmt.Errorf("manifest session_id %q does not match requested session_id %q", manifestSession, requestedSession) } if manifestCampaign == "" { return fmt.Errorf("manifest campaign is required") } if manifestCampaign != requestedCampaign { return fmt.Errorf("manifest campaign %q does not match requested campaign %q", manifestCampaign, requestedCampaign) } if current != nil { if expected := strings.TrimSpace(current.SessionID); expected != "" && manifestSession != expected { return fmt.Errorf("manifest session_id %q does not match discovered session_id %q", manifestSession, expected) } if expected := strings.TrimSpace(current.Campaign); expected != "" && manifestCampaign != expected { return fmt.Errorf("manifest campaign %q does not match discovered campaign %q", manifestCampaign, expected) } } return nil }