mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-07 20:45:53 +08:00
internal/mime: use stdlib to detect mimetype
This commit is contained in:
parent
779fa20704
commit
34613306b3
@ -38,7 +38,7 @@ type guildMemberPageToken struct {
|
|||||||
nextQueryParam string
|
nextQueryParam string
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultPageToken = &guildMemberPageToken{
|
var defaultPageToken = guildMemberPageToken{
|
||||||
guildID: 0,
|
guildID: 0,
|
||||||
nextIndex: 0,
|
nextIndex: 0,
|
||||||
nextRoleID: 2,
|
nextRoleID: 2,
|
||||||
@ -150,7 +150,7 @@ func (bot *CQBot) CQGetGuildMembers(guildID uint64, nextToken string) global.MSG
|
|||||||
if guild == nil {
|
if guild == nil {
|
||||||
return Failed(100, "GUILD_NOT_FOUND")
|
return Failed(100, "GUILD_NOT_FOUND")
|
||||||
}
|
}
|
||||||
token := defaultPageToken
|
token := &defaultPageToken
|
||||||
if nextToken != "" {
|
if nextToken != "" {
|
||||||
i, exists := bot.nextTokenCache.Get(nextToken)
|
i, exists := bot.nextTokenCache.Get(nextToken)
|
||||||
if !exists {
|
if !exists {
|
||||||
|
1
go.mod
1
go.mod
@ -7,7 +7,6 @@ require (
|
|||||||
github.com/Mrs4s/MiraiGo v0.0.0-20220317085721-6d84141b8dd3
|
github.com/Mrs4s/MiraiGo v0.0.0-20220317085721-6d84141b8dd3
|
||||||
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc
|
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc
|
||||||
github.com/fumiama/go-hide-param v0.1.4
|
github.com/fumiama/go-hide-param v0.1.4
|
||||||
github.com/gabriel-vasile/mimetype v1.4.0
|
|
||||||
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
|
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
|
||||||
github.com/mattn/go-colorable v0.1.12
|
github.com/mattn/go-colorable v0.1.12
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
|
6
go.sum
6
go.sum
@ -16,8 +16,6 @@ github.com/fumiama/go-hide-param v0.1.4 h1:y7TRTzZMdCH9GOXnIzU3B+1BSkcmvejVGmGsz
|
|||||||
github.com/fumiama/go-hide-param v0.1.4/go.mod h1:vJkQlJIEI56nIyp7tCQu1/2QOyKtZpudsnJkGk9U1aY=
|
github.com/fumiama/go-hide-param v0.1.4/go.mod h1:vJkQlJIEI56nIyp7tCQu1/2QOyKtZpudsnJkGk9U1aY=
|
||||||
github.com/fumiama/imgsz v0.0.2 h1:fAkC0FnIscdKOXwAxlyw3EUba5NzxZdSxGaq3Uyfxak=
|
github.com/fumiama/imgsz v0.0.2 h1:fAkC0FnIscdKOXwAxlyw3EUba5NzxZdSxGaq3Uyfxak=
|
||||||
github.com/fumiama/imgsz v0.0.2/go.mod h1:dR71mI3I2O5u6+PCpd47M9TZptzP+39tRBcbdIkoqM4=
|
github.com/fumiama/imgsz v0.0.2/go.mod h1:dR71mI3I2O5u6+PCpd47M9TZptzP+39tRBcbdIkoqM4=
|
||||||
github.com/gabriel-vasile/mimetype v1.4.0 h1:Cn9dkdYsMIu56tGho+fqzh7XmvY2YyGU0FnbhiOsEro=
|
|
||||||
github.com/gabriel-vasile/mimetype v1.4.0/go.mod h1:fA8fi6KUiG7MgQQ+mEWotXoEOvmxRtOJlERCzSmRvr8=
|
|
||||||
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
|
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
|
||||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
@ -120,7 +118,6 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
|
|||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||||
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||||
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
|
||||||
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 h1:0qxwC5n+ttVOINCBeRHO0nq9X7uy8SDsPoi5OaCdIEI=
|
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 h1:0qxwC5n+ttVOINCBeRHO0nq9X7uy8SDsPoi5OaCdIEI=
|
||||||
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
@ -139,16 +136,13 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY=
|
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY=
|
||||||
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
@ -137,8 +137,7 @@ func expand(s string, mapping func(string) string) string {
|
|||||||
r := regexp.MustCompile(`\${([a-zA-Z_]+[a-zA-Z0-9_:/.]*)}`)
|
r := regexp.MustCompile(`\${([a-zA-Z_]+[a-zA-Z0-9_:/.]*)}`)
|
||||||
return r.ReplaceAllStringFunc(s, func(s string) string {
|
return r.ReplaceAllStringFunc(s, func(s string) string {
|
||||||
s = strings.Trim(s, "${}")
|
s = strings.Trim(s, "${}")
|
||||||
// todo: use strings.Cut once go1.18 is released
|
before, after, ok := strings.Cut(s, ":")
|
||||||
before, after, ok := cut(s, ":")
|
|
||||||
m := mapping(before)
|
m := mapping(before)
|
||||||
if ok && m == "" {
|
if ok && m == "" {
|
||||||
return after
|
return after
|
||||||
@ -146,10 +145,3 @@ func expand(s string, mapping func(string) string) string {
|
|||||||
return m
|
return m
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func cut(s, sep string) (before, after string, found bool) {
|
|
||||||
if i := strings.Index(s, sep); i >= 0 {
|
|
||||||
return s[:i], s[i+len(sep):], true
|
|
||||||
}
|
|
||||||
return s, "", false
|
|
||||||
}
|
|
||||||
|
@ -3,68 +3,51 @@ package mime
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"github.com/gabriel-vasile/mimetype"
|
"strings"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
"github.com/Mrs4s/go-cqhttp/internal/base"
|
"github.com/Mrs4s/go-cqhttp/internal/base"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
base.IsLawfulAudio = checkImage
|
base.IsLawfulImage = checkImage
|
||||||
base.IsLawfulAudio = checkAudio
|
base.IsLawfulAudio = checkAudio
|
||||||
}
|
}
|
||||||
|
|
||||||
// keep sync with /docs/file.md#MINE
|
const limit = 4 * 1024
|
||||||
var lawfulImage = [...]string{
|
|
||||||
"image/bmp",
|
|
||||||
"image/gif",
|
|
||||||
"image/jpeg",
|
|
||||||
"image/png",
|
|
||||||
"image/webp",
|
|
||||||
}
|
|
||||||
|
|
||||||
var lawfulAudio = [...]string{
|
func scan(r io.ReadSeeker) 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)
|
_, _ = r.Seek(0, io.SeekStart)
|
||||||
defer r.Seek(0, io.SeekStart)
|
defer r.Seek(0, io.SeekStart)
|
||||||
t, err := mimetype.DetectReader(r)
|
in := make([]byte, limit)
|
||||||
if err != nil {
|
_, _ = r.Read(in)
|
||||||
logrus.Debugf("扫描 Mime 时出现问题: %v", err)
|
return http.DetectContentType(in)
|
||||||
return false, ""
|
|
||||||
}
|
|
||||||
for _, lt := range list {
|
|
||||||
if t.Is(lt) {
|
|
||||||
return true, t.String()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false, t.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkImage 判断给定流是否为合法图片
|
// checkImage 判断给定流是否为合法图片
|
||||||
// 返回 是否合法, 实际Mime
|
// 返回 是否合法, 实际Mime
|
||||||
// 判断后会自动将 Stream Seek 至 0
|
// 判断后会自动将 Stream Seek 至 0
|
||||||
func checkImage(r io.ReadSeeker) (bool, string) {
|
func checkImage(r io.ReadSeeker) (ok bool, t string) {
|
||||||
return check(r, lawfulImage[:])
|
if base.SkipMimeScan {
|
||||||
|
return true, ""
|
||||||
|
}
|
||||||
|
t = scan(r)
|
||||||
|
switch t {
|
||||||
|
case "image/bmp", "image/gif", "image/jpeg", "image/png", "image/webp":
|
||||||
|
ok = true
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkImage 判断给定流是否为合法音频
|
// checkImage 判断给定流是否为合法音频
|
||||||
func checkAudio(r io.ReadSeeker) (bool, string) {
|
func checkAudio(r io.ReadSeeker) (bool, string) {
|
||||||
return check(r, lawfulAudio[:])
|
if base.SkipMimeScan {
|
||||||
|
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 true, t
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user