From f86947a8df056822c1a83154f2a28277520124c1 Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Fri, 16 Apr 2021 20:02:54 +0800 Subject: [PATCH] optimize get_image --- coolq/api.go | 6 ++++-- coolq/bot.go | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/coolq/api.go b/coolq/api.go index cbf50dc..97bf14e 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -952,9 +952,11 @@ func (bot *CQBot) CQGetImage(file string) MSG { } local := path.Join(global.CachePath, file+"."+path.Ext(msg["filename"].(string))) if !global.PathExists(local) { - if data, err := global.GetBytes(msg["url"].(string)); err == nil { - _ = ioutil.WriteFile(local, data, 0o644) + f, _ := os.OpenFile(local, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o0644) + if body, err := global.HTTPGetReadCloser(msg["url"].(string)); err != nil { + _, _ = f.ReadFrom(body) } + f.Close() } msg["file"] = local return OK(msg) diff --git a/coolq/bot.go b/coolq/bot.go index c36db69..43fc776 100644 --- a/coolq/bot.go +++ b/coolq/bot.go @@ -371,7 +371,11 @@ func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage) int32 { log.Warnf("记录聊天数据时出现错误: %v", err) return -1 } - if err := bot.db.Put(binary.ToBytes(id), binary.GZipCompress(buf.Bytes()), nil); err != nil { + gw := binary.AcquireGzipWriter() + defer binary.ReleaseGzipWriter(gw) + _, _ = gw.Write(buf.Bytes()) + _ = gw.Close() + if err := bot.db.Put(binary.ToBytes(id), gw.Bytes(), nil); err != nil { log.Warnf("记录聊天数据时出现错误: %v", err) return -1 } @@ -397,7 +401,11 @@ func (bot *CQBot) InsertPrivateMessage(m *message.PrivateMessage) int32 { log.Warnf("记录聊天数据时出现错误: %v", err) return -1 } - if err := bot.db.Put(binary.ToBytes(id), binary.GZipCompress(buf.Bytes()), nil); err != nil { + gw := binary.AcquireGzipWriter() + defer binary.ReleaseGzipWriter(gw) + _, _ = gw.Write(buf.Bytes()) + _ = gw.Close() + if err := bot.db.Put(binary.ToBytes(id), gw.Bytes(), nil); err != nil { log.Warnf("记录聊天数据时出现错误: %v", err) return -1 } @@ -425,7 +433,11 @@ func (bot *CQBot) InsertTempMessage(target int64, m *message.TempMessage) int32 log.Warnf("记录聊天数据时出现错误: %v", err) return -1 } - if err := bot.db.Put(binary.ToBytes(id), binary.GZipCompress(buf.Bytes()), nil); err != nil { + gw := binary.AcquireGzipWriter() + defer binary.ReleaseGzipWriter(gw) + _, _ = gw.Write(buf.Bytes()) + _ = gw.Close() + if err := bot.db.Put(binary.ToBytes(id), gw.Bytes(), nil); err != nil { log.Warnf("记录聊天数据时出现错误: %v", err) return -1 }