mediaapi/fileutils: Move logic from function to call site

Makes for better readability. The only reason it was inside the function
was to avoid being too complex for gocyclo checks.
This commit is contained in:
Robert Swain 2017-05-22 15:21:56 +02:00
parent 6d000794ec
commit 0ecb645f27

View file

@ -87,17 +87,13 @@ var (
) )
// writeToResponse takes bytesToWrite bytes from buffer and writes them to respWriter // writeToResponse takes bytesToWrite bytes from buffer and writes them to respWriter
// Returns bytes written and an error. In case of error, or if there is no respWriter, // Returns bytes written and an error. In case of error, the number of bytes written will be 0.
// the number of bytes written will be 0.
func writeToResponse(respWriter http.ResponseWriter, buffer []byte, bytesToWrite int) (int64, error) { func writeToResponse(respWriter http.ResponseWriter, buffer []byte, bytesToWrite int) (int64, error) {
if respWriter != nil { bytesWritten, respErr := respWriter.Write(buffer[:bytesToWrite])
bytesWritten, respErr := respWriter.Write(buffer[:bytesToWrite]) if bytesWritten != bytesToWrite || (respErr != nil && respErr != io.EOF) {
if bytesWritten != bytesToWrite || (respErr != nil && respErr != io.EOF) { return 0, errResponse
return 0, errResponse
}
return int64(bytesWritten), nil
} }
return 0, nil return int64(bytesWritten), nil
} }
// writeToDiskAndHasher takes bytesToWrite bytes from buffer and writes them to tmpFileWriter and hasher. // writeToDiskAndHasher takes bytesToWrite bytes from buffer and writes them to tmpFileWriter and hasher.
@ -156,8 +152,10 @@ func ReadAndHashAndWriteWithLimit(reqReader io.Reader, maxFileSizeBytes types.Fi
if bytesRead > 0 { if bytesRead > 0 {
// Note: This code allows proxying files larger than maxFileSizeBytes! // Note: This code allows proxying files larger than maxFileSizeBytes!
// write to client request's response body // write to client request's response body
bytesTemp, copyError = writeToResponse(respWriter, buffer, bytesRead) if respWriter != nil {
bytesResponded += bytesTemp bytesTemp, copyError = writeToResponse(respWriter, buffer, bytesRead)
bytesResponded += bytesTemp
}
if copyError == nil { if copyError == nil {
// Note: if we get here then copyError != ErrFileIsTooLarge && copyError != errWrite // Note: if we get here then copyError != ErrFileIsTooLarge && copyError != errWrite
// as if copyError == errResponse || copyError == errWrite then we would have broken // as if copyError == errResponse || copyError == errWrite then we would have broken