Files
ReSendit/internal/web/handler.go
2026-03-21 20:02:00 +01:00

90 lines
1.7 KiB
Go

package web
import (
"ResendIt/internal/file"
"strconv"
"github.com/gin-gonic/gin"
)
type Handler struct {
fileService *file.Service
}
func NewHandler(fileService *file.Service) *Handler {
return &Handler{
fileService: fileService,
}
}
// Homepage
func (h *Handler) Index(c *gin.Context) {
c.HTML(200, "index.html", gin.H{
"title": "Home",
})
}
// Upload page
func (h *Handler) UploadPage(c *gin.Context) {
c.HTML(200, "upload.html", nil)
}
func (h *Handler) LoginPage(c *gin.Context) {
c.HTML(200, "login.html", nil)
}
func (h *Handler) FileView(c *gin.Context) {
id := c.Param("id")
fileRecord, err := h.fileService.GetFileByViewID(id)
if err != nil {
c.HTML(404, "fileNotFound.html", nil)
return
}
downloadKey := fileRecord.ID
deleteKey := fileRecord.DeletionID
c.HTML(200, "complete.html", gin.H{
"Filename": fileRecord.Filename,
"DownloadID": downloadKey,
"DeleteID": deleteKey,
})
}
func (h *Handler) AdminPage(c *gin.Context) {
pageStr := c.Query("page")
page, err := strconv.Atoi(pageStr)
if err != nil || page < 1 {
page = 1
}
limit := 10
offset := (page - 1) * limit
files, totalCount, err := h.fileService.GetPaginatedFiles(limit, offset)
if err != nil {
c.HTML(500, "admin.html", gin.H{
"error": err.Error(),
})
return
}
totalPages := (totalCount + limit - 1) / limit
c.HTML(200, "admin.html", gin.H{
"Files": files,
"Page": page,
"TotalPages": totalPages,
})
}
func (h *Handler) Logout(c *gin.Context) {
c.SetCookie("auth_token", "", -1, "/", "", false, true)
c.Redirect(302, "/")
}
func (h *Handler) ChangePasswordPage(c *gin.Context) {
c.HTML(200, "changePassword.html", nil)
}