authelia/internal/notification/file_notifier.go
James Elliott 3695aa8140
feat(storage): primary key for all tables and general qol refactoring (#2431)
This is a massive overhaul to the SQL Storage for Authelia. It facilitates a whole heap of utility commands to help manage the database, primary keys, ensures all database requests use a context for cancellations, and paves the way for a few other PR's which improve the database.

Fixes #1337
2021-11-23 20:45:38 +11:00

61 lines
1.3 KiB
Go

package notification
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/authelia/authelia/v4/internal/configuration/schema"
)
// FileNotifier a notifier to send emails to SMTP servers.
type FileNotifier struct {
path string
}
// NewFileNotifier create an FileNotifier writing the notification into a file.
func NewFileNotifier(configuration schema.FileSystemNotifierConfiguration) *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
}
}
if err := ioutil.WriteFile(n.path, []byte(""), fileNotifierMode); err != nil {
return err
}
return nil
}
// Send send a identity verification link to a user.
func (n *FileNotifier) Send(recipient, subject, body, _ string) error {
content := fmt.Sprintf("Date: %s\nRecipient: %s\nSubject: %s\nBody: %s", time.Now(), recipient, subject, body)
err := ioutil.WriteFile(n.path, []byte(content), fileNotifierMode)
if err != nil {
return err
}
return nil
}