Skip to content

Commit 669b58e

Browse files
committed
[#358] Test and update getObjectKey
1 parent 8134d67 commit 669b58e

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

server/controllers/aws.controller.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import uuid from 'node-uuid';
22
import S3Policy from 's3-policy-v4';
33
import s3 from '@auth0/s3';
4+
import mongoose from 'mongoose';
45
import { getProjectsForUserId } from './project.controller';
56
import { findUserByUsername } from './user.controller';
67

8+
const { ObjectId } = mongoose.Types;
9+
710
const client = s3.createClient({
811
maxAsyncS3: 20,
912
s3RetryCount: 3,
@@ -27,15 +30,12 @@ function getExtension(filename) {
2730
}
2831

2932
export function getObjectKey(url) {
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];
33+
const urlArray = url.split('/');
34+
const objectKey = urlArray.pop();
35+
const userId = urlArray.pop();
36+
if (ObjectId.isValid(userId) && userId === new ObjectId(userId).toString()) {
37+
return `${userId}/${objectKey}`;
3738
}
38-
const objectKey = matchResults[1];
3939
return objectKey;
4040
}
4141

0 commit comments

Comments
 (0)