1package message
2
3import (
4 "encoding/base64"
5 "encoding/json"
6)
7
8type Attachment struct {
9 FilePath string `json:"file_path"`
10 FileName string `json:"file_name"`
11 MimeType string `json:"mime_type"`
12 Content []byte `json:"content"`
13}
14
15// MarshalJSON implements the [json.Marshaler] interface.
16func (a Attachment) MarshalJSON() ([]byte, error) {
17 // Encode the content as a base64 string
18 type Alias Attachment
19 return json.Marshal(&struct {
20 Content string `json:"content"`
21 *Alias
22 }{
23 Content: base64.StdEncoding.EncodeToString(a.Content),
24 Alias: (*Alias)(&a),
25 })
26}
27
28// UnmarshalJSON implements the [json.Unmarshaler] interface.
29func (a *Attachment) UnmarshalJSON(data []byte) error {
30 // Decode the content from a base64 string
31 type Alias Attachment
32 aux := &struct {
33 Content string `json:"content"`
34 *Alias
35 }{
36 Alias: (*Alias)(a),
37 }
38 if err := json.Unmarshal(data, &aux); err != nil {
39 return err
40 }
41 content, err := base64.StdEncoding.DecodeString(aux.Content)
42 if err != nil {
43 return err
44 }
45 a.Content = content
46 return nil
47}