1
0
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:
2018-05-30 21:23:03 -05:00
parent af8448e028
commit 3d05a49a3e
20 changed files with 125 additions and 196 deletions

View File

@@ -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

View File

@@ -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
}

View File

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