1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00

ref: Collapse image data into one column

This commit is contained in:
Maksim Eltyshev 2022-06-21 12:01:41 +02:00
parent fe9fdc0191
commit 39fec45a7f
10 changed files with 102 additions and 86 deletions

View file

@ -57,11 +57,8 @@ const Attachments = React.memo(
}, [toggleAllVisible]); }, [toggleAllVisible]);
const galleryItemsNode = items.map((item, index) => { const galleryItemsNode = items.map((item, index) => {
const props = item.coverUrl const props = item.image
? { ? item.image
width: item.imageWidth,
height: item.imageHeight,
}
: { : {
content: ( content: (
<Grid verticalAlign="middle" className={styles.contentWrapper}> <Grid verticalAlign="middle" className={styles.contentWrapper}>
@ -91,7 +88,7 @@ const Attachments = React.memo(
createdAt={item.createdAt} createdAt={item.createdAt}
isCover={item.isCover} isCover={item.isCover}
isPersisted={item.isPersisted} isPersisted={item.isPersisted}
onClick={item.coverUrl ? open : undefined} onClick={item.image ? open : undefined}
onCoverSelect={() => handleCoverSelect(item.id)} onCoverSelect={() => handleCoverSelect(item.id)}
onCoverDeselect={handleCoverDeselect} onCoverDeselect={handleCoverDeselect}
onUpdate={(data) => handleUpdate(item.id, data)} onUpdate={(data) => handleUpdate(item.id, data)}

View file

@ -9,6 +9,7 @@ export default class extends Model {
id: attr(), id: attr(),
url: attr(), url: attr(),
coverUrl: attr(), coverUrl: attr(),
image: attr(),
name: attr(), name: attr(),
cardId: fk({ cardId: fk({
to: 'Card', to: 'Card',

View file

@ -54,10 +54,8 @@ module.exports = {
const attachment = await sails.helpers.attachments.createOne( const attachment = await sails.helpers.attachments.createOne(
{ {
dirname: file.extra.dirname, ...file.extra,
filename: file.filename, filename: file.filename,
isImage: file.extra.isImage,
name: file.extra.name,
}, },
currentUser, currentUser,
card, card,

View file

@ -46,7 +46,7 @@ module.exports = {
} }
} }
if (!attachment.isImage) { if (!attachment.image) {
throw Errors.ATTACHMENT_NOT_FOUND; throw Errors.ATTACHMENT_NOT_FOUND;
} }

View file

@ -53,7 +53,7 @@ module.exports = {
} }
this.res.type(attachment.filename); this.res.type(attachment.filename);
if (!attachment.isImage && path.extname(attachment.filename) !== '.pdf') { if (!attachment.image && path.extname(attachment.filename) !== '.pdf') {
this.res.set('Content-Disposition', 'attachment'); this.res.set('Content-Disposition', 'attachment');
} }
this.res.set('Cache-Control', 'private, max-age=900'); // TODO: move to config this.res.set('Cache-Control', 'private, max-age=900'); // TODO: move to config

View file

@ -38,7 +38,7 @@ module.exports = {
inputs.request, inputs.request,
); );
if (!inputs.card.coverAttachmentId && attachment.isImage) { if (!inputs.card.coverAttachmentId && attachment.image) {
await sails.helpers.cards.updateOne(inputs.card, { await sails.helpers.cards.updateOne(inputs.card, {
coverAttachmentId: attachment.id, coverAttachmentId: attachment.id,
}); });

View file

@ -43,15 +43,20 @@ module.exports = {
await writeFile(path.join(rootPath, filename), buffer); await writeFile(path.join(rootPath, filename), buffer);
const image = sharp(buffer); const image = sharp(buffer);
let imageMetadata; let metadata;
try { try {
imageMetadata = await image.metadata(); metadata = await image.metadata();
} catch (error) {} // eslint-disable-line no-empty } catch (error) {} // eslint-disable-line no-empty
if (imageMetadata) { const extra = {
dirname,
name: file.filename,
};
if (metadata && !['svg', 'pdf'].includes(metadata.format)) {
let cover256Buffer; let cover256Buffer;
if (imageMetadata.height > imageMetadata.width) { if (metadata.height > metadata.width) {
cover256Buffer = await image cover256Buffer = await image
.resize(256, 320) .resize(256, 320)
.jpeg({ .jpeg({
@ -75,17 +80,16 @@ module.exports = {
fs.mkdirSync(thumbnailsPath); fs.mkdirSync(thumbnailsPath);
await writeFile(path.join(thumbnailsPath, 'cover-256.jpg'), cover256Buffer); await writeFile(path.join(thumbnailsPath, 'cover-256.jpg'), cover256Buffer);
extra.image = _.pick(metadata, ['width', 'height']);
} else {
extra.image = null;
} }
// eslint-disable-next-line no-param-reassign Object.assign(file, {
file.extra = { filename,
dirname, extra,
isImage: !!imageMetadata, });
name: file.filename,
};
// eslint-disable-next-line no-param-reassign
file.filename = filename;
return done(); return done();
} catch (error) { } catch (error) {

View file

@ -19,20 +19,8 @@ module.exports = {
type: 'string', type: 'string',
required: true, required: true,
}, },
isImage: { image: {
type: 'boolean', type: 'json',
required: true,
columnName: 'is_image',
},
imageWidth: {
type: 'number',
allowNull: true,
columnName: 'image_width',
},
imageHeight: {
type: 'number',
allowNull: true,
columnName: 'image_height',
}, },
name: { name: {
type: 'string', type: 'string',
@ -61,9 +49,9 @@ module.exports = {
customToJSON() { customToJSON() {
return { return {
..._.omit(this, ['dirname', 'filename', 'isImage']), ..._.omit(this, ['dirname', 'filename']),
url: `${sails.config.custom.attachmentsUrl}/${this.id}/download/${this.filename}`, url: `${sails.config.custom.attachmentsUrl}/${this.id}/download/${this.filename}`,
coverUrl: this.isImage coverUrl: this.image
? `${sails.config.custom.attachmentsUrl}/${this.id}/download/thumbnails/cover-256.jpg` ? `${sails.config.custom.attachmentsUrl}/${this.id}/download/thumbnails/cover-256.jpg`
: null, : null,
}; };

View file

@ -1,45 +0,0 @@
const path = require('path');
const sharp = require('sharp');
const getConfig = require('../../get-config');
module.exports.up = async (knex) => {
await knex.schema.table('attachment', (table) => {
/* Columns */
table.integer('image_width');
table.integer('image_height');
});
const config = await getConfig();
const attachments = await knex('attachment');
// eslint-disable-next-line no-restricted-syntax
for (attachment of attachments) {
if (attachment.is_image) {
const image = sharp(
path.join(config.custom.attachmentsPath, attachment.dirname, attachment.filename),
);
let metadata;
try {
metadata = await image.metadata(); // eslint-disable-line no-await-in-loop
} catch (error) {
continue; // eslint-disable-line no-continue
}
// eslint-disable-next-line no-await-in-loop
await knex('attachment')
.update({
image_width: metadata.width,
image_height: metadata.height,
})
.where('id', attachment.id);
}
}
};
module.exports.down = (knex) =>
knex.schema.table('attachment', (table) => {
table.dropColumns('image_width', 'image_height');
});

View file

@ -0,0 +1,73 @@
const path = require('path');
const sharp = require('sharp');
const _ = require('lodash');
const getConfig = require('../../get-config');
module.exports.up = async (knex) => {
await knex.schema.table('attachment', (table) => {
/* Columns */
table.jsonb('image');
});
const config = await getConfig();
const attachments = await knex('attachment');
// eslint-disable-next-line no-restricted-syntax
for (attachment of attachments) {
if (attachment.is_image) {
const image = sharp(
path.join(config.custom.attachmentsPath, attachment.dirname, attachment.filename),
);
let metadata;
try {
metadata = await image.metadata(); // eslint-disable-line no-await-in-loop
} catch (error) {
continue; // eslint-disable-line no-continue
}
if (!['svg', 'pdf'].includes(metadata.format)) {
// eslint-disable-next-line no-await-in-loop
await knex('attachment')
.update({
image: _.pick(metadata, ['width', 'height']),
})
.where('id', attachment.id);
}
}
}
return knex.schema.table('attachment', (table) => {
table.dropColumn('is_image');
});
};
module.exports.down = async (knex) => {
await knex.schema.table('attachment', (table) => {
/* Columns */
table.boolean('is_image');
});
const attachments = await knex('attachment');
// eslint-disable-next-line no-restricted-syntax
for (attachment of attachments) {
// eslint-disable-next-line no-await-in-loop
await knex('attachment')
.update({
is_image: !!attachment.image,
})
.where('id', attachment.id);
}
await knex.schema.table('attachment', (table) => {
table.dropColumn('image');
});
return knex.schema.alterTable('attachment', (table) => {
table.boolean('is_image').notNullable().alter();
});
};