-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicon_api.go
93 lines (58 loc) · 1.33 KB
/
icon_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
package main
import (
//"encoding/json"
"log"
"net/http"
"github.com/madsportslab/glbs"
)
const (
UPDATE_USER_ICON = "UPDATE users " +
"SET icon=? " +
"WHERE id=?"
)
func updateUserIcon(id string, key string) bool {
_, err := data.Exec(
UPDATE_USER_ICON, key, id,
)
if err != nil {
log.Printf("%s updateUserIcon(): %s", APP_NAME, err.Error())
return false
} else {
return true
}
} // updateUserIcon
func iconHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
u := checkToken(r)
if u == nil {
w.WriteHeader(http.StatusUnauthorized)
} else {
file, header, err := r.FormFile("icon")
if err != nil {
log.Println(err)
} else {
defer file.Close()
if header.Size > ICON_MAX_BYTES {
log.Println("Icon exceeds maximum supported size.")
w.WriteHeader(http.StatusRequestEntityTooLarge)
} else {
glbs.SetNamespace("icons")
k := glbs.Put(file)
if k == nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
if !updateUserIcon(u.ID, *glbs.GetPath(*k)) {
w.WriteHeader(http.StatusInternalServerError)
}
}
}
}
}
case http.MethodGet:
case http.MethodDelete:
case http.MethodPut:
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
} // iconHandler