mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-11 09:32:50 +00:00
added docker support
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"regexp"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -16,8 +17,8 @@ func Downloader(c *gin.Context) {
|
||||
fileType := c.Query("fileType")
|
||||
|
||||
// create youtube folder if it does not exist
|
||||
if _, err := os.Stat("youtube"); os.IsNotExist(err) {
|
||||
os.Mkdir("youtube", os.ModePerm)
|
||||
if _, err := os.Stat(config.Config.YoutubePath); os.IsNotExist(err) {
|
||||
os.Mkdir(config.Config.YoutubePath, os.ModePerm)
|
||||
}
|
||||
|
||||
// get the video title
|
||||
@@ -41,7 +42,7 @@ func Downloader(c *gin.Context) {
|
||||
cleanTitle := cleanseTitle(titleOut.String())
|
||||
log.Debug(cleanTitle)
|
||||
|
||||
cmd := exec.Command("youtube-dl", "-x", "--audio-format", "mp3", "-o", "./youtube/"+cleanTitle+".%(ext)s", url)
|
||||
cmd := exec.Command("youtube-dl", "-x", "--audio-format", "mp3", "-o", config.Config.YoutubePath+"/"+cleanTitle+".%(ext)s", url)
|
||||
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
|
||||
@@ -11,8 +11,6 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var soundList []sound
|
||||
|
||||
type sound struct {
|
||||
Prefix string `json:"prefix"`
|
||||
Name string `json:"name"`
|
||||
@@ -22,26 +20,39 @@ type sound struct {
|
||||
// SoundList -
|
||||
func SoundList(c *gin.Context) {
|
||||
|
||||
if len(soundList) < 1 {
|
||||
err := PopulateSoundList()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
soundList, err := readSoundsDir(config.Config.SoundsPath)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, soundList)
|
||||
}
|
||||
|
||||
// PopulateSoundList -
|
||||
func PopulateSoundList() error {
|
||||
// ClipList -
|
||||
func ClipList(c *gin.Context) {
|
||||
|
||||
soundList = []sound{}
|
||||
|
||||
files, err := ioutil.ReadDir(config.Config.SoundsPath)
|
||||
clipList, err := readSoundsDir(config.Config.ClipsPath)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, clipList)
|
||||
}
|
||||
|
||||
func readSoundsDir(dir string) ([]sound, error) {
|
||||
|
||||
soundList := []sound{}
|
||||
|
||||
files, err := ioutil.ReadDir(dir)
|
||||
|
||||
if err != nil {
|
||||
return soundList, err
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
@@ -57,33 +68,5 @@ func PopulateSoundList() error {
|
||||
soundList = append(soundList, listItem)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ClipList -
|
||||
func ClipList(c *gin.Context) {
|
||||
|
||||
clipList := []sound{}
|
||||
|
||||
files, err := ioutil.ReadDir(config.Config.ClipsPath)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
fileName := strings.Split(f.Name(), ".")[0]
|
||||
extension := strings.Split(f.Name(), ".")[1]
|
||||
|
||||
listItem := sound{
|
||||
Name: fileName,
|
||||
Extension: extension,
|
||||
}
|
||||
|
||||
clipList = append(clipList, listItem)
|
||||
}
|
||||
|
||||
c.JSON(200, clipList)
|
||||
return soundList, nil
|
||||
}
|
||||
|
||||
@@ -35,13 +35,13 @@ func FileUpload(c *gin.Context) {
|
||||
file.Filename = strings.Replace(file.Filename, " ", "", -1)
|
||||
|
||||
// check if file already exists
|
||||
if _, err := os.Stat(config.Config.SoundsPath + file.Filename); err == nil {
|
||||
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)
|
||||
err = c.SaveUploadedFile(file, config.Config.SoundsPath+"/"+file.Filename)
|
||||
log.Debug("Saving file", config.Config.SoundsPath+"/"+file.Filename)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
@@ -49,14 +49,5 @@ func FileUpload(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// repopulate sound list
|
||||
err = PopulateSoundList()
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, "Error populating sound list.")
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, "Success")
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ func GetJWT(user discord.User) (string, error) {
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString([]byte(config.Config.JWTKey))
|
||||
return token.SignedString([]byte(config.Config.JWTSecret))
|
||||
}
|
||||
|
||||
func checkEmailPermissions(email string, emails []string) bool {
|
||||
@@ -96,7 +96,7 @@ func AuthorizedJWT() gin.HandlerFunc {
|
||||
|
||||
// parse and verify token
|
||||
token, err := jwt.ParseWithClaims(tokenString[1], &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(config.Config.JWTKey), nil
|
||||
return []byte(config.Config.JWTSecret), nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -16,7 +16,7 @@ func getRouter() *gin.Engine {
|
||||
|
||||
router.StaticFS("/static", box)
|
||||
router.Static("/public/sounds", config.Config.SoundsPath)
|
||||
router.Static("/public/youtube", "./youtube")
|
||||
router.Static("/public/youtube", config.Config.YoutubePath)
|
||||
router.Static("/public/clips", config.Config.ClipsPath)
|
||||
|
||||
router.NoRoute(func(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user