[#119] Add warning for schema'd links https://todo.sr.ht/~alexwennerberg/flounder/119
alex wennerberg alex@alexwennerberg.com
Mon, 04 Jan 2021 20:44:04 -0800
4 files changed,
36 insertions(+),
1 deletions(-)
M
http.go
→
http.go
@@ -104,6 +104,7 @@ fileName := filepath.Clean(r.URL.Path[len("/edit/"):])
filePath := path.Join(c.FilesDirectory, user.Username, fileName) isText := isTextFile(filePath) alert := "" + var warnings []string if r.Method == "POST" { // get post body r.ParseForm()@@ -117,6 +118,13 @@ if err != nil {
log.Println(err) 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)@@ -176,7 +184,8 @@ AuthUser AuthUser
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)
M
templates/edit_file.html
→
templates/edit_file.html
@@ -3,6 +3,13 @@ <h2>Editing <a href="//{{.AuthUser.Username}}.{{.Host}}/{{.FileName}}">{{.FileName}}</a></h2>
<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 }}
M
templates/static/style.css
→
templates/static/style.css
@@ -105,6 +105,11 @@
.error { color: red; } + +.warning { + color: #ffcc00 ; +} + .alert { color: green; }
M
utils.go
→
utils.go
@@ -2,6 +2,7 @@ package main
import ( "archive/zip" + "bufio" "fmt" "io" "mime"@@ -12,6 +13,19 @@ "strings"
"time" "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