2019-12-13 16:02:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-02-04 11:02:29 +00:00
|
|
|
"path"
|
2019-12-13 16:02:48 +00:00
|
|
|
"strconv"
|
2020-02-14 10:06:43 +00:00
|
|
|
"time"
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-03-31 08:37:10 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/refs"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/service"
|
2020-04-22 10:34:48 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-02-28 17:03:56 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
2019-12-13 16:02:48 +00:00
|
|
|
"go.uber.org/zap"
|
2020-02-25 10:31:20 +00:00
|
|
|
"google.golang.org/grpc"
|
2020-04-22 10:34:48 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2019-12-13 16:02:48 +00:00
|
|
|
)
|
|
|
|
|
2020-02-28 17:03:56 +00:00
|
|
|
func (a *app) receiveFile(c *fasthttp.RequestCtx) {
|
2019-12-13 16:02:48 +00:00
|
|
|
var (
|
2020-02-28 17:03:56 +00:00
|
|
|
err error
|
|
|
|
cid refs.CID
|
|
|
|
oid refs.ObjectID
|
|
|
|
start = time.Now()
|
|
|
|
con *grpc.ClientConn
|
|
|
|
cli object.Service_GetClient
|
|
|
|
ctx context.Context
|
|
|
|
sCID, _ = c.UserValue("cid").(string)
|
|
|
|
sOID, _ = c.UserValue("oid").(string)
|
2019-12-13 16:02:48 +00:00
|
|
|
)
|
|
|
|
|
2020-02-28 17:03:56 +00:00
|
|
|
log := a.log.With(
|
2020-02-25 10:31:20 +00:00
|
|
|
// zap.String("node", con.Target()),
|
2020-02-28 17:03:56 +00:00
|
|
|
zap.String("cid", sCID),
|
|
|
|
zap.String("oid", sOID))
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-28 17:03:56 +00:00
|
|
|
if err = cid.Parse(sCID); err != nil {
|
2019-12-21 10:26:14 +00:00
|
|
|
log.Error("wrong container id", zap.Error(err))
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-28 17:03:56 +00:00
|
|
|
c.Error("wrong container id", fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
} else if err = oid.Parse(sOID); err != nil {
|
2019-12-21 10:26:14 +00:00
|
|
|
log.Error("wrong object id", zap.Error(err))
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-28 17:03:56 +00:00
|
|
|
c.Error("wrong object id", fasthttp.StatusBadRequest)
|
|
|
|
return
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 10:31:20 +00:00
|
|
|
{ // try to connect or throw http error:
|
2020-03-31 08:37:10 +00:00
|
|
|
ctx, cancel := context.WithTimeout(c, a.reqTimeout)
|
2020-02-25 10:31:20 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-02-28 17:03:56 +00:00
|
|
|
if con, err = a.pool.getConnection(ctx); err != nil {
|
2020-02-25 15:35:46 +00:00
|
|
|
log.Error("getConnection timeout", zap.Error(err))
|
2020-02-28 17:03:56 +00:00
|
|
|
c.Error("could not get alive connection", fasthttp.StatusBadRequest)
|
|
|
|
return
|
2020-02-25 10:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 08:37:10 +00:00
|
|
|
ctx, cancel := context.WithTimeout(c, a.reqTimeout)
|
2019-12-13 16:02:48 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-02-25 10:31:20 +00:00
|
|
|
log = log.With(zap.String("node", con.Target()))
|
|
|
|
|
2020-02-25 15:35:46 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-27 09:51:11 +00:00
|
|
|
log.Info("object sent to client", zap.Stringer("elapsed", time.Since(start)))
|
2020-02-25 15:35:46 +00:00
|
|
|
}()
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
req := &object.GetRequest{Address: refs.Address{ObjectID: oid, CID: cid}}
|
|
|
|
req.SetTTL(service.SingleForwardingTTL)
|
|
|
|
|
2020-06-03 03:30:58 +00:00
|
|
|
if err = service.SignDataWithSessionToken(a.key, req); err != nil {
|
2019-12-21 10:26:14 +00:00
|
|
|
log.Error("could not sign request", zap.Error(err))
|
2020-02-28 17:03:56 +00:00
|
|
|
c.Error("could not sign request", fasthttp.StatusBadRequest)
|
|
|
|
return
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 15:35:46 +00:00
|
|
|
if cli, err = object.NewServiceClient(con).Get(ctx, req); err != nil {
|
2019-12-21 10:26:14 +00:00
|
|
|
log.Error("could not prepare connection", zap.Error(err))
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-02-28 17:03:56 +00:00
|
|
|
c.Error("could not prepare connection", fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
} else if err = receiveObject(c, cli); err != nil {
|
2020-02-14 10:06:43 +00:00
|
|
|
log.Error("could not receive object",
|
2020-02-25 15:35:46 +00:00
|
|
|
zap.Stringer("elapsed", time.Since(start)),
|
2020-02-14 10:06:43 +00:00
|
|
|
zap.Error(err))
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-04-22 10:34:48 +00:00
|
|
|
var (
|
|
|
|
msg = errors.Wrap(err, "could not receive object").Error()
|
|
|
|
code = fasthttp.StatusBadRequest
|
|
|
|
)
|
|
|
|
|
|
|
|
if st, ok := status.FromError(errors.Cause(err)); ok && st != nil {
|
|
|
|
if st.Code() == codes.NotFound {
|
|
|
|
code = fasthttp.StatusNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = st.Message()
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 10:34:48 +00:00
|
|
|
c.Error(msg, code)
|
2020-02-28 17:03:56 +00:00
|
|
|
return
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 17:03:56 +00:00
|
|
|
func receiveObject(c *fasthttp.RequestCtx, cli object.Service_GetClient) error {
|
|
|
|
var (
|
|
|
|
typ string
|
2020-06-10 17:04:27 +00:00
|
|
|
content_disp_type string
|
2020-02-28 17:03:56 +00:00
|
|
|
put = c.Request.URI().QueryArgs().GetBool("download")
|
|
|
|
)
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
for {
|
|
|
|
resp, err := cli.Recv()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2020-02-28 17:03:56 +00:00
|
|
|
return err
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 17:03:56 +00:00
|
|
|
switch o := resp.R.(type) {
|
|
|
|
case *object.GetResponse_Object:
|
|
|
|
obj := o.Object
|
|
|
|
|
|
|
|
c.Response.Header.Set("Content-Length", strconv.FormatUint(obj.SystemHeader.PayloadLength, 10))
|
|
|
|
c.Response.Header.Set("x-object-id", obj.SystemHeader.ID.String())
|
|
|
|
c.Response.Header.Set("x-owner-id", obj.SystemHeader.OwnerID.String())
|
|
|
|
c.Response.Header.Set("x-container-id", obj.SystemHeader.CID.String())
|
|
|
|
|
|
|
|
for i := range obj.Headers {
|
|
|
|
if hdr := obj.Headers[i].GetUserHeader(); hdr != nil {
|
|
|
|
c.Response.Header.Set("x-"+hdr.Key, hdr.Value)
|
|
|
|
|
2020-06-10 17:04:27 +00:00
|
|
|
if hdr.Key == object.FilenameHeader {
|
|
|
|
if put {
|
|
|
|
content_disp_type = "attachment"
|
2020-06-10 17:30:43 +00:00
|
|
|
} else {
|
2020-06-10 17:28:09 +00:00
|
|
|
content_disp_type = "inline"
|
|
|
|
}
|
2020-02-28 17:03:56 +00:00
|
|
|
// NOTE: we use path.Base because hdr.Value can be something like `/path/to/filename.ext`
|
2020-06-10 17:04:27 +00:00
|
|
|
c.Response.Header.Set("Content-Disposition", content_disp_type+"; filename="+path.Base(hdr.Value))
|
2020-02-28 17:03:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 13:40:54 +00:00
|
|
|
if len(obj.Payload) > 0 {
|
|
|
|
typ = http.DetectContentType(obj.Payload)
|
|
|
|
}
|
2020-02-28 17:03:56 +00:00
|
|
|
|
|
|
|
if _, err = c.Write(obj.Payload); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case *object.GetResponse_Chunk:
|
|
|
|
if typ == "" {
|
|
|
|
typ = http.DetectContentType(o.Chunk)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = c.Write(o.Chunk); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|
2020-02-28 17:03:56 +00:00
|
|
|
|
|
|
|
c.SetContentType(typ)
|
|
|
|
|
|
|
|
return nil
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|