partage

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.z3bra.org/partage.git
Log | Files | Refs

commit 4a574cb7d02d0de66f68a18ba52f9b326886353f
parent 5b3b4556a520c790aa0cc1598cb2d2c9f5b2a0c9
Author: Willy Goiffon <dev@z3bra.org>
Date:   Mon, 18 Oct 2021 17:41:56 +0200

Improve error handling for writefile()

Diffstat:
Mpartage.go | 15++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/partage.go b/partage.go @@ -44,7 +44,7 @@ func contenttype(f *os.File) string { return mime } -func writefile(f *os.File, s io.ReadCloser, contentlength int64) int64 { +func writefile(f *os.File, s io.ReadCloser, contentlength int64) error { buffer := make([]byte, 4096) eof := false sz := int64(0) @@ -54,8 +54,7 @@ func writefile(f *os.File, s io.ReadCloser, contentlength int64) int64 { for !eof { n, err := s.Read(buffer) if err != nil && err != io.EOF { - fmt.Println(err) - return -1 + return err } else if err == io.EOF { eof = true } @@ -69,12 +68,12 @@ func writefile(f *os.File, s io.ReadCloser, contentlength int64) int64 { _, err = f.Write(buffer[:r]) if err != nil { - fmt.Println(err) + return err } sz += r } - return sz + return nil } func servetemplate(w http.ResponseWriter, f string, d templatedata) { @@ -105,8 +104,9 @@ func uploaderPut(w http.ResponseWriter, r *http.Request) { } defer f.Close() - if writefile(f, r.Body, r.ContentLength) < 0 { + if err = writefile(f, r.Body, r.ContentLength); err != nil { w.WriteHeader(http.StatusInternalServerError) + defer os.Remove(tmp.Name()) return } @@ -141,8 +141,9 @@ func uploaderPost(w http.ResponseWriter, r *http.Request) { } defer f.Close() - if writefile(f, post, h.Size) < 0 { + if err = writefile(f, post, h.Size); err != nil { w.WriteHeader(http.StatusInternalServerError) + defer os.Remove(tmp.Name()) return }