2023-08-31 08:37:03 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/response"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/utils"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type readCloser struct {
|
|
|
|
io.Reader
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
// initializes io.Reader with the limited size and detects Content-Type from it.
|
|
|
|
// Returns r's error directly. Also returns the processed data.
|
|
|
|
func readContentType(maxSize uint64, rInit func(uint64) (io.Reader, error)) (string, []byte, error) {
|
|
|
|
if maxSize > sizeToDetectType {
|
|
|
|
maxSize = sizeToDetectType
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, maxSize) // maybe sync-pool the slice?
|
|
|
|
|
|
|
|
r, err := rInit(maxSize)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := r.Read(buf)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = buf[:n]
|
|
|
|
|
|
|
|
return http.DetectContentType(buf), buf, err // to not lose io.EOF
|
|
|
|
}
|
|
|
|
|
2024-09-18 04:35:26 +00:00
|
|
|
type getMultiobjectBodyParams struct {
|
|
|
|
obj *Object
|
|
|
|
req request
|
|
|
|
strSize string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) receiveFile(ctx context.Context, req request, objAddress oid.Address) {
|
2023-08-31 08:37:03 +00:00
|
|
|
var (
|
2024-09-18 04:35:26 +00:00
|
|
|
shouldDownload = req.QueryArgs().GetBool("download")
|
|
|
|
start = time.Now()
|
|
|
|
filename string
|
|
|
|
filepath string
|
|
|
|
contentType string
|
2023-08-31 08:37:03 +00:00
|
|
|
)
|
|
|
|
|
2024-07-15 13:35:08 +00:00
|
|
|
prm := PrmObjectGet{
|
2024-06-17 14:56:43 +00:00
|
|
|
PrmAuth: PrmAuth{
|
|
|
|
BearerToken: bearerToken(ctx),
|
|
|
|
},
|
2024-09-18 04:35:26 +00:00
|
|
|
Address: objAddress,
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
|
|
|
|
2024-07-15 13:35:08 +00:00
|
|
|
rObj, err := h.frostfs.GetObject(ctx, prm)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
|
|
|
req.handleFrostFSErr(err, start)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can't close reader in this function, so how to do it?
|
2024-09-18 04:35:26 +00:00
|
|
|
req.setIDs(rObj.Header)
|
|
|
|
payload := rObj.Payload
|
2024-07-15 13:35:08 +00:00
|
|
|
payloadSize := rObj.Header.PayloadSize()
|
|
|
|
for _, attr := range rObj.Header.Attributes() {
|
2023-08-31 08:37:03 +00:00
|
|
|
key := attr.Key()
|
|
|
|
val := attr.Value()
|
|
|
|
if !isValidToken(key) || !isValidValue(val) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
key = utils.BackwardTransformIfSystem(key)
|
|
|
|
|
|
|
|
req.Response.Header.Set(utils.UserAttributeHeaderPrefix+key, val)
|
|
|
|
switch key {
|
|
|
|
case object.AttributeFileName:
|
|
|
|
filename = val
|
|
|
|
case object.AttributeTimestamp:
|
2024-09-18 04:35:26 +00:00
|
|
|
if err = req.setTimestamp(val); err != nil {
|
|
|
|
req.log.Error(logs.CouldntParseCreationDate,
|
2023-08-31 08:37:03 +00:00
|
|
|
zap.String("val", val),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
case object.AttributeContentType:
|
|
|
|
contentType = val
|
2024-09-26 14:32:27 +00:00
|
|
|
case object.AttributeFilePath:
|
|
|
|
filepath = val
|
2024-09-18 04:35:26 +00:00
|
|
|
case attributeMultipartObjectSize:
|
|
|
|
payload, payloadSize, err = h.getPayload(getMultiobjectBodyParams{
|
|
|
|
obj: rObj,
|
|
|
|
req: req,
|
|
|
|
strSize: val,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
req.handleFrostFSErr(err, start)
|
|
|
|
return
|
|
|
|
}
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-18 04:35:26 +00:00
|
|
|
if filename == "" {
|
|
|
|
filename = filepath
|
|
|
|
}
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2024-09-18 04:35:26 +00:00
|
|
|
req.setDisposition(shouldDownload, filename)
|
|
|
|
|
|
|
|
req.Response.Header.Set(fasthttp.HeaderContentLength, strconv.FormatUint(payloadSize, 10))
|
2023-08-31 08:37:03 +00:00
|
|
|
|
|
|
|
if len(contentType) == 0 {
|
|
|
|
// determine the Content-Type from the payload head
|
|
|
|
var payloadHead []byte
|
|
|
|
|
|
|
|
contentType, payloadHead, err = readContentType(payloadSize, func(uint64) (io.Reader, error) {
|
2024-09-18 04:35:26 +00:00
|
|
|
return payload, nil
|
2023-08-31 08:37:03 +00:00
|
|
|
})
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
req.log.Error(logs.CouldNotDetectContentTypeFromPayload, zap.Error(err))
|
|
|
|
response.Error(req.RequestCtx, "could not detect Content-Type from payload: "+err.Error(), fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset payload reader since a part of the data has been read
|
|
|
|
var headReader io.Reader = bytes.NewReader(payloadHead)
|
|
|
|
|
|
|
|
if err != io.EOF { // otherwise, we've already read full payload
|
2024-09-18 04:35:26 +00:00
|
|
|
headReader = io.MultiReader(headReader, payload)
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// note: we could do with io.Reader, but SetBodyStream below closes body stream
|
|
|
|
// if it implements io.Closer and that's useful for us.
|
2024-09-18 04:35:26 +00:00
|
|
|
payload = readCloser{headReader, payload}
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
|
|
|
req.SetContentType(contentType)
|
2024-09-18 04:35:26 +00:00
|
|
|
req.Response.SetBodyStream(payload, int(payloadSize))
|
|
|
|
}
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2024-09-18 04:35:26 +00:00
|
|
|
func (r *request) setIDs(obj object.Object) {
|
|
|
|
objID, _ := obj.ID()
|
|
|
|
cnrID, _ := obj.ContainerID()
|
|
|
|
r.Response.Header.Set(hdrObjectID, objID.String())
|
|
|
|
r.Response.Header.Set(hdrOwnerID, obj.OwnerID().String())
|
|
|
|
r.Response.Header.Set(hdrContainerID, cnrID.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) setDisposition(shouldDownload bool, filename string) {
|
|
|
|
const (
|
|
|
|
inlineDisposition = "inline"
|
|
|
|
attachmentDisposition = "attachment"
|
|
|
|
)
|
|
|
|
|
|
|
|
dis := inlineDisposition
|
|
|
|
if shouldDownload {
|
|
|
|
dis = attachmentDisposition
|
2024-09-26 14:32:27 +00:00
|
|
|
}
|
|
|
|
|
2024-09-18 04:35:26 +00:00
|
|
|
r.Response.Header.Set(fasthttp.HeaderContentDisposition, dis+"; filename="+path.Base(filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) setTimestamp(timestamp string) error {
|
|
|
|
value, err := strconv.ParseInt(timestamp, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.Response.Header.Set(fasthttp.HeaderLastModified,
|
|
|
|
time.Unix(value, 0).UTC().Format(http.TimeFormat))
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2024-09-18 04:35:26 +00:00
|
|
|
return nil
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|