summaryrefslogtreecommitdiff
path: root/cli/imgup/main.go
blob: a1f67e3b6f0802e8609549aa745b997be9f50d26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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)
}