filegate/web/ui/newFile.go

78 lines
1.8 KiB
Go
Raw Normal View History

2023-12-28 17:23:27 +00:00
package ui
import (
2023-12-30 14:28:54 +00:00
"fmt"
"log"
2023-12-28 17:23:27 +00:00
"net/http"
"github.com/gin-gonic/gin"
2023-12-30 14:28:54 +00:00
"git.jmbit.de/filegate/filegate/files"
2023-12-28 17:23:27 +00:00
"git.jmbit.de/filegate/filegate/utils"
"git.jmbit.de/filegate/filegate/web/templates"
)
func getNewFile(c *gin.Context) {
c.HTML(http.StatusOK, "", templates.NewFilePage(utils.GenMetaContent(c), "New File", nil))
}
2023-12-30 14:28:54 +00:00
func postNewFileUpload(c *gin.Context) {
name := c.PostForm("name")
url := "N/A"
comment := c.PostForm("comment")
file, err := c.FormFile("file")
if err != nil {
newFileError(c, err)
return
}
fileid, err := files.UploadFile(file, name, url, comment)
if err != nil {
newFileError(c, err)
return
}
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("/file/%d", fileid))
}
func postNewFileDownload(c *gin.Context) {
name := c.PostForm("name")
url := c.PostForm("url")
comment := c.PostForm("comment")
file, err := c.FormFile("file")
if err != nil {
newFileError(c, err)
return
}
fileid, err := files.UploadFile(file, name, url, comment)
if err != nil {
newFileError(c, err)
return
}
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("/file/%d", fileid))
}
func postNewFileBrowser(c *gin.Context) {
name := c.PostForm("name")
url := c.PostForm("url")
comment := c.PostForm("comment")
file, err := c.FormFile("file")
if err != nil {
newFileError(c, err)
return
}
fileid, err := files.UploadFile(file, name, url, comment)
if err != nil {
newFileError(c, err)
return
}
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("/file/%d", fileid))
}
func newFileError(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()))
2023-12-30 14:28:54 +00:00
log.Println(err)
return
2023-12-28 17:23:27 +00:00
}