1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-10 09:02:49 +00:00
Files
go-discord-bot/server/webserver/handlers/upload.go

66 lines
1.5 KiB
Go

package handlers
import (
"os"
"strings"
"net/http"
"github.com/gin-gonic/gin"
"github.com/mgerb/go-discord-bot/server/config"
log "github.com/sirupsen/logrus"
)
// FileUpload
func FileUpload(c *gin.Context) {
password := c.PostForm("password")
if string(password) != config.Config.UploadPassword {
c.JSON(http.StatusInternalServerError, "Invalid password.")
return
}
file, err := c.FormFile("file")
if err != nil {
log.Error(err)
c.JSON(http.StatusInternalServerError, "Error reading file.")
return
}
// create uploads folder if it does not exist
if _, err := os.Stat(config.Config.SoundsPath); os.IsNotExist(err) {
os.Mkdir(config.Config.SoundsPath, os.ModePerm)
}
// convert file name to lower case and trim spaces
file.Filename = strings.ToLower(file.Filename)
file.Filename = strings.Replace(file.Filename, " ", "", -1)
// check if file already exists
if _, err := os.Stat(config.Config.SoundsPath + file.Filename); err == nil {
c.JSON(http.StatusInternalServerError, "File already exists.")
return
}
err = c.SaveUploadedFile(file, config.Config.SoundsPath+file.Filename)
log.Debug("Saving file", config.Config.SoundsPath+file.Filename)
if err != nil {
log.Error(err)
c.JSON(http.StatusInternalServerError, "Error creating file.")
return
}
// repopulate sound list
err = PopulateSoundList()
if err != nil {
log.Error(err)
c.JSON(http.StatusInternalServerError, "Error populating sound list.")
return
}
c.JSON(200, "Success")
}