Fail PUT uploads when the file cannot be closed
This commit is contained in:
@@ -262,14 +262,20 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "Failed to create file", http.StatusInternalServerError)
|
http.Error(w, "Failed to create file", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
// MaxBytesReader needs the ResponseWriter the stdlib handed us so it can
|
// MaxBytesReader needs the ResponseWriter the stdlib handed us so it can
|
||||||
// mark the connection for close; the logging wrapper would hide it.
|
// mark the connection for close; the logging wrapper would hide it.
|
||||||
limitedBody := http.MaxBytesReader(baseWriter(w), r.Body, maxUploadSize)
|
limitedBody := http.MaxBytesReader(baseWriter(w), r.Body, maxUploadSize)
|
||||||
written, err := io.Copy(file, limitedBody)
|
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 {
|
if err != nil {
|
||||||
file.Close()
|
|
||||||
os.Remove(targetPath)
|
os.Remove(targetPath)
|
||||||
os.Remove(targetDir)
|
os.Remove(targetDir)
|
||||||
if isMaxBytesError(err) {
|
if isMaxBytesError(err) {
|
||||||
|
|||||||
Reference in New Issue
Block a user