136 lines
3.0 KiB
Go
136 lines
3.0 KiB
Go
package web
|
|
|
|
import (
|
|
"ResendIt/internal/buildinfo"
|
|
"ResendIt/internal/config"
|
|
"ResendIt/internal/file"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Handler struct {
|
|
fileService *file.Service
|
|
configService ConfigService
|
|
}
|
|
|
|
type ConfigService interface {
|
|
GetIntDefault(key string, def int) int
|
|
GetInt64Default(key string, def int64) int64
|
|
GetStringDefault(key string, def string) string
|
|
|
|
//SetInt(key string, value int) error
|
|
//SetInt64(key string, value int64) error
|
|
//SetBool(key string, value bool) error
|
|
SetString(key string, value string) error
|
|
}
|
|
|
|
func NewHandler(fileService *file.Service, cfg ConfigService) *Handler {
|
|
return &Handler{
|
|
fileService: fileService,
|
|
configService: cfg,
|
|
}
|
|
}
|
|
|
|
// Homepage
|
|
func (h *Handler) Index(c *gin.Context) {
|
|
c.HTML(200, "index.html", gin.H{
|
|
"title": "Home",
|
|
"MODT": h.configService.GetStringDefault(config.KeyModtext, config.DefaultModt),
|
|
})
|
|
}
|
|
|
|
// 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, "error.html", gin.H{
|
|
"MODT": h.configService.GetStringDefault(config.KeyModtext, config.DefaultModt),
|
|
})
|
|
return
|
|
}
|
|
|
|
downloadKey := fileRecord.ID
|
|
deleteKey := fileRecord.DeletionID
|
|
|
|
c.HTML(200, "complete.html", gin.H{
|
|
"MODT": h.configService.GetStringDefault(config.KeyModtext, config.DefaultModt),
|
|
"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
|
|
}
|
|
|
|
// Only check files on the current page.
|
|
// Status meanings:
|
|
// - green: file exists on disk
|
|
// - red: file missing
|
|
// - rainbow: stat error (something unexpected)
|
|
type AdminFileView struct {
|
|
file.FileRecord
|
|
ActualStatus string
|
|
}
|
|
|
|
adminFiles := make([]AdminFileView, 0, len(files))
|
|
for _, f := range files {
|
|
status := "red"
|
|
if f.Path != "" {
|
|
if _, err := os.Stat(f.Path); err == nil {
|
|
status = "green"
|
|
} else if os.IsNotExist(err) {
|
|
status = "red"
|
|
} else {
|
|
status = "rainbow"
|
|
}
|
|
}
|
|
adminFiles = append(adminFiles, AdminFileView{FileRecord: f, ActualStatus: status})
|
|
}
|
|
|
|
totalPages := (totalCount + limit - 1) / limit
|
|
|
|
c.HTML(200, "admin.html", gin.H{
|
|
"Files": adminFiles,
|
|
"Page": page,
|
|
"TotalPages": totalPages,
|
|
"BuildCommit": buildinfo.Commit,
|
|
})
|
|
}
|
|
|
|
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)
|
|
}
|