From 1870313424e2f7aa2b000e6fc646463f58f03445 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Mon, 20 Feb 2017 21:16:44 +1030 Subject: [PATCH] Go version, for the easier cross-platform lulz --- dau.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 dau.go diff --git a/dau.go b/dau.go new file mode 100644 index 0000000..e5d917f --- /dev/null +++ b/dau.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "strings" + "github.com/pborman/getopt" + "path/filepath" + "os" + "time" + "net/http" +) + +var last_check = time.Now() +var new_last_check = time.Now() + +func main() { + webhook_url, path := parse_options() + fmt.Println(webhook_url, path) + + // wander the path, forever + for { + err := filepath.Walk(path, check_file) + if err != nil { fmt.Printf("SHIT ERROR") } + //fmt.Printf("filepath.Walk() returned %v\n", err) + last_check = new_last_check + time.Sleep(500 * time.Millisecond) + } +} + + +func parse_options() (webhook_url string, path string) { + + // Declare the flags to be used + // helpFlag := getopt.Bool('h', "display help") + webhookFlag := getopt.StringLong("webhook", 'w', "", "webhook URL") + pathFlag := getopt.StringLong("directory", 'd', "", "directory") + + getopt.Parse() + + return *webhookFlag, *pathFlag + +} + +func check_file(path string, f os.FileInfo, err error) error { + // fmt.Println("Comparing", f.ModTime(), "to", last_check, "for", path) + + if f.ModTime().After(last_check) && f.Mode().IsRegular() { + fmt.Println("YES", path, "is new") + + if file_eligible(path) { + // process file + process_file(path) + } + + if new_last_check.Before(f.ModTime()) { + new_last_check = f.ModTime() + } + } + + return nil +} + +func file_eligible(file string) (bool) { + extension := strings.ToLower(filepath.Ext(file)) + if extension == ".png" || extension == ".jpg" || extension == ".gif" { + return true + } + return false +} + +func process_file(file string) { + fmt.Println("Uploading", file) +}