mirror of
https://github.com/plankanban/planka.git
synced 2025-07-24 07:39:44 +02:00
parent
ad7fb51cfa
commit
2ee1166747
1557 changed files with 76832 additions and 47042 deletions
|
@ -1,24 +1,12 @@
|
|||
const valuesValidator = (value) => {
|
||||
if (!_.isPlainObject(value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!_.isPlainObject(value.card)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!_.isPlainObject(value.creatorUser)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
values: {
|
||||
type: 'ref',
|
||||
custom: valuesValidator,
|
||||
required: true,
|
||||
},
|
||||
project: {
|
||||
|
@ -35,7 +23,6 @@ module.exports = {
|
|||
},
|
||||
requestId: {
|
||||
type: 'string',
|
||||
isNotEmptyString: true,
|
||||
},
|
||||
request: {
|
||||
type: 'ref',
|
||||
|
@ -45,17 +32,17 @@ module.exports = {
|
|||
async fn(inputs) {
|
||||
const { values } = inputs;
|
||||
|
||||
const attachment = await Attachment.create({
|
||||
const attachment = await Attachment.qm.createOne({
|
||||
...values,
|
||||
cardId: values.card.id,
|
||||
creatorUserId: values.creatorUser.id,
|
||||
}).fetch();
|
||||
});
|
||||
|
||||
sails.sockets.broadcast(
|
||||
`board:${values.card.boardId}`,
|
||||
`board:${inputs.board.id}`,
|
||||
'attachmentCreate',
|
||||
{
|
||||
item: attachment,
|
||||
item: sails.helpers.attachments.presentOne(attachment),
|
||||
requestId: inputs.requestId,
|
||||
},
|
||||
inputs.request,
|
||||
|
@ -63,29 +50,31 @@ module.exports = {
|
|||
|
||||
sails.helpers.utils.sendWebhooks.with({
|
||||
event: 'attachmentCreate',
|
||||
data: {
|
||||
item: attachment,
|
||||
buildData: () => ({
|
||||
item: sails.helpers.attachments.presentOne(attachment),
|
||||
included: {
|
||||
projects: [inputs.project],
|
||||
boards: [inputs.board],
|
||||
lists: [inputs.list],
|
||||
cards: [values.card],
|
||||
},
|
||||
},
|
||||
}),
|
||||
user: values.creatorUser,
|
||||
});
|
||||
|
||||
if (!values.card.coverAttachmentId && attachment.image) {
|
||||
await sails.helpers.cards.updateOne.with({
|
||||
record: values.card,
|
||||
values: {
|
||||
coverAttachmentId: attachment.id,
|
||||
},
|
||||
project: inputs.project,
|
||||
board: inputs.board,
|
||||
list: inputs.list,
|
||||
actorUser: values.creatorUser,
|
||||
});
|
||||
if (!values.card.coverAttachmentId) {
|
||||
if (attachment.type === Attachment.Types.FILE && attachment.data.image) {
|
||||
await sails.helpers.cards.updateOne.with({
|
||||
record: values.card,
|
||||
values: {
|
||||
coverAttachmentId: attachment.id,
|
||||
},
|
||||
project: inputs.project,
|
||||
board: inputs.board,
|
||||
list: inputs.list,
|
||||
actorUser: values.creatorUser,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return attachment;
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
record: {
|
||||
|
@ -40,43 +45,38 @@ module.exports = {
|
|||
board: inputs.board,
|
||||
list: inputs.list,
|
||||
actorUser: inputs.actorUser,
|
||||
request: inputs.request,
|
||||
});
|
||||
}
|
||||
|
||||
const attachment = await Attachment.archiveOne(inputs.record.id);
|
||||
const { attachment, fileReference } = await Attachment.qm.deleteOne(inputs.record.id, {
|
||||
isFile: inputs.record.type === Attachment.Types.FILE,
|
||||
});
|
||||
|
||||
if (attachment) {
|
||||
const fileManager = sails.hooks['file-manager'].getInstance();
|
||||
|
||||
try {
|
||||
await fileManager.deleteDir(
|
||||
`${sails.config.custom.attachmentsPathSegment}/${attachment.dirname}`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.warn(error.stack); // eslint-disable-line no-console
|
||||
if (fileReference) {
|
||||
sails.helpers.attachments.removeUnreferencedFiles(fileReference);
|
||||
}
|
||||
|
||||
sails.sockets.broadcast(
|
||||
`board:${inputs.board.id}`,
|
||||
'attachmentDelete',
|
||||
{
|
||||
item: attachment,
|
||||
item: sails.helpers.attachments.presentOne(attachment),
|
||||
},
|
||||
inputs.request,
|
||||
);
|
||||
|
||||
sails.helpers.utils.sendWebhooks.with({
|
||||
event: 'attachmentDelete',
|
||||
data: {
|
||||
item: attachment,
|
||||
buildData: () => ({
|
||||
item: sails.helpers.attachments.presentOne(attachment),
|
||||
included: {
|
||||
projects: [inputs.project],
|
||||
boards: [inputs.board],
|
||||
lists: [inputs.list],
|
||||
cards: [inputs.card],
|
||||
},
|
||||
},
|
||||
}),
|
||||
user: inputs.actorUser,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
const criteriaValidator = (value) => _.isArray(value) || _.isPlainObject(value);
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
criteria: {
|
||||
type: 'json',
|
||||
custom: criteriaValidator,
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
return Attachment.find(inputs.criteria).sort('id');
|
||||
},
|
||||
};
|
39
server/api/helpers/attachments/get-path-to-project-by-id.js
Normal file
39
server/api/helpers/attachments/get-path-to-project-by-id.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
id: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
pathNotFound: {},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
const attachment = await Attachment.qm.getOneById(inputs.id);
|
||||
|
||||
if (!attachment) {
|
||||
throw 'pathNotFound';
|
||||
}
|
||||
|
||||
const pathToProject = await sails.helpers.cards
|
||||
.getPathToProjectById(attachment.cardId)
|
||||
.intercept('pathNotFound', (nodes) => ({
|
||||
pathNotFound: {
|
||||
attachment,
|
||||
...nodes,
|
||||
},
|
||||
}));
|
||||
|
||||
return {
|
||||
attachment,
|
||||
...pathToProject,
|
||||
};
|
||||
},
|
||||
};
|
|
@ -1,34 +0,0 @@
|
|||
module.exports = {
|
||||
inputs: {
|
||||
criteria: {
|
||||
type: 'json',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
pathNotFound: {},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
const attachment = await Attachment.findOne(inputs.criteria);
|
||||
|
||||
if (!attachment) {
|
||||
throw 'pathNotFound';
|
||||
}
|
||||
|
||||
const path = await sails.helpers.cards
|
||||
.getProjectPath(attachment.cardId)
|
||||
.intercept('pathNotFound', (nodes) => ({
|
||||
pathNotFound: {
|
||||
attachment,
|
||||
...nodes,
|
||||
},
|
||||
}));
|
||||
|
||||
return {
|
||||
attachment,
|
||||
...path,
|
||||
};
|
||||
},
|
||||
};
|
19
server/api/helpers/attachments/present-many.js
Normal file
19
server/api/helpers/attachments/present-many.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
sync: true,
|
||||
|
||||
inputs: {
|
||||
records: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
fn(inputs) {
|
||||
return inputs.records.map((record) => sails.helpers.attachments.presentOne(record));
|
||||
},
|
||||
};
|
56
server/api/helpers/attachments/present-one.js
Normal file
56
server/api/helpers/attachments/present-one.js
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
sync: true,
|
||||
|
||||
inputs: {
|
||||
record: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
fn(inputs) {
|
||||
let data;
|
||||
if (inputs.record.type === Attachment.Types.FILE) {
|
||||
data = {
|
||||
...inputs.record,
|
||||
data: {
|
||||
..._.omit(inputs.record.data, [
|
||||
'fileReferenceId',
|
||||
'filename',
|
||||
'image.thumbnailsExtension',
|
||||
]),
|
||||
url: `${sails.config.custom.baseUrl}/attachments/${inputs.record.id}/download/${inputs.record.data.filename}`,
|
||||
thumbnailUrls: inputs.record.data.image && {
|
||||
outside360: `${sails.config.custom.baseUrl}/attachments/${inputs.record.id}/download/thumbnails/outside-360.${inputs.record.data.image.thumbnailsExtension}`,
|
||||
outside720: `${sails.config.custom.baseUrl}/attachments/${inputs.record.id}/download/thumbnails/outside-720.${inputs.record.data.image.thumbnailsExtension}`,
|
||||
},
|
||||
},
|
||||
};
|
||||
} else if (inputs.record.type === Attachment.Types.LINK) {
|
||||
const faviconFilename = `${inputs.record.data.hostname}.png`;
|
||||
|
||||
let faviconUrl = null;
|
||||
if (sails.helpers.utils.isPreloadedFaviconExists(inputs.record.data.hostname)) {
|
||||
faviconUrl = `${sails.config.custom.baseUrl}/preloaded-favicons/${faviconFilename}`;
|
||||
} else {
|
||||
const fileManager = sails.hooks['file-manager'].getInstance();
|
||||
faviconUrl = `${fileManager.buildUrl(`${sails.config.custom.faviconsPathSegment}/${faviconFilename}`)}`;
|
||||
}
|
||||
|
||||
data = {
|
||||
...inputs.record,
|
||||
data: {
|
||||
..._.omit(inputs.record.data, 'hostname'),
|
||||
faviconUrl,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return data;
|
||||
},
|
||||
};
|
28
server/api/helpers/attachments/process-link.js
Normal file
28
server/api/helpers/attachments/process-link.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
const { URL } = require('url');
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
url: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
const { hostname } = new URL(inputs.url);
|
||||
|
||||
if (!sails.helpers.utils.isPreloadedFaviconExists(hostname)) {
|
||||
await sails.helpers.utils.downloadFavicon(inputs.url);
|
||||
}
|
||||
|
||||
return {
|
||||
hostname,
|
||||
url: inputs.url,
|
||||
};
|
||||
},
|
||||
};
|
|
@ -1,8 +1,16 @@
|
|||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
const fsPromises = require('fs').promises;
|
||||
const { rimraf } = require('rimraf');
|
||||
const { v4: uuid } = require('uuid');
|
||||
const { getEncoding } = require('istextorbinary');
|
||||
const mime = require('mime');
|
||||
const sharp = require('sharp');
|
||||
|
||||
const filenamify = require('../../../utils/filenamify');
|
||||
const { MAX_SIZE_IN_BYTES_TO_GET_ENCODING } = require('../../../constants');
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
|
@ -15,78 +23,115 @@ module.exports = {
|
|||
async fn(inputs) {
|
||||
const fileManager = sails.hooks['file-manager'].getInstance();
|
||||
|
||||
const dirname = uuid();
|
||||
const dirPathSegment = `${sails.config.custom.attachmentsPathSegment}/${dirname}`;
|
||||
const { id: fileReferenceId } = await FileReference.create().fetch();
|
||||
const dirPathSegment = `${sails.config.custom.attachmentsPathSegment}/${fileReferenceId}`;
|
||||
const filename = filenamify(inputs.file.filename);
|
||||
|
||||
const mimeType = mime.getType(filename);
|
||||
const sizeInBytes = inputs.file.size;
|
||||
|
||||
let buffer;
|
||||
let encoding = null;
|
||||
|
||||
if (sizeInBytes <= MAX_SIZE_IN_BYTES_TO_GET_ENCODING) {
|
||||
try {
|
||||
buffer = await fsPromises.readFile(inputs.file.fd);
|
||||
} catch (error) {
|
||||
/* empty */
|
||||
}
|
||||
|
||||
if (buffer) {
|
||||
encoding = getEncoding(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
const filePath = await fileManager.move(
|
||||
inputs.file.fd,
|
||||
`${dirPathSegment}/${filename}`,
|
||||
inputs.file.type,
|
||||
);
|
||||
|
||||
let image = sharp(filePath || inputs.file.fd, {
|
||||
animated: true,
|
||||
});
|
||||
|
||||
let metadata;
|
||||
try {
|
||||
metadata = await image.metadata();
|
||||
} catch (error) {} // eslint-disable-line no-empty
|
||||
|
||||
const fileData = {
|
||||
dirname,
|
||||
const data = {
|
||||
fileReferenceId,
|
||||
filename,
|
||||
mimeType,
|
||||
sizeInBytes,
|
||||
encoding,
|
||||
image: null,
|
||||
name: inputs.file.filename,
|
||||
};
|
||||
|
||||
if (metadata && !['svg', 'pdf'].includes(metadata.format)) {
|
||||
let { width, pageHeight: height = metadata.height } = metadata;
|
||||
if (metadata.orientation && metadata.orientation > 4) {
|
||||
[image, width, height] = [image.rotate(), height, width];
|
||||
if (!['image/svg+xml', 'application/pdf'].includes(mimeType)) {
|
||||
let image = sharp(buffer || filePath, {
|
||||
animated: true,
|
||||
});
|
||||
|
||||
let metadata;
|
||||
try {
|
||||
metadata = await image.metadata();
|
||||
} catch (error) {
|
||||
/* empty */
|
||||
}
|
||||
|
||||
const isPortrait = height > width;
|
||||
const thumbnailsExtension = metadata.format === 'jpeg' ? 'jpg' : metadata.format;
|
||||
if (metadata) {
|
||||
let { width, pageHeight: height = metadata.height } = metadata;
|
||||
if (metadata.orientation && metadata.orientation > 4) {
|
||||
[image, width, height] = [image.rotate(), height, width];
|
||||
}
|
||||
|
||||
try {
|
||||
const resizeBuffer = await image
|
||||
.resize(
|
||||
256,
|
||||
isPortrait ? 320 : undefined,
|
||||
width < 256 || (isPortrait && height < 320)
|
||||
? {
|
||||
kernel: sharp.kernel.nearest,
|
||||
}
|
||||
: undefined,
|
||||
)
|
||||
.toBuffer();
|
||||
const thumbnailsPathSegment = `${dirPathSegment}/thumbnails`;
|
||||
const thumbnailsExtension = metadata.format === 'jpeg' ? 'jpg' : metadata.format;
|
||||
|
||||
await fileManager.save(
|
||||
`${dirPathSegment}/thumbnails/cover-256.${thumbnailsExtension}`,
|
||||
resizeBuffer,
|
||||
inputs.file.type,
|
||||
);
|
||||
try {
|
||||
const outside360Buffer = await image
|
||||
.resize(360, 360, {
|
||||
fit: 'outside',
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.png({
|
||||
quality: 75,
|
||||
force: false,
|
||||
})
|
||||
.toBuffer();
|
||||
|
||||
fileData.image = {
|
||||
width,
|
||||
height,
|
||||
thumbnailsExtension,
|
||||
};
|
||||
} catch (error) {
|
||||
console.warn(error.stack); // eslint-disable-line no-console
|
||||
await fileManager.save(
|
||||
`${thumbnailsPathSegment}/outside-360.${thumbnailsExtension}`,
|
||||
outside360Buffer,
|
||||
inputs.file.type,
|
||||
);
|
||||
|
||||
const outside720Buffer = await image
|
||||
.resize(720, 720, {
|
||||
fit: 'outside',
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.png({
|
||||
quality: 75,
|
||||
force: false,
|
||||
})
|
||||
.toBuffer();
|
||||
|
||||
await fileManager.save(
|
||||
`${thumbnailsPathSegment}/outside-720.${thumbnailsExtension}`,
|
||||
outside720Buffer,
|
||||
inputs.file.type,
|
||||
);
|
||||
|
||||
data.image = {
|
||||
width,
|
||||
height,
|
||||
thumbnailsExtension,
|
||||
};
|
||||
} catch (error) {
|
||||
sails.log.warn(error.stack);
|
||||
await fileManager.deleteDir(thumbnailsPathSegment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!filePath) {
|
||||
try {
|
||||
await rimraf(inputs.file.fd);
|
||||
} catch (error) {
|
||||
console.warn(error.stack); // eslint-disable-line no-console
|
||||
}
|
||||
await rimraf(inputs.file.fd);
|
||||
}
|
||||
|
||||
return fileData;
|
||||
return data;
|
||||
},
|
||||
};
|
||||
|
|
35
server/api/helpers/attachments/remove-unreferenced-files.js
Normal file
35
server/api/helpers/attachments/remove-unreferenced-files.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
sync: true,
|
||||
|
||||
inputs: {
|
||||
fileReferenceOrFileReferences: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
fn(inputs) {
|
||||
const fileReferences = _.isPlainObject(inputs.fileReferenceOrFileReferences)
|
||||
? [inputs.fileReferenceOrFileReferences]
|
||||
: inputs.fileReferenceOrFileReferences;
|
||||
|
||||
const fileManager = sails.hooks['file-manager'].getInstance();
|
||||
|
||||
fileReferences.forEach(async (fileReference) => {
|
||||
if (fileReference.total !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
await fileManager.deleteDir(
|
||||
`${sails.config.custom.attachmentsPathSegment}/${fileReference.id}`,
|
||||
);
|
||||
|
||||
await FileReference.destroyOne(fileReference.id);
|
||||
});
|
||||
},
|
||||
};
|
|
@ -1,3 +1,8 @@
|
|||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
record: {
|
||||
|
@ -36,32 +41,32 @@ module.exports = {
|
|||
async fn(inputs) {
|
||||
const { values } = inputs;
|
||||
|
||||
const attachment = await Attachment.updateOne(inputs.record.id).set({ ...values });
|
||||
const attachment = await Attachment.qm.updateOne(inputs.record.id, values);
|
||||
|
||||
if (attachment) {
|
||||
sails.sockets.broadcast(
|
||||
`board:${inputs.board.id}`,
|
||||
'attachmentUpdate',
|
||||
{
|
||||
item: attachment,
|
||||
item: sails.helpers.attachments.presentOne(attachment),
|
||||
},
|
||||
inputs.request,
|
||||
);
|
||||
|
||||
sails.helpers.utils.sendWebhooks.with({
|
||||
event: 'attachmentUpdate',
|
||||
data: {
|
||||
item: attachment,
|
||||
buildData: () => ({
|
||||
item: sails.helpers.attachments.presentOne(attachment),
|
||||
included: {
|
||||
projects: [inputs.project],
|
||||
boards: [inputs.board],
|
||||
lists: [inputs.list],
|
||||
cards: [inputs.card],
|
||||
},
|
||||
},
|
||||
prevData: {
|
||||
item: inputs.record,
|
||||
},
|
||||
}),
|
||||
buildPrevData: () => ({
|
||||
item: sails.helpers.attachments.presentOne(inputs.record),
|
||||
}),
|
||||
user: inputs.actorUser,
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue