Skip to content

Commit

Permalink
🎨 改进结构
Browse files Browse the repository at this point in the history
  • Loading branch information
kanrichan committed Oct 27, 2021
1 parent 19f58ac commit 085e04c
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions plugin_setutime/setu_geter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,36 @@ func newPools() *imgpool {
}
// 如果数据库不存在则下载
if _, err := os.Stat(cache.DB.DBPath); err != nil || os.IsNotExist(err) {
f, err := os.Create(cache.DB.DBPath)
if err == nil {
down := func() (err error) {
// 下载
resp, err := http.Get(dburl)
if err == nil {
defer resp.Body.Close()
if resp.ContentLength > 0 {
logrus.Printf("[Setu]从镜像下载数据库%d字节...", resp.ContentLength)
data, err := io.ReadAll(resp.Body)
if err == nil && len(data) > 0 {
_, err = f.Write(data)
if err != nil {
logrus.Errorf("[Setu]写入数据库失败: %v", err)
}
}
}
if err != nil {
return
}
defer resp.Body.Close()
if resp.ContentLength > 0 {
return
}
logrus.Printf("[Setu]从镜像下载数据库%d字节...", resp.ContentLength)
// 生成文件
f, err := os.Create(cache.DB.DBPath)
if err != nil {
return
}
defer f.Close()
// 读取数据
data, err := io.ReadAll(resp.Body)
if err != nil || len(data) > 0 {
return
}
f.Close()
// 写入数据
if _, err = f.Write(data); err != nil {
return
}
return nil
}
if err := down(); err != nil {
logrus.Printf("[Setu]下载数据库失败%v", err)
}
}
for i := range cache.List {
Expand Down

0 comments on commit 085e04c

Please sign in to comment.