summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHorus32015-04-21 05:57:21 +0200
committerHorus32015-04-21 05:57:21 +0200
commit7e4ccc40be46366fd8c0550aca1bfb6e73c3b5c6 (patch)
tree659e0206d73607dbe2cdb75ab871e078d06d37ad
parentc92ed3c93c379368f615809aa599426890bf2057 (diff)
downloadmandible-7e4ccc40be46366fd8c0550aca1bfb6e73c3b5c6.tar.gz
Add cli program.
-rw-r--r--.gitignore2
-rw-r--r--cli/imgup/main.go65
-rw-r--r--cli/imgup/struct.go18
-rw-r--r--cli/imgup/upload.go102
-rw-r--r--templates/api.html1
-rw-r--r--templates/cli.html39
-rw-r--r--templates/index.html3
-rw-r--r--uploadedfile/uploadedfile.go1
8 files changed, 228 insertions, 3 deletions
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 @@
<h1>Rest API:</h1>
<p>Interfacing with Mandible is extremely simple:</p>
- <p></p>
<blockquote>
<h2>Upload an image file:</h2>
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 @@
<div class="row">
<div class="col-md-12 well">
- <h1>TODO!</h1>
+ <h1>Imgup - CLI</h1>
+ <p>Upload your images comfortable from the command line!</p>
+ <blockquote>
+ <h2>Usage:</h2>
+ <p><code>imgup [FILE] [URL]</code></h2></p>
+ <p>File have to be the path to a valid image file. Url must start with 'http://' or 'https://'</p>
+ </blockquote>
+
+ <h2>Installation:</h2>
+
+ <blockquote>
+ <h2>Download:</h2>
+ <p>Download the precompiled binary from here: </p>
+ <p><a href="http://bin.iamfabulous.de/imgup/">bin.iamfabulous.de/imgup</a></p>
+ <p>Currently there is only Linux x86_64 available.</p>
+ </blockquote>
+
+ <blockquote>
+ <h2>Compiling from source:</h2>
+ <p>You can compile imgup from source. You need a working Golang installation set up. Read <a href="http://golang.org/" title="Golang">here</a> more.</p>
+ <p><code>cd $GOPATH <br>
+ git clone https://git.iamfabulous.de/cgit.cgi/mandible <br>
+ cd mandible/cli/imgup <br>
+ go build
+ </code></p>
+ <p>You have a working binary now.</p>
+ </blockquote>
+
+ <h2>Example Usage:</h2>
+
+ <blockquote>
+ <p><pre><code>$ 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!</code></pre></p>
</div>
</div>
</div>
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 @@
</div>
<div class="col-md-6">
<h2><i class="fa fa-terminal"></i> CLI Program</h2>
- <p><strong>TODO:</strong> Use the image hoster direct from the command line.</p>
+ <p>Use the image hoster direct from the command line.</p>
+ <p><code>$ imgup http://i.imgur.com/s9zxmYe.jpg cat.png</code></p>
</div>
</div>
<div class="row">
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 {