/
handler_upload.go
176 lines (134 loc) · 3.17 KB
/
handler_upload.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
package main
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"runtime"
"github.com/levmv/imgserv/config"
"github.com/levmv/imgserv/vips"
)
type UploadedInfo struct {
Name string `json:"name"`
Width int `json:"width"`
Height int `json:"height"`
}
type preprocessor func(image *vips.Image) error
func nope(image *vips.Image) error {
return nil
}
var (
preprocess preprocessor
maxWidth int
maxHeight int
)
func resize(image *vips.Image) error {
if image.Width() > maxWidth || image.Height() > maxHeight {
if err := image.Thumbnail(maxWidth, maxHeight, 0, vips.SizeDown); err != nil {
return err
}
}
return nil
}
func initUpload(conf config.StorageConf) {
if conf.MaxWidth > 0 && conf.MaxHeight > 0 {
maxWidth = conf.MaxWidth
maxHeight = conf.MaxHeight
preprocess = resize
} else {
preprocess = nope
}
}
func UploadHandler(w http.ResponseWriter, r *http.Request) (int, error) {
IncUploaderRequests()
q := r.URL.Query()
key := q.Get("key")
if key == "" {
key, _ = genUuid()
}
if err := queueSem.Acquire(r.Context(), 1); err != nil {
panic("maxSem")
}
defer queueSem.Release(1)
upInfo, err := uploadPhoto(key, r.Body)
if err != nil {
return 500, err
}
js, _ := json.Marshal(upInfo)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(js)
Free()
return 200, nil
}
func UploadFileHandler(w http.ResponseWriter, r *http.Request) (int, error) {
IncUploaderRequests()
q := r.URL.Query()
key := q.Get("key")
if key == "" {
key, _ = genUuid()
}
filename := q.Get("filename")
file, err := os.Open(filename)
defer file.Close()
if err != nil {
return 500, fmt.Errorf("failed to open file %v (%w)", filename, err)
}
if err := queueSem.Acquire(r.Context(), 1); err != nil {
panic("maxSem")
}
defer queueSem.Release(1)
upInfo, err := uploadPhoto(key, file)
if err != nil {
return 500, err
}
js, _ := json.Marshal(upInfo)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(js)
Free()
return 200, nil
}
func uploadPhoto(name string, r io.Reader) (*UploadedInfo, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
newImg := imgStorage.NewImage()
defer newImg.Close()
if _, err := newImg.ReadFrom(r); err != nil {
return nil, err
}
image := vips.Image{}
defer image.Close()
defer vips.Cleanup()
if err := image.LoadFromBuffer(newImg.Data); err != nil {
return nil, err
}
if image.Width()*image.Height() > 16000*16000 {
return nil, fmt.Errorf("input image is too big %vx%v", image.Width(), image.Height())
}
if err := image.Strip(); err != nil {
return nil, err
}
if err := preprocess(&image); err != nil {
return nil, err
}
imageBytes, _ := image.ExportJpeg(95)
if err := imgStorage.Upload(name, imageBytes); err != nil {
return nil, err
}
return &UploadedInfo{
Name: name,
Width: image.Width(),
Height: image.Height(),
}, nil
}
func genUuid() (string, error) {
uuid := make([]byte, 16)
if _, err := rand.Read(uuid); err != nil {
return "", err
}
uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
return base64.RawURLEncoding.EncodeToString(uuid), nil
}