add lottery

This commit is contained in:
Jay 2018-09-27 21:43:02 +08:00
parent 06ac36b91d
commit 69bcf858d3
4 changed files with 74 additions and 4 deletions

25
model/lottery.go Normal file
View File

@ -0,0 +1,25 @@
package model
import (
"database/sql"
"time"
)
// Lottery -
type Lottery struct {
ID string `db:"id" cc:"id"`
Type string `db:"type" cc:"type"`
Message string `db:"message" cc:"message"`
Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"mtime"`
}
// GetRandomLotteryByType -
func GetRandomLotteryByType(t string) (p *Lottery, err error) {
p = &Lottery{}
err = x.Get(p, `select * from "public"."lottery" where "type" = $1 order random() limit 1`, t)
if err == sql.ErrNoRows {
return nil, nil
}
return
}

View File

@ -6,6 +6,7 @@ import (
"strings" "strings"
"git.trj.tw/golang/mtfosbot/module/apis/twitch" "git.trj.tw/golang/mtfosbot/module/apis/twitch"
"git.trj.tw/golang/mtfosbot/module/config"
"git.trj.tw/golang/mtfosbot/model" "git.trj.tw/golang/mtfosbot/model"
googleapi "git.trj.tw/golang/mtfosbot/module/apis/google" googleapi "git.trj.tw/golang/mtfosbot/module/apis/google"
@ -26,6 +27,8 @@ func selectAct(cmd, sub, txt string, s *lineobj.SourceObject) (res string) {
return delTwitchChannel(sub, txt, s) return delTwitchChannel(sub, txt, s)
case "image": case "image":
return fmt.Sprintf("$image$%s", sub) return fmt.Sprintf("$image$%s", sub)
case "lottery":
return lottery(sub, txt, s)
case "hello": case "hello":
return "World!!" return "World!!"
} }
@ -298,3 +301,26 @@ func addYoutubeChannel(sub, txt string, s *lineobj.SourceObject) (res string) {
return "Success" return "Success"
} }
func lottery(sub, txt string, s *lineobj.SourceObject) (res string) {
if len(sub) == 0 {
return ""
}
data, err := model.GetRandomLotteryByType(sub)
if err != nil || data == nil {
return
}
conf := config.GetConf()
u := conf.URL
if last := len(u); last > 0 && u[last-1] == '/' {
u = u[:last]
}
oriURL := "/image/origin"
thumbURL := "/image/thumbnail"
if len(data.Message) == 0 {
return
}
o := u + oriURL + "/" + data.Message + "?d=" + sub
t := u + thumbURL + "/" + data.Message + "?d=" + sub
return fmt.Sprintf("$image$%s;%s", o, t)
}

View File

@ -45,6 +45,16 @@ func GetOriginImage(c *context.Context) {
return return
} }
subd := c.DefaultQuery("d", "")
if len(subd) > 0 {
imgP = path.Join(imgP, subd)
exists = utils.CheckExists(imgP, true)
if !exists {
c.NotFound("image path not found")
return
}
}
newP := path.Join(imgP, fname) newP := path.Join(imgP, fname)
exists = utils.CheckExists(newP, false) exists = utils.CheckExists(newP, false)
if !exists { if !exists {
@ -126,11 +136,20 @@ func GetThumbnailImage(c *context.Context) {
return return
} }
thumbP := path.Join(imgP, "thumbnail", fname) thumbDir := "thumbnail"
subd := c.DefaultQuery("d", "")
if exists := utils.CheckExists(path.Join(imgP, thumbDir, subd), true); !exists {
if err := os.MkdirAll(path.Join(imgP, thumbDir, subd), 0775); err != nil {
c.ServerError(nil)
return
}
}
thumbP := path.Join(imgP, thumbDir, subd, fname)
genNew := false genNew := false
if !utils.CheckExists(thumbP, false) { if !utils.CheckExists(thumbP, false) {
genNew = true genNew = true
thumbP = path.Join(imgP, fname) thumbP = path.Join(imgP, subd, fname)
exists = utils.CheckExists(thumbP, false) exists = utils.CheckExists(thumbP, false)
if !exists { if !exists {
c.NotFound("image file not found") c.NotFound("image file not found")
@ -174,7 +193,7 @@ func GetThumbnailImage(c *context.Context) {
breader := bytes.NewReader(buf.Bytes()) breader := bytes.NewReader(buf.Bytes())
if genNew { if genNew {
savep := path.Join(conf.ImageRoot, "thumbnail", fname) savep := path.Join(conf.ImageRoot, "thumbnail", subd, fname)
err := saveNewThumbnail(breader, savep) err := saveNewThumbnail(breader, savep)
if err != nil { if err != nil {
c.ServerError(nil) c.ServerError(nil)

2
schema

@ -1 +1 @@
Subproject commit 33f8a1dc6ea0de9323e9a75f5366fa68d8325774 Subproject commit 09deeff71694c8baa68e77dd8285e3a245d2d284