parent
e7495e0067
commit
6268ced4cb
@ -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');
|
|
||||||
});
|
|
||||||
@ -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…
Reference in New Issue