/
handlers.go
221 lines (195 loc) · 6.02 KB
/
handlers.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package govideo
import (
"html/template"
"net"
"net/http"
"os"
"github.com/gogo/protobuf/jsonpb"
"github.com/julienschmidt/httprouter"
"github.com/mailru/easyjson"
"github.com/sickyoon/govideo/govideo/models"
)
const (
DATA = iota
SUBTITLE
)
func (a *App) index(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
t := template.Must(template.ParseFiles("templates/index.html"))
t.Execute(w, nil)
}
func (a *App) loginPost(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err := r.ParseMultipartForm(32 << 20)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusBadRequest)
return
}
// TODO: validate empty values
username := r.Form.Get("username")
password := r.Form.Get("password")
user, err := a.auth.Authenticate(w, r, username, password)
if err != nil {
// Clean up
a.auth.ClearUser(w, r)
ErrorHandler(w, err.Error(), http.StatusUnauthorized)
return
}
marshaller := jsonpb.Marshaler{
EmitDefaults: false,
OrigName: true,
}
err = marshaller.Marshal(w, user)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (a *App) logout(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
err := a.auth.ClearUser(w, r)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusInternalServerError)
return
}
response := models.GetSuccessResponse()
response.Msg = "OK"
easyjson.MarshalToHTTPResponseWriter(response, w)
models.RecycleSuccessResponse(response)
}
func (a *App) curUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
user, err := a.auth.CurUser(r)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusNoContent)
return
}
marshaller := jsonpb.Marshaler{
EmitDefaults: false,
OrigName: true,
}
err = marshaller.Marshal(w, user)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusInternalServerError)
return
}
}
// sync syncs database with real files for details
func (a *App) sync(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
err := a.Sync()
if err != nil {
ErrorHandler(w, err.Error(), http.StatusInternalServerError)
return
}
response := models.GetSuccessResponse()
response.Msg = "OK"
easyjson.MarshalToHTTPResponseWriter(response, w)
models.RecycleSuccessResponse(response)
}
// list returns json list of all available media
// in paths specified in configuration file
// all videos are added to dbs automatically
// this funciton just gets videos from dbs
// everytime video is requested, it returns fake path that lasts 24 hrs
func (a *App) list(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
user, err := a.auth.CurUser(r)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusForbidden)
return
}
mediaList, err := a.db.GetAllMedia(user.Email)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusInternalServerError)
return
}
// get client ip
ipAddr, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusInternalServerError)
return
}
// encode media path
for i := 0; i < len(mediaList.Data); i++ {
media := &mediaList.Data[i]
encodedPath, err := a.cache.GetEncodedPath(media, ipAddr)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusInternalServerError)
}
media.Path = encodedPath
}
easyjson.MarshalToHTTPResponseWriter(mediaList, w)
models.RecycleMediaList(mediaList)
}
// updateAccess updates access control for individual file
func (a *App) updateAccess(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// get file database id
// query to display access control field
// make sure the user has previlege (admin)
// update access control field with post values
}
// updateCategory updates category for individual file
func (a *App) updateCategory(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
}
func (a *App) infoFile(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
filepath := ps.ByName("encodedPath")
// get client ip
ipAddr, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusInternalServerError)
return
}
// query redis with fakepath to get real path
media, err := a.cache.GetMedia(filepath, ipAddr)
if err != nil {
ErrorHandler(w, "Invalid encoded path", http.StatusBadRequest)
return
}
easyjson.MarshalToHTTPResponseWriter(media, w)
models.RecycleMedia(media)
}
// serveFile serves actual video content in chunk based on encoded filepath
func (a *App) serveFile(dataType int) func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
filepath := ps.ByName("encodedPath")
// get client ip
ipAddr, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusInternalServerError)
return
}
// query redis with fakepath to get real path
media, err := a.cache.GetMedia(filepath, ipAddr)
if err != nil {
ErrorHandler(w, "Invalid encoded path", http.StatusBadRequest)
return
}
defer models.RecycleMedia(media)
var path string
switch dataType {
case DATA:
path = media.Path
case SUBTITLE:
path = media.Subtitle
default:
ErrorHandler(w, "invalid data type", http.StatusInternalServerError)
return
}
// matches them against query ip
info, err := os.Stat(path)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusInternalServerError)
return
}
fi, err := os.Open(path)
if err != nil {
ErrorHandler(w, err.Error(), http.StatusInternalServerError)
return
}
defer fi.Close()
http.ServeContent(w, r, "", info.ModTime(), fi)
}
}