forked from TrueCloudLab/frostfs-http-gw
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package uploader
|
|
|
|
import (
|
|
"io"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/uploader/multipart"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// MultipartFile provides standard ReadCloser interface and also allows one to
|
|
// get file name, it's used for multipart uploads.
|
|
type MultipartFile interface {
|
|
io.ReadCloser
|
|
FileName() string
|
|
}
|
|
|
|
func fetchMultipartFile(l *zap.Logger, r io.Reader, boundary string) (MultipartFile, error) {
|
|
// To have a custom buffer (3mb) the custom multipart reader is used.
|
|
// Default reader uses 4KiB chunks, which slow down upload speed up to 400%
|
|
// https://github.com/golang/go/blob/91b9915d3f6f8cd2e9e9fda63f67772803adfa03/src/mime/multipart/multipart.go#L32
|
|
reader := multipart.NewReader(r, boundary)
|
|
|
|
for {
|
|
part, err := reader.NextPart()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
name := part.FormName()
|
|
if name == "" {
|
|
l.Debug(logs.IgnorePartEmptyFormName)
|
|
continue
|
|
}
|
|
|
|
filename := part.FileName()
|
|
|
|
// ignore multipart/form-data values
|
|
if filename == "" {
|
|
l.Debug(logs.IgnorePartEmptyFilename, zap.String("form", name))
|
|
|
|
continue
|
|
}
|
|
|
|
return part, nil
|
|
}
|
|
}
|