forked from TrueCloudLab/frostfs-http-gw
[#19] Add a version with no cdn-sdk deps
Signed-off-by: Pavel Korotkov <pavel@nspcc.ru>
This commit is contained in:
parent
cdab794d62
commit
4c96885a42
20 changed files with 930 additions and 266 deletions
69
upload.go
69
upload.go
|
@ -7,10 +7,11 @@ import (
|
|||
"strconv"
|
||||
"time"
|
||||
|
||||
sdk "github.com/nspcc-dev/cdn-sdk"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
||||
"github.com/nspcc-dev/neofs-http-gate/neofs"
|
||||
"github.com/valyala/fasthttp"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
@ -29,18 +30,17 @@ func newPutResponse(addr *object.Address) *putResponse {
|
|||
}
|
||||
}
|
||||
|
||||
func (pr *putResponse) Encode(w io.Writer) error {
|
||||
func (pr *putResponse) encode(w io.Writer) error {
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetIndent("", "\t")
|
||||
return enc.Encode(pr)
|
||||
}
|
||||
|
||||
func (a *app) fetchOwner(ctx context.Context) *owner.ID {
|
||||
if tkn, err := sdk.BearerToken(ctx); err == nil && tkn != nil {
|
||||
return tkn.Issuer()
|
||||
func (a *app) fetchOwnerAndBearerToken(ctx context.Context) (*owner.ID, *token.BearerToken) {
|
||||
if token, err := loadBearerToken(ctx); err == nil && token != nil {
|
||||
return token.Issuer(), token
|
||||
}
|
||||
|
||||
return a.cli.Owner()
|
||||
return a.plant.OwnerID(), nil
|
||||
}
|
||||
|
||||
func (a *app) upload(c *fasthttp.RequestCtx) {
|
||||
|
@ -50,94 +50,93 @@ func (a *app) upload(c *fasthttp.RequestCtx) {
|
|||
addr *object.Address
|
||||
cid = container.NewID()
|
||||
sCID, _ = c.UserValue("cid").(string)
|
||||
|
||||
log = a.log.With(zap.String("cid", sCID))
|
||||
log = a.log.With(zap.String("cid", sCID))
|
||||
)
|
||||
|
||||
if err = checkAndPropagateBearerToken(c); err != nil {
|
||||
if err = storeBearerToken(c); err != nil {
|
||||
log.Error("could not fetch bearer token", zap.Error(err))
|
||||
c.Error("could not fetch bearer token", fasthttp.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err = cid.Parse(sCID); err != nil {
|
||||
log.Error("wrong container id", zap.Error(err))
|
||||
c.Error("wrong container id", fasthttp.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
// if temporary reader can be closed - close it
|
||||
if file == nil {
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug("close temporary multipart/form file",
|
||||
err := file.Close()
|
||||
log.Debug(
|
||||
"close temporary multipart/form file",
|
||||
zap.Stringer("address", addr),
|
||||
zap.String("filename", file.FileName()),
|
||||
zap.Error(file.Close()))
|
||||
zap.Error(err),
|
||||
)
|
||||
}()
|
||||
|
||||
boundary := string(c.Request.Header.MultipartFormBoundary())
|
||||
if file, err = fetchMultipartFile(a.log, c.RequestBodyStream(), boundary); err != nil {
|
||||
log.Error("could not receive multipart/form", zap.Error(err))
|
||||
c.Error("could not receive multipart/form: "+err.Error(), fasthttp.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
filtered := filterHeaders(a.log, &c.Request.Header)
|
||||
attributes := make([]*object.Attribute, 0, len(filtered))
|
||||
|
||||
// prepares attributes from filtered headers
|
||||
for key, val := range filtered {
|
||||
attribute := object.NewAttribute()
|
||||
attribute.SetKey(key)
|
||||
attribute.SetValue(val)
|
||||
|
||||
attributes = append(attributes, attribute)
|
||||
}
|
||||
|
||||
// sets FileName attribute if it wasn't set from header
|
||||
if _, ok := filtered[object.AttributeFileName]; !ok {
|
||||
filename := object.NewAttribute()
|
||||
filename.SetKey(object.AttributeFileName)
|
||||
filename.SetValue(file.FileName())
|
||||
|
||||
attributes = append(attributes, filename)
|
||||
}
|
||||
|
||||
// sets Timestamp attribute if it wasn't set from header and enabled by settings
|
||||
if _, ok := filtered[object.AttributeTimestamp]; !ok && a.enableDefaultTimestamp {
|
||||
timestamp := object.NewAttribute()
|
||||
timestamp.SetKey(object.AttributeTimestamp)
|
||||
timestamp.SetValue(strconv.FormatInt(time.Now().Unix(), 10))
|
||||
|
||||
attributes = append(attributes, timestamp)
|
||||
}
|
||||
|
||||
oid, bt := a.fetchOwnerAndBearerToken(c)
|
||||
// prepares new object and fill it
|
||||
raw := object.NewRaw()
|
||||
raw.SetContainerID(cid)
|
||||
raw.SetOwnerID(a.fetchOwner(c))
|
||||
raw.SetOwnerID(oid)
|
||||
raw.SetAttributes(attributes...)
|
||||
|
||||
// tries to put file into NeoFS or throw error
|
||||
if addr, err = a.cli.Object().Put(c, raw.Object(), sdk.WithPutReader(file)); err != nil {
|
||||
log.Error("could not store file in NeoFS", zap.Error(err))
|
||||
c.Error("could not store file in NeoFS", fasthttp.StatusBadRequest)
|
||||
|
||||
// if addr, err = a.plant.Object().Put(c, raw.Object(), sdk.WithPutReader(file)); err != nil {
|
||||
// TODO: Take this from a sync pool.
|
||||
putOpts := new(neofs.PutOptions)
|
||||
putOpts.Client, putOpts.SessionToken, err = a.plant.GetReusableArtifacts(c)
|
||||
if err != nil {
|
||||
log.Error("failed to get neofs client's reusable artifacts", zap.Error(err))
|
||||
c.Error("failed to get neofs client's reusable artifacts", fasthttp.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
putOpts.BearerToken = bt
|
||||
putOpts.ContainerID = cid
|
||||
putOpts.OwnerID = oid
|
||||
putOpts.PrepareObjectOnsite = false
|
||||
putOpts.Reader = file
|
||||
if addr, err = a.plant.Object().Put(c, putOpts); err != nil {
|
||||
log.Error("could not store file in NeoFS", zap.Error(err))
|
||||
c.Error("could not store file in NeoFS", fasthttp.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// tries to return response, otherwise, if something went wrong throw error
|
||||
if err = newPutResponse(addr).Encode(c); err != nil {
|
||||
if err = newPutResponse(addr).encode(c); err != nil {
|
||||
log.Error("could not prepare response", zap.Error(err))
|
||||
c.Error("could not prepare response", fasthttp.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// reports status code and content type
|
||||
c.Response.SetStatusCode(fasthttp.StatusOK)
|
||||
c.Response.Header.SetContentType(jsonHeader)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue