-
Notifications
You must be signed in to change notification settings - Fork 0
/
uirequest.go
115 lines (90 loc) · 2.13 KB
/
uirequest.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
package uirequest
import (
"errors"
"fmt"
"net/http"
"net/url"
"sync"
"github.com/dekoch/gouniversal/module/monmotion/dbstorage"
"github.com/dekoch/gouniversal/module/monmotion/mdimg"
"github.com/dekoch/gouniversal/shared/console"
"github.com/dekoch/gouniversal/shared/functions"
"github.com/dekoch/gouniversal/shared/token"
)
type UIRequest struct {
tokens token.Token
active bool
}
type request struct {
Values url.Values
UUID string
Token string
ImageID string
}
type response struct {
Content []byte
Status int
Err error
}
var mut sync.RWMutex
func (rt *UIRequest) LoadConfig() error {
mut.Lock()
defer mut.Unlock()
if rt.active {
return nil
}
http.HandleFunc("/monmotion/viewer/", rt.serve)
rt.active = true
return nil
}
func (rt *UIRequest) GetNewToken(uid string) string {
return rt.tokens.New(uid)
}
func (rt *UIRequest) serve(w http.ResponseWriter, r *http.Request) {
var req request
req.Values = r.URL.Query()
req.UUID = req.Values.Get("uuid")
req.Token = req.Values.Get("token")
req.ImageID = req.Values.Get("imageid")
var resp response
resp.Status = http.StatusOK
for i := 0; i <= 4; i++ {
if resp.Err == nil {
switch i {
case 0:
if functions.IsEmpty(req.UUID) {
resp.Err = errors.New("UUID not set")
resp.Status = http.StatusForbidden
}
case 1:
if functions.IsEmpty(req.Token) {
resp.Err = errors.New("token not set")
resp.Status = http.StatusForbidden
}
case 2:
if rt.tokens.Check(req.UUID, req.Token) == false {
resp.Err = errors.New("invalid token")
resp.Status = http.StatusForbidden
}
case 3:
if functions.IsEmpty(req.ImageID) {
resp.Err = errors.New("imageid not set")
resp.Status = http.StatusForbidden
}
case 4:
var img mdimg.MDImage
resp.Err = dbstorage.Stor.LoadImage(req.ImageID, &img)
resp.Content = img.Jpeg
}
}
}
if resp.Err != nil {
if resp.Status == http.StatusOK {
resp.Status = http.StatusInternalServerError
}
http.Error(w, resp.Err.Error(), resp.Status)
console.Output(req.UUID+"\t"+resp.Err.Error(), "")
} else {
fmt.Fprintf(w, "%s", resp.Content)
}
}