mirror of
https://github.com/plankanban/planka.git
synced 2025-07-18 20:59:44 +02:00
Add file attachments
This commit is contained in:
parent
202abacaec
commit
6a68ec9c1e
103 changed files with 1847 additions and 305 deletions
9
server/.gitignore
vendored
9
server/.gitignore
vendored
|
@ -128,9 +128,12 @@ lib-cov
|
|||
*.pid
|
||||
|
||||
public/*
|
||||
!public/uploads
|
||||
public/uploads/*
|
||||
!public/uploads/.gitkeep
|
||||
!public/user-avatars
|
||||
public/user-avatars/*
|
||||
!public/user-avatars/.gitkeep
|
||||
!public/attachments
|
||||
public/attachments/*
|
||||
!public/attachments/.gitkeep
|
||||
|
||||
views/*
|
||||
!views/.gitkeep
|
||||
|
|
68
server/api/controllers/attachments/create.js
Normal file
68
server/api/controllers/attachments/create.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
const Errors = {
|
||||
CARD_NOT_FOUND: {
|
||||
cardNotFound: 'Card not found',
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
cardId: {
|
||||
type: 'string',
|
||||
regex: /^[0-9]+$/,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
cardNotFound: {
|
||||
responseType: 'notFound',
|
||||
},
|
||||
uploadError: {
|
||||
responseType: 'unprocessableEntity',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
const { currentUser } = this.req;
|
||||
|
||||
const { card, project } = await sails.helpers
|
||||
.getCardToProjectPath(inputs.cardId)
|
||||
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
|
||||
|
||||
const isUserMemberForProject = await sails.helpers.isUserMemberForProject(
|
||||
project.id,
|
||||
currentUser.id,
|
||||
);
|
||||
|
||||
if (!isUserMemberForProject) {
|
||||
throw Errors.CARD_NOT_FOUND; // Forbidden
|
||||
}
|
||||
|
||||
this.req.file('file').upload(sails.helpers.createAttachmentReceiver(), async (error, files) => {
|
||||
if (error) {
|
||||
return exits.uploadError(error.message);
|
||||
}
|
||||
|
||||
if (files.length === 0) {
|
||||
return exits.uploadError('No file was uploaded');
|
||||
}
|
||||
|
||||
const file = files[0];
|
||||
|
||||
const attachment = await sails.helpers.createAttachment(
|
||||
card,
|
||||
{
|
||||
dirname: file.extra.dirname,
|
||||
filename: file.filename,
|
||||
isImage: file.extra.isImage,
|
||||
name: file.filename,
|
||||
},
|
||||
this.req,
|
||||
);
|
||||
|
||||
return exits.success({
|
||||
item: attachment.toJSON(),
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
51
server/api/controllers/attachments/delete.js
Executable file
51
server/api/controllers/attachments/delete.js
Executable file
|
@ -0,0 +1,51 @@
|
|||
const Errors = {
|
||||
ATTACHMENT_NOT_FOUND: {
|
||||
attachmentNotFound: 'Attachment not found',
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
id: {
|
||||
type: 'string',
|
||||
regex: /^[0-9]+$/,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
attachmentNotFound: {
|
||||
responseType: 'notFound',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
const { currentUser } = this.req;
|
||||
|
||||
const attachmentToProjectPath = await sails.helpers
|
||||
.getAttachmentToProjectPath(inputs.id)
|
||||
.intercept('pathNotFound', () => Errors.ATTACHMENT_NOT_FOUND);
|
||||
|
||||
let { attachment } = attachmentToProjectPath;
|
||||
const { board, project } = attachmentToProjectPath;
|
||||
|
||||
const isUserMemberForProject = await sails.helpers.isUserMemberForProject(
|
||||
project.id,
|
||||
currentUser.id,
|
||||
);
|
||||
|
||||
if (!isUserMemberForProject) {
|
||||
throw Errors.ATTACHMENT_NOT_FOUND; // Forbidden
|
||||
}
|
||||
|
||||
attachment = await sails.helpers.deleteAttachment(attachment, board, this.req);
|
||||
|
||||
if (!attachment) {
|
||||
throw Errors.ATTACHMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
return exits.success({
|
||||
item: attachment,
|
||||
});
|
||||
},
|
||||
};
|
57
server/api/controllers/attachments/update.js
Executable file
57
server/api/controllers/attachments/update.js
Executable file
|
@ -0,0 +1,57 @@
|
|||
const Errors = {
|
||||
ATTACHMENT_NOT_FOUND: {
|
||||
attachmentNotFound: 'Attachment not found',
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
id: {
|
||||
type: 'string',
|
||||
regex: /^[0-9]+$/,
|
||||
required: true,
|
||||
},
|
||||
name: {
|
||||
type: 'string',
|
||||
isNotEmptyString: true,
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
attachmentNotFound: {
|
||||
responseType: 'notFound',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
const { currentUser } = this.req;
|
||||
|
||||
const attachmentToProjectPath = await sails.helpers
|
||||
.getAttachmentToProjectPath(inputs.id)
|
||||
.intercept('pathNotFound', () => Errors.ATTACHMENT_NOT_FOUND);
|
||||
|
||||
let { attachment } = attachmentToProjectPath;
|
||||
const { board, project } = attachmentToProjectPath;
|
||||
|
||||
const isUserMemberForProject = await sails.helpers.isUserMemberForProject(
|
||||
project.id,
|
||||
currentUser.id,
|
||||
);
|
||||
|
||||
if (!isUserMemberForProject) {
|
||||
throw Errors.ATTACHMENT_NOT_FOUND; // Forbidden
|
||||
}
|
||||
|
||||
const values = _.pick(inputs, ['name']);
|
||||
|
||||
attachment = await sails.helpers.updateAttachment(attachment, values, board, this.req);
|
||||
|
||||
if (!attachment) {
|
||||
throw Errors.ATTACHMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
return exits.success({
|
||||
item: attachment,
|
||||
});
|
||||
},
|
||||
};
|
|
@ -55,6 +55,7 @@ module.exports = {
|
|||
const cardLabels = await sails.helpers.getCardLabelsForCard(cardIds);
|
||||
|
||||
const tasks = await sails.helpers.getTasksForCard(cardIds);
|
||||
const attachments = await sails.helpers.getAttachmentsForCard(cardIds);
|
||||
|
||||
const isSubscribedByCardId = cardSubscriptions.reduce(
|
||||
(result, cardSubscription) => ({
|
||||
|
@ -80,6 +81,7 @@ module.exports = {
|
|||
cardMemberships,
|
||||
cardLabels,
|
||||
tasks,
|
||||
attachments,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
|
67
server/api/controllers/users/update-avatar.js
Executable file
67
server/api/controllers/users/update-avatar.js
Executable file
|
@ -0,0 +1,67 @@
|
|||
const Errors = {
|
||||
USER_NOT_FOUND: {
|
||||
userNotFound: 'User not found',
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
id: {
|
||||
type: 'string',
|
||||
regex: /^[0-9]+$/,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
userNotFound: {
|
||||
responseType: 'notFound',
|
||||
},
|
||||
uploadError: {
|
||||
responseType: 'unprocessableEntity',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
const { currentUser } = this.req;
|
||||
|
||||
let user;
|
||||
if (currentUser.isAdmin) {
|
||||
user = await sails.helpers.getUser(inputs.id);
|
||||
|
||||
if (!user) {
|
||||
throw Errors.USER_NOT_FOUND;
|
||||
}
|
||||
} else if (inputs.id !== currentUser.id) {
|
||||
throw Errors.USER_NOT_FOUND; // Forbidden
|
||||
} else {
|
||||
user = currentUser;
|
||||
}
|
||||
|
||||
this.req.file('file').upload(sails.helpers.createAvatarReceiver(), async (error, files) => {
|
||||
if (error) {
|
||||
return exits.uploadError(error.message);
|
||||
}
|
||||
|
||||
if (files.length === 0) {
|
||||
return exits.uploadError('No file was uploaded');
|
||||
}
|
||||
|
||||
user = await sails.helpers.updateUser(
|
||||
user,
|
||||
{
|
||||
avatarDirname: files[0].extra.dirname,
|
||||
},
|
||||
this.req,
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
throw Errors.USER_NOT_FOUND;
|
||||
}
|
||||
|
||||
return exits.success({
|
||||
item: user.toJSON().avatarUrl,
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
|
@ -18,7 +18,7 @@ module.exports = {
|
|||
type: 'string',
|
||||
isNotEmptyString: true,
|
||||
},
|
||||
avatar: {
|
||||
avatarUrl: {
|
||||
type: 'json',
|
||||
custom: (value) => _.isNull(value),
|
||||
},
|
||||
|
@ -63,7 +63,7 @@ module.exports = {
|
|||
const values = _.pick(inputs, [
|
||||
'isAdmin',
|
||||
'name',
|
||||
'avatar',
|
||||
'avatarUrl',
|
||||
'phone',
|
||||
'organization',
|
||||
'subscribeToOwnCards',
|
||||
|
|
|
@ -1,122 +0,0 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const util = require('util');
|
||||
const stream = require('stream');
|
||||
const { v4: uuid } = require('uuid');
|
||||
const sharp = require('sharp');
|
||||
|
||||
const Errors = {
|
||||
USER_NOT_FOUND: {
|
||||
userNotFound: 'User not found',
|
||||
},
|
||||
};
|
||||
|
||||
const pipeline = util.promisify(stream.pipeline);
|
||||
|
||||
const createReceiver = () => {
|
||||
const receiver = stream.Writable({ objectMode: true });
|
||||
|
||||
let firstFileHandled = false;
|
||||
// eslint-disable-next-line no-underscore-dangle
|
||||
receiver._write = async (file, receiverEncoding, done) => {
|
||||
if (firstFileHandled) {
|
||||
file.pipe(
|
||||
new stream.Writable({
|
||||
write(chunk, streamEncoding, callback) {
|
||||
callback();
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
return done();
|
||||
}
|
||||
firstFileHandled = true;
|
||||
|
||||
const resize = sharp().resize(100, 100).jpeg();
|
||||
|
||||
const transform = new stream.Transform({
|
||||
transform(chunk, streamEncoding, callback) {
|
||||
callback(null, chunk);
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
await pipeline(file, resize, transform);
|
||||
|
||||
file.fd = `${uuid()}.jpg`; // eslint-disable-line no-param-reassign
|
||||
|
||||
await pipeline(
|
||||
transform,
|
||||
fs.createWriteStream(path.join(sails.config.custom.uploadsPath, file.fd)),
|
||||
);
|
||||
|
||||
return done();
|
||||
} catch (error) {
|
||||
return done(error);
|
||||
}
|
||||
};
|
||||
|
||||
return receiver;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
id: {
|
||||
type: 'string',
|
||||
regex: /^[0-9]+$/,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
userNotFound: {
|
||||
responseType: 'notFound',
|
||||
},
|
||||
uploadError: {
|
||||
responseType: 'unprocessableEntity',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
const { currentUser } = this.req;
|
||||
|
||||
let user;
|
||||
if (currentUser.isAdmin) {
|
||||
user = await sails.helpers.getUser(inputs.id);
|
||||
|
||||
if (!user) {
|
||||
throw Errors.USER_NOT_FOUND;
|
||||
}
|
||||
} else if (inputs.id !== currentUser.id) {
|
||||
throw Errors.USER_NOT_FOUND; // Forbidden
|
||||
} else {
|
||||
user = currentUser;
|
||||
}
|
||||
|
||||
this.req.file('file').upload(createReceiver(), async (error, files) => {
|
||||
if (error) {
|
||||
return exits.uploadError(error.message);
|
||||
}
|
||||
|
||||
if (files.length === 0) {
|
||||
return exits.uploadError('No file was uploaded');
|
||||
}
|
||||
|
||||
user = await sails.helpers.updateUser(
|
||||
user,
|
||||
{
|
||||
avatar: files[0].fd,
|
||||
},
|
||||
this.req,
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
throw Errors.USER_NOT_FOUND;
|
||||
}
|
||||
|
||||
return exits.success({
|
||||
item: user.toJSON().avatar,
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
65
server/api/helpers/create-attachment-receiver.js
Normal file
65
server/api/helpers/create-attachment-receiver.js
Normal file
|
@ -0,0 +1,65 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const util = require('util');
|
||||
const stream = require('stream');
|
||||
const streamToArray = require('stream-to-array');
|
||||
const { v4: uuid } = require('uuid');
|
||||
const sharp = require('sharp');
|
||||
|
||||
const writeFile = util.promisify(fs.writeFile);
|
||||
|
||||
module.exports = {
|
||||
sync: true,
|
||||
|
||||
fn(inputs, exits) {
|
||||
const receiver = stream.Writable({
|
||||
objectMode: true,
|
||||
});
|
||||
|
||||
let firstFileHandled = false;
|
||||
// eslint-disable-next-line no-underscore-dangle
|
||||
receiver._write = async (file, receiverEncoding, done) => {
|
||||
if (firstFileHandled) {
|
||||
file.pipe(new stream.Writable());
|
||||
|
||||
return done();
|
||||
}
|
||||
firstFileHandled = true;
|
||||
|
||||
const buffer = await streamToArray(file).then((parts) =>
|
||||
Buffer.concat(parts.map((part) => (util.isBuffer(part) ? part : Buffer.from(part)))),
|
||||
);
|
||||
|
||||
let thumbnailBuffer;
|
||||
|
||||
try {
|
||||
thumbnailBuffer = await sharp(buffer).resize(240, 240).jpeg().toBuffer();
|
||||
} catch (error) {} // eslint-disable-line no-empty
|
||||
|
||||
try {
|
||||
const dirname = uuid();
|
||||
const dirPath = path.join(sails.config.custom.attachmentsPath, dirname);
|
||||
|
||||
fs.mkdirSync(dirPath);
|
||||
|
||||
if (thumbnailBuffer) {
|
||||
await writeFile(path.join(dirPath, '240.jpg'), thumbnailBuffer);
|
||||
}
|
||||
|
||||
await writeFile(path.join(dirPath, file.filename), buffer);
|
||||
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
file.extra = {
|
||||
dirname,
|
||||
isImage: !!thumbnailBuffer,
|
||||
};
|
||||
|
||||
return done();
|
||||
} catch (error) {
|
||||
return done(error);
|
||||
}
|
||||
};
|
||||
|
||||
return exits.success(receiver);
|
||||
},
|
||||
};
|
33
server/api/helpers/create-attachment.js
Normal file
33
server/api/helpers/create-attachment.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
module.exports = {
|
||||
inputs: {
|
||||
card: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
values: {
|
||||
type: 'json',
|
||||
required: true,
|
||||
},
|
||||
request: {
|
||||
type: 'ref',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
const attachment = await Attachment.create({
|
||||
...inputs.values,
|
||||
cardId: inputs.card.id,
|
||||
}).fetch();
|
||||
|
||||
sails.sockets.broadcast(
|
||||
`board:${inputs.card.boardId}`,
|
||||
'attachmentCreate',
|
||||
{
|
||||
item: attachment,
|
||||
},
|
||||
inputs.request,
|
||||
);
|
||||
|
||||
return exits.success(attachment);
|
||||
},
|
||||
};
|
54
server/api/helpers/create-avatar-receiver.js
Normal file
54
server/api/helpers/create-avatar-receiver.js
Normal file
|
@ -0,0 +1,54 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const util = require('util');
|
||||
const stream = require('stream');
|
||||
const { v4: uuid } = require('uuid');
|
||||
const sharp = require('sharp');
|
||||
|
||||
const pipeline = util.promisify(stream.pipeline);
|
||||
|
||||
module.exports = {
|
||||
sync: true,
|
||||
|
||||
fn(inputs, exits) {
|
||||
const receiver = stream.Writable({
|
||||
objectMode: true,
|
||||
});
|
||||
|
||||
let firstFileHandled = false;
|
||||
// eslint-disable-next-line no-underscore-dangle
|
||||
receiver._write = async (file, receiverEncoding, done) => {
|
||||
if (firstFileHandled) {
|
||||
file.pipe(new stream.Writable());
|
||||
|
||||
return done();
|
||||
}
|
||||
firstFileHandled = true;
|
||||
|
||||
const resize = sharp().resize(100, 100).jpeg();
|
||||
const passThrought = new stream.PassThrough();
|
||||
|
||||
try {
|
||||
await pipeline(file, resize, passThrought);
|
||||
|
||||
const dirname = uuid();
|
||||
const dirPath = path.join(sails.config.custom.userAvatarsPath, dirname);
|
||||
|
||||
fs.mkdirSync(dirPath);
|
||||
|
||||
await pipeline(passThrought, fs.createWriteStream(path.join(dirPath, '100.jpg')));
|
||||
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
file.extra = {
|
||||
dirname,
|
||||
};
|
||||
|
||||
return done();
|
||||
} catch (error) {
|
||||
return done(error);
|
||||
}
|
||||
};
|
||||
|
||||
return exits.success(receiver);
|
||||
},
|
||||
};
|
41
server/api/helpers/delete-attachment.js
Normal file
41
server/api/helpers/delete-attachment.js
Normal file
|
@ -0,0 +1,41 @@
|
|||
const path = require('path');
|
||||
const rimraf = require('rimraf');
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
record: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
board: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
request: {
|
||||
type: 'ref',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
const attachment = await Attachment.archiveOne(inputs.record.id);
|
||||
|
||||
if (attachment) {
|
||||
try {
|
||||
rimraf.sync(path.join(sails.config.custom.attachmentsPath, attachment.dirname));
|
||||
} catch (error) {
|
||||
console.warn(error.stack); // eslint-disable-line no-console
|
||||
}
|
||||
|
||||
sails.sockets.broadcast(
|
||||
`board:${inputs.board.id}`,
|
||||
'attachmentDelete',
|
||||
{
|
||||
item: attachment,
|
||||
},
|
||||
inputs.request,
|
||||
);
|
||||
}
|
||||
|
||||
return exits.success(attachment);
|
||||
},
|
||||
};
|
34
server/api/helpers/get-attachment-to-project-path.js
Executable file
34
server/api/helpers/get-attachment-to-project-path.js
Executable file
|
@ -0,0 +1,34 @@
|
|||
module.exports = {
|
||||
inputs: {
|
||||
criteria: {
|
||||
type: 'json',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
pathNotFound: {},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
const attachment = await Attachment.findOne(inputs.criteria);
|
||||
|
||||
if (!attachment) {
|
||||
throw 'pathNotFound';
|
||||
}
|
||||
|
||||
const path = await sails.helpers
|
||||
.getCardToProjectPath(attachment.cardId)
|
||||
.intercept('pathNotFound', (nodes) => ({
|
||||
pathNotFound: {
|
||||
attachment,
|
||||
...nodes,
|
||||
},
|
||||
}));
|
||||
|
||||
return exits.success({
|
||||
attachment,
|
||||
...path,
|
||||
});
|
||||
},
|
||||
};
|
17
server/api/helpers/get-attachments-for-card.js
Normal file
17
server/api/helpers/get-attachments-for-card.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
module.exports = {
|
||||
inputs: {
|
||||
id: {
|
||||
type: 'json',
|
||||
custom: (value) => _.isString(value) || _.isArray(value),
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
const attachments = await sails.helpers.getAttachments({
|
||||
cardId: inputs.id,
|
||||
});
|
||||
|
||||
return exits.success(attachments);
|
||||
},
|
||||
};
|
14
server/api/helpers/get-attachments.js
Normal file
14
server/api/helpers/get-attachments.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
module.exports = {
|
||||
inputs: {
|
||||
criteria: {
|
||||
type: 'json',
|
||||
custom: (value) => _.isArray(value) || _.isPlainObject(value),
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
const attachments = await Attachment.find(inputs.criteria).sort('id');
|
||||
|
||||
return exits.success(attachments);
|
||||
},
|
||||
};
|
36
server/api/helpers/update-attachment.js
Normal file
36
server/api/helpers/update-attachment.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
module.exports = {
|
||||
inputs: {
|
||||
record: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
values: {
|
||||
type: 'json',
|
||||
required: true,
|
||||
},
|
||||
board: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
request: {
|
||||
type: 'ref',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
const attachment = await Attachment.updateOne(inputs.record.id).set(inputs.values);
|
||||
|
||||
if (attachment) {
|
||||
sails.sockets.broadcast(
|
||||
`board:${inputs.board.id}`,
|
||||
'attachmentUpdate',
|
||||
{
|
||||
item: attachment,
|
||||
},
|
||||
inputs.request,
|
||||
);
|
||||
}
|
||||
|
||||
return exits.success(attachment);
|
||||
},
|
||||
};
|
|
@ -1,6 +1,6 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const bcrypt = require('bcrypt');
|
||||
const rimraf = require('rimraf');
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
|
@ -14,7 +14,8 @@ module.exports = {
|
|||
_.isPlainObject(value) &&
|
||||
(_.isUndefined(value.email) || _.isString(value.email)) &&
|
||||
(_.isUndefined(value.password) || _.isString(value.password)) &&
|
||||
(!value.username || _.isString(value.username)),
|
||||
(!value.username || _.isString(value.username)) &&
|
||||
(_.isUndefined(value.avatarUrl) || _.isNull(value.avatarUrl)),
|
||||
required: true,
|
||||
},
|
||||
request: {
|
||||
|
@ -49,6 +50,13 @@ module.exports = {
|
|||
inputs.values.username = inputs.values.username.toLowerCase();
|
||||
}
|
||||
|
||||
if (!_.isUndefined(inputs.values.avatarUrl)) {
|
||||
/* eslint-disable no-param-reassign */
|
||||
inputs.values.avatarDirname = null;
|
||||
delete inputs.values.avatarUrl;
|
||||
/* eslint-enable no-param-reassign */
|
||||
}
|
||||
|
||||
const user = await User.updateOne({
|
||||
id: inputs.record.id,
|
||||
deletedAt: null,
|
||||
|
@ -70,9 +78,9 @@ module.exports = {
|
|||
);
|
||||
|
||||
if (user) {
|
||||
if (inputs.record.avatar && user.avatar !== inputs.record.avatar) {
|
||||
if (inputs.record.avatarDirname && user.avatarDirname !== inputs.record.avatarDirname) {
|
||||
try {
|
||||
fs.unlinkSync(path.join(sails.config.custom.uploadsPath, inputs.record.avatar));
|
||||
rimraf.sync(path.join(sails.config.custom.userAvatarsPath, inputs.record.avatarDirname));
|
||||
} catch (error) {
|
||||
console.warn(error.stack); // eslint-disable-line no-console
|
||||
}
|
||||
|
|
56
server/api/models/Attachment.js
Normal file
56
server/api/models/Attachment.js
Normal file
|
@ -0,0 +1,56 @@
|
|||
/**
|
||||
* Attachment.js
|
||||
*
|
||||
* @description :: A model definition represents a database table/collection.
|
||||
* @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
attributes: {
|
||||
// ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦ ╦╔═╗╔═╗
|
||||
// ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗
|
||||
// ╩ ╩╚═╩╩ ╩╩ ╩ ╩ ╚╝ ╚═╝╚═╝
|
||||
|
||||
dirname: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
filename: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
isImage: {
|
||||
type: 'boolean',
|
||||
required: true,
|
||||
columnName: 'is_image',
|
||||
},
|
||||
name: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
|
||||
// ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
|
||||
// ║╣ ║║║╠╩╗║╣ ║║╚═╗
|
||||
// ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝
|
||||
|
||||
// ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
|
||||
// ╠═╣╚═╗╚═╗║ ║║ ║╠═╣ ║ ║║ ║║║║╚═╗
|
||||
// ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝
|
||||
|
||||
cardId: {
|
||||
model: 'Card',
|
||||
required: true,
|
||||
columnName: 'card_id',
|
||||
},
|
||||
},
|
||||
|
||||
customToJSON() {
|
||||
return {
|
||||
..._.omit(this, ['dirname', 'filename', 'isImage']),
|
||||
url: `${sails.config.custom.attachmentsUrl}/${this.dirname}/${this.filename}`,
|
||||
thumbnailUrl: this.isImage
|
||||
? `${sails.config.custom.attachmentsUrl}/${this.dirname}/240.jpg`
|
||||
: null,
|
||||
};
|
||||
},
|
||||
};
|
|
@ -69,5 +69,13 @@ module.exports = {
|
|||
collection: 'Task',
|
||||
via: 'cardId',
|
||||
},
|
||||
attachments: {
|
||||
collection: 'Attachment',
|
||||
via: 'cardId',
|
||||
},
|
||||
actions: {
|
||||
collection: 'Action',
|
||||
via: 'cardId',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
|
@ -37,10 +37,11 @@ module.exports = {
|
|||
regex: /^[a-zA-Z0-9]+(_?[a-zA-Z0-9])*$/,
|
||||
allowNull: true,
|
||||
},
|
||||
avatar: {
|
||||
avatarDirname: {
|
||||
type: 'string',
|
||||
isNotEmptyString: true,
|
||||
allowNull: true,
|
||||
columnName: 'avatar_dirname',
|
||||
},
|
||||
phone: {
|
||||
type: 'string',
|
||||
|
@ -91,8 +92,9 @@ module.exports = {
|
|||
|
||||
customToJSON() {
|
||||
return {
|
||||
..._.omit(this, 'password'),
|
||||
avatar: this.avatar && `${sails.config.custom.uploadsUrl}/${this.avatar}`,
|
||||
..._.omit(this, ['password', 'avatarDirname']),
|
||||
avatarUrl:
|
||||
this.avatarDirname && `${sails.config.custom.userAvatarsUrl}/${this.avatarDirname}/100.jpg`,
|
||||
};
|
||||
},
|
||||
};
|
||||
|
|
|
@ -20,6 +20,9 @@ module.exports.custom = {
|
|||
|
||||
baseUrl: process.env.BASE_URL,
|
||||
|
||||
uploadsPath: path.join(sails.config.paths.public, 'uploads'),
|
||||
uploadsUrl: `${process.env.BASE_URL}/uploads`,
|
||||
userAvatarsPath: path.join(sails.config.paths.public, 'user-avatars'),
|
||||
userAvatarsUrl: `${process.env.BASE_URL}/user-avatars`,
|
||||
|
||||
attachmentsPath: path.join(sails.config.paths.public, 'attachments'),
|
||||
attachmentsUrl: `${process.env.BASE_URL}/attachments`,
|
||||
};
|
||||
|
|
7
server/config/env/production.js
vendored
7
server/config/env/production.js
vendored
|
@ -329,7 +329,10 @@ module.exports = {
|
|||
custom: {
|
||||
baseUrl: process.env.BASE_URL,
|
||||
|
||||
uploadsPath: path.join(sails.config.paths.public, 'uploads'),
|
||||
uploadsUrl: `${process.env.BASE_URL}/uploads`,
|
||||
userAvatarsPath: path.join(sails.config.paths.public, 'user-avatars'),
|
||||
userAvatarsUrl: `${process.env.BASE_URL}/user-avatars`,
|
||||
|
||||
attachmentsPath: path.join(sails.config.paths.public, 'attachments'),
|
||||
attachmentsUrl: `${process.env.BASE_URL}/attachments`,
|
||||
},
|
||||
};
|
||||
|
|
|
@ -18,7 +18,7 @@ module.exports.routes = {
|
|||
'PATCH /api/users/:id/email': 'users/update-email',
|
||||
'PATCH /api/users/:id/password': 'users/update-password',
|
||||
'PATCH /api/users/:id/username': 'users/update-username',
|
||||
'POST /api/users/:id/upload-avatar': 'users/upload-avatar',
|
||||
'POST /api/users/:id/update-avatar': 'users/update-avatar',
|
||||
'DELETE /api/users/:id': 'users/delete',
|
||||
|
||||
'GET /api/projects': 'projects/index',
|
||||
|
@ -55,6 +55,10 @@ module.exports.routes = {
|
|||
'PATCH /api/tasks/:id': 'tasks/update',
|
||||
'DELETE /api/tasks/:id': 'tasks/delete',
|
||||
|
||||
'POST /api/cards/:cardId/attachments': 'attachments/create',
|
||||
'PATCH /api/attachments/:id': 'attachments/update',
|
||||
'DELETE /api/attachments/:id': 'attachments/delete',
|
||||
|
||||
'GET /api/cards/:cardId/actions': 'actions/index',
|
||||
|
||||
'POST /api/cards/:cardId/comment-actions': 'comment-actions/create',
|
||||
|
|
|
@ -10,7 +10,7 @@ module.exports.up = (knex) =>
|
|||
table.boolean('is_admin').notNullable();
|
||||
table.text('name').notNullable();
|
||||
table.text('username');
|
||||
table.text('avatar');
|
||||
table.text('avatar_dirname');
|
||||
table.text('phone');
|
||||
table.text('organization');
|
||||
table.boolean('subscribe_to_own_cards').notNullable();
|
||||
|
|
22
server/db/migrations/20180722006688_create_attachment_table.js
Executable file
22
server/db/migrations/20180722006688_create_attachment_table.js
Executable file
|
@ -0,0 +1,22 @@
|
|||
module.exports.up = (knex) =>
|
||||
knex.schema.createTable('attachment', (table) => {
|
||||
/* Columns */
|
||||
|
||||
table.bigInteger('id').primary().defaultTo(knex.raw('next_id()'));
|
||||
|
||||
table.bigInteger('card_id').notNullable();
|
||||
|
||||
table.text('dirname').notNullable();
|
||||
table.text('filename').notNullable();
|
||||
table.boolean('is_image').notNullable();
|
||||
table.text('name').notNullable();
|
||||
|
||||
table.timestamp('created_at', true);
|
||||
table.timestamp('updated_at', true);
|
||||
|
||||
/* Indexes */
|
||||
|
||||
table.index('card_id');
|
||||
});
|
||||
|
||||
module.exports.down = (knex) => knex.schema.dropTable('attachment');
|
47
server/package-lock.json
generated
47
server/package-lock.json
generated
|
@ -190,6 +190,11 @@
|
|||
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz",
|
||||
"integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4="
|
||||
},
|
||||
"any-promise": {
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz",
|
||||
"integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8="
|
||||
},
|
||||
"anymatch": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.1.tgz",
|
||||
|
@ -2555,6 +2560,16 @@
|
|||
"klaw": "^1.0.0",
|
||||
"path-is-absolute": "^1.0.0",
|
||||
"rimraf": "^2.2.8"
|
||||
},
|
||||
"dependencies": {
|
||||
"rimraf": {
|
||||
"version": "2.7.1",
|
||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz",
|
||||
"integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==",
|
||||
"requires": {
|
||||
"glob": "^7.1.3"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"fs-minipass": {
|
||||
|
@ -4168,6 +4183,16 @@
|
|||
"rimraf": "^2.6.1",
|
||||
"semver": "^5.3.0",
|
||||
"tar": "^4.4.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"rimraf": {
|
||||
"version": "2.7.1",
|
||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz",
|
||||
"integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==",
|
||||
"requires": {
|
||||
"glob": "^7.1.3"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"nodemon": {
|
||||
|
@ -5273,9 +5298,9 @@
|
|||
"integrity": "sha1-/s5hv6DBtSoga9axgZgYS91SOjs="
|
||||
},
|
||||
"rimraf": {
|
||||
"version": "2.7.1",
|
||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz",
|
||||
"integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==",
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz",
|
||||
"integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==",
|
||||
"requires": {
|
||||
"glob": "^7.1.3"
|
||||
}
|
||||
|
@ -6357,6 +6382,14 @@
|
|||
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.3.1.tgz",
|
||||
"integrity": "sha1-+vUbnrdKrvOzrPStX2Gr8ky3uT4="
|
||||
},
|
||||
"stream-to-array": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/stream-to-array/-/stream-to-array-2.3.0.tgz",
|
||||
"integrity": "sha1-u/azn19D7DC8cbq8s3VXrOzzQ1M=",
|
||||
"requires": {
|
||||
"any-promise": "^1.1.0"
|
||||
}
|
||||
},
|
||||
"streamifier": {
|
||||
"version": "0.1.1",
|
||||
"resolved": "https://registry.npmjs.org/streamifier/-/streamifier-0.1.1.tgz",
|
||||
|
@ -6931,6 +6964,14 @@
|
|||
"version": "0.2.10",
|
||||
"resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz",
|
||||
"integrity": "sha1-trvgsGdLnXGXCMo43owjfLUmw9E="
|
||||
},
|
||||
"rimraf": {
|
||||
"version": "2.7.1",
|
||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz",
|
||||
"integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==",
|
||||
"requires": {
|
||||
"glob": "^7.1.3"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -46,11 +46,13 @@
|
|||
"knex": "^0.20.13",
|
||||
"lodash": "^4.17.15",
|
||||
"moment": "^2.24.0",
|
||||
"rimraf": "^3.0.2",
|
||||
"sails": "^1.2.4",
|
||||
"sails-hook-orm": "^2.1.1",
|
||||
"sails-hook-sockets": "^2.0.0",
|
||||
"sails-postgresql": "^1.0.2",
|
||||
"sharp": "^0.25.2",
|
||||
"stream-to-array": "^2.3.0",
|
||||
"uuid": "^7.0.3",
|
||||
"validator": "^13.0.0"
|
||||
},
|
||||
|
|
0
server/public/user-avatars/.gitkeep
Normal file
0
server/public/user-avatars/.gitkeep
Normal file
Loading…
Add table
Add a link
Reference in a new issue