/
file_notifier.go
91 lines (73 loc) · 2.13 KB
/
file_notifier.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
package notification
import (
"bufio"
"context"
"fmt"
"net/mail"
"os"
"path/filepath"
"time"
"github.com/authelia/authelia/v4/internal/configuration/schema"
"github.com/authelia/authelia/v4/internal/templates"
)
// FileNotifier a notifier to send emails to SMTP servers.
type FileNotifier struct {
path string
append bool
}
// NewFileNotifier create an FileNotifier writing the notification into a file.
func NewFileNotifier(configuration schema.NotifierFileSystem) *FileNotifier {
return &FileNotifier{
path: configuration.Filename,
}
}
// StartupCheck implements the startup check provider interface.
func (n *FileNotifier) StartupCheck() (err error) {
dir := filepath.Dir(n.path)
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
if err = os.MkdirAll(dir, fileNotifierMode); err != nil {
return err
}
} else {
return err
}
} else if _, err = os.Stat(n.path); err != nil {
if !os.IsNotExist(err) {
return err
}
}
return os.WriteFile(n.path, []byte(""), fileNotifierMode)
}
// Send send a identity verification link to a user.
func (n *FileNotifier) Send(_ context.Context, recipient mail.Address, subject string, et *templates.EmailTemplate, data any) (err error) {
var f *os.File
var flag int
switch {
case n.append:
flag = os.O_APPEND | os.O_CREATE | os.O_WRONLY
default:
flag = os.O_TRUNC | os.O_CREATE | os.O_WRONLY
}
if f, err = os.OpenFile(n.path, flag, fileNotifierMode); err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()
w := bufio.NewWriter(f)
if _, err = w.WriteString(fmt.Sprintf(fileNotifierHeader, time.Now(), recipient, subject)); err != nil {
return fmt.Errorf("failed to write to the buffer: %w", err)
}
if err = et.Text.Execute(w, data); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
if _, err = w.Write(posixDoubleNewLine); err != nil {
return fmt.Errorf("failed to write to the buffer: %w", err)
}
if err = w.Flush(); err != nil {
return fmt.Errorf("failed to flush buffer: %w", err)
}
if err = f.Sync(); err != nil {
return fmt.Errorf("failed to sync the file: %w", err)
}
return nil
}