-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattachment.go
More file actions
229 lines (209 loc) · 5.03 KB
/
Copy pathattachment.go
File metadata and controls
229 lines (209 loc) · 5.03 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package mdsend
import (
"bytes"
"errors"
"fmt"
"io"
"iter"
"log/slog"
"path"
"strings"
"github.com/btcsuite/btcd/btcutil/base58"
"github.com/cespare/xxhash/v2"
"github.com/dkotik/mdsend/media"
)
var _ slog.LogValuer = (*Attachment)(nil)
type AttachmentError uint8
const (
ErrInvalidAttachment AttachmentError = iota
ErrDuplicateAttachment
)
func (err AttachmentError) Error() string {
switch err {
case ErrInvalidAttachment:
return "invalid attachment"
case ErrDuplicateAttachment:
return "duplicate attachment"
default:
return ""
}
}
type Attachment struct {
LetterID string
Name string
Source string
Hash string
// ContentID is the ID of the inline attachment to use in the message.
// It must conform to RFC 2392 format, including the angle brackets:
// <hash@domain.com>. Leave empty for standalone attachments that
// are not referenced by other parts of the message.
ContentID string
ContentType string
Content []byte
}
type AttachmentSource struct {
Name string
Location string
}
type AttachmentReference struct {
ID string
Name string
ContentID string
ContentType string
}
func NewAttachment(b []byte, constraints media.Constraints) (a Attachment, err error) {
a.Content, a.ContentType, err = constraints.ApplyTo(b)
if err != nil {
return a, err
}
a.Hash = media.DeterministicHashStringOf(a.Content)
return a, nil
}
func (a Attachment) Validate() (err error) {
if a.LetterID == "" {
return errors.New("empty letter ID")
}
if a.Name == "" {
return errors.New("empty name")
}
if a.ContentType == "" {
return errors.New("empty content type")
}
if len(a.Content) == 0 {
return errors.New("empty content")
}
return nil
}
func (a Attachment) WithUpdatedHash() Attachment {
hash := xxhash.New()
var err error
if _, err = io.Copy(
hash,
bytes.NewReader(a.Content),
); err != nil {
panic(err)
}
a.Hash = base58.Encode(hash.Sum(nil))
// a.Hash = hash.Sum64()
return a
}
func (a Attachment) LogValue() slog.Value {
return slog.GroupValue(
slog.String("name", a.Name),
slog.String("content_type", a.ContentType),
slog.String("hash", a.Hash),
)
}
func (a Attachment) AssertEqualityTo(b Attachment) error {
if a.LetterID != b.LetterID {
return FieldComparisonMismatchError{
FieldName: "LetterID",
ExpectedValue: a.LetterID,
ActualValue: b.LetterID,
}
}
if a.Name != b.Name {
return FieldComparisonMismatchError{
FieldName: "Name",
ExpectedValue: a.Name,
ActualValue: b.Name,
}
}
if a.ContentID != b.ContentID {
return FieldComparisonMismatchError{
FieldName: "ContentID",
ExpectedValue: a.ContentID,
ActualValue: b.ContentID,
}
}
if a.ContentType != b.ContentType {
return FieldComparisonMismatchError{
FieldName: "Name",
ExpectedValue: a.ContentType,
ActualValue: b.ContentType,
}
}
if !bytes.Equal(a.Content, b.Content) {
return FieldComparisonMismatchError{
FieldName: "Name",
ExpectedValue: a.Content,
ActualValue: b.Content,
}
}
return nil
}
func (a Attachment) IsEqualTo(b Attachment) bool {
return a.AssertEqualityTo(b) == nil
}
func newAttachmentSourceFromMap(fm map[string]any) (a AttachmentSource, _ error) {
a.Location = strings.TrimSpace(fmt.Sprintf("%v", fm[FieldNameAttachmentLocation]))
switch name := fm[FieldNameAttachmentName].(type) {
case string:
a.Name = strings.TrimSpace(name)
if a.Name == "" {
a.Name = path.Base(a.Location)
}
case nil:
a.Name = path.Base(a.Location)
default:
a.Name = strings.TrimSpace(fmt.Sprintf("%v", name))
if a.Name == "" {
a.Name = path.Base(a.Location)
}
}
return a, nil
}
func newAttachmentSourceFromAny(fm any) (AttachmentSource, error) {
switch fm := fm.(type) {
case map[string]any:
return newAttachmentSourceFromMap(fm)
case string:
fm = strings.TrimSpace(fm)
return AttachmentSource{Name: path.Base(fm), Location: fm}, nil
default:
return AttachmentSource{}, fmt.Errorf("invalid attachment source: %T %+v", fm, fm)
}
}
func (l Letter) EachAttachment() iter.Seq2[AttachmentSource, error] {
return func(yield func(AttachmentSource, error) bool) {
switch fm := l.Frontmatter[FieldNameAttachments].(type) {
case []any:
for _, a := range fm {
if !yield(newAttachmentSourceFromAny(a)) {
return
}
}
case map[string]any:
if !yield(newAttachmentSourceFromMap(fm)) {
return
}
case string:
if !yield(newAttachmentSourceFromAny(fm)) {
return
}
case nil:
default:
yield(AttachmentSource{}, fmt.Errorf("invalid attachment source: %T %+v", fm, fm))
}
}
}
func EachAttachmentSourceRelativeWithPathPrefix(prefix string, each iter.Seq2[AttachmentSource, error]) iter.Seq2[AttachmentSource, error] {
if each == nil {
panic("attachment source list is nil")
}
return func(yield func(AttachmentSource, error) bool) {
for a, err := range each {
if err != nil {
if !yield(AttachmentSource{}, err) {
return
}
}
if media.IsPathLocal(a.Location) {
a.Location = path.Join(prefix, a.Location)
}
if !yield(a, nil) {
return
}
}
}
}