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"
|
|
|
|
"strings"
|
2020-02-14 10:06:43 +00:00
|
|
|
"time"
|
2019-12-13 16:02:48 +00:00
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2020-02-04 10:32:38 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api/container"
|
|
|
|
"github.com/nspcc-dev/neofs-api/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api/refs"
|
|
|
|
"github.com/nspcc-dev/neofs-api/service"
|
2019-12-13 16:02:48 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
2020-02-25 10:31:20 +00:00
|
|
|
"google.golang.org/grpc"
|
2019-12-13 16:02:48 +00:00
|
|
|
)
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
func (r *router) receiveFile(c echo.Context) error {
|
2019-12-13 16:02:48 +00:00
|
|
|
var (
|
2020-02-25 10:31:20 +00:00
|
|
|
err error
|
2019-12-13 16:02:48 +00:00
|
|
|
cid refs.CID
|
|
|
|
oid refs.ObjectID
|
|
|
|
obj *object.Object
|
2020-02-14 10:06:43 +00:00
|
|
|
start = time.Now()
|
2020-02-25 10:31:20 +00:00
|
|
|
con *grpc.ClientConn
|
2020-02-13 14:31:33 +00:00
|
|
|
ctx = c.Request().Context()
|
2019-12-13 16:02:48 +00:00
|
|
|
download = c.QueryParam("download") != ""
|
|
|
|
)
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
log := r.log.With(
|
2020-02-25 10:31:20 +00:00
|
|
|
// zap.String("node", con.Target()),
|
2019-12-13 16:02:48 +00:00
|
|
|
zap.String("cid", c.Param("cid")),
|
|
|
|
zap.String("oid", c.Param("oid")))
|
|
|
|
|
|
|
|
if err := cid.Parse(c.Param("cid")); 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
|
|
|
|
|
|
|
return echo.NewHTTPError(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
errors.Wrap(err, "wrong container id").Error(),
|
|
|
|
)
|
|
|
|
} else if err := oid.Parse(c.Param("oid")); 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
|
|
|
|
|
|
|
return echo.NewHTTPError(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
errors.Wrap(err, "wrong object id").Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-25 10:31:20 +00:00
|
|
|
{ // try to connect or throw http error:
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, r.timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if con, err = r.pool.getConnection(ctx); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:31:33 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, r.timeout)
|
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()))
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
req := &object.GetRequest{Address: refs.Address{ObjectID: oid, CID: cid}}
|
|
|
|
req.SetTTL(service.SingleForwardingTTL)
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
if err := service.SignRequestHeader(r.key, req); err != nil {
|
|
|
|
log.Error("could not sign request", zap.Error(err))
|
2019-12-13 16:02:48 +00:00
|
|
|
return echo.NewHTTPError(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
errors.Wrap(err, "could not sign request").Error())
|
|
|
|
}
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
cli, err := object.NewServiceClient(con).Get(ctx, req)
|
2019-12-13 16:02:48 +00:00
|
|
|
if 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
|
|
|
|
|
|
|
return echo.NewHTTPError(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
errors.Wrap(err, "could not prepare connection").Error(),
|
|
|
|
)
|
|
|
|
} else if obj, err = receiveObject(cli); err != nil {
|
2020-02-14 10:06:43 +00:00
|
|
|
log.Error("could not receive object",
|
|
|
|
zap.Duration("elapsed", time.Since(start)),
|
|
|
|
zap.Error(err))
|
2019-12-13 16:02:48 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.Contains(err.Error(), object.ErrNotFound.Error()),
|
|
|
|
strings.Contains(err.Error(), container.ErrNotFound.Error()):
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, err.Error())
|
|
|
|
default:
|
|
|
|
return echo.NewHTTPError(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
errors.Wrap(err, "could not receive object").Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
log.Info("object fetched successfully")
|
2019-12-13 16:02:48 +00:00
|
|
|
|
|
|
|
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-02-04 11:00:45 +00:00
|
|
|
if hdr.Key == object.FilenameHeader && download {
|
2020-02-04 11:02:29 +00:00
|
|
|
// NOTE: we use path.Base because hdr.Value can be something like `/path/to/filename.ext`
|
|
|
|
c.Response().Header().Set("Content-Disposition", "attachment; filename="+path.Base(hdr.Value))
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Blob(http.StatusOK,
|
|
|
|
http.DetectContentType(obj.Payload),
|
|
|
|
obj.Payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
func receiveObject(cli object.Service_GetClient) (*object.Object, error) {
|
|
|
|
var obj *object.Object
|
|
|
|
for {
|
|
|
|
resp, err := cli.Recv()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
} else if obj == nil {
|
|
|
|
obj = resp.GetObject()
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.Payload = append(obj.Payload, resp.GetChunk()...)
|
|
|
|
}
|
|
|
|
return obj, nil
|
|
|
|
}
|