8
8
"fmt"
9
9
"github.com/tuotoo/qrcode"
10
10
"github.com/wechaty/go-wechaty/wechaty-puppet/helper"
11
+ logger "github.com/wechaty/go-wechaty/wechaty-puppet/log"
11
12
"io"
12
13
"io/ioutil"
13
14
"mime"
@@ -33,33 +34,66 @@ var (
33
34
ErrNoUuid = errors .New ("no uuid" )
34
35
)
35
36
37
+ var log = logger .L .WithField ("module" , "filebox" )
38
+
36
39
type fileImplInterface interface {
37
40
toJSONMap () (map [string ]interface {}, error )
38
41
toReader () (io.Reader , error )
39
42
}
40
43
41
44
// FileBox struct
42
45
type 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
50
53
51
54
err error
52
55
}
53
56
54
57
func newFileBox (boxType Type , fileImpl fileImplInterface , options Options ) * FileBox {
55
- return & FileBox {
58
+ fb := & FileBox {
56
59
fileImpl : fileImpl ,
57
60
Name : options .Name ,
58
61
metadata : options .Metadata ,
59
62
boxType : boxType ,
60
63
size : options .Size ,
61
64
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 ))
63
97
}
64
98
}
65
99
@@ -192,12 +226,13 @@ func (fb *FileBox) ToJSON() (string, error) {
192
226
}
193
227
194
228
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 ,
201
236
}
202
237
203
238
switch fb .boxType {
@@ -291,7 +326,7 @@ func (fb *FileBox) ToDataURL() (string, error) {
291
326
if err != nil {
292
327
return "" , nil
293
328
}
294
- return fmt .Sprintf ("data:%s;base64,%s" , fb .mimeType , toBase64 ), nil
329
+ return fmt .Sprintf ("data:%s;base64,%s" , fb .mediaType , toBase64 ), nil
295
330
}
296
331
297
332
// ToQRCode to QRCode
@@ -352,6 +387,12 @@ func (fb *FileBox) Type() Type {
352
387
return fb .boxType
353
388
}
354
389
390
+ // MetaData get metadata
391
+ func (fb * FileBox ) MetaData () map [string ]interface {} {
392
+ // TODO deep copy?
393
+ return fb .metadata
394
+ }
395
+
355
396
// Error ret err
356
397
func (fb * FileBox ) Error () error {
357
398
return fb .err
0 commit comments