Skip to content

Commit

Permalink
feat: 支持发送短视频,删除部分多余代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Redmomn committed Aug 29, 2024
1 parent 12b4d6e commit ce39e96
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 136 deletions.
8 changes: 4 additions & 4 deletions cmd/gocq/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"strings"
"time"

"github.com/LagrangeDev/LagrangeGo/client/packets/wtlogin/qrcodeState"
"github.com/LagrangeDev/LagrangeGo/utils"

"github.com/Mrs4s/go-cqhttp/utils/ternary"
"github.com/LagrangeDev/LagrangeGo/client/packets/wtlogin/qrcodeState"

"github.com/LagrangeDev/LagrangeGo/client/auth"

Expand Down Expand Up @@ -73,7 +73,7 @@ func printQRCode(imgData []byte) {
}

bound := img.Bounds().Max.X
buf := make([]byte, 0, (bound+1)*(bound/2+ternary.BV(bound%2 == 0, 0, 1)))
buf := make([]byte, 0, (bound+1)*(bound/2+utils.Ternary(bound%2 == 0, 0, 1)))

padding := 0
lastColor := img.At(padding, padding).(color.Gray).Y
Expand All @@ -86,7 +86,7 @@ func printQRCode(imgData []byte) {
for y := padding; y < bound-padding; y += 2 {
for x := padding; x < bound-padding; x++ {
isUpWhite := img.At(x, y).(color.Gray).Y == 255
isDownWhite := ternary.BV(y < bound-padding, img.At(x, y+1).(color.Gray).Y == 255, false)
isDownWhite := utils.Ternary(y < bound-padding, img.At(x, y+1).(color.Gray).Y == 255, false)

if !isUpWhite && !isDownWhite {
buf = append(buf, bb...)
Expand Down
2 changes: 1 addition & 1 deletion coolq/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"

"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/Mrs4s/go-cqhttp/db"
"github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/internal/base"
Expand All @@ -28,7 +29,6 @@ import (
"github.com/Mrs4s/go-cqhttp/internal/param"
"github.com/Mrs4s/go-cqhttp/modules/filter"
"github.com/Mrs4s/go-cqhttp/pkg/onebot"
"github.com/Mrs4s/go-cqhttp/utils"
)

// CQGetLoginInfo 获取登录号信息
Expand Down
81 changes: 27 additions & 54 deletions coolq/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/LagrangeDev/LagrangeGo/client/entity"
event2 "github.com/LagrangeDev/LagrangeGo/client/event"
"github.com/Mrs4s/go-cqhttp/utils"
"github.com/LagrangeDev/LagrangeGo/utils"

"github.com/LagrangeDev/LagrangeGo/client"
"github.com/LagrangeDev/LagrangeGo/message"
Expand Down Expand Up @@ -155,22 +155,7 @@ func (w *worker) wait() {

// uploadLocalVoice 上传语音
func (bot *CQBot) uploadLocalVoice(target message.Source, voice *message.VoiceElement) (message.IMessageElement, error) {
switch target.SourceType {
case message.SourceGroup:
i, err := bot.Client.RecordUploadGroup(uint32(target.PrimaryID), voice)
if err != nil {
return nil, err
}
return i, nil
case message.SourcePrivate:
i, err := bot.Client.RecordUploadPrivate(bot.Client.GetUid(uint32(target.PrimaryID)), voice)
if err != nil {
return nil, err
}
return i, nil
default:
return nil, errors.New("unknown target source type")
}
return bot.Client.UploadRecord(target, voice)
}

// uploadLocalImage 上传本地图片
Expand Down Expand Up @@ -199,42 +184,30 @@ func (bot *CQBot) uploadLocalImage(target message.Source, img *msg.LocalImage) (
}
img.Stream = bytes.NewReader(stream.Bytes())
}
switch target.SourceType {
case message.SourceGroup:
i, err := bot.Client.ImageUploadGroup(uint32(target.PrimaryID), message.NewStreamImage(img.Stream))
if err != nil {
return nil, errors.Wrap(err, "upload group error")
}
return i, nil
case message.SourcePrivate:
i, err := bot.Client.ImageUploadPrivate(bot.Client.GetUid(uint32(target.PrimaryID)), message.NewStreamImage(img.Stream))
if err != nil {
return nil, errors.Wrap(err, "upload group error")
}
return i, nil
default:
return nil, errors.New("unknown target source type")
}
return bot.Client.UploadImage(target, message.NewStreamImage(img.Stream))
}

// TODO 短视频上传
//// uploadLocalVideo 上传本地短视频至群聊
//func (bot *CQBot) uploadLocalVideo(target message.Source, v *msg.LocalVideo) (*message.ShortVideoElement, error) {
// video, err := os.Open(v.File)
// if err != nil {
// return nil, err
// }
// defer func() { _ = video.Close() }()
// return bot.Client.UploadShortVideo(target, video, v.Thumb)
//}
// uploadLocalVideo 上传本地短视频至群聊
func (bot *CQBot) uploadLocalVideo(target message.Source, v *msg.LocalVideo) (*message.ShortVideoElement, error) {
video, err := os.Open(v.File)
if err != nil {
return nil, err
}
defer func() { _ = video.Close() }()
return bot.Client.UploadShortVideo(target, message.NewSteramVideo(video, v.Thumb))
}

func removeLocalElement(elements []message.IMessageElement) []message.IMessageElement {
var j int
for i, e := range elements {
switch e.(type) {
switch elem := e.(type) {
case *msg.LocalImage, *msg.LocalVideo:
// todo 这里先不要删,语音消息暂时没有本地表示
// case *message.VoiceElement: // 未上传的语音消息, 也删除
case *message.VoiceElement: // 未上传的语音消息, 也删除
if elem.MsgInfo != nil {
continue
}
case nil:
default:
if j < i {
Expand Down Expand Up @@ -279,16 +252,16 @@ func (bot *CQBot) uploadMedia(target message.Source, elements []message.IMessage
*p = m
}
})
// TODO 短视频上传
//case *msg.LocalVideo:
// w.do(func() {
// m, err := bot.uploadLocalVideo(target, e)
// if err != nil {
// log.Warnf(uploadFailedTemplate, source, target.PrimaryID, "视频", err)
// } else {
// *p = m
// }
// })
// TODO 短视频上传
case *msg.LocalVideo:
w.do(func() {
m, err := bot.uploadLocalVideo(target, e)
if err != nil {
log.Warnf(uploadFailedTemplate, source, target.PrimaryID, "视频", err)
} else {
*p = m
}
})
}
}
w.wait()
Expand Down
90 changes: 46 additions & 44 deletions coolq/cqcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"io"
"net/url"
"os"
"path"
Expand All @@ -14,7 +15,8 @@ import (
"strings"
"time"

"github.com/Mrs4s/go-cqhttp/utils"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/crypto"

"github.com/LagrangeDev/LagrangeGo/message"
"github.com/LagrangeDev/LagrangeGo/utils/binary"
Expand Down Expand Up @@ -822,47 +824,47 @@ func (bot *CQBot) ConvertElement(spec *onebot.Spec, elem msg.Element, sourceType
// return nil, errors.New("send cardimage faild")
// }
// return bot.makeShowPic(img, source, brief, icon, minWidth, minHeight, maxWidth, maxHeight, sourceType == message.SourceGroup)
//case "video":
// file, err := bot.makeImageOrVideoElem(elem, true, sourceType)
// if err != nil {
// return nil, err
// }
// v, ok := file.(*msg.LocalVideo)
// if !ok {
// return file, nil
// }
// if v.File == "" {
// return v, nil
// }
// var data []byte
// if cover := elem.Get("cover"); cover != "" {
// data, _ = global.FindFile(cover, elem.Get("cache"), global.ImagePath)
// } else {
// err = global.ExtractCover(v.File, v.File+".jpg")
// if err != nil {
// return nil, err
// }
// data, _ = os.ReadFile(v.File + ".jpg")
// }
// v.Thumb = bytes.NewReader(data)
// video, _ := os.Open(v.File)
// defer video.Close()
// _, _ = video.Seek(4, io.SeekStart)
// header := make([]byte, 4)
// _, _ = 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.CachePath, hex.EncodeToString(hash)+".mp4")
// if !(elem.Get("cache") == "" || elem.Get("cache") == "1") || !global.PathExists(cacheFile) {
// err = global.EncodeMP4(v.File, cacheFile)
// if err != nil {
// return nil, err
// }
// }
// v.File = cacheFile
// }
// return v, nil
case "video":
file, err := bot.makeImageOrVideoElem(elem, true, sourceType)
if err != nil {
return nil, err
}
v, ok := file.(*msg.LocalVideo)
if !ok {
return file, nil
}
if v.File == "" {
return v, nil
}
var data []byte
if cover := elem.Get("cover"); cover != "" {
data, _ = global.FindFile(cover, elem.Get("cache"), global.ImagePath)
} else {
err = global.ExtractCover(v.File, v.File+".jpg")
if err != nil {
return nil, err
}
data, _ = os.ReadFile(v.File + ".jpg")
}
v.Thumb = bytes.NewReader(data)
video, _ := os.Open(v.File)
defer video.Close()
_, _ = video.Seek(4, io.SeekStart)
header := make([]byte, 4)
_, _ = video.Read(header)
if !bytes.Equal(header, []byte{0x66, 0x74, 0x79, 0x70}) { // check file header ftyp
_, _ = video.Seek(0, io.SeekStart)
hash, _ := crypto.ComputeMd5AndLength(video)
cacheFile := path.Join(global.CachePath, hex.EncodeToString(hash)+".mp4")
if !(elem.Get("cache") == "" || elem.Get("cache") == "1") || !global.PathExists(cacheFile) {
err = global.EncodeMP4(v.File, cacheFile)
if err != nil {
return nil, err
}
}
v.File = cacheFile
}
return v, nil
//case "file":
// path := elem.Get("path")
// name := elem.Get("name")
Expand Down Expand Up @@ -1037,8 +1039,8 @@ func (bot *CQBot) readVideoCache(b []byte) message.IMessageElement {
return &message.ShortVideoElement{ // todo 检查缓存是否有效
Md5: r.ReadBytes(16),
ThumbMd5: r.ReadBytes(16),
Size: r.ReadI32(),
ThumbSize: r.ReadI32(),
Size: r.ReadU32(),
ThumbSize: r.ReadU32(),
Name: r.ReadStringWithLength("u32", true),
Uuid: r.ReadBytes(r.Len()),
}
Expand Down
2 changes: 1 addition & 1 deletion db/leveldb/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/LagrangeDev/LagrangeGo/utils/binary"

"github.com/Mrs4s/go-cqhttp/utils"
"github.com/LagrangeDev/LagrangeGo/utils"

"github.com/pkg/errors"
"github.com/syndtr/goleveldb/leveldb"
Expand Down
2 changes: 1 addition & 1 deletion db/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/LagrangeDev/LagrangeGo/utils/binary"

"github.com/Mrs4s/go-cqhttp/utils"
"github.com/LagrangeDev/LagrangeGo/utils"

sql "github.com/FloatTech/sqlite"
"github.com/pkg/errors"
Expand Down
3 changes: 2 additions & 1 deletion global/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"crypto/md5"
"encoding/hex"
"errors"
"github.com/Mrs4s/go-cqhttp/utils"
"net/netip"
"net/url"
"os"
"path"
"runtime"
"strings"

"github.com/LagrangeDev/LagrangeGo/utils"

b14 "github.com/fumiama/go-base16384"
"github.com/segmentio/asm/base64"
log "github.com/sirupsen/logrus"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/FloatTech/sqlite v1.6.3
github.com/LagrangeDev/LagrangeGo v0.0.0-20240825050647-d4c1927bf305
github.com/LagrangeDev/LagrangeGo v0.0.0-20240829010317-08c3e5126856
github.com/Microsoft/go-winio v0.6.2-0.20230724192519-b29bbd58a65a
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/FloatTech/sqlite v1.6.3 h1:MQkqBNlkPuCoKQQgoNLuTL/2Ci3tBTFAnVYBdD0Wy4
github.com/FloatTech/sqlite v1.6.3/go.mod h1:zFbHzRfB+CJ+VidfjuVbrcin3DAz283F7hF1hIeHzpY=
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1 h1:g4pTnDJUW4VbJ9NvoRfUvdjDrHz/6QhfN/LoIIpICbo=
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
github.com/LagrangeDev/LagrangeGo v0.0.0-20240825050647-d4c1927bf305 h1:JL4Ix8lG92jcS+NNaEItOLgScbmqAJ58OgUEG0qBn6o=
github.com/LagrangeDev/LagrangeGo v0.0.0-20240825050647-d4c1927bf305/go.mod h1:qK/l75YuMYdJWfPcxPhfjybJdXD4kzMFvcbKlvdAoa0=
github.com/LagrangeDev/LagrangeGo v0.0.0-20240829010317-08c3e5126856 h1:/Cu9PVv85qFRQ7i6rleLogDt0odh78bnlt2JoT5HJYs=
github.com/LagrangeDev/LagrangeGo v0.0.0-20240829010317-08c3e5126856/go.mod h1:qK/l75YuMYdJWfPcxPhfjybJdXD4kzMFvcbKlvdAoa0=
github.com/Microsoft/go-winio v0.6.2-0.20230724192519-b29bbd58a65a h1:aU1703IHxupjzipvhu16qYKLMR03e+8WuNR+JMsKfGU=
github.com/Microsoft/go-winio v0.6.2-0.20230724192519-b29bbd58a65a/go.mod h1:OZqLNXdYJHmx7aqq/T6wAdFEdoGm5nmIfC4kU7M8P8o=
github.com/RomiChan/protobuf v0.1.1-0.20230204044148-2ed269a2e54d h1:/Xuj3fIiMY2ls1TwvPKmaqQrtJsPY+c9s+0lOScVHd8=
Expand Down
2 changes: 1 addition & 1 deletion server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strings"
"time"

"github.com/Mrs4s/go-cqhttp/utils"
"github.com/LagrangeDev/LagrangeGo/utils"

log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
Expand Down
3 changes: 2 additions & 1 deletion server/scf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/Mrs4s/go-cqhttp/utils"
"io"
"net/http"
"net/url"
"os"
"runtime/debug"
"strings"

"github.com/LagrangeDev/LagrangeGo/utils"

log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"

Expand Down
3 changes: 2 additions & 1 deletion server/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/Mrs4s/go-cqhttp/utils"
"net"
"net/http"
"net/url"
Expand All @@ -15,6 +14,8 @@ import (
"sync"
"time"

"github.com/LagrangeDev/LagrangeGo/utils"

"github.com/RomiChan/websocket"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
Expand Down
15 changes: 0 additions & 15 deletions utils/string.go

This file was deleted.

Loading

0 comments on commit ce39e96

Please sign in to comment.