forked from pauldemarco/caldav-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachment.go
135 lines (115 loc) · 2.94 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
package values
import (
"encoding/base64"
"log"
"strconv"
"github.com/soft-stech/caldav-go/icalendar/properties"
)
type Attachment struct {
Url *Url
BinaryAttachment *BinaryAttachment
FmtType string
Filename string
Size string
rawValue string
}
var _ = log.Print
type AttachmentValueType string
const (
AttachmentValueType_Binary = "BINARY"
)
func NewBinaryAttachment(filename, formatType string, data []byte) *Attachment {
base64Str := base64.StdEncoding.EncodeToString(data)
return &Attachment{
Filename: filename,
FmtType: formatType,
Size: strconv.Itoa(len(data)),
BinaryAttachment: &BinaryAttachment{
ValueType: AttachmentValueType_Binary,
Encoding: BinaryAttachmentEncoding_Base64,
Value: base64Str,
},
}
}
func NewUrlAttachment(formatType string, urlStr string) *Attachment {
url := &Url{}
url.DecodeICalValue(urlStr)
return &Attachment{
FmtType: formatType,
Url: url,
}
}
func (a *Attachment) EncodeICalName() (properties.PropertyName, error) {
return properties.AttachmentPropertyName, nil
}
func (a *Attachment) EncodeICalValue() (string, error) {
if a.Url != nil {
return a.Url.EncodeICalValue()
} else {
return a.BinaryAttachment.EncodeICalValue()
}
}
func (a *Attachment) EncodeICalParams() (params properties.Params, err error) {
if a.Url != nil {
params, err = a.Url.EncodeICalParams()
} else {
params, err = a.BinaryAttachment.EncodeICalParams()
}
if err != nil {
return
}
if len(a.FmtType) > 0 {
params = append(params, properties.Param{Name: properties.FmtTypePropertyName, Value: a.FmtType})
}
if len(a.Size) > 0 {
params = append(params, properties.Param{Name: properties.SizePropertyName, Value: a.Size})
}
if len(a.Filename) > 0 {
params = append(params, properties.Param{Name: properties.FilenamePropertyName, Value: a.Filename})
}
return
}
func (a *Attachment) DecodeICalValue(value string) error {
a.rawValue = value
return nil
}
func (a *Attachment) DecodeICalParams(params properties.Params) error {
for _, param := range params {
if param.Name == properties.ValuePropertyName {
if param.Value == AttachmentValueType_Binary {
a.BinaryAttachment = &BinaryAttachment{Value: a.rawValue, ValueType: AttachmentValueType_Binary}
}
break
}
}
for _, param := range params {
if param.Name == properties.FmtTypePropertyName {
a.FmtType = param.Value
break
}
}
for _, param := range params {
if param.Name == properties.SizePropertyName {
a.Size = param.Value
break
}
}
for _, param := range params {
if param.Name == properties.FilenamePropertyName {
a.Filename = param.Value
break
}
}
if a.BinaryAttachment != nil {
for _, param := range params {
if param.Name == properties.EncodingPropertyName {
a.BinaryAttachment.Encoding = BinaryAttachmentEncoding(param.Value)
break
}
}
} else {
a.Url = &Url{}
a.Url.DecodeICalValue(a.rawValue)
}
return nil
}