-
Notifications
You must be signed in to change notification settings - Fork 1
/
attachment.go
48 lines (40 loc) · 1.13 KB
/
attachment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package email
import (
"context"
"fmt"
"net/http"
"github.com/domonda/go-types/uu"
"github.com/ungerik/go-fs"
)
// Attachment implements fs.FileReader
var _ fs.FileReader = new(Attachment)
// Attachment of an Email.
// Attachment implements fs.FileReader
type Attachment struct {
PartID string `json:"partID,omitempty"`
ContentID string `json:"contentID,omitempty"`
ContentType string `json:"contentType,omitempty"`
Inline bool `json:"inline,omitempty"`
fs.MemFile
}
func NewAttachment(partID, filename string, content []byte) *Attachment {
return &Attachment{
PartID: partID,
ContentID: uu.IDv4().Hex(),
ContentType: http.DetectContentType(content),
MemFile: fs.MemFile{
FileName: filename,
FileData: content,
},
}
}
func NewAttachmentReadFile(ctx context.Context, partID string, file fs.FileReader) (*Attachment, error) {
data, err := file.ReadAllContext(ctx)
if err != nil {
return nil, err
}
return NewAttachment(partID, file.Name(), data), nil
}
func (a *Attachment) String() string {
return fmt.Sprintf("Attachment{ID: `%s`, File: `%s`, Size: %d}", a.PartID, a.FileName, len(a.FileData))
}