flounder

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

commit a74256627667d2f0329e264adb88477110ef0f25
parent 32fd85e62324db1a34b5e78e1ef7b61cee31edde
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Wed, 18 Nov 2020 19:39:17 -0800

Add simple file renaming

Diffstat:
Mhttp.go | 5+++++
Mtemplates/edit_file.html | 2++
Mutils.go | 3+++
3 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/http.go b/http.go @@ -141,6 +141,11 @@ func editFileHandler(w http.ResponseWriter, r *http.Request) { renderError(w, InternalServerErrorMsg, 500) return } + newName := filepath.Clean(r.Form.Get("rename")) + if newName != fileName { + newPath := path.Join(c.FilesDirectory, authUser, newName) + os.Rename(filePath, newPath) + } http.Redirect(w, r, "/my_site", 303) } } diff --git a/templates/edit_file.html b/templates/edit_file.html @@ -1,6 +1,8 @@ {{template "header" .}} <h2>Editing {{.FileName}}</h2> <form id="edit-form" action="/edit/{{.FileName}}" method="POST"> + <label for="rename">Rename:</label> + <input type="text" value="{{.FileName}}" id="rename" name="rename"> <textarea rows="20" name="file_text" id="editor">{{.FileText}}</textarea> <br> <input type="submit" value="Save file" class="button"> diff --git a/utils.go b/utils.go @@ -45,6 +45,9 @@ func checkIfValidFile(filename string, fileBytes []byte) error { if len(filename) == 0 { return fmt.Errorf("Please enter a filename") } + if len(filename) > 256 { // arbitrarily chosen + return fmt.Errorf("Filename is too long") + } ext := strings.ToLower(path.Ext(filename)) found := false for _, mimetype := range c.OkExtensions {