fixing lint issues

This commit is contained in:
darkgallium 2020-10-25 15:21:44 +01:00
parent a1cba59444
commit d0133176d5

View file

@ -15,12 +15,12 @@
package routing package routing
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"bytes"
"mime" "mime"
"net/http" "net/http"
"net/url" "net/url"
@ -675,6 +675,36 @@ func (r *downloadRequest) fetchRemoteFileAndStoreMetadata(
return nil return nil
} }
func (r *downloadRequest) GetContentLengthAndReader(contentLengthHeader string, body *io.ReadCloser) (int64, io.Reader, error) {
var reader io.Reader
var contentLength int64
if contentLengthHeader != "" {
parsedLength, parseErr := strconv.ParseInt(contentLengthHeader, 10, 64)
if parseErr != nil {
r.Logger.WithError(parseErr).Warn("Failed to parse content length")
return 0, nil, errors.Wrap(parseErr, "invalid response from remote server")
}
reader = *body
contentLength = parsedLength
} else {
// Content-Length header is missing, we need to read the whole body to get its length
bodyBytes, readAllErr := ioutil.ReadAll(*body)
if readAllErr != nil {
r.Logger.WithError(readAllErr).Warn("Could not read response body from remote server")
return 0, nil, errors.Wrap(readAllErr, "invalid response from remote server")
}
contentLength = int64(len(bodyBytes))
reader = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
}
return contentLength, reader, nil
}
func (r *downloadRequest) fetchRemoteFile( func (r *downloadRequest) fetchRemoteFile(
ctx context.Context, ctx context.Context,
client *gomatrixserverlib.Client, client *gomatrixserverlib.Client,
@ -694,31 +724,11 @@ func (r *downloadRequest) fetchRemoteFile(
} }
defer resp.Body.Close() // nolint: errcheck defer resp.Body.Close() // nolint: errcheck
var reader io.Reader contentLength, reader, parseErr := r.GetContentLengthAndReader(resp.Header.Get("Content-Length"), &resp.Body)
var contentLength int64 if parseErr != nil {
return "", false, parseErr
if resp.Header.Get("Content-Length") != "" {
parsedLength, err := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
if err != nil {
r.Logger.WithError(err).Warn("Failed to parse content length")
return "", false, errors.Wrap(err, "invalid response from remote server")
}
reader = resp.Body
contentLength = parsedLength
} else {
// Content-Length header is missing, we need to read the whole body to get its length
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
r.Logger.WithError(err).Warn("Could not read response body from remote server")
return "", false, fmt.Errorf("file could not be downloaded from remote server")
}
contentLength = int64(len(bodyBytes))
reader = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
} }
if contentLength > int64(maxFileSizeBytes) { if contentLength > int64(maxFileSizeBytes) {
// TODO: Bubble up this as a 413 // TODO: Bubble up this as a 413
return "", false, fmt.Errorf("remote file is too large (%v > %v bytes)", contentLength, maxFileSizeBytes) return "", false, fmt.Errorf("remote file is too large (%v > %v bytes)", contentLength, maxFileSizeBytes)