paths safeguard

This commit is contained in:
Ali Gator 2022-05-04 10:22:02 +02:00
parent ec2e5f3750
commit 96f7636f01
3 changed files with 30 additions and 3 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.jpg *.jpg
*.png *.png
output/

View File

@ -8,6 +8,7 @@ import (
"log" "log"
"math" "math"
"os" "os"
"path"
) )
// convert RGBA pixel to grayscale. BT.709 luminosity formula // convert RGBA pixel to grayscale. BT.709 luminosity formula
@ -115,7 +116,7 @@ func blendColor(color1 color.Color, color2 color.Color) color.Color {
// encode the image // encode the image
func encodeImage(imgData *image.RGBA) { func encodeImage(imgData *image.RGBA) {
outputFile := fmt.Sprintf("%s/%s", ConfigRegister.OutputDir, "output.jpg") outputFile := fmt.Sprintf("%s/%s", path.Clean(ConfigRegister.OutputDir), "output.jpg")
out, _ := os.Create(outputFile) out, _ := os.Create(outputFile)
defer out.Close() defer out.Close()
log.Println("Encoding the new image...") log.Println("Encoding the new image...")

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"log"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -48,13 +49,35 @@ func (c *Config) SetOutputDimensions() {
c.OutputHeight = height c.OutputHeight = height
} }
func (c *Config) CheckPaths() {
_, err := os.Stat(c.OutputDir)
if err != nil {
log.Fatal("Output dir does not exist :", c.OutputDir)
}
_, err = os.Stat(c.InputDir)
if err != nil {
log.Fatal("Input dir does not exist :", c.InputDir)
}
if (c.BaseImage != "") {
_, err = os.Stat(c.BaseImage)
if err != nil {
log.Fatal("Base image does not exist :", c.BaseImage)
}
}
}
func initConfigRegister() { func initConfigRegister() {
// default seed for the RNG // default seed for the RNG
seed := time.Now().UnixNano() seed := time.Now().UnixNano()
// command line arguments // command line arguments
flag.StringVar(&ConfigRegister.Method, "blending", "darken", "Blending methods : darken, lighten, average, fuckyfun") flag.StringVar(&ConfigRegister.Method, "blending", "darken", "Blending methods : darken, lighten, average, fuckyfun")
flag.StringVar(&ConfigRegister.OutputDir, "output", "./", "Output directory") flag.StringVar(&ConfigRegister.OutputDir, "output", "./output", "Output directory")
flag.StringVar(&ConfigRegister.InputDir, "input", "/home/gator/Photos", "Input directory. Where to look the images from") flag.StringVar(&ConfigRegister.InputDir, "input", "/home/gator/Photos", "Input directory. Where to look the images from")
flag.StringVar(&ConfigRegister.BaseImage, "base-img", "", "Path to the base image to work with. Random image if not set") flag.StringVar(&ConfigRegister.BaseImage, "base-img", "", "Path to the base image to work with. Random image if not set")
flag.StringVar(&ConfigRegister.Dimensions, "dimensions", "1280x1024", "Out image dimensions. <width>x<height>") flag.StringVar(&ConfigRegister.Dimensions, "dimensions", "1280x1024", "Out image dimensions. <width>x<height>")
@ -66,6 +89,8 @@ func initConfigRegister() {
flag.Parse() flag.Parse()
ConfigRegister.CheckPaths()
// set output's width and height // set output's width and height
ConfigRegister.SetOutputDimensions() ConfigRegister.SetOutputDimensions()
} }