Skip to content

Commit

Permalink
Refactor file handling to use io/fs interface
Browse files Browse the repository at this point in the history
Updated functions to use the io/fs package instead of embed.FS, making the code more flexible with respect to different filesystem implementations. Revised the method signatures and related documentation to reflect this change.
  • Loading branch information
wneessen committed Nov 18, 2024
1 parent bcc3252 commit b137fe4
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
ht "html/template"
"io"
"io/fs"
"mime"
"net/mail"
"os"
Expand Down Expand Up @@ -1964,7 +1965,7 @@ func (m *Msg) AttachFromEmbedFS(name string, fs *embed.FS, opts ...FileOption) e
if fs == nil {
return fmt.Errorf("embed.FS must not be nil")
}
file, err := fileFromEmbedFS(name, fs)
file, err := fileFromIOFS(name, fs)
if err != nil {
return err
}
Expand Down Expand Up @@ -2110,7 +2111,7 @@ func (m *Msg) EmbedFromEmbedFS(name string, fs *embed.FS, opts ...FileOption) er
if fs == nil {
return fmt.Errorf("embed.FS must not be nil")
}
file, err := fileFromEmbedFS(name, fs)
file, err := fileFromIOFS(name, fs)
if err != nil {
return err
}
Expand Down Expand Up @@ -2666,32 +2667,32 @@ func (m *Msg) addDefaultHeader() {
m.SetGenHeader(HeaderMIMEVersion, string(m.mimever))
}

// fileFromEmbedFS returns a File pointer from a given file in the provided embed.FS.
// fileFromIOFS returns a File pointer from a given file in the provided fs.FS.
//
// This method retrieves a file from the embedded filesystem (embed.FS) and returns a File structure
// This method retrieves a file from the provided io/fs (fs.FS) and returns a File structure
// that can be used as an attachment or embed in the email message. The file's content is read when
// writing to an io.Writer, and the file is identified by its base name.
//
// Parameters:
// - name: The name of the file to retrieve from the embedded filesystem.
// - fs: A pointer to the embed.FS from which the file will be opened.
// - fs: An instance that satisfies the fs.FS interface
//
// Returns:
// - A pointer to the File structure representing the embedded file.
// - An error if the file cannot be opened or read from the embedded filesystem.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func fileFromEmbedFS(name string, fs *embed.FS) (*File, error) {
_, err := fs.Open(name)
func fileFromIOFS(name string, iofs fs.FS) (*File, error) {
_, err := iofs.Open(name)
if err != nil {
return nil, fmt.Errorf("failed to open file from embed.FS: %w", err)
}
return &File{
Name: filepath.Base(name),
Header: make(map[string][]string),
Writer: func(writer io.Writer) (int64, error) {
file, err := fs.Open(name)
file, err := iofs.Open(name)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit b137fe4

Please sign in to comment.