add image route , has bug

This commit is contained in:
Jay
2018-09-18 23:54:36 +08:00
parent ce62048c18
commit 2dc337762d
15 changed files with 2306 additions and 1 deletions
+36
View File
@@ -281,3 +281,39 @@ func UpdateDonateSetting(c *context.Context) {
c.Success(nil)
}
// GetDonateBarStatus -
func GetDonateBarStatus(c *context.Context) {
chid := c.Param("chid")
chdata, err := model.GetTwitchChannelWithID(chid)
if err != nil {
c.ServerError(nil)
return
}
if chdata == nil {
c.NotFound(nil)
return
}
ds, err := model.GetDonateSettingByChannel(chdata.ID)
if err != nil {
c.ServerError(nil)
return
}
sum := 0
mapData := map[string]interface{}{}
if ds != nil {
sum, err = model.SumChannelDonatePriceSinceTime(chdata.ID, ds.StartDate)
if err != nil {
c.ServerError(nil)
return
}
sum += ds.StartAmount
mapData = utils.ToMap(ds)
mapData["total"] = sum
}
c.Success(map[string]interface{}{
"setting": mapData,
})
}
+90
View File
@@ -0,0 +1,90 @@
package rimg
import (
"bytes"
"image"
_ "image/gif"
"image/jpeg"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"os"
"path"
"strconv"
"github.com/nfnt/resize"
"git.trj.tw/golang/mtfosbot/module/config"
"git.trj.tw/golang/mtfosbot/module/context"
"git.trj.tw/golang/mtfosbot/module/utils"
)
// GetOriginImage -
func GetOriginImage(c *context.Context) {
fname := c.Param("imgname")
conf := config.GetConf()
imgP := utils.ParsePath(conf.ImageRoot)
exists := utils.CheckExists(imgP, true)
if !exists {
c.NotFound("image path not found")
return
}
newP := path.Join(imgP, fname)
exists = utils.CheckExists(newP, false)
if !exists {
c.NotFound("image file not found")
return
}
fileBuf, err := os.Open(newP)
if err != nil {
c.ServerError(nil)
return
}
defer fileBuf.Close()
imgf, _, err := image.DecodeConfig(fileBuf)
if err != nil {
c.ServerError(nil)
return
}
if imgf.Height > 1024 || imgf.Width > 1024 {
img, _, err := image.Decode(fileBuf)
if err != nil {
c.ServerError(nil)
return
}
w := 0
h := 0
if imgf.Width > imgf.Height {
w = 1024
} else {
h = 1024
}
buf := new(bytes.Buffer)
m := resize.Resize(uint(w), uint(h), img, resize.Bilinear)
err = jpeg.Encode(buf, m, nil)
if err != nil {
c.ServerError(nil)
return
}
c.Writer.Header().Set("Content-Type", "image/jpeg")
c.Writer.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
_, err = c.Writer.Write(buf.Bytes())
if err != nil {
c.ServerError(nil)
}
return
}
buf, err := ioutil.ReadAll(fileBuf)
c.Writer.Header().Set("Content-Type", "image/jpeg")
c.Writer.Header().Set("Content-Length", strconv.Itoa(len(buf)))
_, err = c.Writer.Write(buf)
if err != nil {
c.ServerError(nil)
return
}
}
+16
View File
@@ -1,12 +1,14 @@
package routes
import (
"fmt"
"log"
"git.trj.tw/golang/mtfosbot/module/context"
"git.trj.tw/golang/mtfosbot/router/api"
"git.trj.tw/golang/mtfosbot/router/google"
"git.trj.tw/golang/mtfosbot/router/line"
"git.trj.tw/golang/mtfosbot/router/rimg"
"git.trj.tw/golang/mtfosbot/router/twitch"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/contrib/sessions"
@@ -36,16 +38,30 @@ func NewServ() *gin.Engine {
// SetRoutes - set routes
func SetRoutes(r *gin.Engine) {
r.NoRoute(func(c *gin.Context) {
fmt.Println("match not route")
c.String(404, "404 page not found")
})
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "ok",
})
})
imageProcGroup := r.Group("/image")
{
imageProcGroup.GET("/origin", func(c *gin.Context) {
c.String(200, "test msg")
})
imageProcGroup.GET("/origin/:imgname", context.PatchCtx(rimg.GetOriginImage))
}
apiGroup := r.Group("/api")
{
apiGroup.POST("/login", context.PatchCtx(api.UserLogin))
apiGroup.POST("/logout", context.PatchCtx(api.UserLogout))
apiGroup.GET("/twitch/channel/:chid/opay/bar", context.PatchCtx(api.GetDonateBarStatus))
}
apiTwitchGroup := apiGroup.Group("/twitch", context.PatchCtx(api.CheckSession))
{