resource_manager/main.go

61 lines
1.2 KiB
Go

package main
import (
"flag"
"git.bbr-dev.info/brajkovic/resource_manager/image"
"io/fs"
"io/ioutil"
"log"
"path/filepath"
"strings"
)
func main() {
folder := flag.Bool("folder", false, "copying folders instead of files")
in := flag.String("in", "", "input file/folder")
out := flag.String("out", "", "output file/folder")
x := flag.Int("x", 400, "min width")
y := flag.Int("y", 300, "min height")
flag.Parse()
if *folder {
resizeFolder(*in, *out, *x, *y)
} else {
resize(*in, *out, *x, *y)
}
}
func resizeFolder(src, dest string, x, y int) {
filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
if !info.IsDir() {
destination := strings.ReplaceAll(path, src, dest)
log.Printf("copying %s resized to %s", path, destination)
resize(path, destination, x, y)
}
return err
})
}
func resize(src, dest string, x, y int) {
if src == "" || dest == "" {
log.Fatalln("Missing src or dest")
}
source, err := ioutil.ReadFile(src)
if err != nil {
log.Panic(err)
}
destination, err := image.ResizeImage(source, x, y)
if err != nil {
log.Panic(err)
}
err = ioutil.WriteFile(dest, destination, 0644)
if err != nil {
log.Panic(err)
}
}