This commit is contained in:
Jay 2018-10-01 11:46:55 +08:00
commit 7f9ade4332
3 changed files with 76 additions and 4 deletions

27
model/lottery.go Normal file
View File

@ -0,0 +1,27 @@
package model
import (
"database/sql"
"math/rand"
"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{}
offset := rand.Intn(10)
err = x.Get(p, `select * from "public"."lottery" where "type" = $1 order by random() offset $2 limit 1`, t, offset)
if err == sql.ErrNoRows {
return nil, nil
}
return
}

View File

@ -6,6 +6,7 @@ import (
"strings"
"git.trj.tw/golang/mtfosbot/module/apis/twitch"
"git.trj.tw/golang/mtfosbot/module/config"
"git.trj.tw/golang/mtfosbot/model"
googleapi "git.trj.tw/golang/mtfosbot/module/apis/google"
@ -30,6 +31,8 @@ func selectAct(cmd, sub, txt string, s *lineobj.SourceObject) (res string) {
return addYoutubeChannel(sub, txt, s)
case "delyoutube":
return delYoutubeChannel(sub, txt, s)
case "lottery":
return lottery(sub, txt, s)
case "hello":
return "World!!"
}
@ -321,7 +324,7 @@ func delYoutubeChannel(sub, txt string, s *lineobj.SourceObject) (res string) {
return "channel not exists"
}
rt := &model.LineYoutubeRT{
Line: s.GroupID,
Line: s.GroupID,
Youtube: ytData.ID,
}
err = rt.DelRT()
@ -330,3 +333,26 @@ func delYoutubeChannel(sub, txt string, s *lineobj.SourceObject) (res string) {
}
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
}
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)
exists = utils.CheckExists(newP, false)
if !exists {
@ -126,11 +136,20 @@ func GetThumbnailImage(c *context.Context) {
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
if !utils.CheckExists(thumbP, false) {
genNew = true
thumbP = path.Join(imgP, fname)
thumbP = path.Join(imgP, subd, fname)
exists = utils.CheckExists(thumbP, false)
if !exists {
c.NotFound("image file not found")
@ -174,7 +193,7 @@ func GetThumbnailImage(c *context.Context) {
breader := bytes.NewReader(buf.Bytes())
if genNew {
savep := path.Join(conf.ImageRoot, "thumbnail", fname)
savep := path.Join(conf.ImageRoot, "thumbnail", subd, fname)
err := saveNewThumbnail(breader, savep)
if err != nil {
c.ServerError(nil)