feat: images
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package handleimage
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (h *Handler) getImage(w http.ResponseWriter, r *http.Request) {
|
||||
uid := r.PathValue("uid")
|
||||
|
||||
img, mime, err := h.img.GetImage(r.Context(), uid)
|
||||
if err != nil {
|
||||
slog.Error("error wile serving image", "err", err, "uid", uid)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Add("Content-Type", mime)
|
||||
io.Copy(w, img)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package handleimage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
img imageService
|
||||
}
|
||||
|
||||
func New(mux *http.ServeMux, srv imageService) *Handler {
|
||||
h := &Handler{
|
||||
img: srv,
|
||||
}
|
||||
|
||||
mux.HandleFunc("GET /images/{uid}", h.getImage)
|
||||
return h
|
||||
}
|
||||
|
||||
type imageService interface {
|
||||
GetImage(ctx context.Context, uid string) (img io.Reader, mimeType string, err error)
|
||||
}
|
||||
Reference in New Issue
Block a user