From 7e4ccc40be46366fd8c0550aca1bfb6e73c3b5c6 Mon Sep 17 00:00:00 2001 From: Horus3 Date: Tue, 21 Apr 2015 05:57:21 +0200 Subject: Add cli program. --- .gitignore | 2 + cli/imgup/main.go | 65 +++++++++++++++++++++++++++ cli/imgup/struct.go | 18 ++++++++ cli/imgup/upload.go | 102 +++++++++++++++++++++++++++++++++++++++++++ templates/api.html | 1 - templates/cli.html | 39 ++++++++++++++++- templates/index.html | 3 +- uploadedfile/uploadedfile.go | 1 + 8 files changed, 228 insertions(+), 3 deletions(-) create mode 100644 cli/imgup/main.go create mode 100644 cli/imgup/struct.go create mode 100644 cli/imgup/upload.go diff --git a/.gitignore b/.gitignore index 1fe2226..2218d4a 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,7 @@ tags mandible config/conf.json files/ +cli/imgup/imgup +cli/imgup/imgup.sig *.sw[o-p] 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 +} diff --git a/templates/api.html b/templates/api.html index a88e584..2e11e55 100644 --- a/templates/api.html +++ b/templates/api.html @@ -9,7 +9,6 @@

Rest API:

Interfacing with Mandible is extremely simple:

-

Upload an image file:

diff --git a/templates/cli.html b/templates/cli.html index 9d50adb..0ce7535 100644 --- a/templates/cli.html +++ b/templates/cli.html @@ -7,8 +7,45 @@
-

TODO!

+

Imgup - CLI

+

Upload your images comfortable from the command line!

+
+

Usage:

+

imgup [FILE] [URL]

+

File have to be the path to a valid image file. Url must start with 'http://' or 'https://'

+
+ +

Installation:

+ +
+

Download:

+

Download the precompiled binary from here:

+

bin.iamfabulous.de/imgup

+

Currently there is only Linux x86_64 available.

+
+ +
+

Compiling from source:

+

You can compile imgup from source. You need a working Golang installation set up. Read here more.

+

cd $GOPATH
+ git clone https://git.iamfabulous.de/cgit.cgi/mandible
+ cd mandible/cli/imgup
+ go build +

+

You have a working binary now.

+
+ +

Example Usage:

+ +
+

$ imgup http://i.imgur.com/s9zxmYe.jpg
+Your image (http://i.imgur.com/s9zxmYe.jpg) was uploaded!
+			
+Follow this link: 
+  http://i.iamfabulous.de:8080/i/h/M/hMKg2qS
+			  
+Have a nice day!

diff --git a/templates/index.html b/templates/index.html index 64e06b6..2e64de8 100644 --- a/templates/index.html +++ b/templates/index.html @@ -58,7 +58,8 @@

CLI Program

-

TODO: Use the image hoster direct from the command line.

+

Use the image hoster direct from the command line.

+

$ imgup http://i.imgur.com/s9zxmYe.jpg cat.png

diff --git a/uploadedfile/uploadedfile.go b/uploadedfile/uploadedfile.go index a3c8345..ffe2fab 100644 --- a/uploadedfile/uploadedfile.go +++ b/uploadedfile/uploadedfile.go @@ -110,6 +110,7 @@ func (this *UploadedFile) FileSize() (int64, error) { } func (this *UploadedFile) Clean() { + // PANIC os.Remove(this.path) for _, thumb := range this.thumbs { -- cgit v1.2.3