88 "fmt"
99 "github.com/tuotoo/qrcode"
1010 "github.com/wechaty/go-wechaty/wechaty-puppet/helper"
11+ logger "github.com/wechaty/go-wechaty/wechaty-puppet/log"
1112 "io"
1213 "io/ioutil"
1314 "mime"
@@ -33,33 +34,66 @@ var (
3334 ErrNoUuid = errors .New ("no uuid" )
3435)
3536
37+ var log = logger .L .WithField ("module" , "filebox" )
38+
3639type fileImplInterface interface {
3740 toJSONMap () (map [string ]interface {}, error )
3841 toReader () (io.Reader , error )
3942}
4043
4144// FileBox struct
4245type FileBox struct {
43- fileImpl fileImplInterface
44- Name string
45- metadata map [string ]interface {}
46- boxType Type
47- mimeType string
48- size int64
49- md5 string
46+ fileImpl fileImplInterface
47+ Name string
48+ metadata map [string ]interface {}
49+ boxType Type
50+ mediaType string
51+ size int64
52+ md5 string
5053
5154 err error
5255}
5356
5457func newFileBox (boxType Type , fileImpl fileImplInterface , options Options ) * FileBox {
55- return & FileBox {
58+ fb := & FileBox {
5659 fileImpl : fileImpl ,
5760 Name : options .Name ,
5861 metadata : options .Metadata ,
5962 boxType : boxType ,
6063 size : options .Size ,
6164 md5 : options .Md5 ,
62- mimeType : mime .TypeByExtension (filepath .Ext (options .Name )),
65+ }
66+ if fb .metadata == nil {
67+ fb .metadata = make (map [string ]interface {})
68+ }
69+ fb .correctName ()
70+ fb .guessMediaType ()
71+ return fb
72+ }
73+
74+ func (fb * FileBox ) correctName () {
75+ if strings .HasSuffix (fb .Name , ".silk" ) || strings .HasSuffix (fb .Name , ".slk" ) {
76+ log .Warn ("detect that you want to send voice file which should be <name>.sil pattern. So we help you rename it." )
77+ if strings .HasSuffix (fb .Name , ".silk" ) {
78+ fb .Name = strings .ReplaceAll (fb .Name , ".silk" , ".sil" )
79+ }
80+ if strings .HasSuffix (fb .Name , ".slk" ) {
81+ fb .Name = strings .ReplaceAll (fb .Name , ".slk" , ".sil" )
82+ }
83+ }
84+ }
85+
86+ func (fb * FileBox ) guessMediaType () {
87+ if strings .HasSuffix (fb .Name , ".sil" ) {
88+ fb .mediaType = "audio/silk"
89+ if _ , ok := fb .metadata ["voiceLength" ]; ! ok {
90+ log .Warn ("detect that you want to send voice file, but no voiceLength setting, " +
91+ `so use the default setting: 1000,` +
92+ `you should set it manually: filebox.WithMetadata(map[string]interface{}{"voiceLength": 2000})` )
93+ fb .metadata ["voiceLength" ] = 1000
94+ }
95+ } else {
96+ fb .mediaType = mime .TypeByExtension (filepath .Ext (fb .Name ))
6397 }
6498}
6599
@@ -192,12 +226,13 @@ func (fb *FileBox) ToJSON() (string, error) {
192226 }
193227
194228 jsonMap := map [string ]interface {}{
195- "name" : fb .Name ,
196- "metadata" : fb .metadata ,
197- "type" : fb .boxType ,
198- "boxType" : fb .boxType , //Deprecated
199- "size" : fb .size ,
200- "md5" : fb .md5 ,
229+ "name" : fb .Name ,
230+ "metadata" : fb .metadata ,
231+ "type" : fb .boxType ,
232+ "boxType" : fb .boxType , //Deprecated
233+ "size" : fb .size ,
234+ "md5" : fb .md5 ,
235+ "mediaType" : fb .mediaType ,
201236 }
202237
203238 switch fb .boxType {
@@ -291,7 +326,7 @@ func (fb *FileBox) ToDataURL() (string, error) {
291326 if err != nil {
292327 return "" , nil
293328 }
294- return fmt .Sprintf ("data:%s;base64,%s" , fb .mimeType , toBase64 ), nil
329+ return fmt .Sprintf ("data:%s;base64,%s" , fb .mediaType , toBase64 ), nil
295330}
296331
297332// ToQRCode to QRCode
@@ -352,6 +387,12 @@ func (fb *FileBox) Type() Type {
352387 return fb .boxType
353388}
354389
390+ // MetaData get metadata
391+ func (fb * FileBox ) MetaData () map [string ]interface {} {
392+ // TODO deep copy?
393+ return fb .metadata
394+ }
395+
355396// Error ret err
356397func (fb * FileBox ) Error () error {
357398 return fb .err
0 commit comments