use gabriel-vasile/mimetype for the mail attachments

This commit is contained in:
Gani Georgiev
2025-03-18 00:50:35 +02:00
parent 824c7db388
commit 9f1946057f
4 changed files with 108 additions and 2 deletions

View File

@@ -1,9 +1,11 @@
package mailer
import (
"bytes"
"io"
"net/mail"
"github.com/gabriel-vasile/mimetype"
"github.com/pocketbase/pocketbase/tools/hook"
)
@@ -54,3 +56,17 @@ func addressesToStrings(addresses []mail.Address, withName bool) []string {
return result
}
// detectReaderMimeType reads the first couple bytes of the reader to detect its MIME type.
//
// Returns a new combined reader from the partial read + the remaining of the original reader.
func detectReaderMimeType(r io.Reader) (io.Reader, string, error) {
readCopy := new(bytes.Buffer)
mime, err := mimetype.DetectReader(io.TeeReader(r, readCopy))
if err != nil {
return nil, "", err
}
return io.MultiReader(readCopy, r), mime.String(), nil
}