Skip to content

Commit

Permalink
Feature: detect blob content type
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Nov 16, 2023
1 parent 45a1a15 commit 516bc88
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
7 changes: 6 additions & 1 deletion cmd/api/handler/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,12 @@ func (handler *NamespaceHandler) GetBlob(c echo.Context) error {
return badRequestError(c, err)
}

return c.JSON(http.StatusOK, blob)
response, err := responses.NewBlob(blob)
if err != nil {
return internalServerError(c, err)
}

return c.JSON(http.StatusOK, response)
}

type getNamespaceMessages struct {
Expand Down
25 changes: 24 additions & 1 deletion cmd/api/handler/responses/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,33 @@

package responses

// structure is only for documentation
import (
"encoding/base64"
"net/http"

"github.com/celenium-io/celestia-indexer/pkg/node/types"
)

type Blob struct {
Namespace string `example:"AAAAAAAAAAAAAAAAAAAAAAAAAAAAs2bWWU6FOB0=" format:"base64" json:"namespace" swaggertype:"string"`
Data string `example:"b2sgZGVtbyBkYQ==" format:"base64" json:"data" swaggertype:"string"`
ShareVersion int `example:"0" format:"integer" json:"share_version" swaggertype:"integer"`
Commitment string `example:"vbGakK59+Non81TE3ULg5Ve5ufT9SFm/bCyY+WLR3gg=" format:"base64" json:"commitment" swaggertype:"string"`
ContentType string `example:"image/png" format:"string" json:"content_type" swaggertype:"string"`
}

func NewBlob(blob types.Blob) (Blob, error) {
b := Blob{
Namespace: blob.Namespace,
Data: blob.Data,
Commitment: blob.Commitment,
ShareVersion: blob.ShareVersion,
}

data, err := base64.StdEncoding.DecodeString(blob.Data)
if err != nil {
return b, err
}
b.ContentType = http.DetectContentType(data)
return b, nil
}

0 comments on commit 516bc88

Please sign in to comment.