diff --git a/client/image.go b/client/image.go index 70c80db6..563dda3b 100644 --- a/client/image.go +++ b/client/image.go @@ -2,7 +2,6 @@ package client import ( "bytes" - "crypto/md5" "encoding/hex" "fmt" "github.com/Mrs4s/MiraiGo/binary" @@ -23,9 +22,9 @@ func init() { } func (c *QQClient) UploadGroupImage(groupCode int64, img io.ReadSeeker) (*message.GroupImageElement, error) { - h := md5.New() - length, _ := io.Copy(h, img) - fh := h.Sum(nil) + _, _ = img.Seek(0, io.SeekStart) // safe + fh, length := utils.ComputeMd5AndLength(img) + _, _ = img.Seek(0, io.SeekStart) seq, pkt := c.buildGroupImageStorePacket(groupCode, fh[:], int32(length)) r, err := c.sendAndWait(seq, pkt) if err != nil { @@ -38,7 +37,6 @@ func (c *QQClient) UploadGroupImage(groupCode int64, img io.ReadSeeker) (*messag if rsp.IsExists { goto ok } - _, _ = img.Seek(0, io.SeekStart) if len(c.srvSsoAddrs) == 0 { for i, addr := range rsp.UploadIp { c.srvSsoAddrs = append(c.srvSsoAddrs, fmt.Sprintf("%v:%v", binary.UInt32ToIPV4Address(uint32(addr)), rsp.UploadPort[i])) @@ -66,9 +64,7 @@ func (c *QQClient) UploadGroupImageByFile(groupCode int64, path string) (*messag if err != nil { return nil, err } - h := md5.New() - length, _ := io.Copy(h, img) - fh := h.Sum(nil) + fh, length := utils.ComputeMd5AndLength(img) seq, pkt := c.buildGroupImageStorePacket(groupCode, fh[:], int32(length)) r, err := c.sendAndWait(seq, pkt) if err != nil { @@ -109,9 +105,8 @@ func (c *QQClient) UploadPrivateImage(target int64, img io.ReadSeeker) (*message func (c *QQClient) uploadPrivateImage(target int64, img io.ReadSeeker, count int) (*message.FriendImageElement, error) { count++ - h := md5.New() - length, _ := io.Copy(h, img) - fh := h.Sum(nil) + fh, length := utils.ComputeMd5AndLength(img) + _, _ = img.Seek(0, io.SeekStart) e, err := c.QueryFriendImage(target, fh[:], int32(length)) if errors.Is(err, ErrNotExists) { // use group highway upload and query again for image id. diff --git a/client/ptt.go b/client/ptt.go index 08bf1614..786c4cee 100644 --- a/client/ptt.go +++ b/client/ptt.go @@ -92,8 +92,8 @@ ok: // UploadGroupShortVideo 将视频和封面上传到服务器, 返回 message.ShortVideoElement 可直接发送 // combinedCache 本地文件缓存, 设置后可多线程上传 func (c *QQClient) UploadGroupShortVideo(groupCode int64, video, thumb io.ReadSeeker, combinedCache ...string) (*message.ShortVideoElement, error) { - videoHash, videoLen := utils.GetMd5AndLength(video) - thumbHash, thumbLen := utils.GetMd5AndLength(thumb) + videoHash, videoLen := utils.ComputeMd5AndLength(video) + thumbHash, thumbLen := utils.ComputeMd5AndLength(thumb) cache := "" if len(combinedCache) > 0 { cache = combinedCache[0] diff --git a/utils/sys.go b/utils/sys.go index 9e5ae70d..1dd3d6f8 100644 --- a/utils/sys.go +++ b/utils/sys.go @@ -24,7 +24,7 @@ func IsChanClosed(ch interface{}) bool { return *(*uint32)(unsafe.Pointer(ptr)) > 0 } -func GetMd5AndLength(r io.Reader) ([]byte, int64) { +func ComputeMd5AndLength(r io.Reader) ([]byte, int64) { h := md5.New() length, _ := io.Copy(h, r) fh := h.Sum(nil)