74 lines
1.6 KiB
Go
74 lines
1.6 KiB
Go
|
package controllers
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
|
||
|
"git.myrkvi.com/myrkvi/bin/global"
|
||
|
"git.myrkvi.com/myrkvi/bin/models"
|
||
|
"git.myrkvi.com/myrkvi/bin/utils"
|
||
|
"git.myrkvi.com/myrkvi/bin/views/components"
|
||
|
"git.myrkvi.com/myrkvi/bin/views/pages"
|
||
|
"github.com/labstack/echo/v4"
|
||
|
"github.com/sirupsen/logrus"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
func GetBinHandler(c echo.Context) error {
|
||
|
file := models.File{}
|
||
|
result := global.DB.Where("page_key = ?", c.Param("id")).First(&file)
|
||
|
if result.Error == gorm.ErrRecordNotFound {
|
||
|
logrus.WithError(result.Error).Errorln("no file")
|
||
|
}
|
||
|
if result.Error != nil {
|
||
|
logrus.WithError(result.Error).Errorln("server error")
|
||
|
|
||
|
}
|
||
|
|
||
|
file.AdminKey = c.QueryParam("delcode")
|
||
|
|
||
|
return utils.RenderComponents(c, http.StatusOK,
|
||
|
pages.BinFull(file),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func DeleteBinHandler(c echo.Context) error {
|
||
|
code := c.Param("id")
|
||
|
adminCode := c.FormValue("adminKey")
|
||
|
|
||
|
if adminCode == "" {
|
||
|
return utils.RenderErrorToast(c, "deletion key cannot be empty")
|
||
|
}
|
||
|
|
||
|
file := models.File{}
|
||
|
result := global.DB.Where("page_key = ?", code).First(&file)
|
||
|
|
||
|
if result.Error != nil {
|
||
|
return utils.RenderErrorToast(c, "file not found")
|
||
|
}
|
||
|
|
||
|
if adminCode != file.AdminKey {
|
||
|
return utils.RenderErrorToast(c, "invalid deletion key")
|
||
|
}
|
||
|
|
||
|
global.DB.Delete(&file)
|
||
|
|
||
|
if file.Filename == "" {
|
||
|
file.Filename = "file"
|
||
|
}
|
||
|
|
||
|
utils.SetHeader(c, "HX-Push", "/")
|
||
|
return utils.RenderComponents(
|
||
|
c,
|
||
|
http.StatusOK,
|
||
|
pages.IndexPartial(),
|
||
|
components.SwapOOB(
|
||
|
"beforeend:#toast",
|
||
|
components.ToastSuccess(fmt.Sprintf("%s deleted", file.Filename)),
|
||
|
),
|
||
|
components.NavMenu(models.DefaultMenu, 0, true),
|
||
|
components.SetTitle(""),
|
||
|
)
|
||
|
|
||
|
}
|