|
| 1 | +package file_box |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "github.com/bitly/go-simplejson" |
| 10 | + "github.com/tuotoo/qrcode" |
| 11 | + helper_functions "github.com/wechaty/go-wechaty/wechaty-puppet/helper-functions" |
| 12 | + "io/ioutil" |
| 13 | + "mime" |
| 14 | + "os" |
| 15 | + "path/filepath" |
| 16 | +) |
| 17 | + |
| 18 | +type fileImplInterface interface { |
| 19 | + toJSONMap() map[string]interface{} |
| 20 | + toBytes() ([]byte, error) |
| 21 | +} |
| 22 | + |
| 23 | +// FileBox struct |
| 24 | +type FileBox struct { |
| 25 | + fileImpl fileImplInterface |
| 26 | + name string |
| 27 | + metadata map[string]interface{} |
| 28 | + boxType FileBoxType |
| 29 | + fileBytes []byte |
| 30 | + mimeType string |
| 31 | +} |
| 32 | + |
| 33 | +func newFileBox(common *FileBoxJsonObjectCommon, fileImpl fileImplInterface) *FileBox { |
| 34 | + return &FileBox{ |
| 35 | + fileImpl: fileImpl, |
| 36 | + name: common.Name, |
| 37 | + metadata: common.Metadata, |
| 38 | + boxType: common.BoxType, |
| 39 | + mimeType: mime.TypeByExtension(filepath.Ext(common.Name)), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func NewFileBoxFromJSONString(s string) (*FileBox, error) { |
| 44 | + newJson, err := simplejson.NewJson([]byte(s)) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + boxType, err := newJson.Get("boxType").Int64() |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + switch boxType { |
| 53 | + case FileBoxTypeBase64: |
| 54 | + fileBoxStruct := new(FileBoxJsonObjectBase64) |
| 55 | + if err := json.Unmarshal([]byte(s), fileBoxStruct); err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + return NewFileBoxFromJSONObjectBase64(fileBoxStruct), nil |
| 59 | + case FileBoxTypeQRCode: |
| 60 | + fileBoxStruct := new(FileBoxJsonObjectQRCode) |
| 61 | + if err := json.Unmarshal([]byte(s), fileBoxStruct); err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + return NewFileBoxFromJSONObjectQRCode(fileBoxStruct), nil |
| 65 | + case FileBoxTypeUrl: |
| 66 | + fileBoxStruct := new(FileBoxJsonObjectUrl) |
| 67 | + if err := json.Unmarshal([]byte(s), fileBoxStruct); err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + return NewFileBoxFromJSONObjectUrl(fileBoxStruct), nil |
| 71 | + default: |
| 72 | + return nil, errors.New("invalid value boxType") |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func NewFileBoxFromJSONObjectBase64(data *FileBoxJsonObjectBase64) *FileBox { |
| 77 | + return newFileBox(data.FileBoxJsonObjectCommon, newFileBoxBase64(data.Base64)) |
| 78 | +} |
| 79 | + |
| 80 | +func NewFileBoxFromJSONObjectUrl(data *FileBoxJsonObjectUrl) *FileBox { |
| 81 | + return newFileBox(data.FileBoxJsonObjectCommon, NewFileBoxUrl(data.RemoteUrl, data.Headers)) |
| 82 | +} |
| 83 | + |
| 84 | +func NewFileBoxFromJSONObjectQRCode(data *FileBoxJsonObjectQRCode) *FileBox { |
| 85 | + return newFileBox(data.FileBoxJsonObjectCommon, NewFileBoxQRCode(data.QrCode)) |
| 86 | +} |
| 87 | + |
| 88 | +func (fb *FileBox) ToJSONString() (string, error) { |
| 89 | + jsonMap := map[string]interface{}{ |
| 90 | + "name": fb.name, |
| 91 | + "metadata": fb.metadata, |
| 92 | + "boxType": fb.boxType, |
| 93 | + } |
| 94 | + implJsonMap := fb.fileImpl.toJSONMap() |
| 95 | + for k, v := range implJsonMap { |
| 96 | + jsonMap[k] = v |
| 97 | + } |
| 98 | + marshal, err := json.Marshal(jsonMap) |
| 99 | + return string(marshal), err |
| 100 | +} |
| 101 | + |
| 102 | +func (fb *FileBox) ToFile(filePath string, overwrite bool) error { |
| 103 | + if filePath == "" { |
| 104 | + filePath = fb.name |
| 105 | + } |
| 106 | + path, err := os.Getwd() |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + fullPath := filepath.Join(path, filePath) |
| 111 | + if !overwrite && helper_functions.FileExists(fullPath) { |
| 112 | + return os.ErrExist |
| 113 | + } |
| 114 | + fileBytes, err := fb.ToBytes() |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + return ioutil.WriteFile(filePath, fileBytes, os.ModePerm) |
| 119 | +} |
| 120 | + |
| 121 | +func (fb *FileBox) ToBytes() ([]byte, error) { |
| 122 | + if fb.fileBytes != nil { |
| 123 | + return fb.fileBytes, nil |
| 124 | + } |
| 125 | + toBytes, err := fb.fileImpl.toBytes() |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + fb.fileBytes = toBytes |
| 130 | + return fb.fileBytes, nil |
| 131 | +} |
| 132 | + |
| 133 | +func (fb *FileBox) ToBase64() (string, error) { |
| 134 | + fileBytes, err := fb.ToBytes() |
| 135 | + if err != nil { |
| 136 | + return "", err |
| 137 | + } |
| 138 | + return base64.StdEncoding.EncodeToString(fileBytes), nil |
| 139 | +} |
| 140 | + |
| 141 | +func (fb *FileBox) ToDataURL() (string, error) { |
| 142 | + toBase64, err := fb.ToBase64() |
| 143 | + if err != nil { |
| 144 | + return "", nil |
| 145 | + } |
| 146 | + return fmt.Sprintf("data:%s;base64,%s", fb.mimeType, toBase64), nil |
| 147 | +} |
| 148 | + |
| 149 | +func (fb *FileBox) ToQrCode() (string, error) { |
| 150 | + fileBytes, err := fb.ToBytes() |
| 151 | + if err != nil { |
| 152 | + return "", err |
| 153 | + } |
| 154 | + decode, err := qrcode.Decode(bytes.NewReader(fileBytes)) |
| 155 | + if err != nil { |
| 156 | + return "", nil |
| 157 | + } |
| 158 | + return decode.Content, nil |
| 159 | +} |
0 commit comments