Share sibling temporary file creation for downloads

This commit is contained in:
2026-12-02 21:33:02 +00:00
parent 93876ff2cf
commit a623ecb518
8 changed files with 116 additions and 100 deletions

View File

@@ -112,7 +112,7 @@ func executeRestoreDownloadAction(
return executeRestoreAudioAction(ctx, cfg, safeLocalPath, parent, leaf, action, store)
}
temporaryPath, err := downloadRestoreTemp(safeLocalPath, func(destination io.Writer) error {
temporaryPath, err := fileops.WriteSiblingTemp(safeLocalPath, func(destination io.Writer) error {
_, err := storage.StreamObject(ctx, store, action.RemoteKey, destination, restoreExpectedSize(action))
return err
})
@@ -202,34 +202,6 @@ func validateRestoreDestinationLeaf(parent *os.Root, leaf string) error {
return nil
}
func downloadRestoreTemp(destination string, download func(io.Writer) error) (string, error) {
if strings.TrimSpace(destination) == "" {
return "", fmt.Errorf("restore destination path is required")
}
if download == nil {
return "", fmt.Errorf("restore temporary writer is required")
}
directory := filepath.Dir(destination)
if err := fileops.EnsureWorkspaceDirectory(directory); err != nil {
return "", fmt.Errorf("create destination directory: %w", err)
}
temporary, err := os.CreateTemp(directory, "."+filepath.Base(destination)+".tmp-*")
if err != nil {
return "", fmt.Errorf("create sibling temporary file: %w", err)
}
temporaryPath := temporary.Name()
if err := download(temporary); err != nil {
_ = temporary.Close()
_ = os.Remove(temporaryPath)
return "", err
}
if err := temporary.Close(); err != nil {
_ = os.Remove(temporaryPath)
return "", fmt.Errorf("close sibling temporary file: %w", err)
}
return temporaryPath, nil
}
func verifyRestoredObject(ctx context.Context, store storage.ObjectStore, action RestoreAction, temporaryPath string) error {
if strings.TrimSpace(action.SHA256) == "" && strings.TrimSpace(action.Generation) == "" {
return nil
@@ -292,15 +264,11 @@ func executeRestoreAudioAction(
if cfg == nil || cfg.Pipeline == nil || cfg.Pipeline.Storage.S3 == nil || cfg.Session == nil {
return fmt.Errorf("resolved s3 config and session are required")
}
temporary, err := os.CreateTemp(filepath.Dir(safeLocalPath), "."+filepath.Base(safeLocalPath)+".restore-*")
temporaryPath, err := fileops.WriteSiblingTemp(safeLocalPath, func(io.Writer) error { return nil })
if err != nil {
return fmt.Errorf("create sibling audio staging file: %w", err)
}
temporaryPath := temporary.Name()
if err := temporary.Close(); err != nil {
_ = os.Remove(temporaryPath)
return fmt.Errorf("close sibling audio staging file: %w", err)
}
defer func() { _ = os.Remove(temporaryPath) }()
_, err = audio.MaterializeS3Audio(ctx, audio.S3MaterializeRequest{