package main import ( "encoding/json" "fmt" "os" ) /* TODO: Make this configurable. */ //var apiHost = "http://127.0.0.1:8080" var apiHost = "https://images.iamfabulous.de" var apiUrl = apiHost + "/api/v1/" var updateUrl = "https://bin.iamfabulous.de/imgup/linux-x86_64/imgup" func main() { if len(os.Args) == 1 { printHelp() } for k, v := range os.Args { if k > 0 { if v == "--update" { doUpdate(updateUrl) continue } if v == "--help" || v == "-h" { printHelp() continue } if stat, err := os.Stat(v); err != nil { if isUrl(v) { fmt.Println("Fetching image from " + v) fmt.Print("\n") err := urlUpload(apiUrl+"url", v, v) if err != nil { fmt.Println("There is an error. Are you sure the url is correct?") fmt.Printf("\n") fmt.Println("Technical information: ") fmt.Println(" "+err.Error(), "\n") } } else { fmt.Println("There is an error!") fmt.Print("\n") fmt.Println(v + " is not an url.") fmt.Println("Upload cancelled.\n") continue } } else { if isImage(v) { fmt.Println("Starting to upload " + stat.Name() + "...") fmt.Print("\n") err = fileUpload(apiUrl+"file", v, stat.Name()) if err != nil { fmt.Println("There is an error. Are you sure the url is correct?") fmt.Printf("\n") fmt.Println("Technical information: ") fmt.Println(" "+err.Error(), "\n") } } else { fmt.Println("There is an error!") fmt.Print("\n") fmt.Println(v + " is not an image file.") fmt.Println("Upload cancelled.\n") continue } } } } fmt.Println("Have a nice day!") } func printResponse(res []byte, name string) { r := response{} json.Unmarshal(res, &r) if r.Success { fmt.Println("Your image (" + name + ") was uploaded!\n") fmt.Println("Follow this link: \n" + " " + r.Data.Link) fmt.Print("\n") } else { fmt.Println("There was an error during upload. (" + name + ")") fmt.Print("\n") fmt.Println("The website says: \n " + r.Data.Error) fmt.Print("\n") } } func printHelp() { pstat, _ := os.Stat(os.Args[0]) programName := pstat.Name() fmt.Println("Usage: " + os.Args[0] + " [FILE] [URL]") fmt.Println(" " + os.Args[0] + " --update updates " + programName + " to newest version.") fmt.Print("\n") fmt.Println(programName + " uploads an image to " + apiHost + ".") //fmt.Println("FILE should be a path to a image file.") os.Exit(0) }