forked from axigenmessaging/tnefdecoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattachment.go
161 lines (135 loc) · 3.81 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* specific decoders for attachments
*/
package tnefdecoder
import (
"strings"
rtf "github.com/linanh/rtfconverter"
// "fmt"
)
func NewAttachment() *Attachment {
return &Attachment{
TnefObject: &TnefObject{},
Filename: "",
Data: []byte{},
DecodedAttRendData: make(map[string]int),
}
}
type Attachment struct {
*TnefObject
Filename string
Data []byte
// custom attributes that needs specific decoders
DecodedAttRendData map[string]int
}
/**
* set attachment filename
*/
func (a *Attachment) SetFilename(t string) {
a.Filename = t
}
/**
* set attachment data
*/
func (a *Attachment) SetData(d []byte) {
a.Data = d
}
/**
* get attachment body
*/
func (a *Attachment) GetData() []byte {
if len(a.Data) == 0 {
// if the data is not already set (custom, using SetData, or previously requested) try to extract the data from attributes
attr := a.GetAttribute(AttAttachData, "mapped")
if attr != nil {
a.SetData(attr.Data)
}
}
return a.Data
}
func (a *Attachment) GetFilename() string {
var attr *Attribute
/*
attr = a.GetAttribute(AttAttachTransportFilename, "mapped")
if attr != nil {
fmt.Println("Transport Name:", attr.GetStringValue())
}
*/
if a.Filename == "" {
// if the Filename is not already set (custom, using SetFilename, or previously requested) try to extract the data from attributes
attr = a.GetAttribute(MapiPidTagAttachFilename, "mapi")
if attr != nil && attr.GetStringValue() != "" {
a.SetFilename(attr.GetStringValue())
}
}
if a.Filename == "" {
// if the Filename is not already set (custom, using SetFilename, or previously requested) try to extract the data from attributes
attr = a.GetAttribute(MapiPidTagDisplayName, "mapi")
if attr != nil && attr.GetStringValue() != "" {
a.SetFilename(attr.GetStringValue())
}
}
if a.Filename == "" {
// if the Filename is not already set (custom, using SetFilename, or previously requested) try to extract the data from attributes
attr = a.GetAttribute(AttAttachTitle, "mapped")
if attr != nil && attr.GetStringValue() != "" {
a.SetFilename(attr.GetStringValue())
}
}
if a.Filename == "" {
extension := ""
// if the Filename is not already set (custom, using SetFilename, or previously requested) try to extract the data from attributes
attr = a.GetAttribute(MapiPidTagAttachExtension, "mapi")
if attr != nil && attr.GetStringValue() != "" {
extension = attr.GetStringValue()
}
a.SetFilename("unkown" + extension)
}
if strings.Count(a.Filename, "?") != 0 || len(a.Filename) > 255 {
// the filename has invalid characters or is too long
attr = a.GetAttribute(MapiPidTagAttachFilename, "mapi")
if attr != nil && attr.GetStringValue() != "" {
a.SetFilename(attr.GetStringValue())
}
}
buf, _ := rtf.ConvertToUtf8([]byte(a.Filename), a.Encoding)
a.SetFilename(string(buf))
return a.Filename
}
/**
* check if the attachment has a reference in html as cid
* @param {[type]} a *Attachment) IsMimeRelated( [description]
* @return {[type]} [description]
*/
func (a *Attachment) HasCID() bool {
cid := a.GetCID()
// is not embeded -> the attachment data is kept into attAttachData attribute
return cid != ""
}
func (a *Attachment) GetCID() string {
attContentIdAttr := a.GetAttribute(MapiPidTagAttachContentId, "mapi")
if attContentIdAttr == nil {
return ""
}
return attContentIdAttr.GetStringValue()
}
/*
*
* AttachTypeFile= 1
* AttachTypeOle = 2
* if we cannot find the information return 0
*/
func (a *Attachment) GetRenderType() int {
if r, ok := a.DecodedAttRendData["AttachType"]; ok {
return r
}
return 0
}
//Get attachment mime type
func (a *Attachment) GetMimeType() string {
attMimeTagAttr := a.GetAttribute(MapiPidTagAttachMimeTag, "mapi")
if attMimeTagAttr == nil {
return ""
}
return attMimeTagAttr.GetStringValue()
}