Skip to content

Commit d21e876

Browse files
committed
URL handler working
1 parent a954ef1 commit d21e876

5 files changed

Lines changed: 84 additions & 9 deletions

File tree

cmd/server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func main() {
2626
http.HandleFunc("/index.html", func(w http.ResponseWriter, r *http.Request) { ui.RunTemplate(w, r, "index.tmpl", nil) })
2727
http.HandleFunc("GET /upload.html", uploadGetHandler)
2828
http.HandleFunc("POST /upload.html", uploadPostHandler)
29-
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", uploadGetHandler)
29+
http.HandleFunc("GET /url.html", urlGetHandler)
30+
http.HandleFunc("POST /url.html", urlPostHandler)
3131
http.HandleFunc("GET /clipboard.html", func(w http.ResponseWriter, r *http.Request) { ui.RunTemplate(w, r, "clipboard.tmpl", nil) })
3232
http.HandleFunc("POST /clipboard.html", uploadGetHandler)
3333

cmd/server/uploadHandler.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ func uploadPostHandler(w http.ResponseWriter, r *http.Request) {
5050
}
5151

5252
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",
53+
"source": handler.Filename,
54+
"size": handler.Size,
55+
"mime": handler.Header.Get("Content-Type"),
56+
"data": svgInfo,
57+
"Title": "SVG Analysis Results",
5858
})
5959
}

cmd/server/urlHandler.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"io"
6+
"net/http"
7+
8+
"github.com/FileFormatInfo/svgan/internal/common"
9+
svgan "github.com/FileFormatInfo/svgan/lib"
10+
"github.com/FileFormatInfo/svgan/ui"
11+
)
12+
13+
func urlHandler(w http.ResponseWriter, r *http.Request, e error) {
14+
ui.RunTemplate(w, r, "url.tmpl", map[string]interface{}{
15+
"Err": e,
16+
"Title": "URL Analysis",
17+
})
18+
}
19+
20+
func urlGetHandler(w http.ResponseWriter, r *http.Request) {
21+
urlHandler(w, r, nil)
22+
}
23+
24+
func urlPostHandler(w http.ResponseWriter, r *http.Request) {
25+
26+
url := r.FormValue("url")
27+
if url == "" {
28+
urlHandler(w, r, errors.New("no URL provided"))
29+
return
30+
}
31+
32+
resp, err := http.Get(url)
33+
if err != nil {
34+
urlHandler(w, r, err)
35+
return
36+
}
37+
defer resp.Body.Close()
38+
39+
if resp.StatusCode != http.StatusOK {
40+
urlHandler(w, r, errors.New("failed to fetch URL"))
41+
return
42+
}
43+
44+
body, err := io.ReadAll(resp.Body)
45+
if err != nil {
46+
urlHandler(w, r, err)
47+
return
48+
}
49+
50+
svgInfo, svgErr := svgan.SvgCheck(common.Logger, body)
51+
if svgErr != nil {
52+
urlHandler(w, r, svgErr)
53+
return
54+
}
55+
56+
ui.RunTemplate(w, r, "_results.tmpl", map[string]interface{}{
57+
"source": url,
58+
"size": len(body),
59+
"mime": resp.Header.Get("Content-Type"),
60+
"data": svgInfo,
61+
"Title": "URL Analysis Results",
62+
})
63+
}

ui/views/_results.tmpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33

44
<table class="table table-bordered table-striped w-auto">
55
<tbody>
6+
<tr>
7+
<td>Source</td>
8+
<td>{{toString .source}}</td>
9+
</tr>
10+
<tr>
11+
<td>Size</td>
12+
<td>{{.size}}</td>
13+
</tr>
14+
<tr>
15+
<td>Content-Type</td>
16+
<td>{{toString .mime}}</td>
17+
</tr>
618
<tr>
719
<td>width</td>
820
<td>{{toString .data.SvgWidth}}</td>

ui/views/url.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
<form class="pt-3" method="post" action="url.html" enctype="multipart/form-data">
1616
<div class="mb-3">
17-
<label class="form-label" for="svgurl">SVG URL</label>
18-
<input class="form-control" name="svgurl" value="" />
17+
<label class="form-label" for="url">SVG URL</label>
18+
<input class="form-control" name="url" value="" />
1919
</div>
2020
<input class="btn btn-primary" id="submit" type="submit" value="Upload" />
2121
<a class="btn btn-outline-primary" href="index.html">Cancel</a>

0 commit comments

Comments
 (0)