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

end point for sound list

This commit is contained in:
2017-02-05 04:58:48 +00:00
parent 9c293e056d
commit d6992cbf14
5 changed files with 124 additions and 55 deletions

View File

@@ -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!"))
}