-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdn.go
232 lines (212 loc) · 6.47 KB
/
cdn.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
package main
import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
//"github.com/qiniu/qmgo"
"html/template"
"mime/multipart"
"github.com/davidbyttow/govips/v2/vips"
"github.com/gernest/alien"
"github.com/google/uuid"
)
var (
port = flag.Int("port", 8080, "The port the webserver will listen on")
)
func main() {
flag.Parse()
log.Println("Initializing the router!")
r := alien.New()
vips.Startup(&vips.Config{MaxCacheMem: (256 << 20)})
r.Post("/save", handleSave)
r.Get("/get", handleGet)
r.Get("/", handleForm)
s := &http.Server{
Addr: ":" + strconv.Itoa(*port),
Handler: r,
ReadTimeout: 100 * time.Second,
WriteTimeout: 100 * time.Second,
MaxHeaderBytes: 20 << 20,
}
log.Fatal(s.ListenAndServe())
}
const SAVE_PATH = "images/"
func handleForm(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("template.html"))
tmpl.Execute(w, struct{ Success bool }{true})
}
func handleSave(w http.ResponseWriter, r *http.Request) {
imageData, imageHeader, err := r.FormFile("image")
w.Header().Set("Content-Type", "application/json")
if isImage(imageHeader) {
log.Println(imageHeader.Header.Get("Content-Type"))
log.Println(isSVG(imageHeader))
if isSVG(imageHeader) {
imageId, err := saveSVG(imageData, imageHeader, SAVE_PATH)
if err != nil {
handleErr(w, http.StatusInternalServerError, "Failure while saving to file", err)
log.Panicln(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{\"uuid\": \"" + imageId.String() + "\", \"message\": \"Success\"}"))
return
}
if err != nil {
handleErr(w, http.StatusInternalServerError, "Failure while allocating file", err)
log.Panicln(err)
}
imageId, err := saveImage(imageData, imageHeader, SAVE_PATH)
if err != nil {
handleErr(w, http.StatusInternalServerError, "Failure while saving to file", err)
log.Panicln(err)
}
w.Write([]byte("{\"uuid\": \"" + imageId.String() + "\", \"message\": \"Success\"}"))
} else {
handleErr(w, http.StatusBadRequest, "Is not an image", errors.New("The mimetype of the given file does not match image/*"))
}
}
func saveSVG(data io.Reader, header *multipart.FileHeader, savePath string) (uuid.UUID, error) {
log.Println("Reading the file")
imageId := uuid.New()
svgFile, err := os.Create(savePath + "Image-" + imageId.String() + ".svg")
if err != nil {
return uuid.Nil, err
}
_, err = io.Copy(svgFile, data)
if err != nil {
return uuid.Nil, err
}
return imageId, nil
}
func saveImage(data io.Reader, header *multipart.FileHeader, savePath string) (uuid.UUID, error) {
log.Println("Reading the file")
image, err := vips.NewImageFromReader(data)
if err != nil {
return uuid.Nil, err
}
image.OptimizeICCProfile()
if err != nil {
return uuid.Nil, err
}
log.Println("Starting the export")
imageData, imageMeta, err := image.ExportAvif(&vips.AvifExportParams{StripMetadata: true, Quality: 90, Speed: 5})
if err != nil {
return uuid.Nil, err
}
log.Println("Writing the file")
imageId := uuid.New()
err = ioutil.WriteFile(savePath+"Image-"+imageId.String()+imageMeta.Format.FileExt(), imageData, 0644)
if err != nil {
return uuid.Nil, err
}
return imageId, nil
}
func handleGet(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
requiredFields := []string{"uuid"}
for _, e := range requiredFields {
if !q.Has(e) {
handleErr(w, http.StatusBadRequest, "Missing Fields", errors.New("Missing Fields: "+e))
return
}
}
imageId, err := uuid.Parse(q["uuid"][0])
if err != nil {
handleErr(w, http.StatusBadRequest, "Invalid UUID", err)
return
}
imageName := SAVE_PATH + "Image-" + imageId.String()
if _, err := os.Stat(imageName + ".avif"); err == nil {
imageName += ".avif"
fmt.Println("AVIF File exists")
image, err := vips.NewImageFromFile(imageName)
if err != nil {
handleErr(w, http.StatusInternalServerError, "Couldn't open file", err)
return
}
width, height := image.Width(), image.Height()
if q.Has("width") && len(q["width"]) > 0 {
width, err = strconv.Atoi(q["width"][0])
if err != nil || width < 1 {
handleErr(w, http.StatusBadRequest, "Invalid width", err)
return
}
}
if q.Has("height") && len(q["height"]) > 0 {
height, err = strconv.Atoi(q["height"][0])
if err != nil || height < 1 {
handleErr(w, http.StatusBadRequest, "Invalid height", err)
return
}
}
err = image.Resize(scale(width, height, image.Width(), image.Height()), vips.KernelAuto)
if err != nil {
handleErr(w, http.StatusBadRequest, "Couldn't Resize Image", err)
return
}
fmt.Printf("SIZE: {X: %d, Y: %d}\nSCALE: %f\n", image.Width(), image.Height(), float64(width)/float64(image.Width()))
err = image.ExtractArea((image.Width()-width)/2, (image.Height()-height)/2, width, height)
if err != nil {
handleErr(w, http.StatusBadRequest, "Couldn't Crop Image", err)
return
}
fmt.Printf("SIZE: {X: %d, Y: %d}\nSCALE: %f\n", image.Width(), image.Height(), float64(width)/float64(image.Width()))
imageData, _, err := image.ExportNative()
if err != nil {
handleErr(w, http.StatusInternalServerError, "Couldn't Encode Image", err)
return
}
w.Header().Set("Content-Type", "image/avif")
w.Write(imageData)
} else {
fmt.Printf("AVIF File does not exist\n")
if _, err := os.Stat(imageName + ".svg"); err == nil {
imageName += ".svg"
svgData, err := ioutil.ReadFile(imageName)
if err != nil {
handleErr(w, http.StatusInternalServerError, "Couldn't open file", err)
return
}
w.Header().Set("Content-Type", "image/svg+xml")
w.Write(svgData)
fmt.Printf("SVG File exists\n")
} else {
fmt.Printf("Image does not exist\n")
handleErr(w, http.StatusInternalServerError, "Couldn't open file", errors.New("Specified image does not exist"))
return
}
}
}
func handleErr(w http.ResponseWriter, code int, message string, err error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write([]byte(fmt.Sprintf("{\"error\": [\"%s: %s\", \"%s\"]}", http.StatusText(code), message, err.Error())))
}
func isImage(header *multipart.FileHeader) bool {
return header.Header.Get("Content-Type")[0:6] == "image/"
}
func isSVG(header *multipart.FileHeader) bool {
return header.Header.Get("Content-Type")[0:9] == "image/svg"
}
func scale(x, y, width, height int) float64 {
return float64(max(x, y)) / float64(min(width, height))
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
func min(x, y int) int {
if x < y {
return x
}
return y
}