summaryrefslogtreecommitdiffstats
path: root/utils/inbucket.go
diff options
context:
space:
mode:
authorJonathan <jonfritz@gmail.com>2018-02-07 09:02:46 -0500
committerGitHub <noreply@github.com>2018-02-07 09:02:46 -0500
commitd3e934d07ac0a58a24a435ea7c5b3bd222ef509a (patch)
treeb356b134878ee8686326475b04c90317e195bf86 /utils/inbucket.go
parentb2ee5077931013d308aaf60d790d341e2cb0c3e3 (diff)
downloadchat-d3e934d07ac0a58a24a435ea7c5b3bd222ef509a.tar.gz
chat-d3e934d07ac0a58a24a435ea7c5b3bd222ef509a.tar.bz2
chat-d3e934d07ac0a58a24a435ea7c5b3bd222ef509a.zip
XYZ-35: Added Support for GlobalRelay Compliance Export Format
* Added username to ChannelMemberHistory struct in anticipation of supporting GlobalRelay in Compliance Export * Removed translation from debug output - this makes it complicated to use utils functions from tests in the enterprise repo * Added an advanced email function that allows for greater control over message details. Updated MessageExport config to support GlobalRelay. Added attachment support to InBucket unit tests * Moving templates in from enterprise to solve test issues * Added export format to diagnostics * Changed email attachment code to use FileBackend so that S3 storage is properly supported
Diffstat (limited to 'utils/inbucket.go')
-rw-r--r--utils/inbucket.go52
1 files changed, 46 insertions, 6 deletions
diff --git a/utils/inbucket.go b/utils/inbucket.go
index 46011989b..5c40d5757 100644
--- a/utils/inbucket.go
+++ b/utils/inbucket.go
@@ -4,6 +4,7 @@
package utils
import (
+ "bytes"
"encoding/json"
"fmt"
"io"
@@ -37,6 +38,12 @@ type JSONMessageInbucket struct {
Text string
HTML string `json:"Html"`
}
+ Attachments []struct {
+ Filename string
+ ContentType string `json:"content-type"`
+ DownloadLink string `json:"download-link"`
+ Bytes []byte `json:"-"`
+ }
}
func ParseEmail(email string) string {
@@ -89,21 +96,54 @@ func GetMessageFromMailbox(email, id string) (results JSONMessageInbucket, err e
var record JSONMessageInbucket
url := fmt.Sprintf("%s%s%s/%s", getInbucketHost(), INBUCKET_API, parsedEmail, id)
- req, err := http.NewRequest("GET", url, nil)
+ emailResponse, err := get(url)
if err != nil {
return record, err
}
+ defer emailResponse.Body.Close()
+
+ err = json.NewDecoder(emailResponse.Body).Decode(&record)
+
+ // download attachments
+ if record.Attachments != nil && len(record.Attachments) > 0 {
+ for i := range record.Attachments {
+ if bytes, err := downloadAttachment(record.Attachments[i].DownloadLink); err != nil {
+ return record, err
+ } else {
+ record.Attachments[i].Bytes = make([]byte, len(bytes))
+ copy(record.Attachments[i].Bytes, bytes)
+ }
+ }
+ }
- client := &http.Client{}
+ return record, err
+}
+
+func downloadAttachment(url string) ([]byte, error) {
+ attachmentResponse, err := get(url)
+ if err != nil {
+ return nil, err
+ }
+ defer attachmentResponse.Body.Close()
+
+ buf := new(bytes.Buffer)
+ io.Copy(buf, attachmentResponse.Body)
+ return buf.Bytes(), nil
+}
+
+func get(url string) (*http.Response, error) {
+ req, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ return nil, err
+ }
+ client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
- return record, err
+ return nil, err
}
- defer resp.Body.Close()
- err = json.NewDecoder(resp.Body).Decode(&record)
- return record, err
+ return resp, nil
}
func DeleteMailBox(email string) (err error) {