diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-27 08:59:39 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-27 08:59:39 +0300 |
| commit | 30e63df03544b94ebc5fcb2a004d18a0d32a4247 (patch) | |
| tree | 2933b6a790f01c215806b3efcb644f533c0c60cd /internal/processor/audio.go | |
| parent | c6e0b5cc48dedb52477cb0060e6ebc8ca4f088f2 (diff) | |
processor: introduce PostBuilder registry to replace hardcoded switch
Replace the large, duplicate switch statements in planPost and commitPlan
with a PostBuilder interface registered per file extension.
- PostBuilder interface: Plan(srcPath, ext) (postPlan, error) and
Commit(plan, postDir, id, now) (*post.Post, []string, error)
- Per-type builders: txtBuilder, mdBuilder, imageBuilder, audioBuilder.
- Each builder self-registers via init() into a map[string]PostBuilder.
- Core processor loops are now extension-agnostic, satisfying OCP/DIP.
- All existing tests pass.
Diffstat (limited to 'internal/processor/audio.go')
| -rw-r--r-- | internal/processor/audio.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/internal/processor/audio.go b/internal/processor/audio.go index 68938cf..821b747 100644 --- a/internal/processor/audio.go +++ b/internal/processor/audio.go @@ -4,8 +4,47 @@ import ( "fmt" "io" "os" + "path/filepath" + "time" + + "codeberg.org/snonux/snonux/internal/post" ) +type audioBuilder struct{} + +func (audioBuilder) Plan(srcPath string, ext string) (postPlan, error) { + plan := postPlan{srcPath: srcPath, ext: ext} + if err := validateAudio(srcPath); err != nil { + return postPlan{}, err + } + return plan, nil +} + +func (audioBuilder) Commit(plan postPlan, postDir string, id string, now time.Time) (*post.Post, []string, error) { + outName := filepath.Base(plan.srcPath) + dst := filepath.Join(postDir, outName) + if err := copyFile(plan.srcPath, dst); err != nil { + return nil, nil, err + } + src := fmt.Sprintf("posts/%s/%s", id, outName) + html := fmt.Sprintf( + `<audio controls class="post-audio"><source src="%s" type="audio/mpeg">Your browser does not support audio.</audio>`, + src, + ) + p := &post.Post{ + ID: id, + Timestamp: now, + PostType: post.TypeAudio, + Content: html, + Assets: []string{outName}, + } + return p, nil, nil +} + +func init() { + register(".mp3", audioBuilder{}) +} + // validateAudio confirms the audio source file exists and is readable. func validateAudio(srcPath string) error { f, err := os.Open(srcPath) |
