structural improvements
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user