flounder

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

commit ade0a38005835f48624cd3c356465f7456c79a2f
parent 99e089d9871ab2102787730f0eb5cfcfc54ba470
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat,  5 Dec 2020 17:34:41 -0800

Allow renaming of binary files

Diffstat:
Mhttp.go | 15++++++++-------
Mtemplates/edit_file.html | 2++
Mtemplates/my_site.html | 2--
Mutils.go | 1+
4 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/http.go b/http.go @@ -92,10 +92,7 @@ func editFileHandler(w http.ResponseWriter, r *http.Request) { return } fileName := filepath.Clean(r.URL.Path[len("/edit/"):]) - if !strings.HasPrefix(mime.TypeByExtension(path.Ext(fileName)), "text") { - renderError(w, "Bad Request: Not a text file, cannot be edited here", http.StatusBadRequest) - return - } + isText := strings.HasPrefix(mime.TypeByExtension(path.Ext(fileName)), "text") filePath := path.Join(c.FilesDirectory, authUser, fileName) if r.Method == "GET" { @@ -108,7 +105,7 @@ func editFileHandler(w http.ResponseWriter, r *http.Request) { // Create directories if dne f, err := os.OpenFile(filePath, os.O_RDONLY, 0644) var fileBytes []byte - if os.IsNotExist(err) { + if os.IsNotExist(err) || !isText { fileBytes = []byte{} err = nil } else { @@ -126,7 +123,8 @@ func editFileHandler(w http.ResponseWriter, r *http.Request) { PageTitle string AuthUser string Host string - }{fileName, string(fileBytes), c.SiteTitle, authUser, c.Host} + IsText bool + }{fileName, string(fileBytes), c.SiteTitle, authUser, c.Host, isText} err = t.ExecuteTemplate(w, "edit_file.html", data) if err != nil { log.Println(err) @@ -146,7 +144,9 @@ func editFileHandler(w http.ResponseWriter, r *http.Request) { // create directories if dne os.MkdirAll(path.Dir(filePath), os.ModePerm) if userHasSpace(authUser, len(fileBytes)) { - err = ioutil.WriteFile(filePath, fileBytes, 0644) + if isText { // Cant edit binary files here + err = ioutil.WriteFile(filePath, fileBytes, 0644) + } } else { renderError(w, fmt.Sprintf("Bad Request: Out of file space. Max space: %d.", c.MaxUserBytes), http.StatusBadRequest) return @@ -167,6 +167,7 @@ func editFileHandler(w http.ResponseWriter, r *http.Request) { newPath := path.Join(c.FilesDirectory, authUser, newName) os.MkdirAll(path.Dir(newPath), os.ModePerm) os.Rename(filePath, newPath) + fileName = newName } http.Redirect(w, r, path.Join("/edit", fileName), http.StatusSeeOther) } diff --git a/templates/edit_file.html b/templates/edit_file.html @@ -3,7 +3,9 @@ <form id="edit-form" action="/edit/{{.FileName}}" method="POST"> <label for="rename">Rename:</label> <input type="text" value="{{.FileName}}" id="rename" name="rename"> + {{ if .IsText }} <textarea rows="25" name="file_text" id="editor">{{.FileText}}</textarea> + {{ end }} <br> <input type="submit" value="Save file" class="button"> <a href="/my_site">Back</a> diff --git a/templates/my_site.html b/templates/my_site.html @@ -31,9 +31,7 @@ {{ .Name }}</a> </td> <td> - {{ if .IsText }} <a href="/edit/{{.Name}}">edit</a> - {{ end }} </td> <td> <form action="/delete/{{.Name}}" method="POST" class="inline"> diff --git a/utils.go b/utils.go @@ -40,6 +40,7 @@ func timeago(t *time.Time) string { } } +// TODO move into checkIfValidFile. rename it func userHasSpace(user string, newBytes int) bool { userPath := path.Join(c.FilesDirectory, user) size, err := dirSize(userPath)