-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
138 lines (108 loc) · 3.11 KB
/
main.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"github.com/elgs/gostrgen"
"github.com/joho/godotenv"
"github.com/julienschmidt/httprouter"
"github.com/sirupsen/logrus"
)
type Response struct {
Success bool
Message string
Name string
}
type SimpleResponse struct {
Success bool
Message string
}
var peopleServed = 0
var successfulServed = 0
var unsuccessfulServed = 0
type StatsResponse struct {
PeopleServed int
SuccessfulPeopleServed int
UnsuccessfulPeopleServed int
}
func upload(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
logrus.Info("Handling upload request")
if r.Method == "POST" {
r.ParseMultipartForm(10000000)
key := r.Form.Get("key")
if key != os.Getenv("UPLOAD_KEY") {
json.NewEncoder(w).Encode(SimpleResponse{Success: false, Message: "Invalid upload key"})
return
}
file, _, err := r.FormFile("img")
if err != nil {
logrus.Error(err.Error())
json.NewEncoder(w).Encode(SimpleResponse{Success: false, Message: "Could not upload: " + err.Error()})
return
}
defer file.Close()
randName := randString(6)
f, err := os.OpenFile(os.Getenv("UPLOAD_LOCATION")+"/"+randName+".png", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
logrus.Error(err.Error())
json.NewEncoder(w).Encode(SimpleResponse{Success: false, Message: "Could not open new file"})
return
}
defer f.Close()
io.Copy(f, file)
json.NewEncoder(w).Encode(Response{Success: true, Message: "File uploaded", Name: randName})
} else {
json.NewEncoder(w).Encode(SimpleResponse{Success: false, Message: "Invalid method"})
}
}
func serveImage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
imgName := ps.ByName("imgname")
if imgName == "stats" {
// lol thanks httprouter
stats(w, r, ps)
return
}
peopleServed += 1
filePath := os.Getenv("UPLOAD_LOCATION") + "/" + imgName + ".png"
_, err := ioutil.ReadFile(filePath)
if err != nil {
unsuccessfulServed += 1
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(SimpleResponse{Success: false, Message: "Unknown file"})
return
}
logrus.Info("serving " + imgName)
successfulServed += 1
http.ServeFile(w, r, filePath)
}
func stats(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
json.NewEncoder(w).Encode(StatsResponse{PeopleServed: peopleServed, SuccessfulPeopleServed: successfulServed, UnsuccessfulPeopleServed: unsuccessfulServed})
}
func main() {
err := godotenv.Load()
if err != nil {
fmt.Println("Cannot load .env file, please make sure it is created.")
}
logrus.Info("starting...")
err = os.Mkdir(os.Getenv("UPLOAD_LOCATION"), os.ModeDir)
if err != nil {
logrus.Error("could not make directory: " + err.Error())
}
r := httprouter.New()
r.POST("/upload", upload)
r.GET("/:imgname", serveImage)
if err := http.ListenAndServe(os.Getenv("BIND_HOST")+":"+os.Getenv("BIND_PORT"), r); err != nil {
logrus.Error(err.Error())
os.Exit(1)
}
}
func randString(n int) string {
r, e := gostrgen.RandGen(n, gostrgen.Lower|gostrgen.Upper, "", "")
if e != nil {
logrus.Error("Could not generate random string")
}
return r
}