diff --git a/model/lottery.go b/model/lottery.go new file mode 100644 index 0000000..c09e211 --- /dev/null +++ b/model/lottery.go @@ -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 +} diff --git a/module/message-command/line-group.go b/module/message-command/line-group.go index 12e695d..6bd500d 100644 --- a/module/message-command/line-group.go +++ b/module/message-command/line-group.go @@ -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" @@ -26,6 +27,8 @@ func selectAct(cmd, sub, txt string, s *lineobj.SourceObject) (res string) { return delTwitchChannel(sub, txt, s) case "image": return fmt.Sprintf("$image$%s", sub) + case "lottery": + return lottery(sub, txt, s) case "hello": return "World!!" } @@ -298,3 +301,26 @@ func addYoutubeChannel(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) +} diff --git a/router/rimg/rimg.go b/router/rimg/rimg.go index cf4437d..5ed9e14 100644 --- a/router/rimg/rimg.go +++ b/router/rimg/rimg.go @@ -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) diff --git a/schema b/schema index 33f8a1d..09deeff 160000 --- a/schema +++ b/schema @@ -1 +1 @@ -Subproject commit 33f8a1dc6ea0de9323e9a75f5366fa68d8325774 +Subproject commit 09deeff71694c8baa68e77dd8285e3a245d2d284