Fail PUT uploads when the file cannot be closed

This commit is contained in:
2026-09-08 20:18:52 +02:00
parent cee69470e0
commit bd291e841b
+8 -2
View File
@@ -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) {