-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachment.go
157 lines (123 loc) · 3.02 KB
/
attachment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package visimail
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/url"
)
var (
ErrEmptyAttachment = errors.New("attachment is empty")
ErrEmptyAttachmentFilename = errors.New("attachment filename is empty")
ErrEmptyAttachmentContent = errors.New("attachment content is empty")
ErrInvalidAttachmentURL = errors.New("attachment url is invalid")
)
type AttachmentType int
const (
AttachmentTypeContent AttachmentType = iota
AttachmentTypeURL
)
type Attachment interface {
json.Marshaler
Type() AttachmentType
Validate() error
}
var emptyAttachmentContent = AttachmentContent{}
type AttachmentContent struct {
filename string
content []byte
}
func NewAttachmentContent(filename string, content []byte) AttachmentContent {
return AttachmentContent{filename, content}
}
func (a AttachmentContent) Filename() string {
return a.filename
}
func (a AttachmentContent) Content() []byte {
return a.content
}
func (a AttachmentContent) Base64Content() string {
return base64.StdEncoding.EncodeToString(a.Content())
}
func (a AttachmentContent) Equals(attachment AttachmentContent) bool {
return a.Filename() == attachment.Filename() && bytes.Equal(a.Content(), attachment.Content())
}
func (a AttachmentContent) IsZero() bool {
return a.Equals(emptyAttachmentContent)
}
func (a AttachmentContent) Type() AttachmentType {
return AttachmentTypeContent
}
func (a AttachmentContent) Validate() error {
if a.IsZero() {
return ErrEmptyAttachment
}
if len(a.Filename()) <= 0 {
return ErrEmptyAttachmentFilename
}
if len(a.Content()) <= 0 {
return ErrEmptyAttachmentContent
}
return nil
}
func (a AttachmentContent) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Content string `json:"content"`
Name string `json:"name"`
}{
Content: a.Base64Content(),
Name: a.Filename(),
})
}
var emptyAttachmentURL = AttachmentURL{}
type AttachmentURL struct {
url string
filename string
}
func NewAttachmentURL(url string, opts ...AttachmentURLOption) AttachmentURL {
a := AttachmentURL{url: url}
for _, opt := range opts {
opt(&a)
}
return a
}
func (a AttachmentURL) URL() string {
return a.url
}
func (a AttachmentURL) Filename() string {
return a.filename
}
func (a AttachmentURL) Equals(attachment AttachmentURL) bool {
return a == attachment
}
func (a AttachmentURL) IsZero() bool {
return a.Equals(emptyAttachmentURL)
}
func (a AttachmentURL) Type() AttachmentType {
return AttachmentTypeURL
}
func (a AttachmentURL) Validate() error {
if a.IsZero() {
return ErrEmptyAttachment
}
if _, err := url.Parse(a.URL()); err != nil {
return fmt.Errorf("%w: %v", ErrInvalidAttachmentURL, err)
}
return nil
}
func (a AttachmentURL) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
URL string `json:"url"`
Name string `json:"name,omitempty"`
}{
URL: a.URL(),
Name: a.Filename(),
})
}
type AttachmentURLOption func(*AttachmentURL)
func WithFilename(name string) AttachmentURLOption {
return func(a *AttachmentURL) {
a.filename = name
}
}