/
get_file.go
163 lines (126 loc) · 3.26 KB
/
get_file.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
package main
import (
"bytes"
"fmt"
"github.com/nfnt/resize"
"github.com/wayt/happyngine"
"github.com/wayt/happyngine/env"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io/ioutil"
"mime"
"net/http"
"net/url"
"path/filepath"
"time"
)
type getFileAction struct {
happyngine.Action
}
func newGetFileAction(context *happyngine.Context) happyngine.ActionInterface {
// Init
this := &getFileAction{}
this.Context = context
this.Form = happyngine.NewForm(context,
happyngine.NewFormElement("bucket", "invalid_bucket"),
happyngine.NewFormElement("file", "invalid_file"))
return this
}
func min(v1, v2 uint) uint {
if v1 < v2 {
return v1
}
return v2
}
func (this *getFileAction) getFile() ([]byte, bool) {
bucket := this.Form.Elem("bucket").FormValue()
file := this.Form.Elem("file").FormValue()
u := url.URL{
Scheme: "http",
Host: fmt.Sprintf("s3-%s.amazonaws.com", env.Get("AWS_DEFAULT_REGION")),
Path: fmt.Sprintf("%s/%s", bucket, file),
}
q := u.Query()
for name := range this.Context.Request.URL.Query() {
q.Set(name, this.Context.Request.URL.Query().Get(name))
}
u.RawQuery = q.Encode()
resp, err := http.Get(u.String())
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
if resp.StatusCode >= 300 {
contentType := resp.Header.Get("Content-Type")
this.SendByte(resp.StatusCode, body, fmt.Sprintf("Content-Type: %s", contentType))
return nil, false
}
return body, true
}
func (this *getFileAction) Run() {
file := this.Form.Elem("file").FormValue()
startTime := time.Now()
data, ok := this.getFile()
if !ok {
return
}
s3GetTime := time.Now()
var imgResizeTime, imgEncodeTime time.Time
ext := filepath.Ext(file)
mimeType := mime.TypeByExtension(ext)
width := min(uint(this.GetIntParam("width")), 1920)
height := min(uint(this.GetIntParam("height")), 1920)
if width > 0 || height > 0 {
reader := bytes.NewReader(data)
img, format, err := image.Decode(reader)
if err != nil {
panic(err)
}
// If we're asked for two resize vector, we only keep the smalest one to keep image ratio
if width > 0 && height > 0 {
// Source image size
imgWidth := img.Bounds().Size().X
imgHeight := img.Bounds().Size().Y
// Strange calculus
if imgWidth > imgHeight {
width = 0
} else if imgHeight > imgWidth {
height = 0
} else { // imgHeight == imgWidth
if width > height {
width = 0
} else {
height = 0
}
}
}
m := resize.Resize(width, height, img, resize.Lanczos3)
imgResizeTime = time.Now()
writer := bytes.NewBuffer(nil)
if format == "jpeg" {
if err := jpeg.Encode(writer, m, nil); err != nil {
panic(err)
}
} else if format == "png" {
if err := png.Encode(writer, m); err != nil {
panic(err)
}
} else if format == "gif" {
if err := gif.Encode(writer, m, nil); err != nil {
panic(err)
}
} else {
this.Context.Errorln("Unknown image format:", format, " - ", file)
}
imgEncodeTime = time.Now()
data = writer.Bytes()
}
this.Context.Debugln("S3 GET Time:", s3GetTime.Sub(startTime), " - Resize Time:", imgResizeTime.Sub(s3GetTime), " - Encode Time:", imgEncodeTime.Sub(imgResizeTime))
this.SendByte(200, data, fmt.Sprintf("Content-Type: %s", mimeType))
}