Share sibling temporary file creation for downloads
This commit is contained in:
@@ -97,7 +97,7 @@ func validateLocalAudio(path string, expectedSize int64) error {
|
||||
|
||||
func downloadObjectAtomic(ctx context.Context, store storage.ObjectStore, key, destPath string, expected *int64, reporter logging.Reporter) (storage.ObjectInfo, error) {
|
||||
var observed storage.ObjectInfo
|
||||
temporaryPath, err := writeAudioTemp(destPath, func(destination io.Writer) error {
|
||||
temporaryPath, err := fileops.WriteSiblingTemp(destPath, func(destination io.Writer) error {
|
||||
if reporter != nil {
|
||||
reporter.Progress("downloading audio", "audio", filepath.Base(destPath))
|
||||
}
|
||||
@@ -123,31 +123,3 @@ func downloadObjectAtomic(ctx context.Context, store storage.ObjectStore, key, d
|
||||
}
|
||||
return observed, nil
|
||||
}
|
||||
|
||||
func writeAudioTemp(destination string, write func(io.Writer) error) (string, error) {
|
||||
if strings.TrimSpace(destination) == "" {
|
||||
return "", fmt.Errorf("destination path is required")
|
||||
}
|
||||
if write == nil {
|
||||
return "", fmt.Errorf("temporary audio writer is required")
|
||||
}
|
||||
dir := filepath.Dir(destination)
|
||||
if err := fileops.EnsureWorkspaceDirectory(dir); err != nil {
|
||||
return "", fmt.Errorf("create destination directory: %w", err)
|
||||
}
|
||||
temporary, err := os.CreateTemp(dir, "."+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 "", fmt.Errorf("write sibling temporary file: %w", err)
|
||||
}
|
||||
if err := temporary.Close(); err != nil {
|
||||
_ = os.Remove(temporaryPath)
|
||||
return "", fmt.Errorf("close sibling temporary file: %w", err)
|
||||
}
|
||||
return temporaryPath, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user