owl-blogs/web/media_handler.go

32 lines
574 B
Go
Raw Normal View History

2023-06-25 19:32:36 +00:00
package web
import (
2023-07-19 18:52:19 +00:00
"net/url"
2023-06-25 19:32:36 +00:00
"owl-blogs/app"
"github.com/gofiber/fiber/v2"
)
type MediaHandler struct {
2023-07-08 12:32:20 +00:00
binaryService *app.BinaryService
2023-06-25 19:32:36 +00:00
}
2023-07-08 12:32:20 +00:00
func NewMediaHandler(binaryService *app.BinaryService) *MediaHandler {
return &MediaHandler{binaryService: binaryService}
2023-06-25 19:32:36 +00:00
}
func (h *MediaHandler) Handle(c *fiber.Ctx) error {
2023-07-09 17:09:54 +00:00
id := c.Params("+")
2023-07-19 18:52:19 +00:00
// urldecode
id, err := url.PathUnescape(id)
if err != nil {
return err
}
2023-07-08 12:32:20 +00:00
binary, err := h.binaryService.FindById(id)
2023-07-25 19:39:57 +00:00
c.Set("Content-Type", binary.Mime())
2023-07-08 12:32:20 +00:00
if err != nil {
return err
}
return c.Send(binary.Data)
2023-06-25 19:32:36 +00:00
}