add delete file
alex wennerberg alex@alexwennerberg.com
Sat, 24 Oct 2020 10:30:52 -0700
M
http.go
→
http.go
@@ -89,13 +89,23 @@ } else if r.Method == "POST" {
// get post body r.ParseForm() fileText := r.Form.Get("file_text") - ioutil.WriteFile(filePath, []byte(fileText), 0644) + err := ioutil.WriteFile(filePath, []byte(fileText), 0644) + if err != nil { + log.Println(err) + renderError(w, InternalServerErrorMsg, 500) + return + } http.Redirect(w, r, "/my_site", 302) } } func deleteFileHandler(w http.ResponseWriter, r *http.Request) { + authUser := "alex" + fileName := filepath.Clean(r.URL.Path[len("/delete/"):]) + filePath := path.Join(c.FilesDirectory, authUser, fileName) if r.Method == "POST" { + os.Remove(filePath) + http.Redirect(w, r, "/my_site", 302) } }
M
main.go
→
main.go
@@ -26,6 +26,10 @@ func getUsers() ([]string, error) {
return []string{"me", "other guy"}, nil } +/// Perform some checks to make sure the file is OK +func checkIfValidFile() { +} + func getIndexFiles() ([]*File, error) { // cache this function result := []*File{} err := filepath.Walk(userFilesPath, func(path string, info os.FileInfo, err error) error {