1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-11 01:22:48 +00:00

play sounds from web ui - store uploaded sounds in database

This commit is contained in:
2018-09-12 23:36:44 -05:00
parent 325203cc5e
commit be359f7424
26 changed files with 390 additions and 245 deletions

View File

@@ -7,9 +7,9 @@ import (
// AddConfigRoutes -
func AddConfigRoutes(group *gin.RouterGroup) {
group.GET("/config/client_id", getClientIDHandler)
group.GET("/config/client_id", getConfigHandler)
}
func getClientIDHandler(c *gin.Context) {
func getConfigHandler(c *gin.Context) {
c.JSON(200, map[string]string{"id": config.Config.ClientID})
}

View File

@@ -13,10 +13,10 @@ import (
// AddDownloaderRoutes -
func AddDownloaderRoutes(group *gin.RouterGroup) {
group.GET("/ytdownloader", downloaderHandler)
group.GET("/ytdownloader", getDownloaderHandler)
}
func downloaderHandler(c *gin.Context) {
func getDownloaderHandler(c *gin.Context) {
url := c.Query("url")
fileType := c.Query("fileType")

View File

@@ -4,7 +4,8 @@ import (
"strconv"
"github.com/gin-gonic/gin"
"github.com/mgerb/go-discord-bot/server/logger"
"github.com/mgerb/go-discord-bot/server/db"
"github.com/mgerb/go-discord-bot/server/webserver/model"
)
// AddLoggerRoutes -
@@ -20,7 +21,7 @@ func getMessagesHandler(c *gin.Context) {
page = 0
}
messages, err := logger.GetMessages(page)
messages, err := model.MessageGet(db.GetConn(), page)
if err != nil {
c.JSON(500, err)
@@ -31,7 +32,7 @@ func getMessagesHandler(c *gin.Context) {
}
func getLinkedMessagesHandler(c *gin.Context) {
posts, err := logger.GetLinkedMessages()
posts, err := model.MessageGetLinked(db.GetConn())
if err != nil {
c.JSON(500, err.Error())

View File

@@ -2,8 +2,10 @@ package routes
import (
"github.com/gin-gonic/gin"
"github.com/mgerb/go-discord-bot/server/db"
"github.com/mgerb/go-discord-bot/server/webserver/discord"
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
"github.com/mgerb/go-discord-bot/server/webserver/model"
log "github.com/sirupsen/logrus"
)
@@ -57,5 +59,12 @@ func oauthHandler(c *gin.Context) {
return
}
// save/update user in database
err = model.UserSave(db.GetConn(), &user)
if err != nil {
log.Error(err)
}
c.JSON(200, token)
}

View File

@@ -0,0 +1,110 @@
package routes
import (
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/mgerb/go-discord-bot/server/bothandlers"
"github.com/mgerb/go-discord-bot/server/config"
"github.com/mgerb/go-discord-bot/server/db"
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
"github.com/mgerb/go-discord-bot/server/webserver/model"
"github.com/mgerb/go-discord-bot/server/webserver/response"
log "github.com/sirupsen/logrus"
)
// AddSoundRoutes -
func AddSoundRoutes(group *gin.RouterGroup) {
group.GET("/sound", listSoundHandler)
group.POST("/sound", middleware.AuthorizedJWT(), postSoundHandler)
group.POST("/sound/play", middleware.AuthorizedJWT(), middleware.AuthPermissions(middleware.PermMod), postSoundPlayHandler)
}
func listSoundHandler(c *gin.Context) {
archives, err := model.SoundList(db.GetConn())
if err != nil {
response.InternalError(c, err)
return
}
response.Success(c, archives)
}
func postSoundPlayHandler(c *gin.Context) {
connections := bothandlers.ActiveConnections
params := struct {
Name string `json:"name"`
}{}
c.BindJSON(&params)
// loop through all connections and play audio
// currently only used with one server
// will need selector on UI if used for multiple servers
if len(connections) == 1 && params.Name != "" {
for _, con := range connections {
con.PlayAudio(params.Name, nil)
}
}
response.Success(c, "test")
}
func postSoundHandler(c *gin.Context) {
oc, _ := c.Get("claims")
claims, _ := oc.(*middleware.CustomClaims)
// TODO: verify user for upload
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.Replace(strings.ToLower(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.Info(claims.Username, "uploaded", config.Config.SoundsPath+"/"+file.Filename)
// save who uploaded the clip into the database
uploadSaveDB(claims.ID, file.Filename)
if err != nil {
log.Error(err)
c.JSON(http.StatusInternalServerError, "Error creating file.")
return
}
c.JSON(200, "Success")
}
// save new sound to database
func uploadSaveDB(userID, filename string) {
splitFilename := strings.Split(filename, ".")
extension := splitFilename[len(splitFilename)-1]
name := strings.Join(splitFilename[:len(splitFilename)-1], ".")
model.SoundCreate(db.GetConn(), &model.Sound{
UserID: userID,
Name: name,
Extension: extension,
})
}

View File

@@ -1,58 +0,0 @@
package routes
import (
"os"
"strings"
"net/http"
"github.com/gin-gonic/gin"
"github.com/mgerb/go-discord-bot/server/config"
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
log "github.com/sirupsen/logrus"
)
// AddUploadRoutes - add file upload routes
func AddUploadRoutes(group *gin.RouterGroup) {
group.POST("/upload", middleware.AuthorizedJWT(), fileUploadHandler)
}
func fileUploadHandler(c *gin.Context) {
// originalClaims, _ := c.Get("claims")
// claims, _ := originalClaims.(*middleware.CustomClaims)
// TODO: verify user for upload
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
}
c.JSON(200, "Success")
}

View File

@@ -14,14 +14,14 @@ import (
// AddVideoArchiveRoutes -
func AddVideoArchiveRoutes(group *gin.RouterGroup) {
group.GET("/video-archive", listVideoArchivesHandler)
group.GET("/video-archive", listVideoArchiveHandler)
authGroup := group.Group("", middleware.AuthorizedJWT())
authGroup.POST("/video-archive", middleware.AuthPermissions(middleware.PermMod), postVideoArchivesHandler)
authGroup.DELETE("/video-archive/:id", middleware.AuthPermissions(middleware.PermAdmin), deleteVideoArchivesHandler)
authGroup.POST("/video-archive", middleware.AuthPermissions(middleware.PermMod), postVideoArchiveHandler)
authGroup.DELETE("/video-archive/:id", middleware.AuthPermissions(middleware.PermAdmin), deleteVideoArchiveHandler)
}
func listVideoArchivesHandler(c *gin.Context) {
func listVideoArchiveHandler(c *gin.Context) {
archives, err := model.VideoArchiveList(db.GetConn())
if err != nil {
@@ -32,7 +32,7 @@ func listVideoArchivesHandler(c *gin.Context) {
response.Success(c, archives)
}
func deleteVideoArchivesHandler(c *gin.Context) {
func deleteVideoArchiveHandler(c *gin.Context) {
id := c.Param("id")
if id == "" {
@@ -50,7 +50,7 @@ func deleteVideoArchivesHandler(c *gin.Context) {
response.Success(c, "deleted")
}
func postVideoArchivesHandler(c *gin.Context) {
func postVideoArchiveHandler(c *gin.Context) {
params := struct {
URL string `json:"url"`
}{}