flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit f397be1440c27549f9c7d4b342041677224458a8
parent a4c726a13d208e3a6c3b18de7825918ff0b9a01b
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Mon,  4 Jan 2021 20:44:04 -0800

[#119] Add warning for schema'd links

https://todo.sr.ht/~alexwennerberg/flounder/119

Diffstat:
Mhttp.go | 11++++++++++-
Mtemplates/edit_file.html | 7+++++++
Mtemplates/static/style.css | 5+++++
Mutils.go | 14++++++++++++++
4 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/http.go b/http.go @@ -104,6 +104,7 @@ func editFileHandler(w http.ResponseWriter, r *http.Request) { filePath := path.Join(c.FilesDirectory, user.Username, fileName) isText := isTextFile(filePath) alert := "" + var warnings []string if r.Method == "POST" { // get post body r.ParseForm() @@ -118,6 +119,13 @@ func editFileHandler(w http.ResponseWriter, r *http.Request) { renderError(w, err.Error(), http.StatusBadRequest) return } + sfl := getSchemedFlounderLinkLines(strings.NewReader(fileText)) + if len(sfl) > 0 { + warnings = append(warnings, "Warning! Some of your links to flounder pages use schemas. This means that they may break when viewed in Gemini or over HTTPS. Plase remove gemini: or https: from the start of these links:\n") + for _, l := range sfl { + warnings = append(warnings, l) + } + } // create directories if dne os.MkdirAll(path.Dir(filePath), os.ModePerm) if userHasSpace(user.Username, len(fileBytes)) { @@ -176,7 +184,8 @@ func editFileHandler(w http.ResponseWriter, r *http.Request) { Host string IsText bool Alert string - }{fileName, string(fileBytes), c.SiteTitle, user, c.Host, isText, alert} + Warnings []string + }{fileName, string(fileBytes), c.SiteTitle, user, c.Host, isText, alert, warnings} err = t.ExecuteTemplate(w, "edit_file.html", data) if err != nil { panic(err) diff --git a/templates/edit_file.html b/templates/edit_file.html @@ -3,6 +3,13 @@ <form id="edit-form" action="/edit/{{.FileName}}" method="POST"> <label for="rename">Rename:</label> <input type="text" value="{{.FileName}}" id="rename" name="rename"> + <div class="warning"> + <p> + {{ range .Warnings }} + </p> + {{ . }} + {{ end }} + </div> {{ if .IsText }} <textarea rows="27" name="file_text" id="editor">{{.FileText}}</textarea> {{ end }} diff --git a/templates/static/style.css b/templates/static/style.css @@ -105,6 +105,11 @@ resize: vertical; .error { color: red; } + +.warning { + color: #ffcc00 ; +} + .alert { color: green; } diff --git a/utils.go b/utils.go @@ -2,6 +2,7 @@ package main import ( "archive/zip" + "bufio" "fmt" "io" "mime" @@ -13,6 +14,19 @@ import ( "unicode/utf8" ) +func getSchemedFlounderLinkLines(r io.Reader) []string { + scanner := bufio.NewScanner(r) + result := []string{} + for scanner.Scan() { + text := scanner.Text() + // TODO use actual parser. this could be a little wonky + if strings.HasPrefix(text, "=>") && strings.Contains(text, c.Host) && (strings.Contains(text, "gemini://") || strings.Contains(text, "https://")) { + result = append(result, text) + } + } + return result +} + // Check if it is a text file, first by checking mimetype, then by reading bytes // Stolen from https://github.com/golang/tools/blob/master/godoc/util/util.go func isTextFile(fullPath string) bool {