-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
212 lines (182 loc) · 5.08 KB
/
main.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
package main
import (
_ "embed"
"fmt"
"image"
"image/color"
"image/draw"
"image/gif"
"image/jpeg"
"image/png"
"net/http"
"strings"
spinhttp "github.com/fermyon/spin/sdk/go/http"
"github.com/pkg/errors"
)
//go:embed docs/dist/index.html
var WebDoc string
//go:embed img.png
var samplePng []byte
//go:embed img.jpg
var sampleJpg []byte
var contentTypes = []string{"image/png", "image/jpeg", "image/jpg", "image/gif"}
var prefixes = []string{"/png=", "/jpeg=", "/jpg=", "/gif="}
var sampleFiles = map[string]map[string]*[]byte{
"/img.png": {"image/png": &samplePng},
"/img.jpg": {"image/jpeg": &sampleJpg},
}
func init() {
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
for _, prefix := range prefixes {
if len(r.URL.RequestURI()) > len(prefix) && r.URL.RequestURI()[:len(prefix)] == prefix {
err := convertImage(prefix, w, r)
if err != nil {
errorResponse(w, err)
}
return
}
}
setDefaultResponse(w, r)
})
}
func convertImage(prefix string, w http.ResponseWriter, r *http.Request) error {
url := r.URL.RequestURI()[len(prefix):]
contentType, _, err := checkUrl(url)
if err != nil {
return errors.New("Invalid image url")
}
res, err := loadImage(url)
if err != nil {
return errors.New("Failed to load image")
}
img, err := decodeImage(contentType, res)
if err != nil {
return errors.New("Failed to decode image")
}
err = encodeImage(prefix, img, w)
if err != nil {
return errors.New("Failed to encode image")
}
defer res.Body.Close()
return nil
}
func encodeImage(prefix string, image image.Image, w http.ResponseWriter) error {
switch prefix {
case "/png=":
w.Header().Set("Content-Type", "image/png")
return png.Encode(w, image)
case "/jpeg=":
w.Header().Set("Content-Type", "image/jpeg")
return jpeg.Encode(w, image, nil)
case "/jpg=":
w.Header().Set("Content-Type", "image/jpg")
return jpeg.Encode(w, image, nil)
case "/gif=":
w.Header().Set("Content-Type", "image/gif")
return gif.Encode(w, image, nil)
default:
return errors.New("Unsupported image type")
}
}
func decodeImage(contentType string, res *http.Response) (image.Image, error) {
switch contentType {
case "image/png":
imgSrc, err := png.Decode(res.Body)
if err != nil {
return nil, err
}
//Set White Background for transparent PNGs
newImg := image.NewRGBA(imgSrc.Bounds())
draw.Draw(newImg, newImg.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)
draw.Draw(newImg, newImg.Bounds(), imgSrc, imgSrc.Bounds().Min, draw.Over)
return newImg, nil
case "image/jpeg":
return jpeg.Decode(res.Body)
case "image/jpg":
return jpeg.Decode(res.Body)
case "image/gif":
return gif.Decode(res.Body)
}
return nil, errors.New("Unknown image type")
}
func loadImage(url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
res, err := spinhttp.Send(req)
if err != nil {
return nil, err
}
// defer res.Body.Close()
return res, nil
}
func checkUrl(url string) (string, string, error) {
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
return "", "", err
}
res, err := spinhttp.Send(req)
if err != nil {
return "", "", err
}
if !contains(contentTypes, res.Header.Get("Content-Type")) {
return "", "", errors.New("Unsupported image type")
}
defer res.Body.Close()
return res.Header.Get("Content-Type"), res.Header.Get("Content-Length"), nil
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
func errorResponse(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, err.Error())
}
func setDefaultResponse(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/favicon.ico" {
return
}
//Check if Path countains Sample File
for path, content := range sampleFiles {
if r.URL.Path == path {
for contentType, data := range content {
w.Header().Set("Content-Type", contentType)
w.Write(*data)
return
}
}
}
buildDocResponse(w, r)
}
func buildDocResponse(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.Header.Get("User-Agent"), "curl") {
buildTerminalDocResponse(w)
} else {
buildWebDocResponse(w)
}
}
func buildWebDocResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, "%s", WebDoc)
}
func buildTerminalDocResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprint(w, "# toformat.link Terminal Documentation\n")
fmt.Fprint(w, "-------------------------------\n")
fmt.Fprint(w, "## Format to PNG\n")
fmt.Fprint(w, "curl -L toqr.link/png=https://toformat.link/sample.jpg > img.png\n\n")
fmt.Fprint(w, "## Format to GIF\n")
fmt.Fprint(w, "curl -L toqr.link/png=https://toformat.link/sample.jpg > img.gif\n\n")
fmt.Fprint(w, "## Format to JPG\n")
fmt.Fprint(w, "curl -L toqr.link/png=https://toformat.link/sample.png > img.jpg\n\n")
fmt.Fprint(w, "## Format to JPEG\n")
fmt.Fprint(w, "curl -L toqr.link/png=https://toformat.link/sample.png > img.jpeg\n\n")
}
func main() {}