lib/rest: Calculate correct Content-Length on MultiPart Uploads

This is used in the pcloud backend and in the upcoming 1fichier backend.
This commit is contained in:
Fionera
2019-06-24 20:34:49 +02:00
committed by Nick Craig-Wood
parent 07e2c3a50f
commit 6cd7c3b774
2 changed files with 42 additions and 15 deletions

View File

@@ -9,11 +9,9 @@ package pcloud
// FIXME mime type? Fix overview if implement.
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
@@ -1091,21 +1089,18 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
// opts.Body=0), so upload it as a multpart form POST with
// Content-Length set.
if size == 0 {
formReader, contentType, err := rest.MultipartUpload(in, opts.Parameters, "content", leaf)
formReader, contentType, overhead, err := rest.MultipartUpload(in, opts.Parameters, "content", leaf)
if err != nil {
return errors.Wrap(err, "failed to make multipart upload for 0 length file")
}
formBody, err := ioutil.ReadAll(formReader)
if err != nil {
return errors.Wrap(err, "failed to read multipart upload for 0 length file")
}
length := int64(len(formBody))
contentLength := overhead + size
opts.ContentType = contentType
opts.Body = bytes.NewBuffer(formBody)
opts.Body = formReader
opts.Method = "POST"
opts.Parameters = nil
opts.ContentLength = &length
opts.ContentLength = &contentLength
}
err = o.fs.pacer.CallNoRetry(func() (bool, error) {