From c448abd99a470e1ec541027077dcdef0745270d8 Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Sun, 23 Jun 2019 17:29:07 +0200 Subject: Show expandable thumbnails for images --- gopherproxy.go | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'gopherproxy.go') diff --git a/gopherproxy.go b/gopherproxy.go index 11c08d5..62b7446 100644 --- a/gopherproxy.go +++ b/gopherproxy.go @@ -10,6 +10,7 @@ import ( "log" "net/http" "net/url" + "regexp" "strings" "github.com/temoto/robotstxt" @@ -17,6 +18,8 @@ import ( "github.com/prologic/go-gopher" "github.com/gobuffalo/packr" + + "github.com/davidbyttow/govips/pkg/vips" ) type Item struct { @@ -25,7 +28,7 @@ type Item struct { Text string } -func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext string, uri string, hostport string, d gopher.Directory) error { +func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext string, jstext string, uri string, hostport string, d gopher.Directory) error { var title string out := make([]Item, len(d.Items)) @@ -78,9 +81,10 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext st Title string URI string Style string + Script string Lines []Item RawText string - }{title, fmt.Sprintf("%s/%s", hostport, uri), styletext, out, ""}) + }{title, fmt.Sprintf("%s/%s", hostport, uri), styletext, jstext, out, ""}) } // GopherHandler returns a Handler that proxies requests @@ -88,7 +92,7 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext st // to the request path and renders the content using the provided template. // The optional robots parameters points to a robotstxt.RobotsData struct // to test user agents against a configurable robotst.txt file. -func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, robotsdebug bool, styletext string, uri string) http.HandlerFunc { +func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, robotsdebug bool, styletext string, jstext string, uri string) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { agent := req.UserAgent() path := strings.TrimPrefix(req.URL.Path, "/") @@ -140,14 +144,23 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, rob Title string URI string Style string + Script string RawText string Lines []Item - }{uri, fmt.Sprintf("%s/%s", hostport, uri), styletext, buf.String(), nil}) + }{uri, fmt.Sprintf("%s/%s", hostport, uri), styletext, jstext, buf.String(), nil}) + } else if parts[1] == "T" { + _, _, err = vips.NewTransform(). + Load(res.Body). + ResizeStrategy(vips.ResizeStrategyAuto). + ResizeWidth(160). + Quality(75). + Output(w). + Apply() } else { io.Copy(w, res.Body) } } else { - if err := renderDirectory(w, tpl, styletext, uri, hostport, res.Dir); err != nil { + if err := renderDirectory(w, tpl, styletext, jstext, uri, hostport, res.Dir); err != nil { io.WriteString(w, fmt.Sprintf("Error:
%s
", err)) return } @@ -240,6 +253,11 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, uri string) error styletext = "" } + jstext, err := box.FindString("main.js") + if err != nil { + jstext = "" + } + favicondata, err := box.Find("favicon.ico") if err != nil { favicondata = []byte{} @@ -257,6 +275,9 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, uri string) error "safeCss": func(s string) template.CSS { return template.CSS(s) }, + "safeJs": func(s string) template.JS { + return template.JS(s) + }, "HTMLEscape": func(s string) string { return html.EscapeString(s) }, @@ -267,6 +288,11 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, uri string) error "pop": func(s []string) []string { return s[:len(s)-1] }, + "replace": func(pattern, output string, input interface{}) string { + var re = regexp.MustCompile(pattern) + var inputStr = fmt.Sprintf("%v", input) + return re.ReplaceAllString(inputStr, output) + }, } tpl, err = template.New("gophermenu").Funcs(funcMap).Parse(tpltext) @@ -274,7 +300,11 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, uri string) error log.Fatal(err) } - http.HandleFunc("/", GopherHandler(tpl, robotsdata, robotsdebug, styletext, uri)) + vips.Startup(&vips.Config{ + ConcurrencyLevel: 2, + }) + + http.HandleFunc("/", GopherHandler(tpl, robotsdata, robotsdebug, styletext, jstext, uri)) http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) http.HandleFunc("/favicon.ico", FaviconHandler(favicondata)) http.HandleFunc("/iosevka-term-ss03-regular.woff", FontHandler(false, fontdataw)) -- cgit v1.2.3-54-g00ecf