Skip to content

Commit ff5b95f

Browse files
committed
[#358] Update getObjectKey to work for different url formats
1 parent 055b83f commit ff5b95f

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

server/controllers/aws.controller.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ const client = s3.createClient({
1717
},
1818
});
1919

20-
const s3Bucket = process.env.S3_BUCKET_URL_BASE ||
21-
`https://s3-${process.env.AWS_REGION}.amazonaws.com/${process.env.S3_BUCKET}/`;
20+
const bucketBase = process.env.S3_BUCKET_URL_BASE;
21+
const s3Url = `https://s3-${process.env.AWS_REGION}.amazonaws.com/${process.env.S3_BUCKET}/`;
22+
const s3Bucket = bucketBase || s3Url;
2223

2324
function getExtension(filename) {
2425
const i = filename.lastIndexOf('.');
2526
return (i < 0) ? '' : filename.substr(i);
2627
}
2728

2829
export function getObjectKey(url) {
29-
const urlArray = url.split('/');
30-
let objectKey;
31-
if (urlArray.length === 5) {
32-
const key = urlArray.pop();
33-
const userId = urlArray.pop();
34-
objectKey = `${userId}/${key}`;
35-
} else {
36-
const key = urlArray.pop();
37-
objectKey = key;
30+
const matchResults = url.split(s3Bucket);
31+
// if, for some reason, the object is not using the default bucket url
32+
if (matchResults.length === 1) {
33+
if (bucketBase) {
34+
return url.split(s3Url)[1];
35+
}
36+
return url.split(bucketBase)[1];
3837
}
38+
const objectKey = matchResults[1];
3939
return objectKey;
4040
}
4141

0 commit comments

Comments
 (0)