From e03a989083a7e162205ca9adec5515358c0e051e Mon Sep 17 00:00:00 2001 From: Ink33 <51873347+Ink-33@users.noreply.github.com> Date: Sat, 9 Jan 2021 20:38:33 +0800 Subject: [PATCH 01/11] Fix misspell --- global/fs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global/fs.go b/global/fs.go index fe6fbea..4a00ffb 100644 --- a/global/fs.go +++ b/global/fs.go @@ -216,7 +216,7 @@ func UpdateFromStream(updateWith io.Reader) (err error, errRecover error) { return } - // move the new exectuable in to become the new program + // move the new executable in to become the new program err = os.Rename(newPath, updatePath) if err != nil { From ef713bc59cc1edd75407ddaef7cc66b516606265 Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Mon, 11 Jan 2021 14:36:29 +0800 Subject: [PATCH 02/11] fix show pic error. --- coolq/cqcode.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coolq/cqcode.go b/coolq/cqcode.go index 7cd8c9b..171c92f 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -1039,7 +1039,7 @@ func (bot *CQBot) makeShowPic(elem message.IMessageElement, source string, icon var suf message.IMessageElement if i, ok := elem.(*LocalImageElement); ok { if group == false { - gm, err := bot.Client.UploadPrivateImage(1, i.Stream) + gm, err := bot.UploadLocalImageAsPrivate(1, i) if err != nil { log.Warnf("警告: 好友消息 %v 消息图片上传失败: %v", 1, err) return nil, err @@ -1047,7 +1047,7 @@ func (bot *CQBot) makeShowPic(elem message.IMessageElement, source string, icon suf = gm xml = fmt.Sprintf(``, "", gm.Md5, gm.Md5, len(i.Data), "", minWidth, minHeight, maxWidth, maxHeight, source, icon) } else { - gm, err := bot.Client.UploadGroupImage(1, i.Stream) + gm, err := bot.UploadLocalImageAsGroup(1, i) if err != nil { log.Warnf("警告: 群 %v 消息图片上传失败: %v", 1, err) return nil, err From 1f7dcc4e918c0bd57efa1c3c803df452d908f271 Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Mon, 11 Jan 2021 16:58:07 +0800 Subject: [PATCH 03/11] fix forward img & video upload. --- coolq/api.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/coolq/api.go b/coolq/api.go index 3421903..69dcc3f 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -303,7 +303,7 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupId int64, m gjson.Result) MSG { var newElem []message.IMessageElement for _, elem := range content { if img, ok := elem.(*LocalImageElement); ok { - gm, err := bot.Client.UploadGroupImage(groupId, img.Stream) + gm, err := bot.UploadLocalImageAsGroup(groupId, img) if err != nil { log.Warnf("警告:群 %v 图片上传失败: %v", groupId, err) continue @@ -311,6 +311,15 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupId int64, m gjson.Result) MSG { newElem = append(newElem, gm) continue } + if video, ok := elem.(*LocalVideoElement); ok { + gm, err := bot.UploadLocalVideo(groupId, video) + if err != nil { + log.Warnf("警告:群 %v 视频上传失败: %v", groupId, err) + continue + } + newElem = append(newElem, gm) + continue + } newElem = append(newElem, elem) } nodes = append(nodes, &message.ForwardNode{ From 0c3ba714f7fc93614779e87966cb6e7388ea57b5 Mon Sep 17 00:00:00 2001 From: Ink-33 Date: Wed, 13 Jan 2021 03:52:49 +0800 Subject: [PATCH 04/11] fix #558 --- global/codec/codec.go | 2 +- global/codec/codec2.go | 14 -------------- 2 files changed, 1 insertion(+), 15 deletions(-) delete mode 100644 global/codec/codec2.go diff --git a/global/codec/codec.go b/global/codec/codec.go index 5f7d2d4..c6dcd1a 100644 --- a/global/codec/codec.go +++ b/global/codec/codec.go @@ -1,5 +1,5 @@ // +build linux windows darwin -// +build 386 amd64 arm +// +build 386 amd64 arm arm64 package codec diff --git a/global/codec/codec2.go b/global/codec/codec2.go deleted file mode 100644 index e50133e..0000000 --- a/global/codec/codec2.go +++ /dev/null @@ -1,14 +0,0 @@ -// +build !linux,!windows,!darwin -// +build !386,!amd64,!arm - -package codec - -import "errors" - -func Init() error { - return errors.New("not support now") -} - -func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) { - return nil, errors.New("not support now") -} From c9c6e48b8c466b2e5e07eab3846f8c6dc6a48cb7 Mon Sep 17 00:00:00 2001 From: Ink-33 Date: Wed, 13 Jan 2021 03:55:28 +0800 Subject: [PATCH 05/11] add codec/codec_unsupported.go --- global/codec/codec_unsupportedarch.go | 13 +++++++++++++ global/codec/codec_unsupportedos.go | 13 +++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 global/codec/codec_unsupportedarch.go create mode 100644 global/codec/codec_unsupportedos.go diff --git a/global/codec/codec_unsupportedarch.go b/global/codec/codec_unsupportedarch.go new file mode 100644 index 0000000..f905017 --- /dev/null +++ b/global/codec/codec_unsupportedarch.go @@ -0,0 +1,13 @@ +// +build !386,!arm64,!amd64,!arm + +package codec + +import "errors" + +func Init() error { + return errors.New("Unsupport arch now") +} + +func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) { + return nil, errors.New("Unsupport arch now") +} diff --git a/global/codec/codec_unsupportedos.go b/global/codec/codec_unsupportedos.go new file mode 100644 index 0000000..89c415e --- /dev/null +++ b/global/codec/codec_unsupportedos.go @@ -0,0 +1,13 @@ +// +build !windows,!linux,!darwin + +package codec + +import "errors" + +func Init() error { + return errors.New("not support now") +} + +func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) { + return nil, errors.New("not support now") +} From 7ac31a8b762157b7d1f756a330f20b800cf188ea Mon Sep 17 00:00:00 2001 From: Ink-33 Date: Wed, 13 Jan 2021 04:00:01 +0800 Subject: [PATCH 06/11] Update github action --- .github/workflows/ci.yml | 2 +- .github/workflows/release.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7c7a90c..1df0747 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: matrix: # build and publish in parallel: linux/386, linux/amd64, windows/386, windows/amd64, darwin/amd64 goos: [linux, windows, darwin] - goarch: ["386", amd64, arm] + goarch: ["386", amd64, arm, arm64] exclude: - goos: darwin goarch: arm diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 93fc465..d8b61de 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -10,7 +10,7 @@ jobs: matrix: # build and publish in parallel: linux/386, linux/amd64, windows/386, windows/amd64, darwin/386, darwin/amd64 goos: [linux, windows, darwin] - goarch: ["386", amd64, arm] + goarch: ["386", amd64, arm, arm64] exclude: - goos: darwin goarch: arm @@ -30,4 +30,4 @@ jobs: goarch: ${{ matrix.goarch }} goversion: "https://golang.org/dl/go1.15.3.linux-amd64.tar.gz" ldflags: -w -s -X "github.com/Mrs4s/go-cqhttp/coolq.Version=${{ env.RELEASE_VERSION }}" - \ No newline at end of file + From 0cd06daf088e9b3be5f1d82d7283c3fbcfe59371 Mon Sep 17 00:00:00 2001 From: Ink-33 Date: Wed, 13 Jan 2021 04:05:50 +0800 Subject: [PATCH 07/11] Update github action --- .github/workflows/ci.yml | 4 ++++ .github/workflows/release.yml | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1df0747..336a3cc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,8 +20,12 @@ jobs: exclude: - goos: darwin goarch: arm + - goos: darwin + goarch: arm64 - goos: darwin goarch: "386" + - goos: windows + goarch: arm64 fail-fast: true steps: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d8b61de..589d51a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,8 +14,12 @@ jobs: exclude: - goos: darwin goarch: arm + - goos: darwin + goarch: arm64 - goos: darwin goarch: "386" + - goos: windows + goarch: arm64 steps: - uses: actions/checkout@v2 From b075280d2b75aaf914c1e3ba55aed04dd5c28bc1 Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Wed, 13 Jan 2021 11:22:59 +0800 Subject: [PATCH 08/11] fix build --- global/codec/codec_unsupportedarch.go | 2 +- global/codec/codec_unsupportedos.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/global/codec/codec_unsupportedarch.go b/global/codec/codec_unsupportedarch.go index f905017..62dfc5a 100644 --- a/global/codec/codec_unsupportedarch.go +++ b/global/codec/codec_unsupportedarch.go @@ -4,7 +4,7 @@ package codec import "errors" -func Init() error { +func Init(cachePath, codecPath string) error { return errors.New("Unsupport arch now") } diff --git a/global/codec/codec_unsupportedos.go b/global/codec/codec_unsupportedos.go index 89c415e..c045d14 100644 --- a/global/codec/codec_unsupportedos.go +++ b/global/codec/codec_unsupportedos.go @@ -4,7 +4,7 @@ package codec import "errors" -func Init() error { +func Init(cachePath, codecPath string) error { return errors.New("not support now") } From de4de5052d2539efb6f1af72fe447d514e8ca56d Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Wed, 13 Jan 2021 11:43:39 +0800 Subject: [PATCH 09/11] fix #557 & remove some goto --- coolq/api.go | 2 +- coolq/cqcode.go | 52 ++++++++++++++++++++++--------------------------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/coolq/api.go b/coolq/api.go index 69dcc3f..c6a3295 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -354,7 +354,7 @@ func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}, autoEscape b var str string if m, ok := i.(gjson.Result); ok { if m.Type == gjson.JSON { - elem := bot.ConvertObjectMessage(m, true) + elem := bot.ConvertObjectMessage(m, false) mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem}) if mid == -1 { return Failed(100, "SEND_MSG_API_ERROR", "请参考输出") diff --git a/coolq/cqcode.go b/coolq/cqcode.go index 171c92f..52866cd 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -895,9 +895,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) ( } hasCacheFile: if video { - return &LocalVideoElement{ - File: cacheFile, - }, nil + return &LocalVideoElement{File: cacheFile}, nil } return &LocalImageElement{File: cacheFile}, nil } @@ -917,21 +915,36 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) ( return nil, err } if video { - goto videos + if info.Size() == 0 || info.Size() >= maxVideoSize { + return nil, errors.New("invalid video size") + } + return &LocalVideoElement{File: fu.Path}, nil } if info.Size() == 0 || info.Size() >= maxImageSize { return nil, errors.New("invalid image size") } return &LocalImageElement{File: fu.Path}, nil - videos: - if info.Size() == 0 || info.Size() >= maxVideoSize { - return nil, errors.New("invalid video size") - } - return &LocalVideoElement{File: fu.Path}, nil } rawPath := path.Join(global.IMAGE_PATH, f) if video { - goto video + rawPath = path.Join(global.VIDEO_PATH, f) + if !global.PathExists(rawPath) { + return nil, errors.New("invalid video") + } + if path.Ext(rawPath) == ".video" { + b, _ := ioutil.ReadFile(rawPath) + r := binary.NewReader(b) + return &LocalVideoElement{ShortVideoElement: message.ShortVideoElement{ // todo 检查缓存是否有效 + Md5: r.ReadBytes(16), + ThumbMd5: r.ReadBytes(16), + Size: r.ReadInt32(), + ThumbSize: r.ReadInt32(), + Name: r.ReadString(), + Uuid: r.ReadAvailable(), + }}, nil + } else { + return &LocalVideoElement{File: rawPath}, nil + } } if strings.HasPrefix(f, "base64") { b, err := base64.StdEncoding.DecodeString(strings.ReplaceAll(f, "base64://", "")) @@ -1012,25 +1025,6 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) ( return rsp, nil } return nil, errors.New("invalid image") -video: - rawPath = path.Join(global.VIDEO_PATH, f) - if !global.PathExists(rawPath) { - return nil, errors.New("invalid video") - } - if path.Ext(rawPath) == ".video" { - b, _ := ioutil.ReadFile(rawPath) - r := binary.NewReader(b) - return &LocalVideoElement{ShortVideoElement: message.ShortVideoElement{ // todo 检查缓存是否有效 - Md5: r.ReadBytes(16), - ThumbMd5: r.ReadBytes(16), - Size: r.ReadInt32(), - ThumbSize: r.ReadInt32(), - Name: r.ReadString(), - Uuid: r.ReadAvailable(), - }}, nil - } else { - return &LocalVideoElement{File: rawPath}, nil - } } //makeShowPic 一种xml 方式发送的群消息图片 From 7a9a021c540ae45741cfcbda5158caa11a7d6a5f Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Wed, 13 Jan 2021 22:35:44 +0800 Subject: [PATCH 10/11] fix codec path --- global/codec/codec.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/global/codec/codec.go b/global/codec/codec.go index c6dcd1a..ef16cf7 100644 --- a/global/codec/codec.go +++ b/global/codec/codec.go @@ -14,7 +14,6 @@ import ( ) var ( - codecDir string encoderPath string cachePath string ) @@ -37,23 +36,16 @@ func downloadCodec(url string, path string) (err error) { } func Init(cachePath, codecPath string) error { - appPath, err := os.Executable() - appPath = path.Dir(appPath) - if err != nil { - return err - } - cachePath = path.Join(appPath, cachePath) - codecDir = path.Join(appPath, codecPath) - if !fileExist(codecDir) { - _ = os.MkdirAll(codecDir, os.ModePerm) + if !fileExist(codecPath) { + _ = os.MkdirAll(codecPath, os.ModePerm) } if !fileExist(cachePath) { _ = os.MkdirAll(cachePath, os.ModePerm) } encoderFile := runtime.GOOS + "-" + runtime.GOARCH + "-encoder" - encoderPath = path.Join(codecDir, encoderFile) + encoderPath = path.Join(codecPath, encoderFile) if !fileExist(encoderPath) { - if err = downloadCodec("https://cdn.jsdelivr.net/gh/wdvxdr1123/tosilk/codec/"+encoderFile, encoderPath); err != nil { + if err := downloadCodec("https://cdn.jsdelivr.net/gh/wdvxdr1123/tosilk/codec/"+encoderFile, encoderPath); err != nil { return errors.New("下载依赖失败") } } From 59cd986050025cc754bd46aff4784cfaa33a1d68 Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Wed, 13 Jan 2021 22:56:28 +0800 Subject: [PATCH 11/11] fix #561 --- global/codec.go | 2 +- global/codec/codec.go | 47 ++++++++++++++------------- global/codec/codec_unsupportedarch.go | 2 +- global/codec/codec_unsupportedos.go | 2 +- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/global/codec.go b/global/codec.go index 4f2648d..4bb9cfd 100644 --- a/global/codec.go +++ b/global/codec.go @@ -15,7 +15,7 @@ var useSilkCodec = true func InitCodec() { log.Info("正在加载silk编码器...") - err := codec.Init("data/cache", "codec") + err := codec.Init() if err != nil { log.Error(err) useSilkCodec = false diff --git a/global/codec/codec.go b/global/codec/codec.go index ef16cf7..695bbc9 100644 --- a/global/codec/codec.go +++ b/global/codec/codec.go @@ -13,16 +13,13 @@ import ( "runtime" ) -var ( - encoderPath string - cachePath string +const ( + silkCachePath = "data/cache" + encoderPath = "codec" ) -func downloadCodec(url string, path string) (err error) { +func downloadCodec(url string) (err error) { resp, err := http.Get(url) - if runtime.GOOS == "windows" { - path = path + ".exe" - } if err != nil { return } @@ -31,33 +28,37 @@ func downloadCodec(url string, path string) (err error) { if err != nil { return } - err = ioutil.WriteFile(path, body, os.ModePerm) + err = ioutil.WriteFile(getEncoderFilePath(), body, os.ModePerm) return } -func Init(cachePath, codecPath string) error { - if !fileExist(codecPath) { - _ = os.MkdirAll(codecPath, os.ModePerm) +func getEncoderFilePath() string { + encoderFile := path.Join(encoderPath, runtime.GOOS+"-"+runtime.GOARCH+"-encoder") + if runtime.GOOS == "windows" { + encoderFile = encoderFile + ".exe" } - if !fileExist(cachePath) { - _ = os.MkdirAll(cachePath, os.ModePerm) + return encoderFile +} + +func Init() error { + if !fileExist(silkCachePath) { + _ = os.MkdirAll(silkCachePath, os.ModePerm) } - encoderFile := runtime.GOOS + "-" + runtime.GOARCH + "-encoder" - encoderPath = path.Join(codecPath, encoderFile) if !fileExist(encoderPath) { - if err := downloadCodec("https://cdn.jsdelivr.net/gh/wdvxdr1123/tosilk/codec/"+encoderFile, encoderPath); err != nil { + _ = os.MkdirAll(encoderPath, os.ModePerm) + } + p := getEncoderFilePath() + if !fileExist(p) { + if err := downloadCodec("https://cdn.jsdelivr.net/gh/wdvxdr1123/tosilk/codec/" + runtime.GOOS + "-" + runtime.GOARCH + "-encoder"); err != nil { return errors.New("下载依赖失败") } } - if runtime.GOOS == "windows" { - encoderPath = encoderPath + ".exe" - } return nil } func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) { // 1. 写入缓存文件 - rawPath := path.Join(cachePath, tempName+".wav") + rawPath := path.Join(silkCachePath, tempName+".wav") err := ioutil.WriteFile(rawPath, record, os.ModePerm) if err != nil { return nil, err @@ -65,7 +66,7 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) defer os.Remove(rawPath) // 2.转换pcm - pcmPath := path.Join(cachePath, tempName+".pcm") + pcmPath := path.Join(silkCachePath, tempName+".pcm") cmd := exec.Command("ffmpeg", "-i", rawPath, "-f", "s16le", "-ar", "24000", "-ac", "1", pcmPath) if err = cmd.Run(); err != nil { return nil, err @@ -73,8 +74,8 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) defer os.Remove(pcmPath) // 3. 转silk - silkPath := path.Join(cachePath, tempName+".silk") - cmd = exec.Command(encoderPath, pcmPath, silkPath, "-rate", "24000", "-quiet", "-tencent") + silkPath := path.Join(silkCachePath, tempName+".silk") + cmd = exec.Command(getEncoderFilePath(), pcmPath, silkPath, "-rate", "24000", "-quiet", "-tencent") if err = cmd.Run(); err != nil { return nil, err } diff --git a/global/codec/codec_unsupportedarch.go b/global/codec/codec_unsupportedarch.go index 62dfc5a..f905017 100644 --- a/global/codec/codec_unsupportedarch.go +++ b/global/codec/codec_unsupportedarch.go @@ -4,7 +4,7 @@ package codec import "errors" -func Init(cachePath, codecPath string) error { +func Init() error { return errors.New("Unsupport arch now") } diff --git a/global/codec/codec_unsupportedos.go b/global/codec/codec_unsupportedos.go index c045d14..89c415e 100644 --- a/global/codec/codec_unsupportedos.go +++ b/global/codec/codec_unsupportedos.go @@ -4,7 +4,7 @@ package codec import "errors" -func Init(cachePath, codecPath string) error { +func Init() error { return errors.New("not support now") }