Keep one verified copy of session audio
This commit is contained in:
@@ -12,31 +12,26 @@ import (
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/logging"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
|
||||
// S3MaterializeRequest describes one S3-backed audio materialization.
|
||||
type S3MaterializeRequest struct {
|
||||
Reporter logging.Reporter
|
||||
Store storage.ObjectStore
|
||||
Object storage.ObjectInfo
|
||||
Bucket string
|
||||
CacheRoot string
|
||||
CacheEnabled bool
|
||||
SpoolPath string
|
||||
DestPath string
|
||||
Reporter logging.Reporter
|
||||
Store storage.ObjectStore
|
||||
Object storage.ObjectInfo
|
||||
Bucket string
|
||||
DestPath string
|
||||
Prior *manifest.InputRecord
|
||||
}
|
||||
|
||||
// S3MaterializeResult captures local materialization provenance.
|
||||
type S3MaterializeResult struct {
|
||||
Checksum string
|
||||
CachePath string
|
||||
SpoolPath string
|
||||
CacheHit bool
|
||||
Reused bool
|
||||
Downloaded bool
|
||||
}
|
||||
|
||||
// MaterializeS3Audio installs one S3 audio object into the destination path,
|
||||
// reusing and refreshing the durable local cache when enabled.
|
||||
// MaterializeS3Audio verifies the recorded prepared copy or installs a fresh download.
|
||||
func MaterializeS3Audio(ctx context.Context, req S3MaterializeRequest) (S3MaterializeResult, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return S3MaterializeResult{}, err
|
||||
@@ -51,59 +46,33 @@ func MaterializeS3Audio(ctx context.Context, req S3MaterializeRequest) (S3Materi
|
||||
if strings.TrimSpace(req.DestPath) == "" {
|
||||
return S3MaterializeResult{}, fmt.Errorf("destination path is required")
|
||||
}
|
||||
|
||||
if req.Reporter != nil {
|
||||
req.Reporter.Info("selected audio source", "bucket", req.Bucket, "key", key, "destination", req.DestPath)
|
||||
}
|
||||
result := S3MaterializeResult{}
|
||||
if req.CacheEnabled && cacheIdentityEligible(req) {
|
||||
cachePath, err := artifacts.S3AudioCachePath(req.CacheRoot, req.Bucket, key)
|
||||
if err != nil {
|
||||
return S3MaterializeResult{}, fmt.Errorf("resolve audio cache path: %w", err)
|
||||
}
|
||||
result.CachePath = cachePath
|
||||
if checksum, ok, err := materializeVerifiedCachedAudio(ctx, req, cachePath); err != nil {
|
||||
return S3MaterializeResult{}, err
|
||||
} else if ok {
|
||||
result.Checksum = checksum
|
||||
result.CacheHit = true
|
||||
if req.Reporter != nil {
|
||||
req.Reporter.Progress("reusing cached audio", "audio", filepath.Base(req.DestPath))
|
||||
prior := req.Prior
|
||||
if prior != nil && prior.Kind == "audio" && prior.Source == "s3" && filepath.Clean(prior.Path) == filepath.Clean(req.DestPath) &&
|
||||
strings.TrimSpace(req.Bucket) != "" && prior.S3Bucket == strings.TrimSpace(req.Bucket) && prior.S3Key == key &&
|
||||
strings.TrimSpace(req.Object.ETag) != "" && strings.TrimSpace(prior.S3ETag) == strings.TrimSpace(req.Object.ETag) &&
|
||||
req.Object.Size > 0 && prior.S3Size == req.Object.Size && strings.TrimSpace(prior.Checksum) != "" {
|
||||
info, err := os.Lstat(req.DestPath)
|
||||
if err == nil && info.Mode().IsRegular() && info.Size() == req.Object.Size {
|
||||
checksum, err := artifacts.SHA256File(req.DestPath)
|
||||
if err == nil && strings.EqualFold(checksum, prior.Checksum) {
|
||||
if req.Reporter != nil {
|
||||
req.Reporter.Progress("reusing prepared audio", "audio", filepath.Base(req.DestPath))
|
||||
}
|
||||
return S3MaterializeResult{Checksum: checksum, Reused: true}, nil
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
}
|
||||
|
||||
spoolPath := strings.TrimSpace(req.SpoolPath)
|
||||
if spoolPath == "" {
|
||||
return S3MaterializeResult{}, fmt.Errorf("spool path is required for s3 audio download")
|
||||
}
|
||||
if err := downloadObjectAtomic(ctx, req.Store, key, spoolPath, req.Object.Size, req.Reporter); err != nil {
|
||||
if err := downloadObjectAtomic(ctx, req.Store, key, req.DestPath, req.Object.Size, req.Reporter); err != nil {
|
||||
return S3MaterializeResult{}, fmt.Errorf("download s3 audio object %q: %w", key, err)
|
||||
}
|
||||
|
||||
checksum, err := fileops.CopyFileAtomicWithChecksum(spoolPath, req.DestPath, fileops.WorkspaceFileMode)
|
||||
checksum, err := artifacts.SHA256File(req.DestPath)
|
||||
if err != nil {
|
||||
return S3MaterializeResult{}, fmt.Errorf("materialize downloaded audio %q: %w", filepath.Base(req.DestPath), err)
|
||||
return S3MaterializeResult{}, fmt.Errorf("checksum downloaded audio: %w", err)
|
||||
}
|
||||
result.Checksum = checksum
|
||||
result.SpoolPath = spoolPath
|
||||
result.Downloaded = true
|
||||
|
||||
if result.CachePath != "" {
|
||||
cacheChecksum, err := fileops.CopyFileAtomicWithChecksum(spoolPath, result.CachePath, fileops.WorkspaceFileMode)
|
||||
if err != nil {
|
||||
return S3MaterializeResult{}, fmt.Errorf("populate audio cache %q: %w", result.CachePath, err)
|
||||
}
|
||||
if cacheChecksum != result.Checksum {
|
||||
return S3MaterializeResult{}, fmt.Errorf("audio cache checksum differs from downloaded audio")
|
||||
}
|
||||
if err := writeAudioCacheIdentity(cacheIdentityPath(result.CachePath), cacheIdentityForRequest(req, cacheChecksum)); err != nil {
|
||||
return S3MaterializeResult{}, err
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
return S3MaterializeResult{Checksum: checksum, Downloaded: true}, nil
|
||||
}
|
||||
|
||||
func validateLocalAudio(path string, expectedSize int64) error {
|
||||
@@ -137,6 +106,9 @@ func downloadObjectAtomic(ctx context.Context, store storage.ObjectStore, key, d
|
||||
if err := validateLocalAudio(temporaryPath, expectedSize); err != nil {
|
||||
return fmt.Errorf("validate downloaded audio: %w", err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := fileops.InstallSiblingTempFile(temporaryPath, destPath, fileops.WorkspaceFileMode); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user