2021-01-25 19:36:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
"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/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type putResponse struct {
|
|
|
|
OID string `json:"object_id"`
|
|
|
|
CID string `json:"container_id"`
|
|
|
|
}
|
|
|
|
|
2021-01-26 08:43:40 +00:00
|
|
|
const jsonHeader = "application/json; charset=UTF-8"
|
|
|
|
|
2021-01-25 19:36:46 +00:00
|
|
|
func newPutResponse(addr *object.Address) *putResponse {
|
|
|
|
return &putResponse{
|
|
|
|
OID: addr.ObjectID().String(),
|
|
|
|
CID: addr.ContainerID().String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *putResponse) Encode(w io.Writer) error {
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.SetIndent("", "\t")
|
|
|
|
return enc.Encode(pr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) upload(c *fasthttp.RequestCtx) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
name string
|
2021-01-26 15:36:53 +00:00
|
|
|
tmp io.Reader
|
2021-01-25 19:36:46 +00:00
|
|
|
addr *object.Address
|
|
|
|
form *multipart.Form
|
|
|
|
cid = container.NewID()
|
|
|
|
sCID, _ = c.UserValue("cid").(string)
|
|
|
|
|
|
|
|
log = a.log.With(zap.String("cid", sCID))
|
|
|
|
)
|
|
|
|
|
|
|
|
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() {
|
2021-01-26 15:36:53 +00:00
|
|
|
// if temporary reader can be closed - close it
|
|
|
|
if closer := tmp.(io.Closer); closer != nil {
|
|
|
|
log.Debug("close temporary multipart/form file", zap.Error(closer.Close()))
|
|
|
|
}
|
2021-01-26 08:35:05 +00:00
|
|
|
|
|
|
|
if form == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
log.Debug("cleanup multipart/form", zap.Error(form.RemoveAll()))
|
2021-01-25 19:36:46 +00:00
|
|
|
}()
|
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
// tries to receive multipart/form or throw error
|
2021-01-25 19:36:46 +00:00
|
|
|
if form, err = c.MultipartForm(); 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
|
|
|
}
|
|
|
|
|
|
|
|
// checks that received multipart/form contains only one `file` per request
|
|
|
|
if ln := len(form.File); ln != 1 {
|
|
|
|
log.Error("received multipart/form with more then one file", zap.Int("count", ln))
|
|
|
|
c.Error("received multipart/form with more then one file", fasthttp.StatusBadRequest)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range form.File {
|
2021-01-26 15:36:53 +00:00
|
|
|
// because multipart/form can contains multiple FileHeader records
|
|
|
|
// we should check that we have only one per request or throw error
|
2021-01-25 19:36:46 +00:00
|
|
|
if ln := len(file); ln != 1 {
|
2021-01-26 15:36:53 +00:00
|
|
|
log.Error("received multipart/form file should contains one record", zap.Int("count", ln))
|
|
|
|
c.Error("received multipart/form file should contains one record", fasthttp.StatusBadRequest)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
name = file[0].Filename
|
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
// opens multipart/form file to work within or throw error
|
|
|
|
if tmp, err = file[0].Open(); err != nil {
|
|
|
|
log.Error("could not prepare uploaded file", zap.Error(err))
|
|
|
|
c.Error("could not prepare uploaded file", fasthttp.StatusBadRequest)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filtered := a.hdr.Filter(&c.Request.Header)
|
|
|
|
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-25 19:36:46 +00:00
|
|
|
if _, ok := filtered[object.AttributeFileName]; ok {
|
|
|
|
filename := object.NewAttribute()
|
|
|
|
filename.SetKey(object.AttributeFileName)
|
|
|
|
filename.SetValue(name)
|
|
|
|
|
|
|
|
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-01-26 09:40:01 +00:00
|
|
|
if _, ok := filtered[object.AttributeTimestamp]; ok && a.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-01-26 15:36:53 +00:00
|
|
|
// prepares new object and fill it
|
2021-01-25 19:36:46 +00:00
|
|
|
raw := object.NewRaw()
|
|
|
|
raw.SetContainerID(cid)
|
|
|
|
raw.SetOwnerID(a.cli.Owner()) // should be various: from sdk / BearerToken
|
|
|
|
raw.SetAttributes(attributes...)
|
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
// tries to put file into NeoFS or throw error
|
2021-01-25 19:36:46 +00:00
|
|
|
if addr, err = a.cli.Object().Put(c, raw.Object(), sdk.WithPutReader(tmp)); err != nil {
|
|
|
|
log.Error("could not store file in NeoFS", zap.Error(err))
|
|
|
|
c.Error("could not store file in NeoFS", fasthttp.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tries to return response, otherwise, if something went wrong throw error
|
|
|
|
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-01-26 15:36:53 +00:00
|
|
|
// reports 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
|
|
|
}
|