使用真正lazy的资源下载方式

This commit is contained in:
源文雨
2022-04-16 00:02:41 +08:00
parent fef254031e
commit dfd184724b
22 changed files with 333 additions and 225 deletions

View File

@@ -38,22 +38,21 @@ func (ymgal) TableName() string {
}
// initialize 初始化ymgaldb数据库
func initialize(dbpath string) *ymgaldb {
var err error
func initialize(dbpath string) (db *ymgaldb, err error) {
if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) {
// 生成文件
f, err := os.Create(dbpath)
if err != nil {
return nil
return nil, err
}
defer f.Close()
_ = f.Close()
}
gdb, err := gorm.Open("sqlite3", dbpath)
if err != nil {
panic(err)
return
}
gdb.AutoMigrate(&ymgal{})
return (*ymgaldb)(gdb)
return (*ymgaldb)(gdb), nil
}
func (gdb *ymgaldb) insertOrUpdateYmgalByID(id int64, title, pictureType, pictureDescription, pictureList string) (err error) {

View File

@@ -18,11 +18,20 @@ func init() {
PublicDataFolder: "Ymgal",
})
dbfile := engine.DataFolder() + "ymgal.db"
go func() {
_, _ = file.GetLazyData(dbfile, false, false)
gdb = initialize(dbfile)
}()
engine.OnRegex("^随机gal(CG|表情包)$").Limit(ctxext.LimitByUser).SetBlock(true).
getdb := ctxext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool {
_, err := file.GetLazyData(dbfile, false, false)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return false
}
gdb, err = initialize(dbfile)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return false
}
return true
})
engine.OnRegex("^随机gal(CG|表情包)$", getdb).Limit(ctxext.LimitByUser).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
ctx.Send("少女祈祷中......")
pictureType := ctx.State["regex_matched"].([]string)[1]
@@ -34,7 +43,7 @@ func init() {
}
sendYmgal(y, ctx)
})
engine.OnRegex("^gal(CG|表情包)([一-龥ぁ-んァ-ヶA-Za-z0-9]{1,25})$").Limit(ctxext.LimitByUser).SetBlock(true).
engine.OnRegex("^gal(CG|表情包)([一-龥ぁ-んァ-ヶA-Za-z0-9]{1,25})$", getdb).Limit(ctxext.LimitByUser).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
ctx.Send("少女祈祷中......")
pictureType := ctx.State["regex_matched"].([]string)[1]
@@ -47,7 +56,7 @@ func init() {
}
sendYmgal(y, ctx)
})
engine.OnFullMatch("更新gal", zero.SuperUserPermission).SetBlock(true).Handle(
engine.OnFullMatch("更新gal", zero.SuperUserPermission, getdb).SetBlock(true).Handle(
func(ctx *zero.Ctx) {
ctx.Send("少女祈祷中......")
err := updatePic()