MM-43828: Pass object length for some image operations (#20711)

For user profile and plugin upload, we use a bytes.Buffer.
In that case, we know the object size and can find
it out from the length of the buffer.

This helps reduce multi-part uploads.

This approach can also be taken in thumbnail and preview
images. However, they use an io.Pipe to directly upload
the image as it is being encoded. We could make the whole
process in separate parts of writing the full image in the
buffer and then upload it. But taking a conservative approach
for now.

Also, while here, removed some unused code.

https://mattermost.atlassian.net/browse/MM-43828

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2022-07-27 19:02:24 +05:30
коммит произвёл GitHub
родитель eba08cbb11
Коммит 072c859219
7 изменённых файлов: 30 добавлений и 175 удалений

Просмотреть файл

@@ -4,6 +4,7 @@
package filestore
import (
"bytes"
"context"
"crypto/tls"
"io"
@@ -365,7 +366,13 @@ func (b *S3FileBackend) WriteFile(fr io.Reader, path string) (int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), b.timeout)
defer cancel()
options := s3PutOptions(b.encrypt, contentType)
info, err := b.client.PutObject(ctx, b.bucket, path, fr, -1, options)
objSize := -1
if buf, ok := fr.(*bytes.Buffer); ok {
objSize = buf.Len()
}
info, err := b.client.PutObject(ctx, b.bucket, path, fr, int64(objSize), options)
if err != nil {
return info.Size, errors.Wrapf(err, "unable write the data in the file %s", path)
}
@@ -393,7 +400,11 @@ func (b *S3FileBackend) AppendFile(fr io.Reader, path string) (int64, error) {
partName := fp + ".part"
ctx2, cancel2 := context.WithTimeout(context.Background(), b.timeout)
defer cancel2()
info, err := b.client.PutObject(ctx2, b.bucket, partName, fr, -1, options)
objSize := -1
if buf, ok := fr.(*bytes.Buffer); ok {
objSize = buf.Len()
}
info, err := b.client.PutObject(ctx2, b.bucket, partName, fr, int64(objSize), options)
if err != nil {
return 0, errors.Wrapf(err, "unable append the data in the file %s", path)
}