| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
|
| 7 |
"github.com/ProtonMail/gluon/rfc822" |
| 8 |
"github.com/go-resty/resty/v2" |
| 9 |
) |
| 10 |
|
| 11 |
type ImportReq struct { |
| 12 |
Metadata ImportMetadata |
| 13 |
Message []byte |
| 14 |
} |
| 15 |
|
| 16 |
type namedImportReq struct { |
| 17 |
ImportReq |
| 18 |
|
| 19 |
Name string |
| 20 |
} |
| 21 |
|
| 22 |
type ImportMetadata struct { |
| 23 |
AddressID string |
| 24 |
LabelIDs []string |
| 25 |
Unread Bool |
| 26 |
Flags MessageFlag |
| 27 |
} |
| 28 |
|
| 29 |
type ImportRes struct { |
| 30 |
APIError |
| 31 |
MessageID string |
| 32 |
} |
| 33 |
|
| 34 |
func buildImportReqFields(req []namedImportReq) ([]*resty.MultipartField, error) { |
| 35 |
var fields []*resty.MultipartField |
| 36 |
|
| 37 |
metadata := make(map[string]ImportMetadata, len(req)) |
| 38 |
|
| 39 |
for _, req := range req { |
| 40 |
metadata[req.Name] = req.Metadata |
| 41 |
|
| 42 |
fields = append(fields, &resty.MultipartField{ |
| 43 |
Param: req.Name, |
| 44 |
FileName: req.Name + ".eml", |
| 45 |
ContentType: string(rfc822.MessageRFC822), |
| 46 |
Reader: bytes.NewReader(append(req.Message, "\r\n"...)), |
| 47 |
}) |
| 48 |
} |
| 49 |
|
| 50 |
b, err := json.Marshal(metadata) |
| 51 |
if err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
|
| 55 |
fields = append(fields, &resty.MultipartField{ |
| 56 |
Param: "Metadata", |
| 57 |
ContentType: "application/json", |
| 58 |
Reader: bytes.NewReader(b), |
| 59 |
}) |
| 60 |
|
| 61 |
return fields, nil |
| 62 |
} |