Can upload new gong
This commit is contained in:
parent
f0967f3b9d
commit
cc301da971
4 changed files with 121 additions and 4 deletions
41
api/gongs.go
41
api/gongs.go
|
|
@ -3,6 +3,9 @@ package api
|
|||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
|
|
@ -21,7 +24,43 @@ func declareGongsRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
|||
c.JSON(http.StatusOK, gongs)
|
||||
})
|
||||
router.POST("/gongs", func(c *gin.Context) {
|
||||
c.AbortWithStatusJSON(http.StatusNotImplemented, gin.H{"errmsg": "TODO"})
|
||||
fgong, err := c.FormFile("gongfile")
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "No gong found"})
|
||||
return
|
||||
}
|
||||
|
||||
// Check file extension
|
||||
if path.Ext(fgong.Filename) != ".mp3" && path.Ext(fgong.Filename) != ".flac" && path.Ext(fgong.Filename) != ".wav" {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Bad file type. You should only upload .mp3, .wav or .flac files."})
|
||||
return
|
||||
}
|
||||
|
||||
if strings.Contains(fgong.Filename, "/") {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Bad file name."})
|
||||
return
|
||||
}
|
||||
|
||||
dst := path.Join(cfg.GongsDir, fgong.Filename)
|
||||
err = c.SaveUploadedFile(fgong, dst)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Something goes wrong when saving the gong: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
d, err := os.Stat(dst)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Something goes wrong when saving the gong: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
gong, err := reveil.LoadGong(cfg, dst, d)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to load gong: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gong)
|
||||
})
|
||||
|
||||
gongsRoutes := router.Group("/gongs/:tid")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue