feat: Add gallery for attachments
parent
0be598ca9e
commit
86e4864d1b
@ -0,0 +1,45 @@
|
||||
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,38 @@
|
||||
const dotenv = require('dotenv');
|
||||
const sails = require('sails');
|
||||
const rc = require('sails/accessible/rc');
|
||||
|
||||
process.chdir(__dirname);
|
||||
dotenv.config();
|
||||
|
||||
const config = rc('sails');
|
||||
|
||||
const getConfigPromise = new Promise((resolve) => {
|
||||
sails.load(
|
||||
{
|
||||
...config,
|
||||
hooks: {
|
||||
...config.hooks,
|
||||
logger: false,
|
||||
request: false,
|
||||
views: false,
|
||||
blueprints: false,
|
||||
responses: false,
|
||||
helpers: false,
|
||||
pubsub: false,
|
||||
policies: false,
|
||||
services: false,
|
||||
security: false,
|
||||
i18n: false,
|
||||
session: false,
|
||||
http: false,
|
||||
userhooks: false,
|
||||
},
|
||||
},
|
||||
() => {
|
||||
resolve(sails.config);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
module.exports = () => getConfigPromise;
|
||||
Loading…
Reference in New Issue