|
|
|
|
@ -2,8 +2,13 @@ package main |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"os" |
|
|
|
|
"io/fs" |
|
|
|
|
"fmt" |
|
|
|
|
"path/filepath" |
|
|
|
|
"strings" |
|
|
|
|
"image" |
|
|
|
|
"log" |
|
|
|
|
"regexp" |
|
|
|
|
"image/png" |
|
|
|
|
"github.com/disintegration/gift" |
|
|
|
|
"lcthw.dev/go/jankifier/filters" |
|
|
|
|
@ -48,3 +53,97 @@ func JankImage(settings Conversion, in_file string, out_file string) { |
|
|
|
|
|
|
|
|
|
SaveImage(out_file, out_img) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func UnfuckedPathSplit(path string) []string { |
|
|
|
|
path = filepath.ToSlash(path) |
|
|
|
|
// WARN: have to use strings.Split because fsnotify uses /, even on windows
|
|
|
|
|
return strings.Split(path, "/")[1:] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func SplitPathExt(path string) (string, string, bool) { |
|
|
|
|
split_path := UnfuckedPathSplit(path) |
|
|
|
|
source_name := strings.Join(split_path, "/") // Render wants / even on windows
|
|
|
|
|
|
|
|
|
|
ext := filepath.Ext(source_name) |
|
|
|
|
source_name, found := strings.CutSuffix(source_name, ext) |
|
|
|
|
return source_name, ext, found |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func RePrefixPath(path string, new_prefix string) string { |
|
|
|
|
split_path := UnfuckedPathSplit(path) |
|
|
|
|
|
|
|
|
|
prefixed_path := append([]string{new_prefix}, split_path...) |
|
|
|
|
|
|
|
|
|
res := filepath.Join(prefixed_path...) |
|
|
|
|
return filepath.ToSlash(res) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func SamePath(a string, b string) bool { |
|
|
|
|
return filepath.ToSlash(a) == filepath.ToSlash(b) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func MkdirPath(target_path string) error { |
|
|
|
|
target_dir := filepath.Dir(target_path) |
|
|
|
|
_, err := os.Stat(target_dir) |
|
|
|
|
|
|
|
|
|
if os.IsNotExist(err) { |
|
|
|
|
log.Println("MAKING: ", target_dir) |
|
|
|
|
err = os.MkdirAll(target_dir, 0750) |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal("making path to %s: %v", target_dir, err) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func Included(config Settings, path string) bool { |
|
|
|
|
_, ext, found := SplitPathExt(path) |
|
|
|
|
if !found { return false } |
|
|
|
|
|
|
|
|
|
for _, include_ext := range config.Include { |
|
|
|
|
if include_ext == ext { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func GetConversion(config Settings, path string) Conversion { |
|
|
|
|
for key, conversion := range config.Exceptions { |
|
|
|
|
match, err := regexp.MatchString(key, path) |
|
|
|
|
if err != nil { log.Fatalf("problem matching regex %s: %v", key, err) } |
|
|
|
|
|
|
|
|
|
if match { |
|
|
|
|
return conversion |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return config.Base |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func RenderImages(config Settings) error { |
|
|
|
|
err := filepath.WalkDir(config.Source, |
|
|
|
|
func(path string, d fs.DirEntry, err error) error { |
|
|
|
|
if !d.IsDir() && Included(config, path) { |
|
|
|
|
target := RePrefixPath(path, config.Target) |
|
|
|
|
|
|
|
|
|
err = MkdirPath(target) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatalf("failed to make path %s: %v", path, err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fmt.Println("FILE: ", path, "TARGET:", target) |
|
|
|
|
convert := GetConversion(config, path) |
|
|
|
|
JankImage(convert, path, target) |
|
|
|
|
} else { |
|
|
|
|
fmt.Println("SKIP:", path) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|