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:
parent
fe9fdc0191
commit
39fec45a7f
10 changed files with 102 additions and 86 deletions
|
@ -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();
|
||||
});
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue