Skip to content

Commit

Permalink
Make sure attachments are world readable, stick in org specific dir
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier committed Aug 18, 2017
1 parent 3fd7e65 commit 5b6fea8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
7 changes: 4 additions & 3 deletions backends/rapidpro/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -64,7 +65,7 @@ func writeMsg(b *backend, msg courier.Msg) error {
// if we have media, go download it to S3
for i, attachment := range m.Attachments_ {
if strings.HasPrefix(attachment, "http") {
url, err := downloadMediaToS3(b, m.UUID_, attachment)
url, err := downloadMediaToS3(b, m.OrgID_, m.UUID_, attachment)
if err != nil {
return err
}
Expand Down Expand Up @@ -172,7 +173,7 @@ func readMsgFromDB(b *backend, id courier.MsgID) (*DBMsg, error) {
// Media download and classification
//-----------------------------------------------------------------------------

func downloadMediaToS3(b *backend, msgUUID courier.MsgUUID, mediaURL string) (string, error) {
func downloadMediaToS3(b *backend, orgID OrgID, msgUUID courier.MsgUUID, mediaURL string) (string, error) {
parsedURL, err := url.Parse(mediaURL)
if err != nil {
return "", err
Expand Down Expand Up @@ -231,7 +232,7 @@ func downloadMediaToS3(b *backend, msgUUID courier.MsgUUID, mediaURL string) (st
if extension != "" {
filename = fmt.Sprintf("%s.%s", msgUUID, extension)
}
path := filepath.Join(b.config.S3MediaPrefix, filename[:4], filename[4:8], filename)
path := filepath.Join(b.config.S3MediaPrefix, strconv.FormatInt(orgID.Int64, 10), filename[:4], filename[4:8], filename)
if !strings.HasPrefix(path, "/") {
path = fmt.Sprintf("/%s", path)
}
Expand Down
1 change: 1 addition & 0 deletions utils/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func PutS3File(s3Client s3iface.S3API, bucket string, path string, contentType s
Body: bytes.NewReader(contents),
Key: aws.String(path),
ContentType: aws.String(contentType),
ACL: aws.String(s3.BucketCannedACLPublicRead),
}
_, err := s3Client.PutObject(params)
if err != nil {
Expand Down

0 comments on commit 5b6fea8

Please sign in to comment.