2021-03-31 16:58:42 +00:00
|
|
|
package uploader
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-16 15:38:30 +00:00
|
|
|
"context"
|
2021-01-25 19:36:46 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
2021-03-31 18:24:41 +00:00
|
|
|
"sync"
|
2021-01-25 19:36:46 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-02-16 15:38:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2021-03-30 22:46:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
|
|
|
"github.com/nspcc-dev/neofs-http-gate/neofs"
|
2021-03-31 16:58:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gate/tokens"
|
2021-01-25 19:36:46 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-04-28 08:39:12 +00:00
|
|
|
const (
|
|
|
|
jsonHeader = "application/json; charset=UTF-8"
|
|
|
|
drainBufSize = 4096
|
|
|
|
)
|
2021-04-05 14:48:01 +00:00
|
|
|
|
2021-03-31 18:24:41 +00:00
|
|
|
var putOptionsPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return new(neofs.PutOptions)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Uploader is an upload request handler.
|
2021-03-31 16:58:42 +00:00
|
|
|
type Uploader struct {
|
|
|
|
log *zap.Logger
|
|
|
|
plant neofs.ClientPlant
|
|
|
|
enableDefaultTimestamp bool
|
2021-01-25 19:36:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// New creates a new Uploader using specified logger, connection pool and
|
|
|
|
// other options.
|
2021-03-31 16:58:42 +00:00
|
|
|
func New(log *zap.Logger, plant neofs.ClientPlant, enableDefaultTimestamp bool) *Uploader {
|
|
|
|
return &Uploader{log, plant, enableDefaultTimestamp}
|
2021-01-25 19:36:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Upload handles multipart upload request.
|
2021-03-31 16:58:42 +00:00
|
|
|
func (u *Uploader) Upload(c *fasthttp.RequestCtx) {
|
2021-01-25 19:36:46 +00:00
|
|
|
var (
|
2021-04-28 08:39:12 +00:00
|
|
|
err error
|
|
|
|
file MultipartFile
|
|
|
|
addr *object.Address
|
|
|
|
cid = container.NewID()
|
|
|
|
scid, _ = c.UserValue("cid").(string)
|
|
|
|
log = u.log.With(zap.String("cid", scid))
|
|
|
|
bodyStream = c.RequestBodyStream()
|
|
|
|
drainBuf = make([]byte, drainBufSize)
|
2021-01-25 19:36:46 +00:00
|
|
|
)
|
2021-03-31 16:58:42 +00:00
|
|
|
if err = tokens.StoreBearerToken(c); err != nil {
|
2021-02-16 15:20:15 +00:00
|
|
|
log.Error("could not fetch bearer token", zap.Error(err))
|
|
|
|
c.Error("could not fetch bearer token", fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-03-31 16:58:42 +00:00
|
|
|
if err = cid.Parse(scid); err != nil {
|
2021-01-25 19:36:46 +00:00
|
|
|
log.Error("wrong container id", zap.Error(err))
|
|
|
|
c.Error("wrong container id", fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
2021-04-07 12:54:30 +00:00
|
|
|
// If the temporary reader can be closed - let's close it.
|
2021-02-13 16:17:01 +00:00
|
|
|
if file == nil {
|
2021-01-26 08:35:05 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-30 22:46:33 +00:00
|
|
|
err := file.Close()
|
|
|
|
log.Debug(
|
|
|
|
"close temporary multipart/form file",
|
2021-02-13 16:17:01 +00:00
|
|
|
zap.Stringer("address", addr),
|
|
|
|
zap.String("filename", file.FileName()),
|
2021-03-30 22:46:33 +00:00
|
|
|
zap.Error(err),
|
|
|
|
)
|
2021-01-25 19:36:46 +00:00
|
|
|
}()
|
2021-02-13 16:17:01 +00:00
|
|
|
boundary := string(c.Request.Header.MultipartFormBoundary())
|
2021-04-28 08:39:12 +00:00
|
|
|
if file, err = fetchMultipartFile(u.log, bodyStream, boundary); err != nil {
|
2021-01-26 15:36:53 +00:00
|
|
|
log.Error("could not receive multipart/form", zap.Error(err))
|
|
|
|
c.Error("could not receive multipart/form: "+err.Error(), fasthttp.StatusBadRequest)
|
2021-01-25 19:36:46 +00:00
|
|
|
return
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
2021-03-31 16:58:42 +00:00
|
|
|
filtered := filterHeaders(u.log, &c.Request.Header)
|
2021-01-25 19:36:46 +00:00
|
|
|
attributes := make([]*object.Attribute, 0, len(filtered))
|
2021-01-26 15:36:53 +00:00
|
|
|
// prepares attributes from filtered headers
|
2021-01-25 19:36:46 +00:00
|
|
|
for key, val := range filtered {
|
|
|
|
attribute := object.NewAttribute()
|
|
|
|
attribute.SetKey(key)
|
|
|
|
attribute.SetValue(val)
|
|
|
|
attributes = append(attributes, attribute)
|
|
|
|
}
|
2021-01-26 15:36:53 +00:00
|
|
|
// sets FileName attribute if it wasn't set from header
|
2021-01-28 13:48:45 +00:00
|
|
|
if _, ok := filtered[object.AttributeFileName]; !ok {
|
2021-01-25 19:36:46 +00:00
|
|
|
filename := object.NewAttribute()
|
|
|
|
filename.SetKey(object.AttributeFileName)
|
2021-02-13 16:17:01 +00:00
|
|
|
filename.SetValue(file.FileName())
|
2021-01-25 19:36:46 +00:00
|
|
|
attributes = append(attributes, filename)
|
|
|
|
}
|
2021-01-26 15:36:53 +00:00
|
|
|
// sets Timestamp attribute if it wasn't set from header and enabled by settings
|
2021-03-31 16:58:42 +00:00
|
|
|
if _, ok := filtered[object.AttributeTimestamp]; !ok && u.enableDefaultTimestamp {
|
2021-01-25 19:36:46 +00:00
|
|
|
timestamp := object.NewAttribute()
|
|
|
|
timestamp.SetKey(object.AttributeTimestamp)
|
|
|
|
timestamp.SetValue(strconv.FormatInt(time.Now().Unix(), 10))
|
|
|
|
attributes = append(attributes, timestamp)
|
|
|
|
}
|
2021-03-31 16:58:42 +00:00
|
|
|
oid, bt := u.fetchOwnerAndBearerToken(c)
|
2021-03-31 18:24:41 +00:00
|
|
|
putOpts := putOptionsPool.Get().(*neofs.PutOptions)
|
|
|
|
defer putOptionsPool.Put(putOpts)
|
|
|
|
// Try to put file into NeoFS or throw an error.
|
2021-04-07 12:54:30 +00:00
|
|
|
putOpts.Client, putOpts.SessionToken, err = u.plant.ConnectionArtifacts()
|
2021-03-30 22:46:33 +00:00
|
|
|
if err != nil {
|
2021-04-07 12:54:30 +00:00
|
|
|
log.Error("failed to get neofs connection artifacts", zap.Error(err))
|
|
|
|
c.Error("failed to get neofs connection artifacts", fasthttp.StatusInternalServerError)
|
2021-03-30 22:46:33 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-23 12:28:58 +00:00
|
|
|
putOpts.Attributes = attributes
|
2021-03-30 22:46:33 +00:00
|
|
|
putOpts.BearerToken = bt
|
|
|
|
putOpts.ContainerID = cid
|
|
|
|
putOpts.OwnerID = oid
|
|
|
|
putOpts.Reader = file
|
2021-03-31 16:58:42 +00:00
|
|
|
if addr, err = u.plant.Object().Put(c, putOpts); err != nil {
|
2021-04-07 12:54:30 +00:00
|
|
|
log.Error("could not store file in neofs", zap.Error(err))
|
|
|
|
c.Error("could not store file in neofs", fasthttp.StatusBadRequest)
|
2021-01-25 19:36:46 +00:00
|
|
|
return
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
2021-03-31 18:24:41 +00:00
|
|
|
// Try to return the response, otherwise, if something went wrong, throw an error.
|
2021-03-30 22:46:33 +00:00
|
|
|
if err = newPutResponse(addr).encode(c); err != nil {
|
2021-01-25 19:36:46 +00:00
|
|
|
log.Error("could not prepare response", zap.Error(err))
|
|
|
|
c.Error("could not prepare response", fasthttp.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-04-28 08:39:12 +00:00
|
|
|
// Multipart is multipart and thus can contain more than one part which
|
|
|
|
// we ignore at the moment. Also, when dealing with chunked encoding
|
|
|
|
// the last zero-length chunk might be left unread (because multipart
|
|
|
|
// reader only cares about its boundary and doesn't look further) and
|
|
|
|
// it will be (erroneously) interpreted as the start of the next
|
|
|
|
// pipelined header. Thus we need to drain the body buffer.
|
|
|
|
for {
|
|
|
|
_, err = bodyStream.Read(drainBuf)
|
|
|
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 18:24:41 +00:00
|
|
|
// Report status code and content type.
|
2021-01-25 19:36:46 +00:00
|
|
|
c.Response.SetStatusCode(fasthttp.StatusOK)
|
2021-01-26 08:43:40 +00:00
|
|
|
c.Response.Header.SetContentType(jsonHeader)
|
2021-01-25 19:36:46 +00:00
|
|
|
}
|
2021-03-31 16:58:42 +00:00
|
|
|
|
|
|
|
func (u *Uploader) fetchOwnerAndBearerToken(ctx context.Context) (*owner.ID, *token.BearerToken) {
|
|
|
|
if token, err := tokens.LoadBearerToken(ctx); err == nil && token != nil {
|
|
|
|
return token.Issuer(), token
|
|
|
|
}
|
|
|
|
return u.plant.OwnerID(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type putResponse struct {
|
2021-04-05 14:48:01 +00:00
|
|
|
ObjectID string `json:"object_id"`
|
|
|
|
ContainerID string `json:"container_id"`
|
2021-03-31 16:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newPutResponse(addr *object.Address) *putResponse {
|
|
|
|
return &putResponse{
|
2021-04-05 14:48:01 +00:00
|
|
|
ObjectID: addr.ObjectID().String(),
|
|
|
|
ContainerID: addr.ContainerID().String(),
|
2021-03-31 16:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *putResponse) encode(w io.Writer) error {
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.SetIndent("", "\t")
|
|
|
|
return enc.Encode(pr)
|
|
|
|
}
|