Refactor Upload-Length handling to use early returns

This commit is contained in:
Adam Jensen 2018-06-11 17:59:54 -04:00
parent 7fd0139355
commit 7f11b92fab
1 changed files with 18 additions and 18 deletions

View File

@ -461,26 +461,26 @@ func (handler *UnroutedHandler) PatchFile(w http.ResponseWriter, r *http.Request
} }
if r.Header.Get("Upload-Length") != "" { if r.Header.Get("Upload-Length") != "" {
if handler.composer.UsesLengthDeferrer { if !handler.composer.UsesLengthDeferrer {
if info.SizeIsDeferred { handler.sendError(w, r, ErrNotImplemented)
return
}
if !info.SizeIsDeferred {
handler.sendError(w, r, ErrInvalidUploadLength)
return
}
uploadLength, err := strconv.ParseInt(r.Header.Get("Upload-Length"), 10, 64) uploadLength, err := strconv.ParseInt(r.Header.Get("Upload-Length"), 10, 64)
if err != nil || uploadLength < 0 || uploadLength < info.Offset || uploadLength > handler.config.MaxSize { if err != nil || uploadLength < 0 || uploadLength < info.Offset || uploadLength > handler.config.MaxSize {
handler.sendError(w, r, ErrInvalidUploadLength) handler.sendError(w, r, ErrInvalidUploadLength)
return return
} }
if err := handler.composer.LengthDeferrer.DeclareLength(id, uploadLength); err != nil {
info.Size = uploadLength
info.SizeIsDeferred = false
if err := handler.composer.LengthDeferrer.DeclareLength(id, info.Size); err != nil {
handler.sendError(w, r, err) handler.sendError(w, r, err)
return return
} }
} else {
handler.sendError(w, r, ErrInvalidUploadLength) info.Size = uploadLength
} info.SizeIsDeferred = false
} else {
handler.sendError(w, r, ErrNotImplemented)
}
} }
if err := handler.writeChunk(id, info, w, r); err != nil { if err := handler.writeChunk(id, info, w, r); err != nil {