all repos — flounder @ ade0a38005835f48624cd3c356465f7456c79a2f

A small site builder for the Gemini protocol

Allow renaming of binary files
alex wennerberg alex@alexwennerberg.com
Sat, 05 Dec 2020 17:34:41 -0800
commit

ade0a38005835f48624cd3c356465f7456c79a2f

parent

99e089d9871ab2102787730f0eb5cfcfc54ba470

4 files changed, 11 insertions(+), 9 deletions(-)

jump to
M http.gohttp.go

@@ -92,10 +92,7 @@ renderDefaultError(w, http.StatusForbidden)

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 @@ }

// 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 @@ FileText string

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 @@ }

// 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 @@ if newName != fileName {

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) }
M templates/edit_file.htmltemplates/edit_file.html

@@ -3,7 +3,9 @@ <h2>Editing <a href="//{{.AuthUser}}.{{.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"> + {{ 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>
M templates/my_site.htmltemplates/my_site.html

@@ -31,9 +31,7 @@ <a href="//{{.Creator}}.{{.Host}}/{{.Name}}">

{{ .Name }}</a> </td> <td> - {{ if .IsText }} <a href="/edit/{{.Name}}">edit</a> - {{ end }} </td> <td> <form action="/delete/{{.Name}}" method="POST" class="inline">
M utils.goutils.go

@@ -40,6 +40,7 @@ return fmt.Sprintf("%d days ago", days)

} } +// TODO move into checkIfValidFile. rename it func userHasSpace(user string, newBytes int) bool { userPath := path.Join(c.FilesDirectory, user) size, err := dirSize(userPath)