github.com/ethersphere/bee/v2@v2.2.0/pkg/api/chunk_address.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package api
     6  
     7  import (
     8  	"net/http"
     9  
    10  	"github.com/ethersphere/bee/v2/pkg/jsonhttp"
    11  	"github.com/ethersphere/bee/v2/pkg/swarm"
    12  	"github.com/gorilla/mux"
    13  )
    14  
    15  func (s *Service) hasChunkHandler(w http.ResponseWriter, r *http.Request) {
    16  	logger := s.logger.WithName("get_chunk").Build()
    17  
    18  	paths := struct {
    19  		Address swarm.Address `map:"address" validate:"required"`
    20  	}{}
    21  	if response := s.mapStructure(mux.Vars(r), &paths); response != nil {
    22  		response("invalid path params", logger, w)
    23  		return
    24  	}
    25  
    26  	address := paths.Address
    27  	if v := getAddressFromContext(r.Context()); !v.IsZero() {
    28  		address = v
    29  	}
    30  
    31  	has, err := s.storer.ChunkStore().Has(r.Context(), address)
    32  	if err != nil {
    33  		logger.Debug("has chunk failed", "chunk_address", address, "error", err)
    34  		jsonhttp.BadRequest(w, err)
    35  		return
    36  	}
    37  
    38  	if !has {
    39  		jsonhttp.NotFound(w, nil)
    40  		return
    41  	}
    42  	jsonhttp.OK(w, nil)
    43  }