aboutsummaryrefslogtreecommitdiffstats
path: root/lib/attachment.go
diff options
context:
space:
mode:
authorVitaly Ovchinnikov <v@postbox.nz>2023-08-04 23:33:07 +0300
committerRobin Jarry <robin@jarry.cc>2023-08-05 00:04:35 +0200
commit1144611a1626d8f5d7ffe02d76ea58a97a67aff4 (patch)
tree70fea6988636554d801569c11d779da34e2aa67e /lib/attachment.go
parentc801f1582cf6d5d3e367c5e2931381559746cccf (diff)
downloadaerc-1144611a1626d8f5d7ffe02d76ea58a97a67aff4.tar.gz
attach: add an option to pipe the attachments in
Add the -r option to :attach so that the attachments can be piped in from a command. Example: :attach -r image.jpg read-jpeg-from-clipboard.sh It takes two parameters: the attachment name (to be used in the email and to get the MIME type from) and the command to execute and read the output. Signed-off-by: Vitaly Ovchinnikov <v@postbox.nz> Acked-by: Robin Jarry <robin@jarry.cc> Reviewed-by: Koni Marti <koni.marti@gmail.com>
Diffstat (limited to 'lib/attachment.go')
-rw-r--r--lib/attachment.go40
1 files changed, 22 insertions, 18 deletions
diff --git a/lib/attachment.go b/lib/attachment.go
index f6118780..a8a8103d 100644
--- a/lib/attachment.go
+++ b/lib/attachment.go
@@ -66,24 +66,7 @@ func (fa *FileAttachment) WriteTo(w *mail.Writer) error {
reader := bufio.NewReader(f)
- // if we have an extension, prefer that instead of trying to sniff the header.
- // That's generally more accurate than sniffing as lots of things are zip files
- // under the hood, e.g. most office file types
- ext := filepath.Ext(fa.path)
- var mimeString string
- if mimeString = mime.TypeByExtension(ext); mimeString == "" {
- // Sniff the mime type since it's not in the database
- // http.DetectContentType only cares about the first 512 bytes
- head, err := reader.Peek(512)
- if err != nil && err != io.EOF {
- return errors.Wrap(err, "Peek")
- }
- mimeString = http.DetectContentType(head)
- }
-
- // mimeString can contain type and params (like text encoding),
- // so we need to break them apart before passing them to the headers
- mimeType, params, err := mime.ParseMediaType(mimeString)
+ mimeType, params, err := FindMimeType(fa.path, reader)
if err != nil {
return errors.Wrap(err, "ParseMediaType")
}
@@ -159,3 +142,24 @@ func SetUtf8Charset(origParams map[string]string) map[string]string {
}
return params
}
+
+func FindMimeType(filename string, reader *bufio.Reader) (string, map[string]string, error) {
+ // if we have an extension, prefer that instead of trying to sniff the header.
+ // That's generally more accurate than sniffing as lots of things are zip files
+ // under the hood, e.g. most office file types
+ ext := filepath.Ext(filename)
+ var mimeString string
+ if mimeString = mime.TypeByExtension(ext); mimeString == "" {
+ // Sniff the mime type since it's not in the database
+ // http.DetectContentType only cares about the first 512 bytes
+ head, err := reader.Peek(512)
+ if err != nil && err != io.EOF {
+ return "", map[string]string{}, errors.Wrap(err, "Peek")
+ }
+ mimeString = http.DetectContentType(head)
+ }
+
+ // mimeString can contain type and params (like text encoding),
+ // so we need to break them apart before passing them to the headers
+ return mime.ParseMediaType(mimeString)
+}