Skip to content

Commit a954ef1

Browse files
committed
MVP upload working
1 parent 38c99a8 commit a954ef1

4 files changed

Lines changed: 79 additions & 20 deletions

File tree

cmd/server/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ func main() {
2424
http.HandleFunc("/images/", ui.StaticHandler.ServeHTTP)
2525
http.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/index.html", http.StatusSeeOther) })
2626
http.HandleFunc("/index.html", func(w http.ResponseWriter, r *http.Request) { ui.RunTemplate(w, r, "index.tmpl", nil) })
27-
http.HandleFunc("GET /upload.html", func(w http.ResponseWriter, r *http.Request) { ui.RunTemplate(w, r, "upload.tmpl", nil) })
28-
http.HandleFunc("POST /upload.html", uploadHandler)
27+
http.HandleFunc("GET /upload.html", uploadGetHandler)
28+
http.HandleFunc("POST /upload.html", uploadPostHandler)
2929
http.HandleFunc("GET /url.html", func(w http.ResponseWriter, r *http.Request) { ui.RunTemplate(w, r, "url.tmpl", nil) })
30-
http.HandleFunc("POST /url.html", uploadHandler)
30+
http.HandleFunc("POST /url.html", uploadGetHandler)
3131
http.HandleFunc("GET /clipboard.html", func(w http.ResponseWriter, r *http.Request) { ui.RunTemplate(w, r, "clipboard.tmpl", nil) })
32-
http.HandleFunc("POST /clipboard.html", uploadHandler)
32+
http.HandleFunc("POST /clipboard.html", uploadGetHandler)
3333

3434
err := http.ListenAndServe(listenAddress+":"+strconv.Itoa(listenPort), nil)
3535
if err != nil {

cmd/server/uploadHandler.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,59 @@
11
package main
22

33
import (
4-
"fmt"
54
"net/http"
65

76
"github.com/FileFormatInfo/svgan/internal/common"
87
svgan "github.com/FileFormatInfo/svgan/lib"
8+
"github.com/FileFormatInfo/svgan/ui"
99
)
1010

11-
func uploadHandler(w http.ResponseWriter, r *http.Request) {
11+
func uploadHandler(w http.ResponseWriter, r *http.Request, e error) {
12+
ui.RunTemplate(w, r, "upload.tmpl", map[string]interface{}{
13+
"Err": e,
14+
"Title": "SVG File Upload",
15+
})
16+
}
17+
18+
func uploadGetHandler(w http.ResponseWriter, r *http.Request) {
19+
uploadHandler(w, r, nil)
20+
}
21+
22+
func uploadPostHandler(w http.ResponseWriter, r *http.Request) {
1223

1324
// max = 10 MB
1425
parseErr := r.ParseMultipartForm(10 << 20)
1526
if parseErr != nil {
16-
http.Error(w, fmt.Sprintf("Unable to parse form data (%v)\n", parseErr), http.StatusBadRequest)
27+
uploadHandler(w, r, parseErr)
1728
return
1829
}
1930

2031
file, handler, fileErr := r.FormFile("file")
2132
if fileErr != nil {
22-
http.Error(w, fmt.Sprintf("Unable to get file from form (%v)\n", fileErr), http.StatusBadRequest)
33+
uploadHandler(w, r, fileErr)
2334
return
2435
}
25-
26-
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
2736
defer file.Close()
2837

29-
fmt.Fprintf(w, "File: %+v\n", handler.Filename)
30-
fmt.Fprintf(w, "Size: %+v\n", handler.Size)
31-
fmt.Fprintf(w, "MIME: %+v\n", handler.Header.Get("Content-Type"))
32-
3338
// seems wasteful, but handler.content is private...
3439
raw := make([]byte, handler.Size)
3540
_, readErr := file.Read(raw)
3641
if readErr != nil {
37-
fmt.Fprintf(w, "ERROR: Reading the File (%v)\n", readErr)
42+
uploadHandler(w, r, readErr)
3843
return
3944
}
4045

4146
svgInfo, svgErr := svgan.SvgCheck(common.Logger, raw)
4247
if svgErr != nil {
43-
fmt.Fprintf(w, "ERROR: Parsing the SVG (%v)\n", svgErr)
48+
uploadHandler(w, r, svgErr)
4449
return
4550
}
4651

47-
fmt.Fprintf(w, "INFO: %+v\n", svgInfo)
52+
ui.RunTemplate(w, r, "_results.tmpl", map[string]interface{}{
53+
"filename": handler.Filename,
54+
"size": handler.Size,
55+
"mime": handler.Header.Get("Content-Type"),
56+
"data": svgInfo,
57+
"Title": "SVG Analysis Results",
58+
})
4859
}

ui/templates.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"net/http"
99
"strings"
1010

11+
"encoding/json"
12+
1113
"github.com/FileFormatInfo/svgan/internal/common"
1214
)
1315

@@ -39,6 +41,20 @@ func initTemplates() map[string]TemplateFunc {
3941
}
4042
return result
4143
},
44+
"toJson": func(data any) string {
45+
jsonStr, jsonErr := json.MarshalIndent(data, "", " ")
46+
if jsonErr != nil {
47+
return jsonErr.Error()
48+
}
49+
return string(jsonStr)
50+
},
51+
"toString": func(data any) any {
52+
if data == nil {
53+
return template.HTML("<i>(not set)</i>")
54+
}
55+
56+
return data.(string)
57+
},
4258
}
4359

4460
theCache := make(map[string]TemplateFunc)
@@ -111,7 +127,7 @@ type CrumbtrailEntry struct {
111127
URL string
112128
}
113129

114-
func makeCrumbtrail(r *http.Request) []CrumbtrailEntry {
130+
func makeCrumbtrail(r *http.Request, data TemplateData) []CrumbtrailEntry {
115131
crumbtrail := []CrumbtrailEntry{}
116132

117133
// Add additional entries based on the request path
@@ -125,6 +141,10 @@ func makeCrumbtrail(r *http.Request) []CrumbtrailEntry {
125141
crumbtrail = append(crumbtrail, entry)
126142
}
127143

144+
if data["Title"] != nil {
145+
crumbtrail[len(crumbtrail)-1].Text = data["Title"].(string)
146+
}
147+
128148
return crumbtrail
129149
}
130150

@@ -138,7 +158,7 @@ func RunTemplate(w http.ResponseWriter, r *http.Request, templateName string, da
138158
if data == nil {
139159
data = make(map[string]any)
140160
}
141-
data["crumbtrail"] = makeCrumbtrail(r)
161+
data["crumbtrail"] = makeCrumbtrail(r, data)
142162

143163
result, execErr := fn(data)
144164
if execErr != nil {

ui/views/_results.tmpl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
{{template "above" .}}
22

3-
<p>Results go here</p>
3+
4+
<table class="table table-bordered table-striped w-auto">
5+
<tbody>
6+
<tr>
7+
<td>width</td>
8+
<td>{{toString .data.SvgWidth}}</td>
9+
</tr>
10+
<tr>
11+
<td>height</td>
12+
<td>{{toString .data.SvgHeight}}</td>
13+
</tr>
14+
<tr>
15+
<td>viewbox</td>
16+
<td>{{toString .data.ViewBox}}</td>
17+
</tr>
18+
<tr>
19+
<td>colors</td>
20+
<td>
21+
{{range .data.Colors}}
22+
<span style="height: 2em; width: 7em; display: inline-block; background-color: {{.}}; border: 1px solid #a2a2a2; border-radius: 4px; vertical-align: middle; margin-left: 10px;"></span> {{.}}<br>
23+
{{end}}
24+
</td>
25+
</tbody>
26+
</table>
27+
28+
<details>
29+
<summary>Raw details</summary>
30+
<pre>{{toJson .data}}</pre>
31+
</details>
432

533
{{template "below" .}}

0 commit comments

Comments
 (0)