-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
216 lines (203 loc) · 5.05 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
213
214
215
216
// Code attribution : https://gophercises.com/exercises/transform
package main
import (
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
"github.com/diop/indigo/primitive"
)
func main() {
go func() {
t := time.NewTicker(5 * time.Minute)
for {
<-t.C
}
}()
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
html := `<html><body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<button type="submit">Upload Image</button>
</form>
</body></html>`
fmt.Fprint(w, html)
})
mux.HandleFunc("/modify/", func(w http.ResponseWriter, r *http.Request) {
f, err := os.Open("./images/" + filepath.Base(r.URL.Path))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer f.Close()
ext := filepath.Ext(f.Name())[1:]
modeStr := r.FormValue("mode")
if modeStr == "" {
// Render mode choices
renderModeChoices(w, r, f, ext)
return
}
mode, err := strconv.Atoi(modeStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
nStr := r.FormValue("n")
if nStr == "" {
renderNumShapeChoices(w, r, f, ext, primitive.Mode(mode))
return
}
numShapes, err := strconv.Atoi(nStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
_ = numShapes
http.Redirect(w, r, "/images/"+filepath.Base(f.Name()), http.StatusFound)
})
mux.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
file, header, err := r.FormFile("image")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer file.Close()
ext := filepath.Ext(header.Filename)[1:]
onDisk, err := tempfile("", ext)
if err != nil {
http.Error(w, "Something went wrong", http.StatusInternalServerError)
return
}
defer onDisk.Close()
_, err = io.Copy(onDisk, file)
if err != nil {
http.Error(w, "Something went wrong", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/modify/"+filepath.Base(onDisk.Name()), http.StatusFound)
})
fs := http.FileServer(http.Dir("./images/"))
mux.Handle("/images/", http.StripPrefix("/images", fs))
log.Fatal(http.ListenAndServe(":3000", mux))
}
func renderNumShapeChoices(w http.ResponseWriter, r *http.Request, rs io.ReadSeeker, ext string, mode primitive.Mode) {
opts := []genOpts{
{N: 10, M: mode},
{N: 20, M: mode},
{N: 30, M: mode},
{N: 40, M: mode},
}
imgs, err := genImages(rs, ext, opts...)
if err != nil {
panic(err)
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
}
html := `<html><body>
{{range .}}
<a href="/modify/{{.Name}}?mode={{.Mode}}&n={{.NumShapes}}">
<img style="width: 20%;" src="/images/{{.Name}}">
</a>
{{end}}
</body></html>`
tpl := template.Must(template.New("").Parse(html))
type dataStruct struct {
Name string
Mode primitive.Mode
NumShapes int
}
var data []dataStruct
for i, img := range imgs {
data = append(data, dataStruct{
Name: filepath.Base(img),
Mode: opts[i].M,
NumShapes: opts[i].N,
})
}
err = tpl.Execute(w, data)
if err != nil {
panic(err)
}
}
func renderModeChoices(w http.ResponseWriter, r *http.Request, rs io.ReadSeeker, ext string) {
opts := []genOpts{
{N: 10, M: primitive.ModeCircle},
{N: 10, M: primitive.ModeBeziers},
{N: 10, M: primitive.ModePolygon},
{N: 10, M: primitive.ModeCombo},
}
imgs, err := genImages(rs, ext, opts...)
if err != nil {
// panic(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
html := `<html><body>
{{range .}}
<a href="/modify/{{.Name}}?mode={{.Mode}}">
<img style="width: 20%;" src="/images/{{.Name}}">
</a>
{{end}}
</body></html>`
tpl := template.Must(template.New("").Parse(html))
type dataStruct struct {
Name string
Mode primitive.Mode
}
var data []dataStruct
for i, img := range imgs {
data = append(data, dataStruct{
Name: filepath.Base(img),
Mode: opts[i].M,
})
}
err = tpl.Execute(w, data)
if err != nil {
panic(err)
}
}
type genOpts struct {
N int
M primitive.Mode
}
func genImages(rs io.ReadSeeker, ext string, opts ...genOpts) ([]string, error) {
var ret []string
for _, opt := range opts {
rs.Seek(0, 0)
f, err := genImage(rs, ext, opt.N, opt.M)
if err != nil {
return nil, err
}
ret = append(ret, f)
}
return ret, nil
}
func genImage(r io.Reader, ext string, numShapes int, mode primitive.Mode) (string, error) {
out, err := primitive.Transform(r, ext, numShapes, primitive.WithMode(mode))
if err != nil {
return "", err
}
outFile, err := tempfile("", ext)
if err != nil {
return "", err
}
defer outFile.Close()
io.Copy(outFile, out)
return outFile.Name(), nil
}
func tempfile(prefix, ext string) (*os.File, error) {
in, err := ioutil.TempFile("./images/", prefix)
if err != nil {
return nil, errors.New("main: failed to create temporary file")
}
defer os.Remove(in.Name())
return os.Create(fmt.Sprintf("%s.%s", in.Name(), ext))
}