mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-11 01:22:48 +00:00
added server address to config
This commit is contained in:
@@ -1,15 +1,12 @@
|
||||
package webserver
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"../config"
|
||||
"github.com/buaazp/fasthttprouter"
|
||||
"github.com/valyala/fasthttp"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func logger(next fasthttp.RequestHandler) fasthttp.RequestHandler {
|
||||
@@ -22,7 +19,6 @@ func logger(next fasthttp.RequestHandler) fasthttp.RequestHandler {
|
||||
|
||||
func applyMiddleware(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
|
||||
newHandler := logger(handler)
|
||||
//newHandler = fasthttp.CompressHandler(newHandler)
|
||||
|
||||
return newHandler
|
||||
}
|
||||
@@ -47,128 +43,54 @@ func Start() {
|
||||
handlers := applyMiddleware(router.Handler)
|
||||
|
||||
// start web server
|
||||
log.Fatal(fasthttp.ListenAndServe("0.0.0.0:8080", handlers))
|
||||
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.SetStatusCode(500)
|
||||
fmt.Fprint(ctx, "Error reading file.")
|
||||
ctx.Error("Error reading file.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
ctx.SetStatusCode(500)
|
||||
fmt.Fprint(ctx, "Error opening file.")
|
||||
ctx.Error("Error opening file.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
defer src.Close()
|
||||
|
||||
// create uploads folder if it does not exist
|
||||
if _, err := os.Stat("./uploads"); os.IsNotExist(err) {
|
||||
os.Mkdir("./uploads", os.ModePerm)
|
||||
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("./uploads/" + file.Filename); err == nil {
|
||||
ctx.SetStatusCode(403)
|
||||
fmt.Fprint(ctx, "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("./uploads/" + file.Filename)
|
||||
dst, err := os.Create(config.Config.SoundsPath + file.Filename)
|
||||
if err != nil {
|
||||
ctx.SetStatusCode(500)
|
||||
fmt.Fprint(ctx, "Error writing file.")
|
||||
ctx.Error("Error creating file.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
defer dst.Close()
|
||||
|
||||
if _, err = io.Copy(dst, src); err != nil {
|
||||
ctx.SetStatusCode(500)
|
||||
fmt.Fprint(ctx, "Error writing file.")
|
||||
ctx.Error("Error writing file.", 400)
|
||||
return
|
||||
}
|
||||
|
||||
err = convertFile(file.Filename)
|
||||
if err != nil {
|
||||
ctx.SetStatusCode(500)
|
||||
fmt.Fprint(ctx, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprint(ctx, "Success")
|
||||
}
|
||||
|
||||
func convertFile(fileName string) error {
|
||||
trimmedName := strings.Split(fileName, ".")[0]
|
||||
outputPath := "./sounds/"
|
||||
inputPath := "./uploads/"
|
||||
|
||||
fmt.Println(inputPath + fileName)
|
||||
fmt.Println(outputPath + trimmedName + ".dca")
|
||||
|
||||
command := "./dca-rs " + "--raw " + "--i " + inputPath + fileName + " > " + outputPath + trimmedName + ".dca"
|
||||
|
||||
wg := new(sync.WaitGroup)
|
||||
commands := []string{command}
|
||||
for _, str := range commands {
|
||||
wg.Add(1)
|
||||
go exe_cmd(str, wg)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
/*
|
||||
cmd := exec.Command("./dca-rs", "--raw", "--i", inputPath+fileName+" > "+outputPath+trimmedName+".dca")
|
||||
|
||||
_, err := cmd.Output()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
err = cmd.Wait()
|
||||
if err == nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
outFile, err := os.Create(outputPath + trimmedName + ".dca")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
stdoutPipe, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
writer := bufio.NewWriter(outFile)
|
||||
defer writer.Flush()
|
||||
|
||||
fmt.Println(stdoutPipe)
|
||||
go io.Copy(writer, stdoutPipe)
|
||||
*/
|
||||
|
||||
fmt.Println("working")
|
||||
return nil
|
||||
}
|
||||
|
||||
func exe_cmd(cmd string, wg *sync.WaitGroup) {
|
||||
fmt.Println(cmd)
|
||||
parts := strings.Fields(cmd)
|
||||
out, err := exec.Command(parts[0], parts[1]).Output()
|
||||
if err != nil {
|
||||
fmt.Println("error occured")
|
||||
fmt.Printf("%s", err)
|
||||
}
|
||||
fmt.Printf("%s", out)
|
||||
wg.Done()
|
||||
ctx.Success("application/json", []byte("Success!"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user