From d6992cbf14a8b750435f0b64ede2ef90bc5d713c Mon Sep 17 00:00:00 2001 From: mgerb42 Date: Sun, 5 Feb 2017 04:58:48 +0000 Subject: [PATCH] end point for sound list --- server/{handlers => bothandlers}/sounds.go | 2 +- server/main.go | 4 +- server/webserver/handlers/soundlist.go | 53 ++++++++++++++++++ server/webserver/handlers/upload.go | 65 ++++++++++++++++++++++ server/webserver/server.go | 55 +----------------- 5 files changed, 124 insertions(+), 55 deletions(-) rename server/{handlers => bothandlers}/sounds.go (99%) create mode 100644 server/webserver/handlers/soundlist.go create mode 100644 server/webserver/handlers/upload.go diff --git a/server/handlers/sounds.go b/server/bothandlers/sounds.go similarity index 99% rename from server/handlers/sounds.go rename to server/bothandlers/sounds.go index 5c3d1ae..a69dd30 100644 --- a/server/handlers/sounds.go +++ b/server/bothandlers/sounds.go @@ -1,4 +1,4 @@ -package handlers +package bothandlers import ( "../config" diff --git a/server/main.go b/server/main.go index f180fec..55501c8 100644 --- a/server/main.go +++ b/server/main.go @@ -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() diff --git a/server/webserver/handlers/soundlist.go b/server/webserver/handlers/soundlist.go new file mode 100644 index 0000000..3bf8f02 --- /dev/null +++ b/server/webserver/handlers/soundlist.go @@ -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 +} diff --git a/server/webserver/handlers/upload.go b/server/webserver/handlers/upload.go new file mode 100644 index 0000000..2255fa2 --- /dev/null +++ b/server/webserver/handlers/upload.go @@ -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!")) +} diff --git a/server/webserver/server.go b/server/webserver/server.go index e8cec88..80d6d5e 100644 --- a/server/webserver/server.go +++ b/server/webserver/server.go @@ -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!")) -}