Allow folders to exist
alex wennerberg alex@alexwennerberg.com
Fri, 13 Nov 2020 00:03:40 -0800
M
http.go
→
http.go
@@ -89,6 +89,7 @@ renderError(w, "Not a text file, cannot be edited here", 400) // correct status code?
return } filePath := path.Join(c.FilesDirectory, authUser, fileName) + if r.Method == "GET" { err := checkIfValidFile(filePath, nil) if err != nil {@@ -96,6 +97,8 @@ log.Println(err)
renderError(w, err.Error(), 400) return } + // create directories if dne + os.MkdirAll(path.Dir(filePath), os.ModePerm) f, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, 0644) defer f.Close() fileBytes, err := ioutil.ReadAll(f)@@ -125,6 +128,8 @@ log.Println(err)
renderError(w, err.Error(), 400) return } + // create directories if dne + os.MkdirAll(path.Dir(filePath), os.ModePerm) err = ioutil.WriteFile(filePath, fileBytes, 0644) if err != nil { log.Println(err)