Skip to content

Commit

Permalink
feat: add webp image convert function
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikoTan committed Jan 5, 2023
1 parent 4061904 commit 2a4ea28
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
16 changes: 15 additions & 1 deletion coolq/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,23 @@ func (bot *CQBot) uploadLocalImage(target message.Source, img *LocalImageElement
defer func() { _ = f.Close() }()
img.Stream = f
}
if mt, ok := mime.CheckImage(img.Stream); !ok {
mt, ok := mime.CheckImage(img.Stream)
if !ok {
return nil, errors.New("image type error: " + mt)
}
if mt == "image/webp" && base.ConvertWebpImage != false {
newname := img.File + ".png"
err := global.ConvertImagePng(img.File, newname)
if err != nil {
return nil, errors.Wrap(err, "convert webp image error")
}
f, err := os.Open(newname)
if err != nil {
return nil, errors.Wrap(err, "open image error")
}
defer func() { _ = f.Close() }()
img.Stream = f
}
i, err := bot.Client.UploadImage(target, img.Stream, 4)
if err != nil {
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions global/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ func ExtractCover(src string, target string) error {
cmd := exec.Command("ffmpeg", "-i", src, "-y", "-ss", "0", "-frames:v", "1", target)
return errors.Wrap(cmd.Run(), "extract video cover failed")
}

func ConvertImagePng(src string, target string) error {
cmd := exec.Command("ffmpeg", "-i", src, "-y", "-f", "png", target)
return errors.Wrap(cmd.Run(), "convert image to png failed")
}
2 changes: 2 additions & 0 deletions internal/base/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
SplitURL bool // 是否分割URL
ForceFragmented bool // 是否启用强制分片
SkipMimeScan bool // 是否跳过Mime扫描
ConvertWebpImage bool // 是否转换Webp图片
ReportSelfMessage bool // 是否上报自身消息
UseSSOAddress bool // 是否使用服务器下发的新地址进行重连
LogForceNew bool // 是否在每次启动时强制创建全新的文件储存日志
Expand Down Expand Up @@ -79,6 +80,7 @@ func Init() {
ExtraReplyData = conf.Message.ExtraReplyData
ForceFragmented = conf.Message.ForceFragment
SkipMimeScan = conf.Message.SkipMimeScan
ConvertWebpImage = conf.Message.ConvertWebpImage
ReportSelfMessage = conf.Message.ReportSelfMessage
UseSSOAddress = conf.Account.UseSSOAddress
AllowTempSession = conf.Account.AllowTempSession
Expand Down
1 change: 1 addition & 0 deletions modules/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Config struct {
RemoveReplyAt bool `yaml:"remove-reply-at"`
ExtraReplyData bool `yaml:"extra-reply-data"`
SkipMimeScan bool `yaml:"skip-mime-scan"`
ConvertWebpImage bool `yaml:"convert-webp-image"`
} `yaml:"message"`

Output struct {
Expand Down
2 changes: 2 additions & 0 deletions modules/config/default_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ message:
extra-reply-data: false
# 跳过 Mime 扫描, 忽略错误数据
skip-mime-scan: false
# 是否自动转换 WebP 图片
convert-webp-image: false

output:
# 日志等级 trace,debug,info,warn,error
Expand Down

0 comments on commit 2a4ea28

Please sign in to comment.