2023-08-31 08:37:03 +00:00
|
|
|
package handler
|
2021-02-13 16:17:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/handler/multipart"
|
2023-08-27 15:09:02 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs"
|
2021-02-13 16:17:01 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// MultipartFile provides standard ReadCloser interface and also allows one to
|
|
|
|
// get file name, it's used for multipart uploads.
|
2021-02-13 16:17:01 +00:00
|
|
|
type MultipartFile interface {
|
|
|
|
io.ReadCloser
|
|
|
|
FileName() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchMultipartFile(l *zap.Logger, r io.Reader, boundary string) (MultipartFile, error) {
|
2022-04-29 15:25:26 +00:00
|
|
|
// To have a custom buffer (3mb) the custom multipart reader is used.
|
2023-05-16 14:40:31 +00:00
|
|
|
// 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
|
2021-02-13 16:17:01 +00:00
|
|
|
reader := multipart.NewReader(r, boundary)
|
|
|
|
|
|
|
|
for {
|
|
|
|
part, err := reader.NextPart()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := part.FormName()
|
|
|
|
if name == "" {
|
2023-08-27 15:09:02 +00:00
|
|
|
l.Debug(logs.IgnorePartEmptyFormName)
|
2021-02-13 16:17:01 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := part.FileName()
|
|
|
|
|
|
|
|
// ignore multipart/form-data values
|
|
|
|
if filename == "" {
|
2023-08-27 15:09:02 +00:00
|
|
|
l.Debug(logs.IgnorePartEmptyFilename, zap.String("form", name))
|
2021-02-13 16:17:01 +00:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return part, nil
|
|
|
|
}
|
|
|
|
}
|