2023-08-31 08:37:03 +00:00
|
|
|
package handler
|
2021-02-13 16:17:01 +00:00
|
|
|
|
|
|
|
import (
|
2024-09-18 04:35:26 +00:00
|
|
|
"errors"
|
2021-02-13 16:17:01 +00:00
|
|
|
"io"
|
2024-09-18 04:35:26 +00:00
|
|
|
"strconv"
|
2021-02-13 16:17:01 +00:00
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2024-09-18 04:35:26 +00:00
|
|
|
const attributeMultipartObjectSize = "S3-Multipart-Object-Size"
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2024-09-18 04:35:26 +00:00
|
|
|
|
|
|
|
// getPayload returns initial payload if object is not multipart else composes new reader with parts data.
|
|
|
|
func (h *Handler) getPayload(p getMultiobjectBodyParams) (io.ReadCloser, uint64, error) {
|
|
|
|
cid, ok := p.obj.Header.ContainerID()
|
|
|
|
if !ok {
|
|
|
|
return nil, 0, errors.New("no container id set")
|
|
|
|
}
|
|
|
|
oid, ok := p.obj.Header.ID()
|
|
|
|
if !ok {
|
|
|
|
return nil, 0, errors.New("no object id set")
|
|
|
|
}
|
|
|
|
size, err := strconv.ParseUint(p.strSize, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
ctx := p.req.RequestCtx
|
|
|
|
params := PrmInitMultiObjectReader{
|
|
|
|
Addr: newAddress(cid, oid),
|
|
|
|
Bearer: bearerToken(ctx),
|
|
|
|
}
|
|
|
|
payload, err := h.frostfs.InitMultiObjectReader(ctx, params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return io.NopCloser(payload), size, nil
|
|
|
|
}
|