From 40b3e26f243f4ba684b19bc2e74bcbf17d11138f Mon Sep 17 00:00:00 2001 From: Cassie Tarakajian Date: Thu, 13 Apr 2017 14:17:30 -0400 Subject: [PATCH] #339 make duplication backwards compatible with old s3 links --- server/controllers/aws.controller.js | 17 +++++++++++++++-- server/controllers/file.controller.js | 8 +++----- server/controllers/project.controller.js | 7 ++----- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/server/controllers/aws.controller.js b/server/controllers/aws.controller.js index 88ea0979..5e548d9c 100644 --- a/server/controllers/aws.controller.js +++ b/server/controllers/aws.controller.js @@ -21,6 +21,20 @@ function getExtension(filename) { return (i < 0) ? '' : filename.substr(i); } +export function getObjectKey(url) { + const urlArray = url.split('/'); + let objectKey; + if (urlArray.length === 6) { + const key = urlArray.pop(); + const userId = urlArray.pop(); + objectKey = `${userId}/${key}` + } else { + const key = urlArray.pop(); + objectKey = key; + } + return objectKey; +} + export function deleteObjectsFromS3(keyList, callback) { const keys = keyList.map((key) => { return { Key: key }; }); // eslint-disable-line if (keyList.length > 0) { @@ -71,8 +85,7 @@ export function signS3(req, res) { export function copyObjectInS3(req, res) { const url = req.body.url; - const objectKey = url.split('/').pop(); - + const objectKey = getObjectKey(url); const fileExtension = getExtension(objectKey); const newFilename = uuid.v4() + fileExtension; const params = { diff --git a/server/controllers/file.controller.js b/server/controllers/file.controller.js index 9fd92ce6..a6bc7d51 100644 --- a/server/controllers/file.controller.js +++ b/server/controllers/file.controller.js @@ -3,7 +3,7 @@ import moment from 'moment'; import Project from '../models/project'; import { resolvePathToFile } from '../utils/filePath'; -import { deleteObjectsFromS3 } from './aws.controller'; +import { deleteObjectsFromS3, getObjectKey } from './aws.controller'; // Bug -> timestamps don't get created, but it seems like this will // be fixed in mongoose soon @@ -49,10 +49,8 @@ function deleteMany(files, ids) { each(ids, (id, cb) => { if (files.id(id).url) { if (!process.env.S3_DATE || (process.env.S3_DATE && moment(process.env.S3_DATE) < moment(files.id(id).createdAt))) { - const urlComponents = files.id(id).url.split('/'); - const key = urlComponents.pop(); - const userId = urlComponents.pop(); - objectKeys.push(`${userId}/${key}`); + const objectKey = getObjectKey(files.id(id).url); + objectKeys.push(objectKey); } } files.id(id).remove(); diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index 79b49c77..84b2afa1 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -3,7 +3,7 @@ import request from 'request'; import moment from 'moment'; import Project from '../models/project'; import User from '../models/user'; -import { deleteObjectsFromS3 } from './aws.controller'; +import { deleteObjectsFromS3, getObjectKey } from './aws.controller'; export function createProject(req, res) { if (!req.user) { @@ -101,10 +101,7 @@ function deleteFilesFromS3(files) { return false; }) .map((file) => { - const urlComponents = file.url.split('/'); - const key = urlComponents.pop(); - const userId = urlComponents.pop(); - return `${userId}/${key}`; + return getObjectKey(file.url); }) ); }