func()

in pkg/handlers/files/handler.go [23:60]


func (h *FilesHandler) Handle(c pcontext.Context) {
	log := pcontext.Logger(c).With().Str("blob", pcontext.BlobUrl(c)).Bool("p2p", pcontext.IsRequestFromAPeer(c)).Logger()
	log.Debug().Msg("files handler start")
	s := time.Now()
	defer func() {
		dur := time.Since(s)
		h.metricsRecorder.RecordRequest(c.Request.Method, "files", float64(dur.Milliseconds()))
		log.Debug().Dur("duration", dur).Msg("files handler stop")
	}()

	err := h.fill(c)
	if err != nil {
		log.Debug().Err(err).Msg("failed to fill context")
		// nolint
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	f, err := h.store.Open(c)
	if err == os.ErrNotExist {
		c.AbortWithStatus(http.StatusNotFound)
		return
	}
	if err != nil {
		// nolint
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	w := c.Writer

	w.Header().Set("Content-Type", "application/octet-stream")
	w.Header().Del("Content-Length")
	w.Header().Set(pcontext.NodeHeaderKey, pcontext.NodeName)
	w.Header().Set(pcontext.CorrelationHeaderKey, c.GetString(pcontext.CorrelationIdCtxKey))

	http.ServeContent(w, c.Request, "file", time.Now(), f)
}