From 30e63df03544b94ebc5fcb2a004d18a0d32a4247 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 27 Apr 2026 08:59:39 +0300 Subject: 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. --- internal/processor/audio.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'internal/processor/audio.go') 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( + ``, + 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) -- cgit v1.2.3