From d2d408a8e9d7a573f7f92a435c01549e112960cf Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Tue, 22 Sep 2020 11:17:53 +0800 Subject: [PATCH] feature set_group_portrait MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [2020-09-22 10:50:46] [ERROR]: 读取服务器地址时出现错误: %vEOF --- coolq/api.go | 13 ++++++++++++ global/fs.go | 52 +++++++++++++++++++++++++++++++++++++++++++-- go.mod | 2 +- main.go | 2 +- server/http.go | 10 +++++++++ server/websocket.go | 3 +++ 6 files changed, 78 insertions(+), 4 deletions(-) diff --git a/coolq/api.go b/coolq/api.go index dedaadd..132a52f 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -667,6 +667,19 @@ func (bot *CQBot) CQReloadEventFilter() MSG { return OK(nil) } +func (bot *CQBot) CQSetGroupPortrait(groupId int64, file, cache string) MSG { + if g := bot.Client.FindGroup(groupId); g != nil { + img, err := global.FindFile(file, cache, global.IMAGE_PATH) + if err != nil { + log.Warnf("set group portrait error: %v", err) + return Failed(100) + } + g.UpdateGroupHeadPortrait(img) + return OK(nil) + } + return Failed(100) +} + func (bot *CQBot) CQGetStatus() MSG { return OK(MSG{ "app_initialized": true, diff --git a/global/fs.go b/global/fs.go index f3dc6b0..9aa164d 100644 --- a/global/fs.go +++ b/global/fs.go @@ -2,11 +2,17 @@ package global import ( "bytes" + "crypto/md5" + "encoding/base64" + "encoding/hex" + "errors" + log "github.com/sirupsen/logrus" "io/ioutil" + "net/url" "os" "path" - - log "github.com/sirupsen/logrus" + "runtime" + "strings" ) var ( @@ -45,3 +51,45 @@ func Check(err error) { func IsAMRorSILK(b []byte) bool { return bytes.HasPrefix(b, HEADER_AMR) || bytes.HasPrefix(b, HEADER_SILK) } + +func FindFile(f, cache, PATH string) (data []byte, err error) { + data, err = nil, errors.New("can't find the file: "+f) + if strings.HasPrefix(f, "http") || strings.HasPrefix(f, "https") { + if cache == "" { + cache = "1" + } + hash := md5.Sum([]byte(f)) + cacheFile := path.Join(CACHE_PATH, hex.EncodeToString(hash[:])+".cache") + if PathExists(cacheFile) && cache == "1" { + return ioutil.ReadFile(cacheFile) + } + data, err = GetBytes(f) + _ = ioutil.WriteFile(cacheFile, data, 0644) + if err != nil { + return nil, err + } + } else if strings.HasPrefix(f, "base64") { + data, err = base64.StdEncoding.DecodeString(strings.ReplaceAll(f, "base64://", "")) + if err != nil { + return nil, err + } + } else if strings.HasPrefix(f, "file") { + fu, err := url.Parse(f) + if err != nil { + return nil, err + } + if strings.HasPrefix(fu.Path, "/") && runtime.GOOS == `windows` { + fu.Path = fu.Path[1:] + } + data, err = ioutil.ReadFile(fu.Path) + if err != nil { + return nil, err + } + } else if PathExists(path.Join(PATH, f)) { + data, err = ioutil.ReadFile(path.Join(PATH, f)) + if err != nil { + return nil, err + } + } + return +} diff --git a/go.mod b/go.mod index 8a9524b..c943e5f 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( github.com/xujiajun/nutsdb v0.5.0 github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189 golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect - golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect + golang.org/x/sys v0.0.0-20200916084744-dbad9cb7cb7a // indirect golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e gopkg.in/yaml.v2 v2.3.0 // indirect ) diff --git a/main.go b/main.go index 75207f6..919849e 100644 --- a/main.go +++ b/main.go @@ -260,7 +260,7 @@ func main() { func() { defer func() { if pan := recover(); pan != nil { - log.Error("读取服务器地址时出现错误: %v", pan) + log.Error("读取服务器地址时出现错误: ", pan) } }() r := binary.NewReader(data) diff --git a/server/http.go b/server/http.go index 743c040..5d61c76 100644 --- a/server/http.go +++ b/server/http.go @@ -354,6 +354,13 @@ func (s *httpServer) OcrImage(c *gin.Context) { c.JSON(200, s.bot.CQOcrImage(img)) } +func (s *httpServer) SetGroupPortrait(c *gin.Context) { + gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64) + file := getParam(c, "file") + cache := getParam(c, "cache") + c.JSON(200, s.bot.CQSetGroupPortrait(gid, file, cache)) +} + func getParamOrDefault(c *gin.Context, k, def string) string { r := getParam(c, k) if r != "" { @@ -502,6 +509,9 @@ var httpApi = map[string]func(s *httpServer, c *gin.Context){ "reload_event_filter": func(s *httpServer, c *gin.Context) { s.ReloadEventFilter(c) }, + "set_group_portrait": func(s *httpServer, c *gin.Context) { + s.SetGroupPortrait(c) + }, ".handle_quick_operation": func(s *httpServer, c *gin.Context) { s.HandleQuickOperation(c) }, diff --git a/server/websocket.go b/server/websocket.go index 3a28559..40ba112 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -504,6 +504,9 @@ var wsApi = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{ ".ocr_image": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { return bot.CQOcrImage(p.Get("image").Str) }, + "set_group_portrait": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { + return bot.CQSetGroupPortrait(p.Get("group_id").Int(), p.Get("file").String(), p.Get("cache").String()) + }, ".handle_quick_operation": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { return bot.CQHandleQuickOperation(p.Get("context"), p.Get("operation")) },