Skip to content

Commit

Permalink
fix GET and HEAD
Browse files Browse the repository at this point in the history
Fix filename and Content-type for GET and HEAD APIs.

Signed-off-by: Tatiana Nesterenko <tatiana@nspcc.io>
  • Loading branch information
tatiana-nspcc committed Dec 27, 2023
1 parent 4fbd6bb commit 8e80e09
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions handlers/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ func (a *API) GetContainerObject(params operations.GetContainerObjectParams, pri
payloadSize := header.PayloadSize()
res := operations.NewGetContainerObjectOK()

responder, contentType := a.setAttributes(res, payloadSize, params.ContainerID, params.ObjectID, header, params.HTTPRequest.URL.Query().Get("download"))
responder := a.setAttributes(res, payloadSize, params.ContainerID, params.ObjectID, header, params.HTTPRequest.URL.Query().Get("download"))
contentType := res.ContentType
var payload io.ReadCloser = payloadReader
if len(contentType) == 0 {
if payloadSize > 0 {
Expand Down Expand Up @@ -431,7 +432,8 @@ func (a *API) HeadContainerObject(params operations.HeadContainerObjectParams, p
payloadSize := header.PayloadSize()
res := operations.NewHeadContainerObjectOK()

responder, contentType := a.setAttributes(res, payloadSize, params.ContainerID, params.ObjectID, *header, params.HTTPRequest.URL.Query().Get("download"))
responder := a.setAttributes(res, payloadSize, params.ContainerID, params.ObjectID, *header, params.HTTPRequest.URL.Query().Get("download"))
contentType := res.ContentType
if len(contentType) == 0 {
if payloadSize > 0 {
contentType, _, err = readContentType(payloadSize, func(sz uint64) (io.Reader, error) {
Expand Down Expand Up @@ -469,6 +471,7 @@ func isNotFoundError(err error) bool {

type attributeSetter interface {
SetContentLength(contentLength string)
SetContentType(contentType string)
SetXContainerID(xContainerID string)
SetXObjectID(xObjectID string)
SetXOwnerID(xOwnerID string)
Expand All @@ -479,16 +482,14 @@ type attributeSetter interface {
WriteResponse(rw http.ResponseWriter, producer runtime.Producer)
}

func (a *API) setAttributes(res attributeSetter, payloadSize uint64, cid string, oid string, header object.Object, download string) (middleware.Responder, string) {
func (a *API) setAttributes(res attributeSetter, payloadSize uint64, cid string, oid string, header object.Object, download string) middleware.Responder {
res.SetContentLength(strconv.FormatUint(payloadSize, 10))
res.SetXContainerID(cid)
res.SetXObjectID(oid)
res.SetXOwnerID(header.OwnerID().EncodeToString())

var (
contentType, filename string
responder middleware.Responder
)
var responder middleware.Responder
dis := "inline"
attributes := header.Attributes()
if len(attributes) > 0 {
responder = middleware.ResponderFunc(func(rw http.ResponseWriter, pr runtime.Producer) {
Expand All @@ -500,7 +501,11 @@ func (a *API) setAttributes(res attributeSetter, payloadSize uint64, cid string,
}
switch key {
case object.AttributeFileName:
filename = val
switch download {
case "1", "t", "T", "true", "TRUE", "True", "y", "yes", "Y", "YES", "Yes":
dis = "attachment"
}
res.SetContentDisposition(dis + "; filename=" + path.Base(val))
res.SetXAttributeFileName(val)
case object.AttributeTimestamp:
attrTimestamp, err := strconv.ParseInt(val, 10, 64)
Expand All @@ -514,7 +519,7 @@ func (a *API) setAttributes(res attributeSetter, payloadSize uint64, cid string,
res.SetXAttributeTimestamp(attrTimestamp)
res.SetLastModified(time.Unix(attrTimestamp, 0).UTC().Format(http.TimeFormat))
case object.AttributeContentType:
contentType = val
res.SetContentType(val)
default:
if strings.HasPrefix(key, container.SysAttributePrefix) {
key = systemBackwardTranslator(key)
Expand All @@ -525,15 +530,7 @@ func (a *API) setAttributes(res attributeSetter, payloadSize uint64, cid string,
res.WriteResponse(rw, pr)
})
}

var dis = "inline"
switch download {
case "1", "t", "T", "true", "TRUE", "True", "y", "yes", "Y", "YES", "Yes":
dis = "attachment"
}
res.SetContentDisposition(dis + "; filename=" + path.Base(filename))

return responder, contentType
return responder
}

// initializes io.Reader with the limited size and detects Content-Type from it.
Expand Down

0 comments on commit 8e80e09

Please sign in to comment.