owl-blogs/web/media_handler.go

32 lines
605 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)
if err != nil {
2023-07-27 18:56:26 +00:00
return c.SendStatus(fiber.StatusNotFound)
2023-07-08 12:32:20 +00:00
}
2023-07-27 18:55:50 +00:00
c.Set("Content-Type", binary.Mime())
2023-07-08 12:32:20 +00:00
return c.Send(binary.Data)
2023-06-25 19:32:36 +00:00
}