From 887a20e38adb4d717828fafe302b4158be03ac8b Mon Sep 17 00:00:00 2001 From: Daniel Schubert Date: Sun, 6 Sep 2026 20:33:15 +0200 Subject: [PATCH] structural improvements --- .../convertVideo.go | 2 +- go.mod | 3 + main.go | 51 ++++--------- optimizeImage.go | 38 ---------- optimizeImage/optimizeImage.go | 72 +++++++++++++++++++ 5 files changed, 91 insertions(+), 75 deletions(-) rename convertVideo.go => convertVideo/convertVideo.go (96%) create mode 100644 go.mod delete mode 100644 optimizeImage.go create mode 100644 optimizeImage/optimizeImage.go diff --git a/convertVideo.go b/convertVideo/convertVideo.go similarity index 96% rename from convertVideo.go rename to convertVideo/convertVideo.go index ab0a1e1..1d63cc5 100644 --- a/convertVideo.go +++ b/convertVideo/convertVideo.go @@ -1,4 +1,4 @@ -package main +package convertVideo import ( "fmt" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1118f6d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module media-converter + +go 1.27.1 diff --git a/main.go b/main.go index b91ace3..3dcb097 100644 --- a/main.go +++ b/main.go @@ -4,16 +4,23 @@ import ( "fmt" "io/fs" "log" - "os" + "media-converter/convertVideo" + "media-converter/optimizeImage" "path/filepath" "strings" "sync" ) -const touchedFile = ".touched" - -var touched = loadTouched() - +var imageExts = map[string]bool{ + ".jpeg": true, + ".jpg": true, + ".png": true, + ".gif": true, + ".bmp": true, + ".tiff": true, + ".tif": true, + ".webp": true, +} var wg sync.WaitGroup func main() { @@ -29,14 +36,14 @@ func main() { switch { case ext == ".mov": wg.Go(func() { - ConvertVideo(path) + convertVideo.ConvertVideo(path) }) case imageExts[ext]: - if touched[path] { + if optimizeImage.Touched[path] { fmt.Printf("Skipping %s (already touched)\n", path) return nil } - wg.Go(func() { OptimizeImage(path) }) + wg.Go(func() { optimizeImage.OptimizeImage(path) }) } return nil }) @@ -47,31 +54,3 @@ func main() { log.Fatal(err) } } - -func loadTouched() map[string]bool { - set := map[string]bool{} - data, err := os.ReadFile(touchedFile) - if err != nil { - return set - } - for _, line := range strings.Split(string(data), "\n") { - line = strings.TrimSpace(line) - if line != "" && !set[line] { - set[line] = true - } - } - return set -} - -func markTouched(path string) { - f, err := os.OpenFile(touchedFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - fmt.Printf("Error updating %s: %v\n", touchedFile, err) - return - } - defer f.Close() - - if _, err := fmt.Fprintln(f, path); err == nil { - touched[path] = true - } -} diff --git a/optimizeImage.go b/optimizeImage.go deleted file mode 100644 index 9f4db60..0000000 --- a/optimizeImage.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - "fmt" - "os/exec" -) - -var imageExts = map[string]bool{ - ".jpeg": true, - ".jpg": true, - ".png": true, - ".gif": true, - ".bmp": true, - ".tiff": true, - ".tif": true, - ".webp": true, -} - -func OptimizeImage(path string) { - - fmt.Println("optiimg") - cmd := exec.Command("mogrify", - "-strip", - "-interlace", "Plane", - "-sampling-factor", "4:2:0", - "-resize", "5000x3000", - "-quality", "80%", - path, - ) - - out, err := cmd.CombinedOutput() - if err != nil { - fmt.Printf("Error optimizing %s: %v\n%s", path, err, out) - } else { - markTouched(path) - fmt.Printf("Optimized %s\n", path) - } -} diff --git a/optimizeImage/optimizeImage.go b/optimizeImage/optimizeImage.go new file mode 100644 index 0000000..9dee245 --- /dev/null +++ b/optimizeImage/optimizeImage.go @@ -0,0 +1,72 @@ +package optimizeImage + +import ( + "fmt" + "os" + "os/exec" + "strings" +) + +var ImageExts = map[string]bool{ + ".jpeg": true, + ".jpg": true, + ".png": true, + ".gif": true, + ".bmp": true, + ".tiff": true, + ".tif": true, + ".webp": true, +} + +const touchedFile = ".touched" + +var Touched = loadTouched() + +func OptimizeImage(path string) { + + fmt.Println("optiimg") + cmd := exec.Command("mogrify", + "-strip", + "-interlace", "Plane", + "-sampling-factor", "4:2:0", + "-resize", "5000x3000", + "-quality", "80%", + path, + ) + + out, err := cmd.CombinedOutput() + if err != nil { + fmt.Printf("Error optimizing %s: %v\n%s", path, err, out) + } else { + markTouched(path) + fmt.Printf("Optimized %s\n", path) + } +} + +func loadTouched() map[string]bool { + set := map[string]bool{} + data, err := os.ReadFile(touchedFile) + if err != nil { + return set + } + for _, line := range strings.Split(string(data), "\n") { + line = strings.TrimSpace(line) + if line != "" && !set[line] { + set[line] = true + } + } + return set +} + +func markTouched(path string) { + f, err := os.OpenFile(touchedFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + fmt.Printf("Error updating %s: %v\n", touchedFile, err) + return + } + defer f.Close() + + if _, err := fmt.Fprintln(f, path); err == nil { + Touched[path] = true + } +}