Merge imported products into local session state

This commit is contained in:
2026-12-02 16:24:33 +00:00
parent ad8a5df97b
commit e7fada4372
26 changed files with 638 additions and 1254 deletions

View File

@@ -1,7 +1,6 @@
package app
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
@@ -17,7 +16,6 @@ import (
"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.
@@ -58,23 +56,11 @@ func executeRestorePlan(
return nil, fmt.Errorf("open session root for restore: %w", err)
}
defer func() { _ = root.Close() }()
manifestActions := make([]RestoreAction, 0, 1)
actions := make([]RestoreAction, 0, len(plan.Actions))
var actions []RestoreAction
for _, action := range plan.Actions {
if action.Kind != RestoreActionDownload {
continue
if action.Kind == RestoreActionDownload {
actions = append(actions, action)
}
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{}
@@ -127,10 +113,6 @@ func executeRestoreDownloadAction(
}
temporaryPath, err := downloadRestoreTemp(safeLocalPath, func(destination io.Writer) error {
if len(action.VerifiedContent) > 0 {
_, err := io.Copy(destination, bytes.NewReader(action.VerifiedContent))
return err
}
_, err := storage.StreamObject(ctx, store, action.RemoteKey, destination, restoreExpectedSize(action))
return err
})
@@ -142,33 +124,6 @@ func executeRestoreDownloadAction(
return err
}
if isRestoreManifest(action.LocalRelativePath) {
file, err := os.Open(temporaryPath)
if err != nil {
return fmt.Errorf("open restored manifest: %w", err)
}
destinationRoot := sessionRoot
requireCurrentIdentity := action.LocalRelativePath == config.PathManifestFile
if !requireCurrentIdentity {
destinationRoot = filepath.Dir(safeLocalPath)
}
restored, prepareErr := prepareRestoredManifest(ctx, cfg, current, file, destinationRoot, requireCurrentIdentity)
closeErr := file.Close()
if prepareErr != nil {
return prepareErr
}
if closeErr != nil {
return fmt.Errorf("close restored manifest: %w", closeErr)
}
if err := validateRestoreDestinationLeaf(parent, leaf); err != nil {
return err
}
if err := (&manifest.LocalStore{}).Save(ctx, safeLocalPath, restored); err != nil {
return fmt.Errorf("install rebased manifest atomically: %w", err)
}
return nil
}
if err := validateRestoreDestinationLeaf(parent, leaf); err != nil {
return err
}
@@ -247,11 +202,6 @@ func validateRestoreDestinationLeaf(parent *os.Root, leaf string) error {
return nil
}
func isRestoreManifest(relativePath string) bool {
clean := filepath.ToSlash(filepath.Clean(strings.TrimSpace(relativePath)))
return clean == config.PathManifestFile || clean == config.PathPreviousDirSegment+"/"+config.PathManifestFile
}
func downloadRestoreTemp(destination string, download func(io.Writer) error) (string, error) {
if strings.TrimSpace(destination) == "" {
return "", fmt.Errorf("restore destination path is required")