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"
|
"encoding/gob"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/crc32"
|
"hash/crc32"
|
||||||
"math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
@ -126,6 +125,24 @@ func (bot *CQBot) UploadLocalImageAsGroup(groupCode int64, img *LocalImageElemen
|
|||||||
return bot.Client.UploadGroupImageByFile(groupCode, img.File)
|
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) {
|
func (bot *CQBot) UploadLocalImageAsPrivate(userId int64, img *LocalImageElement) (*message.FriendImageElement, error) {
|
||||||
if img.Stream != nil {
|
if img.Stream != nil {
|
||||||
return bot.Client.UploadPrivateImage(userId, img.Stream)
|
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)
|
newElem = append(newElem, gv)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if i, ok := elem.(*LocalVideoElement); ok { // todo:cache & multiThread
|
if i, ok := elem.(*LocalVideoElement); ok {
|
||||||
gv, err := bot.Client.UploadGroupShortVideo(groupId, i.video, i.thumb)
|
gv, err := bot.UploadLocalVideo(groupId, i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("警告: 群 %v 消息短视频上传失败: %v", groupId, err)
|
log.Warnf("警告: 群 %v 消息短视频上传失败: %v", groupId, err)
|
||||||
continue
|
continue
|
||||||
@ -289,10 +306,10 @@ func (bot *CQBot) SendPrivateMessage(target int64, m *message.SendingMessage) in
|
|||||||
newElem = append(newElem, fv)
|
newElem = append(newElem, fv)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if i, ok := elem.(*LocalVideoElement); ok { // todo:cache & multiThread
|
if i, ok := elem.(*LocalVideoElement); ok {
|
||||||
gv, err := bot.Client.UploadGroupShortVideo(target, i.video, i.thumb)
|
gv, err := bot.UploadLocalVideo(target, i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("警告: 私聊 %v 消息短视频上传失败: %v", int64(rand.Uint32()), err)
|
log.Warnf("警告: 私聊 %v 消息短视频上传失败: %v", target, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
newElem = append(newElem, gv)
|
newElem = append(newElem, gv)
|
||||||
|
@ -82,7 +82,6 @@ type LocalVoiceElement struct {
|
|||||||
type LocalVideoElement struct {
|
type LocalVideoElement struct {
|
||||||
message.ShortVideoElement
|
message.ShortVideoElement
|
||||||
File string
|
File string
|
||||||
video io.ReadSeeker
|
|
||||||
thumb 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
|
return nil, err
|
||||||
}
|
}
|
||||||
v := file.(*LocalVideoElement)
|
v := file.(*LocalVideoElement)
|
||||||
|
var data []byte
|
||||||
if cover, ok := d["cover"]; ok {
|
if cover, ok := d["cover"]; ok {
|
||||||
data, _ := global.FindFile(cover, cache, global.IMAGE_PATH)
|
data, _ = global.FindFile(cover, cache, global.IMAGE_PATH)
|
||||||
v.thumb = bytes.NewReader(data)
|
} else {
|
||||||
}
|
|
||||||
if v.thumb == nil {
|
|
||||||
_ = global.ExtractCover(v.File, v.File+".jpg")
|
_ = 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)
|
v.thumb = bytes.NewReader(data)
|
||||||
_, err = v.video.Seek(4, io.SeekStart)
|
video, _ := os.Open(v.File)
|
||||||
|
defer video.Close()
|
||||||
|
_, err = video.Seek(4, io.SeekStart)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var header = make([]byte, 4)
|
var header = make([]byte, 4)
|
||||||
_, err = v.video.Read(header)
|
_, err = video.Read(header)
|
||||||
if !bytes.Equal(header, []byte{0x66, 0x74, 0x79, 0x70}) { // ftyp
|
if !bytes.Equal(header, []byte{0x66, 0x74, 0x79, 0x70}) { // check file header ftyp
|
||||||
_, _ = v.video.Seek(0, io.SeekStart)
|
_, _ = video.Seek(0, io.SeekStart)
|
||||||
hash, _ := utils.ComputeMd5AndLength(v.video)
|
hash, _ := utils.ComputeMd5AndLength(video)
|
||||||
cacheFile := path.Join(global.CACHE_PATH, hex.EncodeToString(hash[:])+".mp4")
|
cacheFile := path.Join(global.CACHE_PATH, hex.EncodeToString(hash[:])+".mp4")
|
||||||
if global.PathExists(cacheFile) {
|
if global.PathExists(cacheFile) && cache == "1" {
|
||||||
goto ok
|
goto ok
|
||||||
}
|
}
|
||||||
err = global.EncodeMP4(v.File, cacheFile)
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
ok:
|
ok:
|
||||||
v.video, _ = os.Open(cacheFile)
|
v.File = cacheFile
|
||||||
}
|
}
|
||||||
_, _ = v.video.Seek(0, io.SeekStart)
|
|
||||||
return v, nil
|
return v, nil
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("unsupported cq code: " + t)
|
return nil, errors.New("unsupported cq code: " + t)
|
||||||
|
@ -39,7 +39,7 @@ func EncoderSilk(data []byte) ([]byte, error) {
|
|||||||
return slk, nil
|
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)
|
cmd := exec.Command("ffmpeg", "-i", src, "-y", "-c", "copy", "-map", "0", dst)
|
||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user