diff --git a/coolq/bot.go b/coolq/bot.go index 126de63..de84131 100644 --- a/coolq/bot.go +++ b/coolq/bot.go @@ -23,6 +23,7 @@ import ( "github.com/Mrs4s/go-cqhttp/db" "github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/internal/base" + "github.com/Mrs4s/go-cqhttp/internal/mime" ) // CQBot CQBot结构体,存储Bot实例相关配置 @@ -152,10 +153,9 @@ func (bot *CQBot) uploadLocalImage(target message.Source, img *LocalImageElement defer func() { _ = f.Close() }() img.Stream = f } - if lawful, mime := base.IsLawfulImage(img.Stream); !lawful { - return nil, errors.New("image type error: " + mime) + if mt, ok := mime.CheckImage(img.Stream); !ok { + return nil, errors.New("image type error: " + mt) } - // todo: enable multi-thread upload, now got error code 81 i, err := bot.Client.UploadImage(target, img.Stream, 4) if err != nil { return nil, err diff --git a/coolq/cqcode.go b/coolq/cqcode.go index 283213b..2c3bc65 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -28,6 +28,7 @@ import ( "github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/internal/base" "github.com/Mrs4s/go-cqhttp/internal/cache" + "github.com/Mrs4s/go-cqhttp/internal/mime" "github.com/Mrs4s/go-cqhttp/internal/param" ) @@ -844,8 +845,8 @@ func (bot *CQBot) ToElement(t string, d map[string]string, sourceType message.So return nil, err } if !global.IsAMRorSILK(data) { - lawful, mt := base.IsLawfulAudio(bytes.NewReader(data)) - if !lawful { + mt, ok := mime.CheckAudio(bytes.NewReader(data)) + if !ok { return nil, errors.New("audio type error: " + mt) } data, err = global.EncoderSilk(data) diff --git a/internal/base/feature.go b/internal/base/feature.go index f27c0a0..d57a9aa 100644 --- a/internal/base/feature.go +++ b/internal/base/feature.go @@ -1,8 +1,6 @@ package base import ( - "io" - "github.com/pkg/errors" ) @@ -19,13 +17,3 @@ func encodeSilk(_ []byte, _ string) ([]byte, error) { func resampleSilk(data []byte) []byte { return data } - -// Mime scan feature -var ( - IsLawfulImage = nocheck // 检查图片MIME - IsLawfulAudio = nocheck // 检查音频MIME -) - -func nocheck(_ io.ReadSeeker) (bool, string) { - return true, "" -} diff --git a/modules/mime/mime.go b/internal/mime/mime.go similarity index 65% rename from modules/mime/mime.go rename to internal/mime/mime.go index f6bfa4b..c8765bd 100644 --- a/modules/mime/mime.go +++ b/internal/mime/mime.go @@ -9,11 +9,6 @@ import ( "github.com/Mrs4s/go-cqhttp/internal/base" ) -func init() { - base.IsLawfulImage = checkImage - base.IsLawfulAudio = checkAudio -} - const limit = 4 * 1024 func scan(r io.ReadSeeker) string { @@ -24,15 +19,15 @@ func scan(r io.ReadSeeker) string { return http.DetectContentType(in) } -// checkImage 判断给定流是否为合法图片 +// CheckImage 判断给定流是否为合法图片 // 返回 是否合法, 实际Mime // 判断后会自动将 Stream Seek 至 0 -func checkImage(r io.ReadSeeker) (ok bool, t string) { +func CheckImage(r io.ReadSeeker) (t string, ok bool) { if base.SkipMimeScan { - return true, "" + return "", true } if r == nil { - return false, "image/nil-stream" + return "image/nil-stream", false } t = scan(r) switch t { @@ -42,15 +37,15 @@ func checkImage(r io.ReadSeeker) (ok bool, t string) { return } -// checkImage 判断给定流是否为合法音频 -func checkAudio(r io.ReadSeeker) (bool, string) { +// CheckAudio 判断给定流是否为合法音频 +func CheckAudio(r io.ReadSeeker) (string, bool) { if base.SkipMimeScan { - return true, "" + return "", true } t := scan(r) // std mime type detection is not full supported for audio if strings.Contains(t, "text") || strings.Contains(t, "image") { - return false, t + return t, false } - return true, t + return t, true } diff --git a/main.go b/main.go index fa75ded..1d79704 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "github.com/Mrs4s/go-cqhttp/cmd/gocq" _ "github.com/Mrs4s/go-cqhttp/db/leveldb" // leveldb - _ "github.com/Mrs4s/go-cqhttp/modules/mime" // mime检查模块 _ "github.com/Mrs4s/go-cqhttp/modules/silk" // silk编码模块 // 其他模块 // _ "github.com/Mrs4s/go-cqhttp/db/mongodb" // mongodb 数据库支持