mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 10:10:25 +00:00
✏️ 统一懒加载验证
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package plugin_book_review
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -14,7 +12,6 @@ import (
|
||||
|
||||
const dbpath = "data/BookReview/"
|
||||
const dbfile = dbpath + "bookreview.db"
|
||||
const dburl = "https://codechina.csdn.net/u011570312/ZeroBot-Plugin/-/raw/master/" + dbfile
|
||||
|
||||
var db = &sql.Sqlite{DBPath: dbfile}
|
||||
|
||||
@@ -24,28 +21,7 @@ func init() {
|
||||
process.SleepAbout1sTo2s()
|
||||
// os.RemoveAll(dbpath)
|
||||
_ = os.MkdirAll(dbpath, 0755)
|
||||
if !file.IsExist(dbfile) { // 如果没有数据库,则从 url 下载
|
||||
f, err := os.Create(dbfile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
resp, err := http.Get(dburl)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.ContentLength > 0 {
|
||||
log.Printf("[bookreview]从镜像下载数据库%d字节...", resp.ContentLength)
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err == nil && len(data) > 0 {
|
||||
_, _ = f.Write(data)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
_, _ = file.GetLazyData(dbfile, false, true)
|
||||
err := db.Create("bookreview", &book{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
Reference in New Issue
Block a user