summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/imgup/main.go74
-rw-r--r--cli/imgup/stuff.go37
-rw-r--r--templates/cli.html6
3 files changed, 95 insertions, 22 deletions
diff --git a/cli/imgup/main.go b/cli/imgup/main.go
index 299a09f..a1f67e3 100644
--- a/cli/imgup/main.go
+++ b/cli/imgup/main.go
@@ -11,36 +11,58 @@ import (
//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 {
- 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)
+ 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 {
- 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")
+ 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 {
- 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")
+ 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
}
}
}
@@ -63,3 +85,15 @@ func printResponse(res []byte, name string) {
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)
+}
diff --git a/cli/imgup/stuff.go b/cli/imgup/stuff.go
new file mode 100644
index 0000000..13e3c29
--- /dev/null
+++ b/cli/imgup/stuff.go
@@ -0,0 +1,37 @@
+package main
+
+import (
+ "fmt"
+ "github.com/canerdogan/go-autoupdate"
+ "net/http"
+ "os"
+ "strings"
+)
+
+func isUrl(url string) bool {
+ return strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")
+}
+
+func isImage(filePath string) bool {
+ file, err := os.Open(filePath)
+ if err != nil {
+ fmt.Println("There was an error.")
+ fmt.Println("Technical details: " + err.Error())
+ return false
+ }
+ buff := make([]byte, 512)
+ _, err = file.Read(buff)
+ return strings.Contains(http.DetectContentType(buff), "image")
+}
+
+func doUpdate(url string) {
+ fmt.Println("Update binary from "+url, "...")
+ err, _ := update.FromUrl(url)
+ if err != nil {
+ fmt.Print("\n")
+ fmt.Println("Update failed!")
+ os.Exit(1)
+ }
+ fmt.Println("Update completed!")
+ fmt.Print("\n")
+}
diff --git a/templates/cli.html b/templates/cli.html
index 31a3a9c..df669fa 100644
--- a/templates/cli.html
+++ b/templates/cli.html
@@ -25,8 +25,10 @@
<p>You can compile imgup from source. You need a working Golang installation already 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
+ cd mandible <br>
+ go get . # Get all dependencies.<br>
+ cd cli/imgup <br>
+ go build # Compile the build.
</code></p>
<p>You are finished. Now you can start using imgup.</p>
</blockquote>