Share sibling temporary file creation for downloads
This commit is contained in:
@@ -58,7 +58,7 @@ func hydratePreviousSessionArtifacts(
|
||||
return nil, fmt.Errorf("create previous-session path directory for %q: %w", record.LocalPath, err)
|
||||
}
|
||||
|
||||
temporaryPath, err := writePreviousTemp(record.LocalPath, func(destination io.Writer) error {
|
||||
temporaryPath, err := fileops.WriteSiblingTemp(record.LocalPath, func(destination io.Writer) error {
|
||||
_, err := storage.StreamObject(ctx, env.ObjectStore, record.RemoteKey, destination, &record.Size)
|
||||
return err
|
||||
})
|
||||
@@ -99,34 +99,6 @@ func hydratePreviousSessionArtifacts(
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func writePreviousTemp(destination string, write func(io.Writer) error) (string, error) {
|
||||
if strings.TrimSpace(destination) == "" {
|
||||
return "", fmt.Errorf("previous-session destination path is required")
|
||||
}
|
||||
if write == nil {
|
||||
return "", fmt.Errorf("previous-session temporary writer is required")
|
||||
}
|
||||
directory := filepath.Dir(destination)
|
||||
if err := fileops.EnsureWorkspaceDirectory(directory); err != nil {
|
||||
return "", fmt.Errorf("create previous-session path 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 := write(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 validatePreviousTemp(record previouscache.Record, temporaryPath string) (string, error) {
|
||||
info, err := os.Stat(temporaryPath)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user