Debian Codegraph @

lib/attachment.go

raw
1 package lib
2
3 import (
4 "bufio"
5 "bytes"
6 "io"
7 "mime"
8 "net/http"
9 "os"
10 "path/filepath"
11 "strings"
12
13 "git.sr.ht/~rjarry/aerc/lib/log"
14 "git.sr.ht/~rjarry/aerc/lib/xdg"
15 "github.com/emersion/go-message/mail"
16 "github.com/pkg/errors"
17 )
18
19 type Part struct {
20 MimeType string
21 Params map[string]string
22 Data []byte
23 Converted bool
24 ConversionError error
25 }
26
27 func NewPart(mimetype string, params map[string]string, body io.Reader,
28 ) (*Part, error) {
29 var d []byte
30 var err error
31 var converted bool
32 if body == nil {
33 converted = true
34 } else {
35 d, err = io.ReadAll(body)
36 if err != nil {
37 return nil, err
38 }
39 }
40 return &Part{
41 MimeType: mimetype,
42 Params: params,
43 Data: d,
44 Converted: converted,
45 }, nil
46 }
47
48 func (p *Part) NewReader() io.Reader {
49 return bytes.NewReader(p.Data)
50 }
51
52 type Attachment interface {
53 Name() string
54 WriteTo(w *mail.Writer) error
55 }
56
57 type FileAttachment struct {
58 path string
59 }
60
61 func NewFileAttachment(path string) *FileAttachment {
62 return &FileAttachment{
63 path,
64 }
65 }
66
67 func (fa *FileAttachment) Name() string {
68 return fa.path
69 }
70
71 func (fa *FileAttachment) WriteTo(w *mail.Writer) error {
72 f, err := os.Open(xdg.ExpandHome(fa.path))
73 if err != nil {
74 return errors.Wrap(err, "os.Open")
75 }
76 defer f.Close()
77
78 reader := bufio.NewReader(f)
79
80 mimeType, params, err := FindMimeType(fa.path, reader)
81 if err != nil {
82 return errors.Wrap(err, "ParseMediaType")
83 }
84 filename := filepath.Base(fa.path)
85 params["name"] = filename
86
87 // set header fields
88 ah := mail.AttachmentHeader{}
89 ah.SetContentType(mimeType, params)
90 // setting the filename auto sets the content disposition
91 ah.SetFilename(filename)
92
93 fixContentTransferEncoding(mimeType, &ah)
94
95 aw, err := w.CreateAttachment(ah)
96 if err != nil {
97 return errors.Wrap(err, "CreateAttachment")
98 }
99 defer aw.Close()
100
101 if _, err := reader.WriteTo(aw); err != nil {
102 return errors.Wrap(err, "reader.WriteTo")
103 }
104
105 return nil
106 }
107
108 type PartAttachment struct {
109 part *Part
110 name string
111 }
112
113 func NewPartAttachment(part *Part, name string) *PartAttachment {
114 return &PartAttachment{
115 part,
116 name,
117 }
118 }
119
120 func (pa *PartAttachment) Name() string {
121 return pa.name
122 }
123
124 func (pa *PartAttachment) WriteTo(w *mail.Writer) error {
125 // set header fields
126 ah := mail.AttachmentHeader{}
127 ah.SetContentType(pa.part.MimeType, pa.part.Params)
128
129 // setting the filename auto sets the content disposition
130 ah.SetFilename(pa.Name())
131
132 fixContentTransferEncoding(pa.part.MimeType, &ah)
133
134 aw, err := w.CreateAttachment(ah)
135 if err != nil {
136 return errors.Wrap(err, "CreateAttachment")
137 }
138 defer aw.Close()
139
140 if _, err := io.Copy(aw, pa.part.NewReader()); err != nil {
141 return errors.Wrap(err, "io.Copy")
142 }
143 return nil
144 }
145
146 // SetUtf8Charset sets the charset in a params map to UTF-8.
147 func SetUtf8Charset(origParams map[string]string) map[string]string {
148 params := make(map[string]string)
149 for k, v := range origParams {
150 switch strings.ToLower(k) {
151 case "charset":
152 log.Debugf("substitute charset %s with utf-8", v)
153 params[k] = "utf-8"
154 default:
155 params[k] = v
156 }
157 }
158 return params
159 }
160
161 func FindMimeType(filename string, reader *bufio.Reader) (string, map[string]string, error) {
162 // if we have an extension, prefer that instead of trying to sniff the header.
163 // That's generally more accurate than sniffing as lots of things are zip files
164 // under the hood, e.g. most office file types
165 ext := filepath.Ext(filename)
166 var mimeString string
167 if mimeString = mime.TypeByExtension(ext); mimeString == "" {
168 // Sniff the mime type since it's not in the database
169 // http.DetectContentType only cares about the first 512 bytes
170 head, err := reader.Peek(512)
171 if err != nil && err != io.EOF {
172 return "", map[string]string{}, errors.Wrap(err, "Peek")
173 }
174 mimeString = http.DetectContentType(head)
175 }
176
177 // mimeString can contain type and params (like text encoding),
178 // so we need to break them apart before passing them to the headers
179 return mime.ParseMediaType(mimeString)
180 }
181
182 // fixContentTransferEncoding checks the mime type of the attachment and
183 // corrects the content-transfer-encoding if necessary.
184 //
185 // It's expressly forbidden by RFC2046 to set any other
186 // content-transfer-encoding than 7bit, 8bit, or binary for
187 // message/rfc822 mime types (see RFC2046, section 5.2.1)
188 func fixContentTransferEncoding(mimeType string, header *mail.AttachmentHeader) {
189 if strings.ToLower(mimeType) == "message/rfc822" {
190 header.Add("Content-Transfer-Encoding", "binary")
191 }
192 }