all repos — flounder @ 2772a6a12025ad55c52e87a9be1ee129d0e6750b

A small site builder for the Gemini protocol

Add alert messsage to saved file
alex wennerberg alex@alexwennerberg.com
Wed, 30 Dec 2020 10:34:04 -0800
commit

2772a6a12025ad55c52e87a9be1ee129d0e6750b

parent

f73a980efa7867e44024c4427954c4a017db70ca

3 files changed, 43 insertions(+), 35 deletions(-)

jump to
M http.gohttp.go

@@ -105,40 +105,8 @@ }

fileName := filepath.Clean(r.URL.Path[len("/edit/"):]) filePath := path.Join(c.FilesDirectory, user.Username, fileName) isText := isTextFile(filePath) - - if r.Method == "GET" { - err := checkIfValidFile(filePath, nil) - if err != nil { - log.Println(err) - renderError(w, err.Error(), http.StatusBadRequest) - return - } - // Create directories if dne - f, err := os.OpenFile(filePath, os.O_RDONLY, 0644) - var fileBytes []byte - if os.IsNotExist(err) || !isText { - fileBytes = []byte{} - err = nil - } else { - defer f.Close() - fileBytes, err = ioutil.ReadAll(f) - } - if err != nil { - panic(err) - } - data := struct { - FileName string - FileText string - PageTitle string - AuthUser AuthUser - Host string - IsText bool - }{fileName, string(fileBytes), c.SiteTitle, user, c.Host, isText} - err = t.ExecuteTemplate(w, "edit_file.html", data) - if err != nil { - panic(err) - } - } else if r.Method == "POST" { + alert := "" + if r.Method == "POST" { // get post body r.ParseForm() fileText := r.Form.Get("file_text")

@@ -165,6 +133,7 @@ }

if err != nil { panic(err) } + alert = "saved" newName := filepath.Clean(r.Form.Get("rename")) err = checkIfValidFile(newName, fileBytes) if err != nil {

@@ -177,8 +146,42 @@ newPath := path.Join(c.FilesDirectory, user.Username, newName)

os.MkdirAll(path.Dir(newPath), os.ModePerm) os.Rename(filePath, newPath) fileName = newName + filePath = newPath + alert += " and renamed" } - http.Redirect(w, r, path.Join("/edit", fileName), http.StatusSeeOther) + } + + err := checkIfValidFile(filePath, nil) + if err != nil { + log.Println(err) + renderError(w, err.Error(), http.StatusBadRequest) + return + } + // Create directories if dne + f, err := os.OpenFile(filePath, os.O_RDONLY, 0644) + var fileBytes []byte + if os.IsNotExist(err) || !isText { + fileBytes = []byte{} + err = nil + } else { + defer f.Close() + fileBytes, err = ioutil.ReadAll(f) + } + if err != nil { + panic(err) + } + data := struct { + FileName string + FileText string + PageTitle string + AuthUser AuthUser + Host string + IsText bool + Alert string + }{fileName, string(fileBytes), c.SiteTitle, user, c.Host, isText, alert} + err = t.ExecuteTemplate(w, "edit_file.html", data) + if err != nil { + panic(err) } }
M templates/edit_file.htmltemplates/edit_file.html

@@ -9,5 +9,7 @@ {{ end }}

<br> <input type="submit" value="Save file" class="button"> <a href="/my_site">Back</a> + <script type="text/javascript">window.setTimeout("document.getElementById('save-message').style.display='none';", 2000); </script> + <div id="save-message" class="alert">{{.Alert}}</div> </form> {{template "footer" .}}
M templates/static/style.csstemplates/static/style.css

@@ -105,6 +105,9 @@

.error { color: red; } +.alert { + color: green; +} .nav { color: blue;