owl-blogs/web/binary_manager_handler.go

112 lines
2.3 KiB
Go
Raw Permalink Normal View History

2023-07-27 20:02:13 +00:00
package web
import (
"owl-blogs/app"
"owl-blogs/app/repository"
2024-02-21 20:21:58 +00:00
"owl-blogs/domain/model"
2023-07-27 20:02:13 +00:00
"owl-blogs/render"
"sort"
"strings"
"github.com/gofiber/fiber/v2"
)
type BinaryManageHandler struct {
configRepo repository.ConfigRepository
service *app.BinaryService
}
func NewBinaryManageHandler(configRepo repository.ConfigRepository, service *app.BinaryService) *BinaryManageHandler {
return &BinaryManageHandler{
configRepo: configRepo,
service: service,
}
}
func (h *BinaryManageHandler) Handle(c *fiber.Ctx) error {
2024-02-23 11:34:05 +00:00
filter := c.Query("filter", "")
allIds, err := h.service.ListIds(filter)
2023-07-27 20:02:13 +00:00
sort.Slice(allIds, func(i, j int) bool {
return strings.ToLower(allIds[i]) < strings.ToLower(allIds[j])
})
if err != nil {
return err
}
pageData := paginate(c, allIds, 50)
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
return render.RenderTemplateWithBase(c, "views/binary_manager", fiber.Map{
2023-07-27 20:02:13 +00:00
"Binaries": pageData.items,
"Page": pageData.page,
"NextPage": pageData.page + 1,
"PrevPage": pageData.page - 1,
"FirstPage": pageData.page == 1,
"LastPage": pageData.lastPage,
2024-02-23 11:34:05 +00:00
"Filter": filter,
2023-07-27 20:02:13 +00:00
})
}
2024-02-21 20:21:58 +00:00
func (h *BinaryManageHandler) saveFileUpload(c *fiber.Ctx) (*model.BinaryFile, error) {
2023-07-27 20:02:13 +00:00
file, err := c.FormFile("file")
if err != nil {
2024-02-21 20:21:58 +00:00
return nil, err
2023-07-27 20:02:13 +00:00
}
reader, err := file.Open()
if err != nil {
2024-02-21 20:21:58 +00:00
return nil, err
2023-07-27 20:02:13 +00:00
}
defer reader.Close()
content := make([]byte, file.Size)
_, err = reader.Read(content)
if err != nil {
2024-02-21 20:21:58 +00:00
return nil, err
2023-07-27 20:02:13 +00:00
}
binary, err := h.service.Create(file.Filename, content)
if err != nil {
2024-02-21 20:21:58 +00:00
return nil, err
2023-07-27 20:02:13 +00:00
}
2024-02-21 20:21:58 +00:00
return binary, nil
}
2023-07-27 20:02:13 +00:00
2024-02-21 20:21:58 +00:00
func (h *BinaryManageHandler) HandleUpload(c *fiber.Ctx) error {
binary, err := h.saveFileUpload(c)
if err != nil {
return err
}
2023-07-27 20:02:13 +00:00
return c.Redirect("/media/" + binary.Id)
}
2024-02-21 20:21:58 +00:00
func (h *BinaryManageHandler) HandleUploadApi(c *fiber.Ctx) error {
binary, err := h.saveFileUpload(c)
if err != nil {
return err
}
return c.JSON(map[string]string{
"location": "/media/" + binary.Id,
})
}
2023-07-27 20:02:13 +00:00
func (h *BinaryManageHandler) HandleDelete(c *fiber.Ctx) error {
id := c.FormValue("file")
binary, err := h.service.FindById(id)
if err != nil {
return err
}
confirm := c.FormValue("confirm")
if confirm != "on" {
return c.Redirect("/admin/binaries/")
}
err = h.service.Delete(binary)
if err != nil {
return err
}
return c.Redirect("/admin/binaries/")
}