From 7e4ccc40be46366fd8c0550aca1bfb6e73c3b5c6 Mon Sep 17 00:00:00 2001 From: Horus3 Date: Tue, 21 Apr 2015 05:57:21 +0200 Subject: Add cli program. --- cli/imgup/main.go | 65 +++++++++++++++++++++++++++++++++ cli/imgup/struct.go | 18 ++++++++++ cli/imgup/upload.go | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 cli/imgup/main.go create mode 100644 cli/imgup/struct.go create mode 100644 cli/imgup/upload.go (limited to 'cli/imgup') diff --git a/cli/imgup/main.go b/cli/imgup/main.go new file mode 100644 index 0000000..660d07c --- /dev/null +++ b/cli/imgup/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" +) + +/* TODO: Make this configurable. */ + +//var apiHost = "http://127.0.0.1:8080" +var apiHost = "http://images.iamfabulous.de" +var apiUrl = apiHost + "/api/v1/" + +func main() { + if len(os.Args) == 1 { + pstat, _ := os.Stat(os.Args[0]) + programName := pstat.Name() + + fmt.Println("Usage: " + os.Args[0] + " [FILE] [URL]") + 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) + } + + for k, v := range os.Args { + if k > 0 { + if stat, err := os.Stat(v); err != nil { + 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 { + 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") + } + } + } + } + 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") + } +} diff --git a/cli/imgup/struct.go b/cli/imgup/struct.go new file mode 100644 index 0000000..9881ef6 --- /dev/null +++ b/cli/imgup/struct.go @@ -0,0 +1,18 @@ +package main + +type Data struct { + Height int `json:"height"` + Width int `json:"width"` + Link string `json:"link"` + Mime string `json:"mime"` + Error string `json:"error"` + Thumbs +} + +type Thumbs struct{} + +type response struct { + Data `json:"data"` + Success bool `json:"success"` + Status int `json:"status"` +} diff --git a/cli/imgup/upload.go b/cli/imgup/upload.go new file mode 100644 index 0000000..d2da497 --- /dev/null +++ b/cli/imgup/upload.go @@ -0,0 +1,102 @@ +package main + +import ( + "bytes" + "fmt" + "io" + "io/ioutil" + "mime/multipart" + "net/http" + "net/url" + "os" +) + +func fileUpload(url, file, name string) (err error) { + // Prepare a form that you will submit to that URL. + var b bytes.Buffer + w := multipart.NewWriter(&b) + // Add your image file + f, err := os.Open(file) + if err != nil { + return + } + fw, err := w.CreateFormFile("image", file) + if err != nil { + return + } + if _, err = io.Copy(fw, f); err != nil { + return + } + // Add the other fields + if fw, err = w.CreateFormField("key"); err != nil { + return + } + if _, err = fw.Write([]byte("KEY")); err != nil { + return + } + // Don't forget to close the multipart writer. + // If you don't close it, your request will be missing the terminating boundary. + w.Close() + + // Now that you have a form, you can submit it to your handler. + req, err := http.NewRequest("POST", url, &b) + if err != nil { + return + } + // Don't forget to set the content type, this will contain the boundary. + req.Header.Set("Content-Type", w.FormDataContentType()) + + // Submit the request + client := &http.Client{} + res, err := client.Do(req) + if err != nil { + return + } + + // Check the response + if res.StatusCode != http.StatusOK { + err = fmt.Errorf("bad status: %s", res.Status) + } + + defer res.Body.Close() + contents, err := ioutil.ReadAll(res.Body) + if err != nil { + return + } + + printResponse(contents, name) + + return +} + +func urlUpload(targetUrl, file, name string) (err error) { + form := url.Values{} + form.Set("image", file) + imageUrl := form.Encode() + req, err := http.NewRequest("POST", targetUrl, bytes.NewBuffer([]byte(imageUrl))) + if err != nil { + return + } + + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + client := &http.Client{} + res, err := client.Do(req) + if err != nil { + return + } + + if res.StatusCode != http.StatusOK { + err = fmt.Errorf("bad status: %s", res.Status) + } + + defer res.Body.Close() + contents, err := ioutil.ReadAll(res.Body) + if err != nil { + return + } + + printResponse(contents, name) + + return +} -- cgit v1.2.3