Skip to content

Commit 5ffd59e

Browse files
author
David Gatti
committed
Added unescape string when coping
1 parent 052577c commit 5ffd59e

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

index.js

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,24 @@ let s3 = new AWS.S3({
1717
exports.handler = (event) => {
1818

1919
//
20-
// 1. We need to process the path received by S3 since AWS dose escape
21-
// the string in a special way. They escape the string in a HTML style
22-
// but for whatever reason they convert spaces in to +ses.
23-
//
24-
let s3_key = event.Records[0].s3.object.key;
25-
26-
//
27-
// 2. So first we convert the + in to spaces.
28-
//
29-
let plus_to_space = s3_key.replace(/\+/g, ' ');
30-
31-
//
32-
// 3. And then we unescape the string, other wise we lose
33-
// real + characters.
34-
//
35-
let unescaped_key = decodeURIComponent(plus_to_space);
36-
37-
//
38-
// 4. This JS object will contain all the data within the chain.
20+
// 1. This JS object will contain all the data within the chain.
3921
//
4022
let container = {
4123
bucket: event.Records[0].s3.bucket.name,
42-
key: unescaped_key
24+
unescaped_key: '',
25+
escaped_key: event.Records[0].s3.object.key
4326
};
4427

4528
//
4629
// -> Start the chain.
4730
//
48-
load_the_email(container)
31+
unescape_key(container)
4932
.then(function(container) {
5033

34+
return load_the_email(container);
35+
36+
}).then(function(container) {
37+
5138
return parse_the_email(container);
5239

5340
}).then(function(container) {
@@ -84,6 +71,41 @@ exports.handler = (event) => {
8471
// |_| |_| \_\ \____/ |_| |_| |_____| |_____/ |______| |_____/
8572
//
8673

74+
//
75+
// We need to process the path received by S3 since AWS dose escape
76+
// the string in a special way. They escape the string in a HTML style
77+
// but for whatever reason they convert spaces in to +ses.
78+
//
79+
function unescape_key(container)
80+
{
81+
return new Promise(function(resolve, reject) {
82+
83+
console.info("unescape_key");
84+
85+
//
86+
// 1. First we convert the + in to spaces.
87+
//
88+
let plus_to_space = container.escaped_key.replace(/\+/g, ' ');
89+
90+
//
91+
// 2. And then we unescape the string, other wise we lose
92+
// real + characters.
93+
//
94+
let unescaped_key = decodeURIComponent(plus_to_space);
95+
96+
//
97+
// 3. Save the result for the next promise.
98+
//
99+
container.unescaped_key = unescaped_key;
100+
101+
//
102+
// -> Move to the next chain.
103+
//
104+
return resolve(container);
105+
106+
});
107+
}
108+
87109
//
88110
// Load the email that we received from SES.
89111
//
@@ -98,7 +120,7 @@ function load_the_email(container)
98120
//
99121
let params = {
100122
Bucket: container.bucket,
101-
Key: container.key
123+
Key: container.unescaped_key
102124
};
103125

104126
//
@@ -267,6 +289,10 @@ function extract_data(container)
267289
// already have in memory since the system requires a COPY action and not
268290
// a PUT action.
269291
//
292+
// WARNING: We are using the escaped_key value, because there is a
293+
// know bug in the AWS JS SDK which won't unescape the
294+
// string, so you have to do it - AWS is aware of this issue.
295+
//
270296
function copy_the_email(container)
271297
{
272298
return new Promise(function(resolve, reject) {
@@ -278,7 +304,7 @@ function copy_the_email(container)
278304
//
279305
let params = {
280306
Bucket: container.bucket,
281-
CopySource: container.bucket + '/' + container.key,
307+
CopySource: container.bucket + '/' + container.escaped_key,
282308
Key: container.path
283309
};
284310

@@ -320,7 +346,7 @@ function delete_the_email(container)
320346
//
321347
let params = {
322348
Bucket: container.bucket,
323-
Key: container.key
349+
Key: container.unescaped_key
324350
};
325351

326352
//

0 commit comments

Comments
 (0)