diff --git a/main.go b/main.go index 8b45600..4dad4c8 100644 --- a/main.go +++ b/main.go @@ -262,14 +262,20 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { http.Error(w, "Failed to create file", http.StatusInternalServerError) return } - defer file.Close() // MaxBytesReader needs the ResponseWriter the stdlib handed us so it can // mark the connection for close; the logging wrapper would hide it. limitedBody := http.MaxBytesReader(baseWriter(w), r.Body, maxUploadSize) written, err := io.Copy(file, limitedBody) + // Closed here rather than in a defer: nothing above returns early, and a + // write-back failure only surfacing at close would otherwise arrive after + // the 200 and the URL had gone out, leaving the client believing a + // truncated file was stored. + closeErr := file.Close() + if err == nil { + err = closeErr + } if err != nil { - file.Close() os.Remove(targetPath) os.Remove(targetDir) if isMaxBytesError(err) {