mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-09 16:42:48 +00:00
end point for sound list
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package bothandlers
|
||||
|
||||
import (
|
||||
"../config"
|
||||
@@ -2,8 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"./bot"
|
||||
"./bothandlers"
|
||||
"./config"
|
||||
"./handlers"
|
||||
"./webserver"
|
||||
)
|
||||
|
||||
@@ -15,7 +15,7 @@ func main() {
|
||||
bot.Connect(config.Config.Token)
|
||||
|
||||
//add handlers
|
||||
bot.AddHandler(handlers.SoundsHandler)
|
||||
bot.AddHandler(bothandlers.SoundsHandler)
|
||||
|
||||
// start new go routine for the discord websockets
|
||||
go bot.Start()
|
||||
|
||||
53
server/webserver/handlers/soundlist.go
Normal file
53
server/webserver/handlers/soundlist.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"../../config"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/valyala/fasthttp"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var soundList = make([]string, 0)
|
||||
|
||||
func SoundList(ctx *fasthttp.RequestCtx) {
|
||||
|
||||
if len(soundList) < 1 {
|
||||
err := PopulateSoundList()
|
||||
if err != nil {
|
||||
ctx.Error(err.Error(), 400)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
response, err := json.Marshal(soundList)
|
||||
|
||||
if err != nil {
|
||||
ctx.Error("Error marshaling json", 400)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.SetContentType("application/json")
|
||||
ctx.Write(response)
|
||||
}
|
||||
|
||||
func PopulateSoundList() error {
|
||||
fmt.Println("Populating sound list.")
|
||||
|
||||
soundList = make([]string, 0)
|
||||
|
||||
var fileName string
|
||||
files, err := ioutil.ReadDir(config.Config.SoundsPath)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
fileName = config.Config.BotPrefix + strings.Split(f.Name(), ".")[0]
|
||||
soundList = append(soundList, fileName)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
65
server/webserver/handlers/upload.go
Normal file
65
server/webserver/handlers/upload.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"../../config"
|
||||
"github.com/valyala/fasthttp"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func FileUpload(ctx *fasthttp.RequestCtx) {
|
||||
password := ctx.FormValue("password")
|
||||
|
||||
if string(password) != config.Config.UploadPassword {
|
||||
ctx.Error("Invalid password.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := ctx.FormFile("file")
|
||||
if err != nil {
|
||||
ctx.Error("Error reading file.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
ctx.Error("Error opening file.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
defer src.Close()
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// check if file already exists
|
||||
if _, err := os.Stat(config.Config.SoundsPath + file.Filename); err == nil {
|
||||
ctx.Error("File already exists.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
dst, err := os.Create(config.Config.SoundsPath + file.Filename)
|
||||
if err != nil {
|
||||
ctx.Error("Error creating file.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
defer dst.Close()
|
||||
|
||||
if _, err = io.Copy(dst, src); err != nil {
|
||||
ctx.Error("Error writing file.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
// repopulate sound list
|
||||
err = PopulateSoundList()
|
||||
|
||||
if err != nil {
|
||||
ctx.Error("File uploaded, but error populating sound list.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Success("application/json", []byte("Success!"))
|
||||
}
|
||||
@@ -2,11 +2,10 @@ package webserver
|
||||
|
||||
import (
|
||||
"../config"
|
||||
"./handlers"
|
||||
"github.com/buaazp/fasthttprouter"
|
||||
"github.com/valyala/fasthttp"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func logger(next fasthttp.RequestHandler) fasthttp.RequestHandler {
|
||||
@@ -25,7 +24,8 @@ func applyMiddleware(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
|
||||
|
||||
func registerRoutes(router *fasthttprouter.Router) {
|
||||
|
||||
router.PUT("/upload", fileUpload)
|
||||
router.GET("/soundlist", handlers.SoundList)
|
||||
router.PUT("/upload", handlers.FileUpload)
|
||||
|
||||
router.ServeFiles("/static/*filepath", "./static")
|
||||
|
||||
@@ -45,52 +45,3 @@ func Start() {
|
||||
// start web server
|
||||
log.Fatal(fasthttp.ListenAndServe(config.Config.ServerAddr, handlers))
|
||||
}
|
||||
|
||||
func fileUpload(ctx *fasthttp.RequestCtx) {
|
||||
password := ctx.FormValue("password")
|
||||
|
||||
if string(password) != config.Config.UploadPassword {
|
||||
ctx.Error("Invalid password.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := ctx.FormFile("file")
|
||||
if err != nil {
|
||||
ctx.Error("Error reading file.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
ctx.Error("Error opening file.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
defer src.Close()
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// check if file already exists
|
||||
if _, err := os.Stat(config.Config.SoundsPath + file.Filename); err == nil {
|
||||
ctx.Error("File already exists.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
dst, err := os.Create(config.Config.SoundsPath + file.Filename)
|
||||
if err != nil {
|
||||
ctx.Error("Error creating file.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
defer dst.Close()
|
||||
|
||||
if _, err = io.Copy(dst, src); err != nil {
|
||||
ctx.Error("Error writing file.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Success("application/json", []byte("Success!"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user