2016-07-16 01:05:18 +02:00
|
|
|
import uuid from 'node-uuid';
|
|
|
|
import policy from 's3-policy';
|
2017-03-23 19:23:54 +01:00
|
|
|
import s3 from 's3';
|
2017-07-11 17:37:43 +02:00
|
|
|
import { getProjectsForUserId } from './project.controller';
|
|
|
|
import { findUserByUsername } from './user.controller';
|
2017-03-23 19:23:54 +01:00
|
|
|
|
|
|
|
const client = s3.createClient({
|
|
|
|
maxAsyncS3: 20,
|
|
|
|
s3RetryCount: 3,
|
|
|
|
s3RetryDelay: 1000,
|
2017-04-06 20:34:14 +02:00
|
|
|
multipartUploadThreshold: 20971520, // this is the default (20 MB)
|
|
|
|
multipartUploadSize: 15728640, // this is the default (15 MB)
|
2017-03-23 19:23:54 +01:00
|
|
|
s3Options: {
|
|
|
|
accessKeyId: `${process.env.AWS_ACCESS_KEY}`,
|
|
|
|
secretAccessKey: `${process.env.AWS_SECRET_KEY}`,
|
2017-04-13 20:39:03 +02:00
|
|
|
region: `${process.env.AWS_REGION}`
|
2017-03-23 19:23:54 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-12-13 20:56:24 +01:00
|
|
|
const s3Bucket = process.env.S3_BUCKET_URL_BASE ||
|
|
|
|
`https://s3-${process.env.AWS_REGION}.amazonaws.com/${process.env.S3_BUCKET}/`;
|
2016-07-16 01:05:18 +02:00
|
|
|
|
|
|
|
function getExtension(filename) {
|
|
|
|
const i = filename.lastIndexOf('.');
|
|
|
|
return (i < 0) ? '' : filename.substr(i);
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:17:30 +02:00
|
|
|
export function getObjectKey(url) {
|
|
|
|
const urlArray = url.split('/');
|
|
|
|
let objectKey;
|
|
|
|
if (urlArray.length === 6) {
|
|
|
|
const key = urlArray.pop();
|
|
|
|
const userId = urlArray.pop();
|
2017-06-13 22:47:36 +02:00
|
|
|
objectKey = `${userId}/${key}`;
|
2017-04-13 20:17:30 +02:00
|
|
|
} else {
|
|
|
|
const key = urlArray.pop();
|
|
|
|
objectKey = key;
|
|
|
|
}
|
2017-06-13 22:47:36 +02:00
|
|
|
return objectKey;
|
2017-04-13 20:17:30 +02:00
|
|
|
}
|
|
|
|
|
2017-03-30 22:52:36 +02:00
|
|
|
export function deleteObjectsFromS3(keyList, callback) {
|
2017-04-06 20:34:14 +02:00
|
|
|
const keys = keyList.map((key) => { return { Key: key }; }); // eslint-disable-line
|
2017-03-30 22:52:36 +02:00
|
|
|
if (keyList.length > 0) {
|
2017-03-30 18:14:07 +02:00
|
|
|
const params = {
|
|
|
|
Bucket: `${process.env.S3_BUCKET}`,
|
|
|
|
Delete: {
|
2017-03-30 22:52:36 +02:00
|
|
|
Objects: keys,
|
2017-03-30 18:14:07 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
const del = client.deleteObjects(params);
|
2017-04-06 20:34:14 +02:00
|
|
|
del.on('end', () => {
|
|
|
|
if (callback) {
|
2017-03-30 18:14:07 +02:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
2017-04-06 20:34:14 +02:00
|
|
|
} else if (callback) {
|
|
|
|
callback();
|
2017-03-30 18:14:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:52:36 +02:00
|
|
|
export function deleteObjectFromS3(req, res) {
|
|
|
|
const objectKey = req.params.object_key;
|
2017-04-06 20:34:14 +02:00
|
|
|
deleteObjectsFromS3([objectKey], () => {
|
|
|
|
res.json({ success: true });
|
2017-03-30 22:52:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-16 01:05:18 +02:00
|
|
|
export function signS3(req, res) {
|
2017-01-11 21:50:36 +01:00
|
|
|
const fileExtension = getExtension(req.body.name);
|
|
|
|
const filename = uuid.v4() + fileExtension;
|
|
|
|
const acl = 'public-read';
|
|
|
|
const p = policy({
|
|
|
|
acl,
|
|
|
|
secret: process.env.AWS_SECRET_KEY,
|
|
|
|
length: 5000000, // in bytes?
|
|
|
|
bucket: process.env.S3_BUCKET,
|
|
|
|
key: filename,
|
|
|
|
expires: new Date(Date.now() + 60000),
|
|
|
|
});
|
|
|
|
const result = {
|
|
|
|
AWSAccessKeyId: process.env.AWS_ACCESS_KEY,
|
2017-04-06 20:34:14 +02:00
|
|
|
key: `${req.body.userId}/${filename}`,
|
2017-01-11 21:50:36 +01:00
|
|
|
policy: p.policy,
|
|
|
|
signature: p.signature
|
|
|
|
};
|
2016-07-16 01:05:18 +02:00
|
|
|
return res.json(result);
|
2016-11-17 17:15:35 +01:00
|
|
|
}
|
2017-02-22 20:29:35 +01:00
|
|
|
|
2017-03-23 19:23:54 +01:00
|
|
|
export function copyObjectInS3(req, res) {
|
2018-05-05 02:59:43 +02:00
|
|
|
const { url } = req.body;
|
2017-04-13 20:17:30 +02:00
|
|
|
const objectKey = getObjectKey(url);
|
2017-03-23 19:23:54 +01:00
|
|
|
const fileExtension = getExtension(objectKey);
|
|
|
|
const newFilename = uuid.v4() + fileExtension;
|
2017-05-14 03:17:58 +02:00
|
|
|
const userId = req.user.id;
|
2017-03-23 19:23:54 +01:00
|
|
|
const params = {
|
|
|
|
Bucket: `${process.env.S3_BUCKET}`,
|
|
|
|
CopySource: `${process.env.S3_BUCKET}/${objectKey}`,
|
2017-05-14 04:23:07 +02:00
|
|
|
Key: `${userId}/${newFilename}`,
|
|
|
|
ACL: 'public-read'
|
2017-03-23 19:23:54 +01:00
|
|
|
};
|
|
|
|
const copy = client.copyObject(params);
|
2017-05-14 03:38:48 +02:00
|
|
|
copy.on('err', (err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
2017-05-14 03:46:58 +02:00
|
|
|
copy.on('end', (data) => {
|
2017-05-14 03:17:58 +02:00
|
|
|
res.json({ url: `${s3Bucket}${userId}/${newFilename}` });
|
2017-03-23 19:23:54 +01:00
|
|
|
});
|
|
|
|
}
|
2017-07-11 17:37:43 +02:00
|
|
|
|
|
|
|
export function listObjectsInS3ForUser(req, res) {
|
2018-05-09 04:30:33 +02:00
|
|
|
const { username } = req.params;
|
2017-07-11 17:37:43 +02:00
|
|
|
findUserByUsername(username, (user) => {
|
|
|
|
const userId = user.id;
|
|
|
|
const params = {
|
|
|
|
s3Params: {
|
|
|
|
Bucket: `${process.env.S3_BUCKET}`,
|
|
|
|
Prefix: `${userId}/`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let assets = [];
|
2017-10-16 05:27:05 +02:00
|
|
|
client.listObjects(params)
|
2017-07-11 17:37:43 +02:00
|
|
|
.on('data', (data) => {
|
2017-10-16 05:27:05 +02:00
|
|
|
assets = assets.concat(data.Contents.map(object => ({ key: object.Key, size: object.Size })));
|
2017-07-11 17:37:43 +02:00
|
|
|
})
|
|
|
|
.on('end', () => {
|
|
|
|
const projectAssets = [];
|
|
|
|
getProjectsForUserId(userId).then((projects) => {
|
|
|
|
projects.forEach((project) => {
|
|
|
|
project.files.forEach((file) => {
|
|
|
|
if (!file.url) return;
|
|
|
|
|
2017-10-16 05:27:05 +02:00
|
|
|
const foundAsset = assets.find(asset => file.url.includes(asset.key));
|
2017-07-11 17:37:43 +02:00
|
|
|
if (!foundAsset) return;
|
|
|
|
projectAssets.push({
|
|
|
|
name: file.name,
|
|
|
|
sketchName: project.name,
|
|
|
|
sketchId: project.id,
|
|
|
|
url: file.url,
|
|
|
|
key: foundAsset.key,
|
|
|
|
size: foundAsset.size
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-10-16 05:27:05 +02:00
|
|
|
res.json({ assets: projectAssets });
|
2017-07-11 17:37:43 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|