#339 make duplication backwards compatible with old s3 links

This commit is contained in:
Cassie Tarakajian 2017-04-13 14:17:30 -04:00
parent 94694c5a72
commit 40b3e26f24
3 changed files with 20 additions and 12 deletions

View file

@ -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 = {

View file

@ -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();

View file

@ -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);
})
);
}