mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 03:23:49 +08:00
fix file close
This commit is contained in:
parent
fe83ce716e
commit
1eeae13b4a
29
coolq/bot.go
29
coolq/bot.go
@ -5,7 +5,6 @@ import (
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path"
|
||||
"runtime/debug"
|
||||
@ -126,6 +125,24 @@ func (bot *CQBot) UploadLocalImageAsGroup(groupCode int64, img *LocalImageElemen
|
||||
return bot.Client.UploadGroupImageByFile(groupCode, img.File)
|
||||
}
|
||||
|
||||
func (bot *CQBot) UploadLocalVideo(target int64, v *LocalVideoElement) (*message.ShortVideoElement, error) {
|
||||
if v.File != "" {
|
||||
video, err := os.Open(v.File)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer video.Close()
|
||||
// todo 加缓存上传失败:短视频上传失败: resp is empty 迷
|
||||
/*
|
||||
hash1, _ := utils.ComputeMd5AndLength(video)
|
||||
hash2, _ := utils.ComputeMd5AndLength(v.thumb)
|
||||
cacheFile := path.Join(global.CACHE_PATH, hex.EncodeToString(hash1[:])+hex.EncodeToString(hash2[:])+".video")
|
||||
*/
|
||||
return bot.Client.UploadGroupShortVideo(target, video, v.thumb)
|
||||
}
|
||||
return &v.ShortVideoElement, nil
|
||||
}
|
||||
|
||||
func (bot *CQBot) UploadLocalImageAsPrivate(userId int64, img *LocalImageElement) (*message.FriendImageElement, error) {
|
||||
if img.Stream != nil {
|
||||
return bot.Client.UploadPrivateImage(userId, img.Stream)
|
||||
@ -160,8 +177,8 @@ func (bot *CQBot) SendGroupMessage(groupId int64, m *message.SendingMessage) int
|
||||
newElem = append(newElem, gv)
|
||||
continue
|
||||
}
|
||||
if i, ok := elem.(*LocalVideoElement); ok { // todo:cache & multiThread
|
||||
gv, err := bot.Client.UploadGroupShortVideo(groupId, i.video, i.thumb)
|
||||
if i, ok := elem.(*LocalVideoElement); ok {
|
||||
gv, err := bot.UploadLocalVideo(groupId, i)
|
||||
if err != nil {
|
||||
log.Warnf("警告: 群 %v 消息短视频上传失败: %v", groupId, err)
|
||||
continue
|
||||
@ -289,10 +306,10 @@ func (bot *CQBot) SendPrivateMessage(target int64, m *message.SendingMessage) in
|
||||
newElem = append(newElem, fv)
|
||||
continue
|
||||
}
|
||||
if i, ok := elem.(*LocalVideoElement); ok { // todo:cache & multiThread
|
||||
gv, err := bot.Client.UploadGroupShortVideo(target, i.video, i.thumb)
|
||||
if i, ok := elem.(*LocalVideoElement); ok {
|
||||
gv, err := bot.UploadLocalVideo(target, i)
|
||||
if err != nil {
|
||||
log.Warnf("警告: 私聊 %v 消息短视频上传失败: %v", int64(rand.Uint32()), err)
|
||||
log.Warnf("警告: 私聊 %v 消息短视频上传失败: %v", target, err)
|
||||
continue
|
||||
}
|
||||
newElem = append(newElem, gv)
|
||||
|
@ -82,7 +82,6 @@ type LocalVoiceElement struct {
|
||||
type LocalVideoElement struct {
|
||||
message.ShortVideoElement
|
||||
File string
|
||||
video io.ReadSeeker
|
||||
thumb io.ReadSeeker
|
||||
}
|
||||
|
||||
@ -793,26 +792,27 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (m interf
|
||||
return nil, err
|
||||
}
|
||||
v := file.(*LocalVideoElement)
|
||||
var data []byte
|
||||
if cover, ok := d["cover"]; ok {
|
||||
data, _ := global.FindFile(cover, cache, global.IMAGE_PATH)
|
||||
v.thumb = bytes.NewReader(data)
|
||||
}
|
||||
if v.thumb == nil {
|
||||
data, _ = global.FindFile(cover, cache, global.IMAGE_PATH)
|
||||
} else {
|
||||
_ = global.ExtractCover(v.File, v.File+".jpg")
|
||||
v.thumb, _ = os.Open(v.File + ".jpg")
|
||||
data, _ = ioutil.ReadFile(v.File + ".jpg")
|
||||
}
|
||||
v.video, _ = os.Open(v.File)
|
||||
_, err = v.video.Seek(4, io.SeekStart)
|
||||
v.thumb = bytes.NewReader(data)
|
||||
video, _ := os.Open(v.File)
|
||||
defer video.Close()
|
||||
_, err = video.Seek(4, io.SeekStart)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var header = make([]byte, 4)
|
||||
_, err = v.video.Read(header)
|
||||
if !bytes.Equal(header, []byte{0x66, 0x74, 0x79, 0x70}) { // ftyp
|
||||
_, _ = v.video.Seek(0, io.SeekStart)
|
||||
hash, _ := utils.ComputeMd5AndLength(v.video)
|
||||
_, err = video.Read(header)
|
||||
if !bytes.Equal(header, []byte{0x66, 0x74, 0x79, 0x70}) { // check file header ftyp
|
||||
_, _ = video.Seek(0, io.SeekStart)
|
||||
hash, _ := utils.ComputeMd5AndLength(video)
|
||||
cacheFile := path.Join(global.CACHE_PATH, hex.EncodeToString(hash[:])+".mp4")
|
||||
if global.PathExists(cacheFile) {
|
||||
if global.PathExists(cacheFile) && cache == "1" {
|
||||
goto ok
|
||||
}
|
||||
err = global.EncodeMP4(v.File, cacheFile)
|
||||
@ -820,9 +820,8 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (m interf
|
||||
return nil, err
|
||||
}
|
||||
ok:
|
||||
v.video, _ = os.Open(cacheFile)
|
||||
v.File = cacheFile
|
||||
}
|
||||
_, _ = v.video.Seek(0, io.SeekStart)
|
||||
return v, nil
|
||||
default:
|
||||
return nil, errors.New("unsupported cq code: " + t)
|
||||
|
@ -39,7 +39,7 @@ func EncoderSilk(data []byte) ([]byte, error) {
|
||||
return slk, nil
|
||||
}
|
||||
|
||||
func EncodeMP4(src string, dst string) error {
|
||||
func EncodeMP4(src string, dst string) error { // -y 覆盖文件
|
||||
cmd := exec.Command("ffmpeg", "-i", src, "-y", "-c", "copy", "-map", "0", dst)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user