-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdsend.go
More file actions
330 lines (305 loc) · 8.41 KB
/
Copy pathmdsend.go
File metadata and controls
330 lines (305 loc) · 8.41 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package mdsend
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"path"
"path/filepath"
"github.com/dkotik/mdsend/markdown"
"github.com/dkotik/mdsend/media"
"github.com/oklog/ulid/v2"
"github.com/yuin/goldmark/parser"
)
type FileReadError struct {
Path string
Cause error
}
func (err FileReadError) Error() string {
return fmt.Sprintf(
"file <%s> cannot be read: %v",
err.Path,
err.Cause,
)
}
func (err FileReadError) Unwrap() error {
return err.Cause
}
func NewFileReadError(p string, cause error) error {
return FileReadError{
Path: p,
Cause: cause,
}
}
type IdentifierGenerator interface {
GenerateID() (string, error)
}
type IdentifierGeneratorFunc func() (string, error)
func (f IdentifierGeneratorFunc) GenerateID() (string, error) {
return f()
}
type Mailer interface {
SendMail(context.Context, Message) (string, error)
}
type Loader interface {
LoadLetter(context.Context, string) (Letter, []Attachment, error)
}
type Defaults struct {
LetterIdentifierGenerator IdentifierGenerator
// Language language.Tag
MediaContraints media.Constraints
Schedule Schedule
}
type loader struct {
LetterIdentifierGenerator IdentifierGenerator
FileSystem fs.FS
// DefaultLanguage language.Tag
DefaultMediaContraints media.Constraints
DefaultSchedule Schedule
Parser parser.Parser
}
func New(fs fs.FS, options Defaults) (_ Loader, err error) {
if fs == nil {
return nil, errors.New("nil file system")
}
options.MediaContraints = options.MediaContraints.WithDefaults()
if err = options.MediaContraints.Validate(); err != nil {
return nil, err
}
if err = options.Schedule.Validate(); err != nil {
return nil, err
}
// if options.Language.String() == "und" {
// options.Language = language.English
// }
// if !locale.IsValidLanguageTag(options.Language) {
// return nil, fmt.Errorf("invalid language choice: %s", options.Language.String())
// }
if options.LetterIdentifierGenerator == nil {
options.LetterIdentifierGenerator = IdentifierGeneratorFunc(func() (string, error) {
id, err := ulid.New(ulid.Now(), ulid.DefaultEntropy())
if err != nil {
return "", err
}
return id.String(), nil
})
}
return loader{
LetterIdentifierGenerator: options.LetterIdentifierGenerator,
FileSystem: fs,
// DefaultLanguage: options.Language,
DefaultMediaContraints: options.MediaContraints,
DefaultSchedule: options.Schedule,
Parser: markdown.NewParser(markdown.DefaultLightTheme), // to options
}, nil
}
func (loader loader) loadLetterFromFile(
ctx context.Context,
p string,
rootDirectory string,
) (letter Letter, err error) {
file, err := loader.FileSystem.Open(p)
if err != nil {
return letter, NewFileReadError(p, err)
}
data, err := io.ReadAll(file)
if err != nil {
return letter, NewFileReadError(p, errors.Join(err, file.Close()))
}
if err = file.Close(); err != nil {
return letter, NewFileReadError(p, err)
}
letter, err = newLetter(data)
if err != nil {
return letter, NewFileReadError(p, err)
}
if letter.ID == "" {
id, err := loader.LetterIdentifierGenerator.GenerateID()
if err != nil {
return letter, err
}
letter.ID = id
}
letter, err = extend(ctx, letter, rootDirectory, loader.FileSystem)
if err != nil {
return letter, err
}
if _, err = newSubject(letter.Frontmatter[FieldNameSubject]); err != nil {
if errors.Is(err, ErrNoSubject) {
// pull the subject from the first heading text
letter.Frontmatter[FieldNameSubject] = markdown.GetFirstHeadingText([]byte(letter.Content))
if letter.Frontmatter[FieldNameSubject] == "" {
return letter, err
}
} else {
return letter, err
}
}
templates, err := getTemplates(letter.Frontmatter, rootDirectory)
if err != nil {
return letter, err
}
for _, t := range templates {
// if media.IsPathLocal(t) {
// t = path.Join(path.Dir(p), t)
// }
file, err := loader.FileSystem.Open(t)
if err != nil {
return letter, NewFileReadError(t, err)
}
data, err := io.ReadAll(file)
if err != nil {
return letter, NewFileReadError(t, errors.Join(err, file.Close()))
}
if err = file.Close(); err != nil {
return letter, NewFileReadError(t, err)
}
letter.Templates = append(letter.Templates, Attachment{
Name: t,
Content: data,
ContentType: media.ContentTypeTextHTML,
})
}
return letter, nil
}
func (loader loader) LoadAttachment(
ctx context.Context,
source AttachmentSource,
constraints media.Constraints,
) (a Attachment, err error) {
select {
case <-ctx.Done():
return a, ctx.Err()
default:
}
file, err := loader.FileSystem.Open(source.Location)
if err != nil {
return a, NewFileReadError(source.Location, err)
}
defer func() { err = errors.Join(err, file.Close()) }()
b, err := io.ReadAll(file)
if err != nil {
return a, NewFileReadError(source.Location, err)
}
a, err = NewAttachment(b, constraints)
if err != nil {
return a, err
}
a.Name = source.Name
return a, err
}
func (loader loader) LoadLetter(ctx context.Context, p string) (letter Letter, attachments []Attachment, err error) {
rootDirectory := path.Dir(p)
letter, err = loader.loadLetterFromFile(ctx, p, rootDirectory)
if err != nil {
return letter, nil, NewFileReadError(p, err)
}
domain, err := letter.GetDomain()
if err != nil {
return letter, nil, NewFileReadError(p, err)
}
// language, err := letter.GetLanguage()
// if err != nil {
// return letter, nil, err
// }
// if language.String() == "und" {
// letter.Frontmatter[FieldNameLanguage] = loader.DefaultLanguage.String()
// }
constraints, err := letter.GetMediaConstraints()
if err != nil {
return letter, nil, err
}
if constraints.Quality == 0 {
constraints.Quality = loader.DefaultMediaContraints.Quality
}
if constraints.Width == 0 {
constraints.Width = loader.DefaultMediaContraints.Width
}
if constraints.Height == 0 {
constraints.Height = loader.DefaultMediaContraints.Height
}
// load attachments from the front matter
for source, err := range letter.EachAttachment() {
if err != nil {
return letter, nil, fmt.Errorf("unable to decode attachment source %q: %w", source.Location, err)
}
if !filepath.IsAbs(source.Location) {
source.Location = filepath.Join(rootDirectory, source.Location)
}
for _, attachment := range attachments {
if attachment.Source == source.Location {
return letter, attachments, fmt.Errorf("duplicate attachment: %s", attachment.Source)
}
}
attachment, err := loader.LoadAttachment(
ctx,
source,
constraints,
)
if err != nil {
return letter, nil, NewFileReadError(source.Location, err)
}
attachment.LetterID = letter.ID
attachments = append(attachments, attachment)
}
// load inline attachments from the templates directory
for i, t := range letter.Templates {
if !filepath.IsAbs(t.Source) {
t.Source = filepath.Join(rootDirectory, t.Source)
}
letter.Templates[i].Content, err = inlineTemplateAttachments(
t.Content,
func(source AttachmentSource) (string, error) {
if !filepath.IsAbs(source.Location) {
source.Location = filepath.Join(t.Source, source.Location)
}
attachment, err := loader.LoadAttachment(
ctx,
source,
constraints,
)
if err != nil {
return "", NewFileReadError(source.Location, err)
}
attachment.ContentID = attachment.Hash + "@" + domain
for _, a := range attachments {
if a.ContentID == attachment.ContentID {
return "cid:" + attachment.ContentID, nil
}
}
attachment.LetterID = letter.ID
attachments = append(attachments, attachment)
return "cid:" + attachment.ContentID, nil
},
)
}
// load inline attachments from the Markdown content
letter.Content, err = inlineMarkdownAttachments(
loader.Parser,
[]byte(letter.Content),
func(source AttachmentSource) (string, error) {
if !filepath.IsAbs(source.Location) {
source.Location = filepath.Join(rootDirectory, source.Location)
}
attachment, err := loader.LoadAttachment(
ctx,
source,
constraints,
)
if err != nil {
return "", NewFileReadError(source.Location, err)
}
attachment.ContentID = attachment.Hash + "@" + domain
for _, a := range attachments {
if a.ContentID == attachment.ContentID {
return "cid:" + attachment.ContentID, nil
}
}
attachment.LetterID = letter.ID
attachments = append(attachments, attachment)
return "cid:" + attachment.ContentID, nil
},
)
return letter, attachments, err
}