1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +08:00

move mime to internal package

This commit is contained in:
wdvxdr 2022-08-31 16:31:47 +08:00
parent 65b050c781
commit 98712bf9ca
5 changed files with 15 additions and 32 deletions

View File

@ -23,6 +23,7 @@ import (
"github.com/Mrs4s/go-cqhttp/db" "github.com/Mrs4s/go-cqhttp/db"
"github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/internal/base" "github.com/Mrs4s/go-cqhttp/internal/base"
"github.com/Mrs4s/go-cqhttp/internal/mime"
) )
// CQBot CQBot结构体,存储Bot实例相关配置 // CQBot CQBot结构体,存储Bot实例相关配置
@ -152,10 +153,9 @@ func (bot *CQBot) uploadLocalImage(target message.Source, img *LocalImageElement
defer func() { _ = f.Close() }() defer func() { _ = f.Close() }()
img.Stream = f img.Stream = f
} }
if lawful, mime := base.IsLawfulImage(img.Stream); !lawful { if mt, ok := mime.CheckImage(img.Stream); !ok {
return nil, errors.New("image type error: " + mime) 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) i, err := bot.Client.UploadImage(target, img.Stream, 4)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -28,6 +28,7 @@ import (
"github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/internal/base" "github.com/Mrs4s/go-cqhttp/internal/base"
"github.com/Mrs4s/go-cqhttp/internal/cache" "github.com/Mrs4s/go-cqhttp/internal/cache"
"github.com/Mrs4s/go-cqhttp/internal/mime"
"github.com/Mrs4s/go-cqhttp/internal/param" "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 return nil, err
} }
if !global.IsAMRorSILK(data) { if !global.IsAMRorSILK(data) {
lawful, mt := base.IsLawfulAudio(bytes.NewReader(data)) mt, ok := mime.CheckAudio(bytes.NewReader(data))
if !lawful { if !ok {
return nil, errors.New("audio type error: " + mt) return nil, errors.New("audio type error: " + mt)
} }
data, err = global.EncoderSilk(data) data, err = global.EncoderSilk(data)

View File

@ -1,8 +1,6 @@
package base package base
import ( import (
"io"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -19,13 +17,3 @@ func encodeSilk(_ []byte, _ string) ([]byte, error) {
func resampleSilk(data []byte) []byte { func resampleSilk(data []byte) []byte {
return data return data
} }
// Mime scan feature
var (
IsLawfulImage = nocheck // 检查图片MIME
IsLawfulAudio = nocheck // 检查音频MIME
)
func nocheck(_ io.ReadSeeker) (bool, string) {
return true, ""
}

View File

@ -9,11 +9,6 @@ import (
"github.com/Mrs4s/go-cqhttp/internal/base" "github.com/Mrs4s/go-cqhttp/internal/base"
) )
func init() {
base.IsLawfulImage = checkImage
base.IsLawfulAudio = checkAudio
}
const limit = 4 * 1024 const limit = 4 * 1024
func scan(r io.ReadSeeker) string { func scan(r io.ReadSeeker) string {
@ -24,15 +19,15 @@ func scan(r io.ReadSeeker) string {
return http.DetectContentType(in) return http.DetectContentType(in)
} }
// checkImage 判断给定流是否为合法图片 // CheckImage 判断给定流是否为合法图片
// 返回 是否合法, 实际Mime // 返回 是否合法, 实际Mime
// 判断后会自动将 Stream Seek 至 0 // 判断后会自动将 Stream Seek 至 0
func checkImage(r io.ReadSeeker) (ok bool, t string) { func CheckImage(r io.ReadSeeker) (t string, ok bool) {
if base.SkipMimeScan { if base.SkipMimeScan {
return true, "" return "", true
} }
if r == nil { if r == nil {
return false, "image/nil-stream" return "image/nil-stream", false
} }
t = scan(r) t = scan(r)
switch t { switch t {
@ -42,15 +37,15 @@ func checkImage(r io.ReadSeeker) (ok bool, t string) {
return return
} }
// checkImage 判断给定流是否为合法音频 // CheckAudio 判断给定流是否为合法音频
func checkAudio(r io.ReadSeeker) (bool, string) { func CheckAudio(r io.ReadSeeker) (string, bool) {
if base.SkipMimeScan { if base.SkipMimeScan {
return true, "" return "", true
} }
t := scan(r) t := scan(r)
// std mime type detection is not full supported for audio // std mime type detection is not full supported for audio
if strings.Contains(t, "text") || strings.Contains(t, "image") { if strings.Contains(t, "text") || strings.Contains(t, "image") {
return false, t return t, false
} }
return true, t return t, true
} }

View File

@ -5,7 +5,6 @@ import (
"github.com/Mrs4s/go-cqhttp/cmd/gocq" "github.com/Mrs4s/go-cqhttp/cmd/gocq"
_ "github.com/Mrs4s/go-cqhttp/db/leveldb" // leveldb _ "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/modules/silk" // silk编码模块
// 其他模块 // 其他模块
// _ "github.com/Mrs4s/go-cqhttp/db/mongodb" // mongodb 数据库支持 // _ "github.com/Mrs4s/go-cqhttp/db/mongodb" // mongodb 数据库支持