Skip to content

Commit

Permalink
Fix/bytes api content type (#4915)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinconic authored Nov 28, 2024
1 parent 4dc554e commit 0d36247
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
18 changes: 17 additions & 1 deletion openapi/Swarm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
7 changes: 3 additions & 4 deletions pkg/api/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -202,22 +202,21 @@ 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

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))
Expand Down

0 comments on commit 0d36247

Please sign in to comment.