98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
package ui
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/a-h/templ"
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.jmbit.de/filegate/filegate/db"
|
|
"git.jmbit.de/filegate/filegate/utils"
|
|
"git.jmbit.de/filegate/filegate/web/templates"
|
|
)
|
|
|
|
func getFileView(c *gin.Context) {
|
|
metaContent := utils.GenMetaContent(c)
|
|
id64, err := strconv.ParseUint(c.Param("id"), 10, 0)
|
|
if err != nil {
|
|
fileViewError(c, err)
|
|
}
|
|
id := uint(id64)
|
|
file := db.GetFileByID(id)
|
|
log.Println(err)
|
|
c.HTML(http.StatusOK, "", templates.FileViewPage(metaContent, file.Name, file, err))
|
|
}
|
|
|
|
func getFileViewEditName(c *gin.Context) {
|
|
id64, err := strconv.ParseUint(c.Param("id"), 10, 0)
|
|
if err != nil {
|
|
fileViewError(c, err)
|
|
}
|
|
id := uint(id64)
|
|
name := db.GetFileByID(id).Name
|
|
|
|
c.HTML(http.StatusOK, "", templates.FileViewEditTitle(name, id))
|
|
}
|
|
|
|
func putFileViewName(c *gin.Context) {
|
|
id64, err := strconv.ParseUint(c.Param("id"), 10, 0)
|
|
if err != nil {
|
|
fileViewError(c, err)
|
|
}
|
|
id := uint(id64)
|
|
file := db.GetFileByID(id)
|
|
name := templ.EscapeString(c.PostForm("fileName"))
|
|
if len(name) < 128 {
|
|
name = name[:128]
|
|
}
|
|
file.Name = name
|
|
err = file.Save()
|
|
if err != nil {
|
|
fileViewError(c, err)
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "", templates.FileViewTitle(file.Name, id))
|
|
}
|
|
|
|
func getFileViewEditComment(c *gin.Context) {
|
|
id64, err := strconv.ParseUint(c.Param("id"), 10, 0)
|
|
if err != nil {
|
|
fileViewError(c, err)
|
|
}
|
|
id := uint(id64)
|
|
comment := db.GetFileByID(id).Comment
|
|
|
|
c.HTML(http.StatusOK, "", templates.FileViewEditComment(comment, id))
|
|
}
|
|
|
|
func putFileViewComment(c *gin.Context) {
|
|
id64, err := strconv.ParseUint(c.Param("id"), 10, 0)
|
|
if err != nil {
|
|
fileViewError(c, err)
|
|
}
|
|
id := uint(id64)
|
|
file := db.GetFileByID(id)
|
|
comment := templ.EscapeString(c.PostForm("fileComment"))
|
|
if len(comment) < 1024 {
|
|
comment = comment[:128]
|
|
}
|
|
file.Comment = &comment
|
|
err = file.Save()
|
|
if err != nil {
|
|
fileViewError(c, err)
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "", templates.FileViewComment(&comment, id))
|
|
}
|
|
|
|
func fileViewError(c *gin.Context, err error) {
|
|
metaContent := utils.GenMetaContent(c)
|
|
metaContent.ErrorTitle = "Error"
|
|
metaContent.ErrorText = err.Error()
|
|
c.HTML(http.StatusOK, "", templates.ErrorMessage("Could not create File", err.Error()))
|
|
log.Println(err)
|
|
c.Abort()
|
|
}
|