Handle handles a request for a file.
(c pcontext.Context)
| 21 | |
| 22 | // Handle handles a request for a file. |
| 23 | func (h *FilesHandler) Handle(c pcontext.Context) { |
| 24 | log := pcontext.Logger(c).With().Str("blob", pcontext.BlobUrl(c)).Bool("p2p", pcontext.IsRequestFromAPeer(c)).Logger() |
| 25 | log.Debug().Msg("files handler start") |
| 26 | s := time.Now() |
| 27 | defer func() { |
| 28 | dur := time.Since(s) |
| 29 | h.metricsRecorder.RecordRequest(c.Request.Method, "files", float64(dur.Milliseconds())) |
| 30 | log.Debug().Dur("duration", dur).Msg("files handler stop") |
| 31 | }() |
| 32 | |
| 33 | err := h.fill(c) |
| 34 | if err != nil { |
| 35 | log.Debug().Err(err).Msg("failed to fill context") |
| 36 | // nolint |
| 37 | c.AbortWithError(http.StatusBadRequest, err) |
| 38 | return |
| 39 | } |
| 40 | |
| 41 | f, err := h.store.Open(c) |
| 42 | if err == os.ErrNotExist { |
| 43 | c.AbortWithStatus(http.StatusNotFound) |
| 44 | return |
| 45 | } |
| 46 | if err != nil { |
| 47 | // nolint |
| 48 | c.AbortWithError(http.StatusInternalServerError, err) |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | w := c.Writer |
| 53 | |
| 54 | w.Header().Set("Content-Type", "application/octet-stream") |
| 55 | w.Header().Del("Content-Length") |
| 56 | w.Header().Set(pcontext.NodeHeaderKey, pcontext.NodeName) |
| 57 | w.Header().Set(pcontext.CorrelationHeaderKey, c.GetString(pcontext.CorrelationIdCtxKey)) |
| 58 | |
| 59 | http.ServeContent(w, c.Request, "file", time.Now(), f) |
| 60 | } |
| 61 | |
| 62 | // fill fills the context with handler specific information. |
| 63 | func (h *FilesHandler) fill(c pcontext.Context) error { |