Add simple file renaming
alex wennerberg alex@alexwennerberg.com
Wed, 18 Nov 2020 19:39:17 -0800
3 files changed,
10 insertions(+),
0 deletions(-)
M
http.go
→
http.go
@@ -141,6 +141,11 @@ log.Println(err)
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) } }
M
templates/edit_file.html
→
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">
M
utils.go
→
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 {