-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
153 lines (131 loc) · 3.56 KB
/
api.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package api
import (
"fmt"
"log"
"net/http"
"os"
"vvoid.pw/archivebuffer"
"vvoid.pw/randomizer"
)
const apiVersion = "v1"
var (
apiRootPath = formatAPIPath("")
apiPushPath = formatAPIPath("push")
)
var (
serverPort *int
apiStoragePath *string
apiSecret *string
)
const (
rootWelcomeMsg = "Welcome to the Pimba API."
methodNotAllowed = `{"error":"Method not allowed."}`
badRequest = `{"error":"Bad request."}`
internalServerError = `{"error":"Internal Server Error. Please, try again later."}`
unauthorized = `{"error":"Token is invalid!"}`
)
func Serve(port int, storagePath, secret string) {
serverPort = &port
apiStoragePath = &storagePath
apiSecret = &secret
http.Handle("/", http.FileServer(http.Dir(*apiStoragePath)))
http.HandleFunc(apiRootPath, rootHandler)
http.HandleFunc(apiPushPath, pushHandler)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", *serverPort), nil))
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, rootWelcomeMsg)
return
}
func pushHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
r.ParseMultipartForm(32 << 20)
w.Header().Set("Content-Type", "application/json")
file, _, err := r.FormFile("file")
if err != nil {
log.Println("pushHandler:", err)
http.Error(w, badRequest, http.StatusBadRequest)
return
}
defer file.Close()
ungzippedFile, err := archivebuffer.UngzipToBuffer(file)
if err != nil {
log.Println("pushHandler:", err)
http.Error(w, internalServerError, http.StatusInternalServerError)
return
}
var untarID string
if r.FormValue("name") != "" {
untarID = r.FormValue("name")
} else {
untarID = randomizer.GenerateRandomString(10)
}
untarPath := *apiStoragePath + "/" + untarID
dirChecker, err := isDirExist(untarPath)
if err != nil {
http.Error(w, internalServerError, http.StatusInternalServerError)
return
}
token := r.FormValue("token")
if dirChecker && token == "" {
http.Error(w, unauthorized, http.StatusUnauthorized)
return
} else if dirChecker {
claims, err := ParseToken(token, *apiSecret)
if err != nil {
http.Error(w, unauthorized, http.StatusUnauthorized)
return
}
if claims["bucket"] == untarID {
err = archivebuffer.UntarToFile(ungzippedFile, untarPath)
if err != nil {
log.Println("pushHandler:", err)
http.Error(w, internalServerError, http.StatusInternalServerError)
return
}
} else {
http.Error(w, unauthorized, http.StatusUnauthorized)
return
}
} else {
err = os.MkdirAll(untarPath, 0766)
if err != nil {
log.Println("untar:", err)
http.Error(w, internalServerError, http.StatusInternalServerError)
return
}
token, err = NewToken(untarID, *apiSecret)
if err != nil {
log.Println("pushHandler:", err)
http.Error(w, internalServerError, http.StatusInternalServerError)
return
}
err = archivebuffer.UntarToFile(ungzippedFile, untarPath)
if err != nil {
log.Println("pushHandler:", err)
http.Error(w, internalServerError, http.StatusInternalServerError)
return
}
}
jsonResponse := fmt.Sprintf(`{ "push-url": "%v", "token": "%v" }`,
r.Host+"/"+untarID, token)
fmt.Fprintf(w, jsonResponse)
return
} else {
http.Error(w, methodNotAllowed, http.StatusMethodNotAllowed)
return
}
}
func formatAPIPath(apiCall string) string {
return "/" + apiVersion + "/" + apiCall
}
func isDirExist(path string) (bool, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}