From 0d36247937363523f0721b72a7af6a0cba4c2790 Mon Sep 17 00:00:00 2001 From: Calin Martinconi Date: Thu, 28 Nov 2024 11:07:08 +0200 Subject: [PATCH] Fix/bytes api content type (#4915) --- openapi/Swarm.yaml | 18 +++++++++++++++++- pkg/api/bytes.go | 7 +++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/openapi/Swarm.yaml b/openapi/Swarm.yaml index 5aca29f9e22..0eb2e44c727 100644 --- a/openapi/Swarm.yaml +++ b/openapi/Swarm.yaml @@ -270,7 +270,23 @@ paths: - $ref: "SwarmCommon.yaml#/components/parameters/SwarmActHistoryAddress" responses: "200": - description: Chunk exists + description: The chunk exists. + headers: + Content-Type: + description: The MIME type of the resource (e.g., application/octet-stream). + schema: + type: string + example: application/octet-stream + Content-Length: + description: The size of the chunk in bytes. + schema: + type: integer + example: 1024 + Access-Control-Expose-Headers: + description: Headers exposed for CORS. + schema: + type: string + example: Accept-Ranges, Content-Encoding "400": $ref: "SwarmCommon.yaml#/components/responses/400" "404": diff --git a/pkg/api/bytes.go b/pkg/api/bytes.go index 5eabf41458b..c2c14d21db6 100644 --- a/pkg/api/bytes.go +++ b/pkg/api/bytes.go @@ -192,7 +192,7 @@ func (s *Service) bytesHeadHandler(w http.ResponseWriter, r *http.Request) { Address swarm.Address `map:"address,resolve" validate:"required"` }{} if response := s.mapStructure(mux.Vars(r), &paths); response != nil { - response("invalid path params", logger, w) + w.WriteHeader(http.StatusBadRequest) return } @@ -202,14 +202,14 @@ func (s *Service) bytesHeadHandler(w http.ResponseWriter, r *http.Request) { } getter := s.storer.Download(true) - ch, err := getter.Get(r.Context(), address) if err != nil { logger.Debug("get root chunk failed", "chunk_address", address, "error", err) - logger.Error(nil, "get rook chunk failed") + logger.Error(nil, "get root chunk failed") w.WriteHeader(http.StatusNotFound) return } + w.Header().Add("Access-Control-Expose-Headers", "Accept-Ranges, Content-Encoding") w.Header().Add(ContentTypeHeader, "application/octet-stream") var span int64 @@ -217,7 +217,6 @@ func (s *Service) bytesHeadHandler(w http.ResponseWriter, r *http.Request) { if cac.Valid(ch) { span = int64(binary.LittleEndian.Uint64(ch.Data()[:swarm.SpanSize])) } else { - // soc span = int64(len(ch.Data())) } w.Header().Set(ContentLengthHeader, strconv.FormatInt(span, 10))