package web import ( "ResendIt/internal/file" "os" "strconv" "github.com/gin-gonic/gin" ) type Handler struct { fileService *file.Service configService ConfigService } // ConfigService is the small interface needed by the web layer. type ConfigService interface { GetIntDefault(key string, def int) int GetInt64Default(key string, def int64) int64 SetString(key, 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", }) } // 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 } // 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, }) } 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) }