filegate/web/ui/newFile.go

78 lines
1.8 KiB
Go

package ui
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
"git.jmbit.de/filegate/filegate/files"
"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))
}
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()))
log.Println(err)
return
}