This repository has been archived by the owner on Jan 11, 2022. It is now read-only.
forked from swyuuki/ZeroBot-Plugin-Gif
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontext.go
91 lines (83 loc) · 2.03 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package plugin_gif
import (
"bufio"
"io"
"net/http"
"os"
"strconv"
"strings"
"github.com/sirupsen/logrus"
)
type context struct {
usrdir string
headimgsdir []string
}
func dlchan(name string, c *chan *string) {
target := datapath + `materials/` + name
_, err := os.Stat(target)
if err != nil {
download(`https://codechina.csdn.net/u011570312/imagematerials/-/raw/main/`+name, target)
} else {
logrus.Debugln("[gif] dl", name, "exists")
}
*c <- &target
}
func dlblock(name string) string {
target := datapath + `materials/` + name
_, err := os.Stat(target)
if err != nil {
download(`https://codechina.csdn.net/u011570312/imagematerials/-/raw/main/`+name, target)
}
return target
}
func dlrange(prefix string, suffix string, end int) *[]chan *string {
c := make([]chan *string, end)
for i := range c {
c[i] = make(chan *string)
go dlchan(prefix+strconv.Itoa(i)+suffix, &c[i])
}
return &c
}
// 新的上下文
func newContext(user int64) *context {
c := new(context)
c.usrdir = datapath + "users/" + strconv.FormatInt(user, 10) + `/`
os.MkdirAll(c.usrdir, 0755)
c.headimgsdir = make([]string, 2)
c.headimgsdir[0] = c.usrdir + "0.gif"
c.headimgsdir[1] = c.usrdir + "1.gif"
return c
}
// 下载图片
func download(url, dlpath string) error {
// 创建目录
var List = strings.Split(dlpath, `/`)
err := os.MkdirAll(strings.TrimSuffix(dlpath, List[len(List)-1]), 0755)
if err != nil {
logrus.Errorln("[gif] mkdir err:", err)
return err
}
res, err := http.Get(url)
if err != nil {
logrus.Errorln("[gif] http get err:", err)
return err
}
// 获得get请求响应的reader对象
reader := bufio.NewReaderSize(res.Body, 32*1024)
// 创建文件
file, err := os.Create(dlpath)
if err != nil {
logrus.Errorln("[gif] create file err:", err)
return err
}
// 获得文件的writer对象
writer := bufio.NewWriter(file)
written, err := io.Copy(writer, reader)
if err != nil {
logrus.Errorln("[gif] copy err:", err)
return err
}
res.Body.Close()
logrus.Debugln("[gif] dl len:", written)
return nil
}