Don't create file until save
alex wennerberg alex@alexwennerberg.com
Wed, 18 Nov 2020 19:21:32 -0800
M
http.go
→
http.go
@@ -98,10 +98,15 @@ 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) + f, err := os.OpenFile(filePath, os.O_RDONLY, 0644) + var fileBytes []byte + if os.IsNotExist(err) { + fileBytes = []byte{} + err = nil + } else { + defer f.Close() + fileBytes, err = ioutil.ReadAll(f) + } if err != nil { log.Println(err) renderError(w, InternalServerErrorMsg, 500)