2019-12-13 16:02:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-02-04 11:02:29 +00:00
|
|
|
"path"
|
2019-12-13 16:02:48 +00:00
|
|
|
"strconv"
|
2020-11-09 13:43:23 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-02-14 10:06:43 +00:00
|
|
|
"time"
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2020-12-03 15:00:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
2020-11-09 13:43:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-03-30 22:46:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gate/neofs"
|
2021-03-31 16:58:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gate/tokens"
|
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-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-12-03 15:00:43 +00:00
|
|
|
type (
|
|
|
|
detector struct {
|
|
|
|
io.Writer
|
|
|
|
sync.Once
|
|
|
|
contentType string
|
|
|
|
}
|
|
|
|
|
|
|
|
request struct {
|
|
|
|
*fasthttp.RequestCtx
|
|
|
|
log *zap.Logger
|
2021-03-30 22:46:33 +00:00
|
|
|
obj neofs.ObjectClient
|
2020-12-03 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
objectIDs []*object.ID
|
|
|
|
)
|
2020-11-09 13:43:23 +00:00
|
|
|
|
|
|
|
func newDetector(w io.Writer) *detector {
|
|
|
|
return &detector{Writer: w}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *detector) Write(data []byte) (int, error) {
|
|
|
|
d.Once.Do(func() {
|
|
|
|
d.contentType = http.DetectContentType(data)
|
|
|
|
})
|
|
|
|
return d.Writer.Write(data)
|
|
|
|
}
|
|
|
|
|
2021-03-30 22:46:33 +00:00
|
|
|
func (r *request) receiveFile(options *neofs.GetOptions) {
|
2019-12-13 16:02:48 +00:00
|
|
|
var (
|
2020-12-03 15:00:43 +00:00
|
|
|
err error
|
|
|
|
dis = "inline"
|
|
|
|
start = time.Now()
|
2020-11-09 13:43:23 +00:00
|
|
|
filename string
|
2019-12-13 16:02:48 +00:00
|
|
|
)
|
2021-03-31 16:58:42 +00:00
|
|
|
if err = tokens.StoreBearerToken(r.RequestCtx); err != nil {
|
2021-03-30 22:46:33 +00:00
|
|
|
r.log.Error("could not fetch and store bearer token", zap.Error(err))
|
|
|
|
r.Error("could not fetch and store bearer token", fasthttp.StatusBadRequest)
|
2021-02-16 15:20:15 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-03 15:00:43 +00:00
|
|
|
writer := newDetector(r.Response.BodyWriter())
|
2021-03-30 22:46:33 +00:00
|
|
|
options.Writer = writer
|
|
|
|
obj, err := r.obj.Get(r.RequestCtx, options)
|
2020-11-09 13:43:23 +00:00
|
|
|
if err != nil {
|
2021-03-30 22:46:33 +00:00
|
|
|
r.log.Error(
|
|
|
|
"could not receive object",
|
2020-02-25 15:35:46 +00:00
|
|
|
zap.Stringer("elapsed", time.Since(start)),
|
2021-03-30 22:46:33 +00:00
|
|
|
zap.Error(err),
|
|
|
|
)
|
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-12-03 15:00:43 +00:00
|
|
|
r.Error(msg, code)
|
2020-02-28 17:03:56 +00:00
|
|
|
return
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|
2020-12-03 15:00:43 +00:00
|
|
|
if r.Request.URI().QueryArgs().GetBool("download") {
|
|
|
|
dis = "attachment"
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
2020-12-03 15:00:43 +00:00
|
|
|
r.Response.Header.Set("Content-Length", strconv.FormatUint(obj.PayloadSize(), 10))
|
|
|
|
r.Response.Header.Set("x-object-id", obj.ID().String())
|
|
|
|
r.Response.Header.Set("x-owner-id", obj.OwnerID().String())
|
|
|
|
r.Response.Header.Set("x-container-id", obj.ContainerID().String())
|
2020-11-23 09:32:03 +00:00
|
|
|
for _, attr := range obj.Attributes() {
|
|
|
|
key := attr.Key()
|
|
|
|
val := attr.Value()
|
2020-12-03 15:00:43 +00:00
|
|
|
r.Response.Header.Set("x-"+key, val)
|
2020-11-23 09:32:03 +00:00
|
|
|
switch key {
|
|
|
|
case object.AttributeFileName:
|
2020-11-09 13:43:23 +00:00
|
|
|
filename = val
|
2020-11-23 09:32:03 +00:00
|
|
|
case object.AttributeTimestamp:
|
|
|
|
value, err := strconv.ParseInt(val, 10, 64)
|
|
|
|
if err != nil {
|
2020-12-03 15:00:43 +00:00
|
|
|
r.log.Info("couldn't parse creation date",
|
2020-11-23 09:32:03 +00:00
|
|
|
zap.String("key", key),
|
|
|
|
zap.String("val", val),
|
|
|
|
zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2020-12-03 15:00:43 +00:00
|
|
|
r.Response.Header.Set("Last-Modified",
|
2020-11-23 09:32:03 +00:00
|
|
|
time.Unix(value, 0).Format(time.RFC1123))
|
2020-02-28 17:03:56 +00:00
|
|
|
}
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
2020-12-03 15:00:43 +00:00
|
|
|
r.SetContentType(writer.contentType)
|
|
|
|
r.Response.Header.Set("Content-Disposition", dis+"; filename="+path.Base(filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o objectIDs) Slice() []string {
|
|
|
|
res := make([]string, 0, len(o))
|
|
|
|
for _, oid := range o {
|
|
|
|
res = append(res, oid.String())
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) request(ctx *fasthttp.RequestCtx, log *zap.Logger) *request {
|
|
|
|
return &request{
|
|
|
|
RequestCtx: ctx,
|
2021-03-31 16:58:42 +00:00
|
|
|
log: log,
|
|
|
|
obj: a.plant.Object(),
|
2020-12-03 15:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) byAddress(c *fasthttp.RequestCtx) {
|
|
|
|
var (
|
2021-03-30 22:46:33 +00:00
|
|
|
err error
|
|
|
|
address = object.NewAddress()
|
|
|
|
cid, _ = c.UserValue("cid").(string)
|
|
|
|
oid, _ = c.UserValue("oid").(string)
|
|
|
|
val = strings.Join([]string{cid, oid}, "/")
|
|
|
|
log = a.log.With(zap.String("cid", cid), zap.String("oid", oid))
|
2020-12-03 15:00:43 +00:00
|
|
|
)
|
2021-03-30 22:46:33 +00:00
|
|
|
if err = address.Parse(val); err != nil {
|
2020-12-03 15:00:43 +00:00
|
|
|
log.Error("wrong object address", zap.Error(err))
|
|
|
|
c.Error("wrong object address", fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-03-30 22:46:33 +00:00
|
|
|
// TODO: Take this from a sync-pool.
|
|
|
|
getOpts := new(neofs.GetOptions)
|
|
|
|
getOpts.Client = a.getOperations.client
|
|
|
|
getOpts.SessionToken = a.getOperations.sessionToken
|
|
|
|
getOpts.ObjectAddress = address
|
|
|
|
getOpts.Writer = nil
|
|
|
|
a.request(c, log).receiveFile(getOpts)
|
2020-12-03 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) byAttribute(c *fasthttp.RequestCtx) {
|
|
|
|
var (
|
|
|
|
err error
|
2021-03-30 22:46:33 +00:00
|
|
|
scid, _ = c.UserValue("cid").(string)
|
2020-12-03 15:00:43 +00:00
|
|
|
key, _ = c.UserValue("attr_key").(string)
|
|
|
|
val, _ = c.UserValue("attr_val").(string)
|
2021-03-30 22:46:33 +00:00
|
|
|
log = a.log.With(zap.String("cid", scid), zap.String("attr_key", key), zap.String("attr_val", val))
|
2020-12-03 15:00:43 +00:00
|
|
|
)
|
2021-03-30 22:46:33 +00:00
|
|
|
cid := container.NewID()
|
|
|
|
if err = cid.Parse(scid); err != nil {
|
2020-12-03 15:00:43 +00:00
|
|
|
log.Error("wrong container id", zap.Error(err))
|
|
|
|
c.Error("wrong container id", fasthttp.StatusBadRequest)
|
|
|
|
return
|
2021-03-30 22:46:33 +00:00
|
|
|
}
|
|
|
|
// TODO: Take this from a sync-pool.
|
|
|
|
searchOpts := new(neofs.SearchOptions)
|
|
|
|
searchOpts.Client = a.getOperations.client
|
|
|
|
searchOpts.SessionToken = a.getOperations.sessionToken
|
|
|
|
searchOpts.BearerToken = nil
|
|
|
|
searchOpts.ContainerID = cid
|
|
|
|
searchOpts.Attribute.Key = key
|
|
|
|
searchOpts.Attribute.Value = val
|
|
|
|
var ids []*object.ID
|
|
|
|
if ids, err = a.plant.Object().Search(c, searchOpts); err != nil {
|
2020-12-03 15:00:43 +00:00
|
|
|
log.Error("something went wrong", zap.Error(err))
|
|
|
|
c.Error("something went wrong", fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
} else if len(ids) == 0 {
|
2021-01-23 13:28:12 +00:00
|
|
|
log.Debug("object not found")
|
2021-03-30 22:46:33 +00:00
|
|
|
c.Error("object not found", fasthttp.StatusNotFound)
|
2020-12-03 15:00:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(ids) > 1 {
|
|
|
|
log.Debug("found multiple objects",
|
|
|
|
zap.Strings("object_ids", objectIDs(ids).Slice()),
|
|
|
|
zap.Stringer("show_object_id", ids[0]))
|
|
|
|
}
|
2021-03-30 22:46:33 +00:00
|
|
|
address := object.NewAddress()
|
|
|
|
address.SetContainerID(cid)
|
|
|
|
address.SetObjectID(ids[0])
|
|
|
|
// TODO: Take this from a sync-pool.
|
|
|
|
getOpts := new(neofs.GetOptions)
|
|
|
|
getOpts.Client = a.getOperations.client
|
|
|
|
getOpts.SessionToken = a.getOperations.sessionToken
|
|
|
|
getOpts.ObjectAddress = address
|
|
|
|
getOpts.Writer = nil
|
|
|
|
a.request(c, log).receiveFile(getOpts)
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|