mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 03:23:49 +08:00
refactor: new package modules
move silk, mime to modules
This commit is contained in:
parent
883fca089d
commit
5acb01c1a3
51
coolq/bot.go
51
coolq/bot.go
@ -14,8 +14,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gabriel-vasile/mimetype"
|
|
||||||
|
|
||||||
"github.com/Mrs4s/MiraiGo/binary"
|
"github.com/Mrs4s/MiraiGo/binary"
|
||||||
"github.com/Mrs4s/MiraiGo/client"
|
"github.com/Mrs4s/MiraiGo/client"
|
||||||
"github.com/Mrs4s/MiraiGo/message"
|
"github.com/Mrs4s/MiraiGo/message"
|
||||||
@ -70,29 +68,6 @@ func (e *Event) JSONString() string {
|
|||||||
return utils.B2S(e.buffer.Bytes())
|
return utils.B2S(e.buffer.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
// keep sync with /docs/file.md#MINE
|
|
||||||
var lawfulImageTypes = [...]string{
|
|
||||||
"image/bmp",
|
|
||||||
"image/gif",
|
|
||||||
"image/jpeg",
|
|
||||||
"image/png",
|
|
||||||
"image/webp",
|
|
||||||
}
|
|
||||||
|
|
||||||
var lawfulAudioTypes = [...]string{
|
|
||||||
"audio/aac",
|
|
||||||
"audio/aiff",
|
|
||||||
"audio/amr",
|
|
||||||
"audio/ape",
|
|
||||||
"audio/flac",
|
|
||||||
"audio/midi",
|
|
||||||
"audio/mp4",
|
|
||||||
"audio/mpeg",
|
|
||||||
"audio/ogg",
|
|
||||||
"audio/wav",
|
|
||||||
"audio/x-m4a",
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewQQBot 初始化一个QQBot实例
|
// NewQQBot 初始化一个QQBot实例
|
||||||
func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot {
|
func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot {
|
||||||
bot := &CQBot{
|
bot := &CQBot{
|
||||||
@ -203,7 +178,7 @@ func (bot *CQBot) UploadLocalImageAsGroup(groupCode int64, img *LocalImageElemen
|
|||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
img.Stream = f
|
img.Stream = f
|
||||||
}
|
}
|
||||||
if lawful, mime := IsLawfulImage(img.Stream); !lawful {
|
if lawful, mime := base.IsLawfulImage(img.Stream); !lawful {
|
||||||
return nil, errors.New("image type error: " + mime)
|
return nil, errors.New("image type error: " + mime)
|
||||||
}
|
}
|
||||||
i, err = bot.Client.UploadGroupImage(groupCode, img.Stream)
|
i, err = bot.Client.UploadGroupImage(groupCode, img.Stream)
|
||||||
@ -238,7 +213,7 @@ func (bot *CQBot) UploadLocalImageAsPrivate(userID int64, img *LocalImageElement
|
|||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
img.Stream = f
|
img.Stream = f
|
||||||
}
|
}
|
||||||
if lawful, mime := IsLawfulImage(img.Stream); !lawful {
|
if lawful, mime := base.IsLawfulImage(img.Stream); !lawful {
|
||||||
return nil, errors.New("image type error: " + mime)
|
return nil, errors.New("image type error: " + mime)
|
||||||
}
|
}
|
||||||
i, err = bot.Client.UploadPrivateImage(userID, img.Stream)
|
i, err = bot.Client.UploadPrivateImage(userID, img.Stream)
|
||||||
@ -609,25 +584,3 @@ func (bot *CQBot) uploadMedia(raw message.IMessageElement, target int64, group b
|
|||||||
}
|
}
|
||||||
return nil, errors.New("unsupported message element type")
|
return nil, errors.New("unsupported message element type")
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsLawfulImage 判断给定流是否为合法图片
|
|
||||||
// 返回 是否合法, 实际Mime
|
|
||||||
// 判断后会自动将 Stream Seek 至 0
|
|
||||||
func IsLawfulImage(r io.ReadSeeker) (bool, string) {
|
|
||||||
if base.SkipMimeScan {
|
|
||||||
return true, ""
|
|
||||||
}
|
|
||||||
_, _ = r.Seek(0, io.SeekStart)
|
|
||||||
defer func() { _, _ = r.Seek(0, io.SeekStart) }()
|
|
||||||
t, err := mimetype.DetectReader(r)
|
|
||||||
if err != nil {
|
|
||||||
log.Debugf("扫描 Mime 时出现问题: %v", err)
|
|
||||||
return false, ""
|
|
||||||
}
|
|
||||||
for _, lt := range lawfulImageTypes {
|
|
||||||
if t.Is(lt) {
|
|
||||||
return true, t.String()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false, t.String()
|
|
||||||
}
|
|
||||||
|
@ -20,12 +20,10 @@ import (
|
|||||||
"github.com/Mrs4s/MiraiGo/binary"
|
"github.com/Mrs4s/MiraiGo/binary"
|
||||||
"github.com/Mrs4s/MiraiGo/message"
|
"github.com/Mrs4s/MiraiGo/message"
|
||||||
"github.com/Mrs4s/MiraiGo/utils"
|
"github.com/Mrs4s/MiraiGo/utils"
|
||||||
"github.com/gabriel-vasile/mimetype"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
"github.com/Mrs4s/go-cqhttp/global"
|
"github.com/Mrs4s/go-cqhttp/global"
|
||||||
"github.com/Mrs4s/go-cqhttp/global/codec"
|
|
||||||
"github.com/Mrs4s/go-cqhttp/internal/base"
|
"github.com/Mrs4s/go-cqhttp/internal/base"
|
||||||
"github.com/Mrs4s/go-cqhttp/internal/param"
|
"github.com/Mrs4s/go-cqhttp/internal/param"
|
||||||
)
|
)
|
||||||
@ -715,7 +713,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &message.VoiceElement{Data: codec.RecodeTo24K(data)}, nil
|
return &message.VoiceElement{Data: base.ResampleSilk(data)}, nil
|
||||||
case "record":
|
case "record":
|
||||||
f := d["file"]
|
f := d["file"]
|
||||||
data, err := global.FindFile(f, d["cache"], global.VoicePath)
|
data, err := global.FindFile(f, d["cache"], global.VoicePath)
|
||||||
@ -725,17 +723,10 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if !base.SkipMimeScan && !global.IsAMRorSILK(data) {
|
if !global.IsAMRorSILK(data) {
|
||||||
mt := mimetype.Detect(data)
|
lawful, mt := base.IsLawfulAudio(bytes.NewReader(data))
|
||||||
lawful := false
|
|
||||||
for _, lt := range lawfulAudioTypes {
|
|
||||||
if mt.Is(lt) {
|
|
||||||
lawful = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !lawful {
|
if !lawful {
|
||||||
return nil, errors.New("audio type error: " + mt.String())
|
return nil, errors.New("audio type error: " + mt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !global.IsAMRorSILK(data) {
|
if !global.IsAMRorSILK(data) {
|
||||||
|
@ -7,9 +7,9 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"github.com/Mrs4s/go-cqhttp/global/codec"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"github.com/Mrs4s/go-cqhttp/internal/base"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EncoderSilk 将音频编码为Silk
|
// EncoderSilk 将音频编码为Silk
|
||||||
@ -23,7 +23,7 @@ func EncoderSilk(data []byte) ([]byte, error) {
|
|||||||
if silkPath := path.Join("data/cache", tempName+".silk"); PathExists(silkPath) {
|
if silkPath := path.Join("data/cache", tempName+".silk"); PathExists(silkPath) {
|
||||||
return os.ReadFile(silkPath)
|
return os.ReadFile(silkPath)
|
||||||
}
|
}
|
||||||
slk, err := codec.EncodeToSilk(data, tempName, true)
|
slk, err := base.EncodeSilk(data, tempName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "encode silk failed")
|
return nil, errors.Wrap(err, "encode silk failed")
|
||||||
}
|
}
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
// Package codec Slik编码核心模块
|
|
||||||
package codec
|
|
31
internal/base/feature.go
Normal file
31
internal/base/feature.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package base
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// silk encode features
|
||||||
|
var (
|
||||||
|
EncodeSilk = encodeSilk // 编码 SilkV3 音频
|
||||||
|
ResampleSilk = resampleSilk // 将silk重新编码为 24000 bit rate
|
||||||
|
)
|
||||||
|
|
||||||
|
func encodeSilk(_ []byte, _ string) ([]byte, error) {
|
||||||
|
return nil, errors.New("not supported now")
|
||||||
|
}
|
||||||
|
|
||||||
|
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, ""
|
||||||
|
}
|
3
main.go
3
main.go
@ -23,6 +23,9 @@ import (
|
|||||||
"golang.org/x/crypto/pbkdf2"
|
"golang.org/x/crypto/pbkdf2"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
|
|
||||||
|
_ "github.com/Mrs4s/go-cqhttp/modules/mime" // mime检查模块
|
||||||
|
_ "github.com/Mrs4s/go-cqhttp/modules/silk" // silk编码模块
|
||||||
|
|
||||||
"github.com/Mrs4s/go-cqhttp/coolq"
|
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||||
"github.com/Mrs4s/go-cqhttp/global"
|
"github.com/Mrs4s/go-cqhttp/global"
|
||||||
"github.com/Mrs4s/go-cqhttp/global/config"
|
"github.com/Mrs4s/go-cqhttp/global/config"
|
||||||
|
70
modules/mime/mime.go
Normal file
70
modules/mime/mime.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
// Package mime 提供MIME检查功能
|
||||||
|
package mime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/gabriel-vasile/mimetype"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/Mrs4s/go-cqhttp/internal/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
base.IsLawfulAudio = checkImage
|
||||||
|
base.IsLawfulAudio = checkAudio
|
||||||
|
}
|
||||||
|
|
||||||
|
// keep sync with /docs/file.md#MINE
|
||||||
|
var lawfulImage = [...]string{
|
||||||
|
"image/bmp",
|
||||||
|
"image/gif",
|
||||||
|
"image/jpeg",
|
||||||
|
"image/png",
|
||||||
|
"image/webp",
|
||||||
|
}
|
||||||
|
|
||||||
|
var lawfulAudio = [...]string{
|
||||||
|
"audio/aac",
|
||||||
|
"audio/aiff",
|
||||||
|
"audio/amr",
|
||||||
|
"audio/ape",
|
||||||
|
"audio/flac",
|
||||||
|
"audio/midi",
|
||||||
|
"audio/mp4",
|
||||||
|
"audio/mpeg",
|
||||||
|
"audio/ogg",
|
||||||
|
"audio/wav",
|
||||||
|
"audio/x-m4a",
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(r io.ReadSeeker, list []string) (bool, string) {
|
||||||
|
if base.SkipMimeScan {
|
||||||
|
return true, ""
|
||||||
|
}
|
||||||
|
_, _ = r.Seek(0, io.SeekStart)
|
||||||
|
defer r.Seek(0, io.SeekStart) // nolint
|
||||||
|
t, err := mimetype.DetectReader(r)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Debugf("扫描 Mime 时出现问题: %v", err)
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
for _, lt := range list {
|
||||||
|
if t.Is(lt) {
|
||||||
|
return true, t.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, t.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkImage 判断给定流是否为合法图片
|
||||||
|
// 返回 是否合法, 实际Mime
|
||||||
|
// 判断后会自动将 Stream Seek 至 0
|
||||||
|
func checkImage(r io.ReadSeeker) (bool, string) {
|
||||||
|
return check(r, lawfulImage[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkImage 判断给定流是否为合法音频
|
||||||
|
func checkAudio(r io.ReadSeeker) (bool, string) {
|
||||||
|
return check(r, lawfulAudio[:])
|
||||||
|
}
|
@ -4,7 +4,7 @@
|
|||||||
// +build !race
|
// +build !race
|
||||||
// +build !nosilk
|
// +build !nosilk
|
||||||
|
|
||||||
package codec
|
package silk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
@ -19,8 +19,8 @@ import (
|
|||||||
|
|
||||||
const silkCachePath = "data/cache"
|
const silkCachePath = "data/cache"
|
||||||
|
|
||||||
// EncodeToSilk 将音频编码为Silk
|
// encode 将音频编码为Silk
|
||||||
func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte, err error) {
|
func encode(record []byte, tempName string) (silkWav []byte, err error) {
|
||||||
// 1. 写入缓存文件
|
// 1. 写入缓存文件
|
||||||
rawPath := path.Join(silkCachePath, tempName+".wav")
|
rawPath := path.Join(silkCachePath, tempName+".wav")
|
||||||
err = os.WriteFile(rawPath, record, os.ModePerm)
|
err = os.WriteFile(rawPath, record, os.ModePerm)
|
||||||
@ -50,15 +50,13 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "silk encode error")
|
return nil, errors.Wrap(err, "silk encode error")
|
||||||
}
|
}
|
||||||
if useCache {
|
|
||||||
silkPath := path.Join(silkCachePath, tempName+".silk")
|
silkPath := path.Join(silkCachePath, tempName+".silk")
|
||||||
err = os.WriteFile(silkPath, silkWav, 0o666)
|
err = os.WriteFile(silkPath, silkWav, 0o666)
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// RecodeTo24K 将silk重新编码为 24000 bit rate
|
// resample 将silk重新编码为 24000 bit rate
|
||||||
func RecodeTo24K(data []byte) []byte {
|
func resample(data []byte) []byte {
|
||||||
pcm, err := silk.DecodeSilkBuffToPcm(data, 24000)
|
pcm, err := silk.DecodeSilkBuffToPcm(data, 24000)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
@ -1,16 +1,16 @@
|
|||||||
//go:build (!arm && !arm64 && !amd64 && !386) || (!windows && !linux && !darwin) || (windows && arm) || (windows && arm64) || race || nosilk
|
//go:build (!arm && !arm64 && !amd64 && !386) || (!windows && !linux && !darwin) || (windows && arm) || (windows && arm64) || race || nosilk
|
||||||
// +build !arm,!arm64,!amd64,!386 !windows,!linux,!darwin windows,arm windows,arm64 race nosilk
|
// +build !arm,!arm64,!amd64,!386 !windows,!linux,!darwin windows,arm windows,arm64 race nosilk
|
||||||
|
|
||||||
package codec
|
package silk
|
||||||
|
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
// EncodeToSilk 将音频编码为Silk
|
// encode 将音频编码为Silk
|
||||||
func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) {
|
func encode(record []byte, tempName string, useCache bool) ([]byte, error) {
|
||||||
return nil, errors.New("not supported now")
|
return nil, errors.New("not supported now")
|
||||||
}
|
}
|
||||||
|
|
||||||
// RecodeTo24K 将silk重新编码为 24000 bit rate
|
// resample 将silk重新编码为 24000 bit rate
|
||||||
func RecodeTo24K(data []byte) []byte {
|
func resample(data []byte) []byte {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
11
modules/silk/stubs.go
Normal file
11
modules/silk/stubs.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// Package silk Silk编码核心模块
|
||||||
|
package silk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Mrs4s/go-cqhttp/internal/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
base.EncodeSilk = encode
|
||||||
|
base.ResampleSilk = resample
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user