Skip to content

Commit 4e94a56

Browse files
authored
Merge pull request #678 from nyaruka/rename_media_stuff
Rename media things to attachments
2 parents e1ec059 + fcf018f commit 4e94a56

File tree

12 files changed

+48
-48
lines changed

12 files changed

+48
-48
lines changed

core/models/orgs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (o *Org) AirtimeService(httpClient *http.Client, httpRetries *httpx.RetryCo
171171

172172
// StoreAttachment saves an attachment to storage
173173
func (o *Org) StoreAttachment(ctx context.Context, rt *runtime.Runtime, filename string, contentType string, content io.ReadCloser) (utils.Attachment, error) {
174-
prefix := rt.Config.S3MediaPrefix
174+
prefix := rt.Config.S3AttachmentsPrefix
175175

176176
// read the content
177177
contentBytes, err := io.ReadAll(content)
@@ -187,7 +187,7 @@ func (o *Org) StoreAttachment(ctx context.Context, rt *runtime.Runtime, filename
187187

188188
path := o.attachmentPath(prefix, filename)
189189

190-
url, err := rt.MediaStorage.Put(ctx, path, contentType, contentBytes)
190+
url, err := rt.AttachmentStorage.Put(ctx, path, contentType, contentBytes)
191191
if err != nil {
192192
return "", errors.Wrapf(err, "unable to store attachment content")
193193
}

core/models/orgs_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestStoreAttachment(t *testing.T) {
7171
attachment, err := org.StoreAttachment(context.Background(), rt, "668383ba-387c-49bc-b164-1213ac0ea7aa.jpg", "image/jpeg", image)
7272
require.NoError(t, err)
7373

74-
assert.Equal(t, utils.Attachment("image/jpeg:_test_media_storage/media/1/6683/83ba/668383ba-387c-49bc-b164-1213ac0ea7aa.jpg"), attachment)
74+
assert.Equal(t, utils.Attachment("image/jpeg:_test_attachments_storage/attachments/1/6683/83ba/668383ba-387c-49bc-b164-1213ac0ea7aa.jpg"), attachment)
7575

7676
// err trying to read from same reader again
7777
_, err = org.StoreAttachment(context.Background(), rt, "668383ba-387c-49bc-b164-1213ac0ea7aa.jpg", "image/jpeg", image)

mailroom.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,22 @@ func (mr *Mailroom) Start() error {
129129
if err != nil {
130130
return err
131131
}
132-
mr.rt.MediaStorage = storage.NewS3(s3Client, mr.rt.Config.S3MediaBucket, c.S3Region, s3.BucketCannedACLPublicRead, 32)
132+
mr.rt.AttachmentStorage = storage.NewS3(s3Client, mr.rt.Config.S3AttachmentsBucket, c.S3Region, s3.BucketCannedACLPublicRead, 32)
133133
mr.rt.SessionStorage = storage.NewS3(s3Client, mr.rt.Config.S3SessionBucket, c.S3Region, s3.ObjectCannedACLPrivate, 32)
134134
} else {
135-
mr.rt.MediaStorage = storage.NewFS("_storage", 0766)
135+
mr.rt.AttachmentStorage = storage.NewFS("_storage", 0766)
136136
mr.rt.SessionStorage = storage.NewFS("_storage", 0766)
137137
}
138138

139-
// test our media storage
139+
// test our attachment storage
140140
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
141-
err = mr.rt.MediaStorage.Test(ctx)
141+
err = mr.rt.AttachmentStorage.Test(ctx)
142142
cancel()
143143

144144
if err != nil {
145-
log.WithError(err).Error(mr.rt.MediaStorage.Name() + " media storage not available")
145+
log.WithError(err).Error(mr.rt.AttachmentStorage.Name() + " attachment storage not available")
146146
} else {
147-
log.Info(mr.rt.MediaStorage.Name() + " media storage ok")
147+
log.Info(mr.rt.AttachmentStorage.Name() + " attachment storage ok")
148148
}
149149

150150
ctx, cancel = context.WithTimeout(context.Background(), time.Second*10)

runtime/config.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ type Config struct {
4949
MaxValueLength int `help:"the maximum size in characters for contact field values and run result values"`
5050
SessionStorage string `validate:"omitempty,session_storage" help:"where to store session output (s3|db)"`
5151

52-
S3Endpoint string `help:"the S3 endpoint we will write attachments to"`
53-
S3Region string `help:"the S3 region we will write attachments to"`
54-
S3MediaBucket string `help:"the S3 bucket we will write attachments to"`
55-
S3MediaPrefix string `help:"the prefix that will be added to attachment filenames"`
56-
S3SessionBucket string `help:"the S3 bucket we will write attachments to"`
57-
S3SessionPrefix string `help:"the prefix that will be added to attachment filenames"`
58-
S3DisableSSL bool `help:"whether we disable SSL when accessing S3. Should always be set to False unless you're hosting an S3 compatible service within a secure internal network"`
59-
S3ForcePathStyle bool `help:"whether we force S3 path style. Should generally need to default to False unless you're hosting an S3 compatible service"`
52+
S3Endpoint string `help:"the S3 endpoint we will write attachments to"`
53+
S3Region string `help:"the S3 region we will write attachments to"`
54+
S3AttachmentsBucket string `help:"the S3 bucket we will write attachments to"`
55+
S3AttachmentsPrefix string `help:"the prefix that will be added to attachment filenames"`
56+
S3SessionBucket string `help:"the S3 bucket we will write attachments to"`
57+
S3SessionPrefix string `help:"the prefix that will be added to attachment filenames"`
58+
S3DisableSSL bool `help:"whether we disable SSL when accessing S3. Should always be set to False unless you're hosting an S3 compatible service within a secure internal network"`
59+
S3ForcePathStyle bool `help:"whether we force S3 path style. Should generally need to default to False unless you're hosting an S3 compatible service"`
6060

6161
AWSAccessKeyID string `help:"the access key id to use when authenticating S3"`
6262
AWSSecretAccessKey string `help:"the secret access key id to use when authenticating S3"`
@@ -106,14 +106,14 @@ func NewDefaultConfig() *Config {
106106
MaxValueLength: 640,
107107
SessionStorage: "db",
108108

109-
S3Endpoint: "https://s3.amazonaws.com",
110-
S3Region: "us-east-1",
111-
S3MediaBucket: "mailroom-media",
112-
S3MediaPrefix: "/media/",
113-
S3SessionBucket: "mailroom-sessions",
114-
S3SessionPrefix: "/",
115-
S3DisableSSL: false,
116-
S3ForcePathStyle: false,
109+
S3Endpoint: "https://s3.amazonaws.com",
110+
S3Region: "us-east-1",
111+
S3AttachmentsBucket: "mailroom-attachments",
112+
S3AttachmentsPrefix: "/attachments/",
113+
S3SessionBucket: "mailroom-sessions",
114+
S3SessionPrefix: "/",
115+
S3DisableSSL: false,
116+
S3ForcePathStyle: false,
117117

118118
AWSAccessKeyID: "",
119119
AWSSecretAccessKey: "",

runtime/runtime.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
// Runtime represents the set of services required to run many Mailroom functions. Used as a wrapper for
1111
// those services to simplify call signatures but not create a direct dependency to Mailroom or Server
1212
type Runtime struct {
13-
DB *sqlx.DB
14-
ReadonlyDB *sqlx.DB
15-
RP *redis.Pool
16-
ES *elastic.Client
17-
MediaStorage storage.Storage
18-
SessionStorage storage.Storage
19-
Config *Config
13+
DB *sqlx.DB
14+
ReadonlyDB *sqlx.DB
15+
RP *redis.Pool
16+
ES *elastic.Client
17+
AttachmentStorage storage.Storage
18+
SessionStorage storage.Storage
19+
Config *Config
2020
}

services/tickets/mailgun/testdata/receive.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@
301301
},
302302
"db_assertions": [
303303
{
304-
"query": "select count(*) from msgs_msg where direction = 'O' AND uuid = '5802813d-6c58-4292-8228-9728778b6c98' AND attachments = '{text/plain:https:///_test_media_storage/media/1/8720/f157/8720f157-ca1c-432f-9c0b-2014ddc77094.txt,image/jpeg:https:///_test_media_storage/media/1/c34b/6c7d/c34b6c7d-fa06-4563-92a3-d648ab64bccb.jpg}'",
304+
"query": "select count(*) from msgs_msg where direction = 'O' AND uuid = '5802813d-6c58-4292-8228-9728778b6c98' AND attachments = '{text/plain:https:///_test_attachments_storage/attachments/1/8720/f157/8720f157-ca1c-432f-9c0b-2014ddc77094.txt,image/jpeg:https:///_test_attachments_storage/attachments/1/c34b/6c7d/c34b6c7d-fa06-4563-92a3-d648ab64bccb.jpg}'",
305305
"count": 1
306306
},
307307
{

services/tickets/rocketchat/testdata/event_callback.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
},
193193
"db_assertions": [
194194
{
195-
"query": "select count(*) from msgs_msg where direction = 'O' and attachments = '{text/plain:https:///_test_media_storage/media/1/6929/26ea/692926ea-09d6-4942-bd38-d266ec8d3716.jpg}'",
195+
"query": "select count(*) from msgs_msg where direction = 'O' and attachments = '{text/plain:https:///_test_attachments_storage/attachments/1/6929/26ea/692926ea-09d6-4942-bd38-d266ec8d3716.jpg}'",
196196
"count": 1
197197
}
198198
]

services/tickets/utils_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ func TestSendReply(t *testing.T) {
131131

132132
assert.Equal(t, "I'll get back to you", msg.Text())
133133
assert.Equal(t, testdata.Cathy.ID, msg.ContactID())
134-
assert.Equal(t, []utils.Attachment{"image/jpeg:https:///_test_media_storage/media/1/e718/7099/e7187099-7d38-4f60-955c-325957214c42.jpg"}, msg.Attachments())
135-
assert.FileExists(t, "_test_media_storage/media/1/e718/7099/e7187099-7d38-4f60-955c-325957214c42.jpg")
134+
assert.Equal(t, []utils.Attachment{"image/jpeg:https:///_test_attachments_storage/attachments/1/e718/7099/e7187099-7d38-4f60-955c-325957214c42.jpg"}, msg.Attachments())
135+
assert.FileExists(t, "_test_attachments_storage/attachments/1/e718/7099/e7187099-7d38-4f60-955c-325957214c42.jpg")
136136

137137
// try with file that can't be read (i.e. same file again which is already closed)
138138
_, err = tickets.SendReply(ctx, rt, modelTicket, "I'll get back to you", []*tickets.File{image})

services/tickets/zendesk/service.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func (s *service) push(msg *ExternalResource, logHTTP flows.HTTPLogCallback) err
252252
// is sent to Zendesk as file/1/01c1/1aa4/01c11aa4-770a-4783.jpg
253253
// which it will request as POST https://textit.com/tickets/types/zendesk/file/1/01c1/1aa4/01c11aa4-770a-4783.jpg
254254
func (s *service) convertAttachments(attachments []utils.Attachment) ([]string, error) {
255-
prefix := s.rtConfig.S3MediaPrefix
255+
prefix := s.rtConfig.S3AttachmentsPrefix
256256
if !strings.HasPrefix(prefix, "/") {
257257
prefix = "/" + prefix
258258
}

services/tickets/zendesk/service_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func TestOpenAndForward(t *testing.T) {
114114
dbTicket,
115115
flows.MsgUUID("ca5607f0-cba8-4c94-9cd5-c4fbc24aa767"),
116116
"It's urgent",
117-
[]utils.Attachment{utils.Attachment("image/jpg:http://myfiles.com/media/0123/attachment1.jpg")},
117+
[]utils.Attachment{utils.Attachment("image/jpg:http://myfiles.com/attachments/0123/attachment1.jpg")},
118118
logger.Log,
119119
)
120120

services/tickets/zendesk/testdata/channelback.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
},
6767
"db_assertions": [
6868
{
69-
"query": "select count(*) from msgs_msg where direction = 'O' and text = 'Like this' and attachments = '{text/plain:https:///_test_media_storage/media/1/6929/26ea/692926ea-09d6-4942-bd38-d266ec8d3716.jpg}'",
69+
"query": "select count(*) from msgs_msg where direction = 'O' and text = 'Like this' and attachments = '{text/plain:https:///_test_attachments_storage/attachments/1/6929/26ea/692926ea-09d6-4942-bd38-d266ec8d3716.jpg}'",
7070
"count": 1
7171
}
7272
]

testsuite/testsuite.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030

3131
var _db *sqlx.DB
3232

33-
const MediaStorageDir = "_test_media_storage"
33+
const AttachmentStorageDir = "_test_attachments_storage"
3434
const SessionStorageDir = "_test_session_storage"
3535

3636
// Refresh is our type for the pieces of org assets we want fresh (not cached)
@@ -67,13 +67,13 @@ func Get() (context.Context, *runtime.Runtime, *sqlx.DB, *redis.Pool) {
6767
db := getDB()
6868
rp := getRP()
6969
rt := &runtime.Runtime{
70-
DB: db,
71-
ReadonlyDB: db,
72-
RP: rp,
73-
ES: nil,
74-
MediaStorage: storage.NewFS(MediaStorageDir, 0766),
75-
SessionStorage: storage.NewFS(SessionStorageDir, 0766),
76-
Config: runtime.NewDefaultConfig(),
70+
DB: db,
71+
ReadonlyDB: db,
72+
RP: rp,
73+
ES: nil,
74+
AttachmentStorage: storage.NewFS(AttachmentStorageDir, 0766),
75+
SessionStorage: storage.NewFS(SessionStorageDir, 0766),
76+
Config: runtime.NewDefaultConfig(),
7777
}
7878

7979
logrus.SetLevel(logrus.DebugLevel)
@@ -176,7 +176,7 @@ func resetRedis() {
176176

177177
// clears our storage for tests
178178
func resetStorage() {
179-
must(os.RemoveAll(MediaStorageDir))
179+
must(os.RemoveAll(AttachmentStorageDir))
180180
must(os.RemoveAll(SessionStorageDir))
181181
}
182182

0 commit comments

Comments
 (0)