Files
narratio/internal/adapters/storage/temp_download.go

35 lines
852 B
Go
Raw Normal View History

package storage
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
// DownloadObjectToTemp downloads an object into a temporary file and returns
// the cleaned local path.
func DownloadObjectToTemp(ctx context.Context, store ObjectStore, key, pattern string, expected *int64) (string, error) {
if store == nil {
return "", fmt.Errorf("object store is required")
}
if strings.TrimSpace(pattern) == "" {
return "", fmt.Errorf("temp file pattern is required")
}
tmp, err := os.CreateTemp("", pattern)
if err != nil {
return "", fmt.Errorf("create temp file: %w", err)
}
path := tmp.Name()
_, transferErr := StreamObject(ctx, store, key, tmp, expected)
closeErr := tmp.Close()
if err := errors.Join(transferErr, closeErr); err != nil {
_ = os.Remove(path)
return "", err
}
return filepath.Clean(path), nil
}