forked from tinode/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdl_files.go
254 lines (220 loc) · 6.83 KB
/
hdl_files.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/******************************************************************************
*
* Description :
*
* Handler of large file uploads/downloads. Validates request first then calls
* a handler.
*
*****************************************************************************/
package main
import (
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"time"
"github.com/tinode/chat/server/logs"
"github.com/tinode/chat/server/store"
"github.com/tinode/chat/server/store/types"
)
func largeFileServe(wrt http.ResponseWriter, req *http.Request) {
now := types.TimeNow()
enc := json.NewEncoder(wrt)
mh := store.Store.GetMediaHandler()
statsInc("FileDownloadsTotal", 1)
writeHttpResponse := func(msg *ServerComMessage, err error) {
// Gorilla CompressHandler requires Content-Type to be set.
wrt.Header().Set("Content-Type", "application/json; charset=utf-8")
wrt.WriteHeader(msg.Ctrl.Code)
enc.Encode(msg)
if err != nil {
logs.Warn.Println("media serve:", err)
}
}
// Check for API key presence
if isValid, _ := checkAPIKey(getAPIKey(req)); !isValid {
writeHttpResponse(ErrAPIKeyRequired(now), nil)
return
}
// Check authorization: either auth information or SID must be present
uid, challenge, err := authHttpRequest(req)
if err != nil {
writeHttpResponse(decodeStoreError(err, "", "", now, nil), err)
return
}
if challenge != nil {
writeHttpResponse(InfoChallenge("", now, challenge), nil)
return
}
if uid.IsZero() {
// Not authenticated
writeHttpResponse(ErrAuthRequired("", "", now, now), nil)
return
}
// Check if this is a GET/OPTIONS/HEAD request.
if req.Method != http.MethodGet && req.Method != http.MethodHead && req.Method != http.MethodOptions {
writeHttpResponse(ErrOperationNotAllowed("", "", now), errors.New("method '"+req.Method+"' not allowed"))
return
}
// Check if media handler redirects or adds headers.
headers, statusCode, err := mh.Headers(req, true)
if err != nil {
writeHttpResponse(decodeStoreError(err, "", "", now, nil), err)
return
}
for name, value := range headers {
wrt.Header().Set(name, value)
}
if statusCode != 0 {
// The handler requested to terminate further processing.
wrt.WriteHeader(statusCode)
if req.Method == http.MethodGet {
enc.Encode(&ServerComMessage{
Ctrl: &MsgServerCtrl{
Code: statusCode,
Text: http.StatusText(statusCode),
Timestamp: now,
},
})
}
logs.Info.Println("media serve: completed with status", statusCode)
return
}
if req.Method == http.MethodHead || req.Method == http.MethodOptions {
wrt.WriteHeader(http.StatusOK)
logs.Info.Println("media serve: completed", req.Method)
return
}
fd, rsc, err := mh.Download(req.URL.String())
if err != nil {
writeHttpResponse(decodeStoreError(err, "", "", now, nil), err)
return
}
defer rsc.Close()
wrt.Header().Set("Content-Type", fd.MimeType)
wrt.Header().Set("Content-Disposition", "attachment")
http.ServeContent(wrt, req, "", fd.UpdatedAt, rsc)
logs.Info.Println("media serve: OK")
}
// largeFileReceive receives files from client over HTTP(S) and passes them to the configured media handler.
func largeFileReceive(wrt http.ResponseWriter, req *http.Request) {
now := types.TimeNow()
enc := json.NewEncoder(wrt)
mh := store.Store.GetMediaHandler()
statsInc("FileUploadsTotal", 1)
writeHttpResponse := func(msg *ServerComMessage, err error) {
// Gorilla CompressHandler requires Content-Type to be set.
wrt.Header().Set("Content-Type", "application/json; charset=utf-8")
wrt.WriteHeader(msg.Ctrl.Code)
enc.Encode(msg)
logs.Info.Println("media upload:", msg.Ctrl.Code, msg.Ctrl.Text, "/", err)
}
// Check if this is a POST/PUT/OPTIONS/HEAD request.
if req.Method != http.MethodPost && req.Method != http.MethodPut &&
req.Method != http.MethodHead && req.Method != http.MethodOptions {
writeHttpResponse(ErrOperationNotAllowed("", "", now), errors.New("method '"+req.Method+"' not allowed"))
return
}
if globals.maxFileUploadSize > 0 {
// Enforce maximum upload size.
req.Body = http.MaxBytesReader(wrt, req.Body, globals.maxFileUploadSize)
}
// Check for API key presence
if isValid, _ := checkAPIKey(getAPIKey(req)); !isValid {
writeHttpResponse(ErrAPIKeyRequired(now), nil)
return
}
msgID := req.FormValue("id")
// Check authorization: either auth information or SID must be present
uid, challenge, err := authHttpRequest(req)
if err != nil {
writeHttpResponse(decodeStoreError(err, msgID, "", now, nil), err)
return
}
if challenge != nil {
writeHttpResponse(InfoChallenge(msgID, now, challenge), nil)
return
}
if uid.IsZero() {
// Not authenticated
writeHttpResponse(ErrAuthRequired(msgID, "", now, now), nil)
return
}
// Check if uploads are handled elsewhere.
headers, statusCode, err := mh.Headers(req, false)
if err != nil {
writeHttpResponse(decodeStoreError(err, "", "", now, nil), err)
return
}
for name, value := range headers {
wrt.Header().Set(name, value)
}
if statusCode != 0 {
// The handler requested to terminate further processing.
wrt.WriteHeader(statusCode)
if req.Method == http.MethodPost || req.Method == http.MethodPut {
enc.Encode(&ServerComMessage{
Ctrl: &MsgServerCtrl{
Code: statusCode,
Text: http.StatusText(statusCode),
Timestamp: now,
},
})
}
logs.Info.Println("media upload: completed with status", statusCode)
return
}
if req.Method == http.MethodHead || req.Method == http.MethodOptions {
wrt.WriteHeader(http.StatusOK)
logs.Info.Println("media upload: completed", req.Method)
return
}
file, _, err := req.FormFile("file")
if err != nil {
if strings.Contains(err.Error(), "request body too large") {
writeHttpResponse(ErrTooLarge(msgID, "", now), err)
} else {
writeHttpResponse(ErrMalformed(msgID, "", now), err)
}
return
}
fdef := types.FileDef{}
fdef.Id = store.Store.GetUidString()
fdef.InitTimes()
fdef.User = uid.String()
buff := make([]byte, 512)
if _, err = file.Read(buff); err != nil {
writeHttpResponse(ErrUnknown(msgID, "", now), err)
return
}
fdef.MimeType = http.DetectContentType(buff)
if _, err = file.Seek(0, io.SeekStart); err != nil {
writeHttpResponse(ErrUnknown(msgID, "", now), err)
return
}
url, err := mh.Upload(&fdef, file)
if err != nil {
writeHttpResponse(decodeStoreError(err, msgID, "", now, nil), err)
return
}
writeHttpResponse(NoErrParams(msgID, "", now, map[string]string{"url": url}), nil)
}
func largeFileRunGarbageCollection(period time.Duration, block int) chan<- bool {
// Unbuffered stop channel. Whoever stops it must wait for the process to finish.
stop := make(chan bool)
go func() {
gcTimer := time.Tick(period)
for {
select {
case <-gcTimer:
if err := store.Files.DeleteUnused(time.Now().Add(-time.Hour), block); err != nil {
logs.Warn.Println("media gc:", err)
}
case <-stop:
return
}
}
}()
return stop
}