Check path before starting to prevent crash. Show id of upload.

This commit is contained in:
Justin Hawkins 2017-02-21 17:10:00 +10:30
parent d2d7843b6f
commit 68d9ab7859

38
dau.go
View File

@ -24,20 +24,12 @@ var new_last_check = time.Now()
var webhook_url string var webhook_url string
var username string var username string
type webhook_response struct {
Test string
}
func keepLines(s string, n int) string {
result := strings.Join(strings.Split(s, "\n")[:n], "\n")
return strings.Replace(result, "\r", "", -1)
}
func main() { func main() {
webhook_opt, path, watch, username_opt := parse_options() webhook_opt, path, watch, username_opt := parse_options()
webhook_url = webhook_opt webhook_url = webhook_opt
username = username_opt username = username_opt
check_path(path)
check_updates() check_updates()
log.Print("Waiting for images to appear in ", path) log.Print("Waiting for images to appear in ", path)
@ -45,13 +37,23 @@ func main() {
// wander the path, forever // wander the path, forever
for { for {
err := filepath.Walk(path, check_file) err := filepath.Walk(path, check_file)
if err != nil { log.Fatal("oh dear") } if err != nil { log.Fatal("could not watch path", err) }
//fmt.Printf("filepath.Walk() returned %v\n", err)
last_check = new_last_check last_check = new_last_check
time.Sleep(time.Duration(watch)*time.Second) time.Sleep(time.Duration(watch)*time.Second)
} }
} }
func check_path(path string) {
src, err := os.Stat(path)
if err != nil {
log.Fatal(err)
}
if !src.IsDir() {
log.Fatal(path, " is not a directory")
os.Exit(1)
}
}
func check_updates() { func check_updates() {
type GithubRelease struct { type GithubRelease struct {
@ -80,16 +82,14 @@ func check_updates() {
} }
if (current_version < latest.Tag_name) { if (current_version < latest.Tag_name) {
fmt.Println("A new version is available:", latest.Tag_name) fmt.Printf("You are currently on version %s, but version %s is available\n", current_version, latest.Tag_name)
fmt.Println("----------- Release Info -----------") fmt.Println("----------- Release Info -----------")
fmt.Println(latest.Body) fmt.Println(latest.Body)
fmt.Println("------------------------------------") fmt.Println("------------------------------------")
fmt.Println("( You are currently on version:", current_version, ")")
} }
} }
func parse_options() (webhook_url string, path string, watch int, username string) { func parse_options() (webhook_url string, path string, watch int, username string) {
// Declare the flags to be used // Declare the flags to be used
@ -154,9 +154,7 @@ func file_eligible(file string) (bool) {
func process_file(file string) { func process_file(file string) {
log.Print("Uploading ", file) log.Print("Uploading ", file)
extraParams := map[string]string{ extraParams := map[string]string{ }
// "username": "Some username",
}
if (username != "") { if (username != "") {
extraParams["username"] = username extraParams["username"] = username
@ -173,7 +171,7 @@ func process_file(file string) {
type DiscordAPIResponse struct { type DiscordAPIResponse struct {
Attachments []DiscordAPIResponseAttachment Attachments []DiscordAPIResponseAttachment
id int64 Id int64 `json:",string"`
} }
request, err := newfileUploadRequest(webhook_url, extraParams, "file", file) request, err := newfileUploadRequest(webhook_url, extraParams, "file", file)
@ -205,7 +203,7 @@ func process_file(file string) {
if (err != nil) { if (err != nil) {
log.Print("could not parse JSON: ", err) log.Print("could not parse JSON: ", err)
fmt.Println("Response was:", res_body) fmt.Println("Response was:", string(res_body[:]))
return return
} }
if (len(res.Attachments) < 1) { if (len(res.Attachments) < 1) {
@ -217,7 +215,7 @@ func process_file(file string) {
rate := float64(a.Size) / elapsed.Seconds() / 1024.0 rate := float64(a.Size) / elapsed.Seconds() / 1024.0
log.Printf("Uploaded to %s %dx%d", a.Url, a.Width, a.Height) log.Printf("Uploaded to %s %dx%d", a.Url, a.Width, a.Height)
log.Printf("%d bytes transferred in %.2f seconds (%.2f KiB/s)", a.Size, elapsed.Seconds(), rate) log.Printf("id: %d, %d bytes transferred in %.2f seconds (%.2f KiB/s)", res.Id, a.Size, elapsed.Seconds(), rate)
} }
} }