Skip to content

Commit

Permalink
feat(filebox): support send voice message (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
dchaofei authored Nov 19, 2022
1 parent d6c4a33 commit cce7d5a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
2 changes: 1 addition & 1 deletion wechaty-puppet-service/puppet_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ func (p *PuppetService) messageSendFileNonStream(conversationID string, fileBox
if err != nil {
return "", err
}
jsonText, err = filebox.FromBase64(base64, filebox.WithName(fileBox.Name)).ToJSON()
jsonText, err = filebox.FromBase64(base64, filebox.WithName(fileBox.Name), filebox.WithMetadata(fileBox.MetaData())).ToJSON()
if err != nil {
return "", err
}
Expand Down
73 changes: 57 additions & 16 deletions wechaty-puppet/filebox/file_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"github.com/tuotoo/qrcode"
"github.com/wechaty/go-wechaty/wechaty-puppet/helper"
logger "github.com/wechaty/go-wechaty/wechaty-puppet/log"
"io"
"io/ioutil"
"mime"
Expand All @@ -33,33 +34,66 @@ var (
ErrNoUuid = errors.New("no uuid")
)

var log = logger.L.WithField("module", "filebox")

type fileImplInterface interface {
toJSONMap() (map[string]interface{}, error)
toReader() (io.Reader, error)
}

// FileBox struct
type FileBox struct {
fileImpl fileImplInterface
Name string
metadata map[string]interface{}
boxType Type
mimeType string
size int64
md5 string
fileImpl fileImplInterface
Name string
metadata map[string]interface{}
boxType Type
mediaType string
size int64
md5 string

err error
}

func newFileBox(boxType Type, fileImpl fileImplInterface, options Options) *FileBox {
return &FileBox{
fb := &FileBox{
fileImpl: fileImpl,
Name: options.Name,
metadata: options.Metadata,
boxType: boxType,
size: options.Size,
md5: options.Md5,
mimeType: mime.TypeByExtension(filepath.Ext(options.Name)),
}
if fb.metadata == nil {
fb.metadata = make(map[string]interface{})
}
fb.correctName()
fb.guessMediaType()
return fb
}

func (fb *FileBox) correctName() {
if strings.HasSuffix(fb.Name, ".silk") || strings.HasSuffix(fb.Name, ".slk") {
log.Warn("detect that you want to send voice file which should be <name>.sil pattern. So we help you rename it.")
if strings.HasSuffix(fb.Name, ".silk") {
fb.Name = strings.ReplaceAll(fb.Name, ".silk", ".sil")
}
if strings.HasSuffix(fb.Name, ".slk") {
fb.Name = strings.ReplaceAll(fb.Name, ".slk", ".sil")
}
}
}

func (fb *FileBox) guessMediaType() {
if strings.HasSuffix(fb.Name, ".sil") {
fb.mediaType = "audio/silk"
if _, ok := fb.metadata["voiceLength"]; !ok {
log.Warn("detect that you want to send voice file, but no voiceLength setting, " +
`so use the default setting: 1000,` +
`you should set it manually: filebox.WithMetadata(map[string]interface{}{"voiceLength": 2000})`)
fb.metadata["voiceLength"] = 1000
}
} else {
fb.mediaType = mime.TypeByExtension(filepath.Ext(fb.Name))
}
}

Expand Down Expand Up @@ -192,12 +226,13 @@ func (fb *FileBox) ToJSON() (string, error) {
}

jsonMap := map[string]interface{}{
"name": fb.Name,
"metadata": fb.metadata,
"type": fb.boxType,
"boxType": fb.boxType, //Deprecated
"size": fb.size,
"md5": fb.md5,
"name": fb.Name,
"metadata": fb.metadata,
"type": fb.boxType,
"boxType": fb.boxType, //Deprecated
"size": fb.size,
"md5": fb.md5,
"mediaType": fb.mediaType,
}

switch fb.boxType {
Expand Down Expand Up @@ -291,7 +326,7 @@ func (fb *FileBox) ToDataURL() (string, error) {
if err != nil {
return "", nil
}
return fmt.Sprintf("data:%s;base64,%s", fb.mimeType, toBase64), nil
return fmt.Sprintf("data:%s;base64,%s", fb.mediaType, toBase64), nil
}

// ToQRCode to QRCode
Expand Down Expand Up @@ -352,6 +387,12 @@ func (fb *FileBox) Type() Type {
return fb.boxType
}

// MetaData get metadata
func (fb *FileBox) MetaData() map[string]interface{} {
// TODO deep copy?
return fb.metadata
}

// Error ret err
func (fb *FileBox) Error() error {
return fb.err
Expand Down
2 changes: 1 addition & 1 deletion wechaty-puppet/filebox/file_box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestFileBox_ToJSON(t *testing.T) {
t.Run("ToJSON success", func(t *testing.T) {
const base64Encode = "RmlsZUJveEJhc2U2NAo="
const base64Filename = "test.txt"
const want = `{"base64":"RmlsZUJveEJhc2U2NAo=","boxType":1,"md5":"","metadata":null,"name":"test.txt","size":14,"type":1}`
const want = `{"base64":"RmlsZUJveEJhc2U2NAo=","boxType":1,"md5":"","mediaType":"text/plain; charset=utf-8","metadata":{},"name":"test.txt","size":14,"type":1}`
jsonString, err := FromBase64(base64Encode, WithName(base64Filename)).ToJSON()
if err != nil {
t.Error(err)
Expand Down

0 comments on commit cce7d5a

Please sign in to comment.