Save avatars to le cloud in beforeSave hooks

Added encryption to uploads
Updated icon for team settings
This commit is contained in:
Tom Moor
2018-06-01 18:00:48 -04:00
parent a99a804ea0
commit 4c9f86c7f7
7 changed files with 39 additions and 16 deletions

View File

@@ -1,5 +1,7 @@
// @flow
import uuid from 'uuid';
import { DataTypes, sequelize, Op } from '../sequelize';
import { publicS3Endpoint, uploadToS3FromUrl } from '../utils/s3';
import Collection from './Collection';
import User from './User';
@@ -33,6 +35,18 @@ Team.associate = models => {
Team.hasMany(models.User, { as: 'users' });
};
const uploadAvatar = async model => {
const endpoint = publicS3Endpoint();
if (model.avatarUrl && !model.avatarUrl.startsWith(endpoint)) {
const newUrl = await uploadToS3FromUrl(
model.avatarUrl,
`avatars/${model.id}/${uuid.v4()}`
);
if (newUrl) model.avatarUrl = newUrl;
}
};
Team.prototype.createFirstCollection = async function(userId) {
return await Collection.create({
name: 'General',
@@ -82,4 +96,6 @@ Team.prototype.activateUser = async function(user: User, admin: User) {
});
};
Team.beforeSave(uploadAvatar);
export default Team;