-
Notifications
You must be signed in to change notification settings - Fork 7
/
jfu.go
399 lines (381 loc) · 10.3 KB
/
jfu.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Copyright (c) 2012 Jason McVetta. This is Free Software, released under the
* terms of the WTFPL v2. It comes without any warranty, express or implied.
* See http://sam.zoy.org/wtfpl/COPYING for more details.
*
*
* Derived from:
*
* jQuery File Upload Plugin GAE Go Example 2.0
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2011, Sebastian Tschan
* https://blueimp.net
*
* Original software by Tschan licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*/
// Package jfu provides backend support for the jQuery File Upload plugin.
package jfu
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/bmizerany/mc"
"github.com/jmcvetta/jfu/resize"
"image"
"image/png"
"io"
"log"
"mime/multipart"
"net/http"
"net/url"
"regexp"
"strings"
// Register image handling libraries by importing them.
_ "image/gif"
_ "image/jpeg"
_ "image/png"
)
const (
IMAGE_TYPES = "image/(gif|p?jpeg|(x-)?png)"
)
var (
DefaultConfig = Config{
MinFileSize: 1, // bytes
MaxFileSize: 5000000, // bytes
AcceptFileTypes: regexp.MustCompile(IMAGE_TYPES),
ExpirationTime: 300,
ThumbnailMaxWidth: 80,
ThumbnailMaxHeight: 80,
}
imageRegex = regexp.MustCompile(IMAGE_TYPES)
FileNotFoundError = errors.New("File Not Found")
)
// Config holds configuration settings for an UploadHandler.
type Config struct {
MinFileSize int // bytes
MaxFileSize int // bytes
AcceptFileTypes *regexp.Regexp
ExpirationTime int // seconds
ThumbnailMaxWidth int // pixels
ThumbnailMaxHeight int // pixels
}
// DataStore provides a simple interface to a blob store.
type DataStore interface {
Create(*FileInfo, io.Reader) error // Create a new file and return its key
Get(key string) (FileInfo, io.Reader, error) // Get the file blob identified by key
Delete(key string) error // Delete the file identified by key
}
// UploadHandler is an http Handler for uploading files and serving thumbnails.
type UploadHandler struct {
Prefix string // URL prefix to serve
Conf *Config // Configuration
Store *DataStore // Persistant storage for files
Cache *mc.Conn // Cache for image thumbnails
}
// FileInfo describes a file that has been uploaded.
type FileInfo struct {
Key string `json:"-"`
Url string `json:"url,omitempty"`
ThumbnailUrl string `json:"thumbnail_url,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
Size int64 `json:"size"`
Error string `json:"error,omitempty"`
DeleteUrl string `json:"delete_url,omitempty"`
DeleteType string `json:"delete_type,omitempty"`
}
// http500 Raises an HTTP 500 Internal Server Error if err is non-nil
func http500(w http.ResponseWriter, err error) {
if err != nil {
msg := "500 Internal Server Error: " + err.Error()
http.Error(w, msg, http.StatusInternalServerError)
log.Panic(err)
}
}
func escape(s string) string {
return strings.Replace(url.QueryEscape(s), "+", "%20", -1)
}
func (h *UploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Remove h.Prefix from the URL path - based on http.StripPrefix()
if !strings.HasPrefix(r.URL.Path, h.Prefix) {
http.NotFound(w, r)
return
}
r.URL.Path = r.URL.Path[len(h.Prefix):]
params, err := url.ParseQuery(r.URL.RawQuery)
http500(w, err)
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add(
"Access-Control-Allow-Methods",
// "OPTIONS, HEAD, GET, POST, PUT, DELETE",
"GET, POST, PUT, DELETE",
)
switch r.Method {
// case "OPTIONS":
// case "HEAD":
case "GET":
h.get(w, r)
case "POST":
if len(params["_method"]) > 0 && params["_method"][0] == "DELETE" {
h.delete(w, r)
// http.Error(w, "POST-delete support not yet implmented", http.StatusNotImplemented)
} else {
h.post(w, r)
}
case "DELETE":
h.delete(w, r)
default:
http.Error(w, "501 Not Implemented", http.StatusNotImplemented)
}
}
func (h *UploadHandler) get(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
// Noop - not sure why JFU plugin sometimes calls this with no args
return
}
parts := strings.Split(r.URL.Path, "/")
if len(parts) >= 2 && parts[1] == "thumbnails" {
h.serveThumbnails(w, r)
return
}
//
//
if len(parts) != 3 || parts[1] == "" {
log.Printf("Invalid URL: '%v'", r.URL)
http.Error(w, "404 Invalid URL", http.StatusNotFound)
return
}
key := parts[1]
log.Println("Get", key)
fi, file, err := (*h.Store).Get(parts[1])
if err == FileNotFoundError {
log.Println("Not Found", key)
http.NotFound(w, r)
return
}
http500(w, err)
log.Println("Found", key, fi.Type)
//
//
w.Header().Add(
"Cache-Control",
fmt.Sprintf("public,max-age=%d", h.Conf.ExpirationTime),
)
if imageRegex.MatchString(fi.Type) {
w.Header().Add("X-Content-Type-Options", "nosniff")
} else {
w.Header().Add("Content-Type", "application/octet-stream")
w.Header().Add(
"Content-Disposition:",
fmt.Sprintf("attachment; filename=%s;", parts[2]),
)
}
io.Copy(w, file)
return
}
// uploadFile handles the upload of a single file from a multipart form.
func (h *UploadHandler) uploadFile(w http.ResponseWriter, p *multipart.Part) (fi *FileInfo) {
fi = &FileInfo{
Name: p.FileName(),
Type: p.Header.Get("Content-Type"),
}
//
// Validate file type
//
if h.Conf.AcceptFileTypes.MatchString(fi.Type) == false {
fi.Error = "acceptFileTypes"
return
}
isImage := imageRegex.MatchString(fi.Type)
//
// Copy into buffers for save and thumbnail generation
//
// Max + 1 for LimitedReader size, so we can detect below if file size is
// greater than max.
lr := &io.LimitedReader{R: p, N: int64(h.Conf.MaxFileSize + 1)}
var bSave bytes.Buffer // Buffer to be saved
var bThumb bytes.Buffer // Buffer to be thumbnailed
var wr io.Writer
if isImage {
wr = io.MultiWriter(&bSave, &bThumb)
} else {
wr = &bSave
}
_, err := io.Copy(wr, lr)
http500(w, err)
//
// Validate file size
//
size := bSave.Len()
if size < h.Conf.MinFileSize {
log.Println("File failed validation: too small.", size, h.Conf.MinFileSize)
fi.Error = "minFileSize"
return
} else if size > h.Conf.MaxFileSize {
log.Println("File failed validation: too large.", size, h.Conf.MaxFileSize)
fi.Error = "maxFileSize"
return
}
//
// Save to data store
//
err = (*h.Store).Create(fi, &bSave)
http500(w, err)
log.Println("Create", size)
//
// Set URLs in FileInfo
//
u := &url.URL{
Path: h.Prefix + "/",
}
uString := u.String()
fi.Url = uString + escape(string(fi.Key)) + "/" +
escape(string(fi.Name))
fi.DeleteUrl = fi.Url
fi.DeleteType = "DELETE"
fi.ThumbnailUrl = uString + "thumbnails/" + escape(string(fi.Key))
//
// Create thumbnail
//
if isImage && size > 0 {
_, err = h.createThumbnail(fi, &bThumb)
if err != nil {
log.Println("Error creating thumbnail:", err)
}
// If we wanted to save thumbnails to peristent storage, this would be
// a good spot to do it.
}
return
}
func getFormValue(p *multipart.Part) string {
var b bytes.Buffer
io.CopyN(&b, p, int64(1<<20)) // Copy max: 1 MiB
return b.String()
}
func (h *UploadHandler) post(w http.ResponseWriter, r *http.Request) {
//
// We may potentially handle multiple uploads
//
fileInfos := make([]*FileInfo, 0)
mr, err := r.MultipartReader()
http500(w, err)
r.Form, err = url.ParseQuery(r.URL.RawQuery)
http500(w, err)
for {
// var part *multipart.Part
part, err := mr.NextPart()
if err == io.EOF {
break
}
http500(w, err)
if name := part.FormName(); name != "" {
if part.FileName() != "" {
fileInfos = append(fileInfos, h.uploadFile(w, part))
} else {
r.Form[name] = append(r.Form[name], getFormValue(part))
}
}
}
js, err := json.Marshal(fileInfos)
http500(w, err)
//
//
//
if redirect := r.FormValue("redirect"); redirect != "" {
http.Redirect(w, r, fmt.Sprintf(
redirect,
escape(string(js)),
), http.StatusFound)
return
}
jsonType := "application/json"
if strings.Index(r.Header.Get("Accept"), jsonType) != -1 {
w.Header().Set("Content-Type", jsonType)
}
fmt.Fprintln(w, string(js))
}
func (h *UploadHandler) delete(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) != 3 || parts[1] == "" {
log.Println("Invalid URL:", r.URL)
http.Error(w, "404 Invalid URL", http.StatusNotFound)
return
}
key := parts[1]
log.Println("Delete", key)
err := (*h.Store).Delete(key)
http500(w, err)
return
}
func (h *UploadHandler) serveThumbnails(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) == 3 {
if key := parts[2]; key != "" {
var data []byte
val, _, _, err := h.Cache.Get(key)
if err == nil {
data = []byte(val)
} else {
if fi, r, err := (*h.Store).Get(key); err == nil {
_, err = h.createThumbnail(&fi, r)
}
}
if err == nil && len(data) > 3 {
w.Header().Add(
"Cache-Control",
fmt.Sprintf("public,max-age=%d", h.Conf.ExpirationTime),
)
contentType := "image/png"
if string(data[:3]) == "GIF" {
contentType = "image/gif"
} else if string(data[1:4]) != "PNG" {
contentType = "image/jpeg"
}
w.Header().Set("Content-Type", contentType)
fmt.Fprintln(w, string(data))
return
}
}
}
http.Error(w, "404 Not Found", http.StatusNotFound)
}
// createThumbnail generates a thumbnail and adds it to the cache.
func (h *UploadHandler) createThumbnail(fi *FileInfo, r io.Reader) (data []byte, err error) {
defer func() {
if rec := recover(); rec != nil {
log.Println(rec)
// 1x1 pixel transparent GIf, bas64 encoded:
s := "R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
data, _ = base64.StdEncoding.DecodeString(s)
fi.ThumbnailUrl = "data:image/gif;base64," + s
}
h.Cache.Set(fi.Key, string(data), 0, 0, h.Conf.ExpirationTime)
}()
img, _, err := image.Decode(r)
if err != nil {
panic(err)
}
if bounds := img.Bounds(); bounds.Dx() > h.Conf.ThumbnailMaxWidth ||
bounds.Dy() > h.Conf.ThumbnailMaxHeight {
w, h := h.Conf.ThumbnailMaxWidth, h.Conf.ThumbnailMaxHeight
if bounds.Dx() > bounds.Dy() {
h = bounds.Dy() * h / bounds.Dx()
} else {
w = bounds.Dx() * w / bounds.Dy()
}
img = resize.Resize(img, img.Bounds(), w, h)
}
var b bytes.Buffer
err = png.Encode(&b, img)
if err != nil {
panic(err)
}
data = b.Bytes()
fi.ThumbnailUrl = "data:image/png;base64," +
base64.StdEncoding.EncodeToString(data)
return
}