merges image resize bugfix
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const mongoose = require('mongoose');
|
||||
const autopopulate = require('mongoose-autopopulate');
|
||||
const mongooseHidden = require('mongoose-hidden');
|
||||
const fieldToSchemaMap = require('../mongoose/schema/fieldToSchemaMap');
|
||||
const localizationPlugin = require('../localization/plugin');
|
||||
|
||||
@@ -25,7 +26,8 @@ const registerSchema = (globalConfigs, config) => {
|
||||
'globals',
|
||||
new mongoose.Schema({ ...globalSchemaGroups, timestamps: false })
|
||||
.plugin(localizationPlugin, config.localization)
|
||||
.plugin(autopopulate),
|
||||
.plugin(autopopulate)
|
||||
.plugin(mongooseHidden()),
|
||||
);
|
||||
|
||||
return globals;
|
||||
|
||||
@@ -28,7 +28,7 @@ class Payload {
|
||||
this.getCollections.bind(this);
|
||||
this.getGlobals.bind(this);
|
||||
|
||||
// Setup & inititalization
|
||||
// Setup & initialization
|
||||
connectMongoose(options.config.mongoURL);
|
||||
registerExpressMiddleware(options);
|
||||
initPassport(options.app);
|
||||
@@ -78,6 +78,7 @@ class Payload {
|
||||
}
|
||||
|
||||
registerUpload() {
|
||||
// TODO: mongooseHidden on our upload model is hiding all the fields
|
||||
const uploadSchema = buildCollectionSchema(
|
||||
this.config.upload,
|
||||
this.config,
|
||||
@@ -91,14 +92,15 @@ class Payload {
|
||||
});
|
||||
|
||||
this.Upload = mongoose.model(this.config.upload.labels.singular, uploadSchema);
|
||||
// TODO: image type hard coded, but in the future we need some way of customizing how uploads are handled in customizable pattern
|
||||
this.Upload.discriminator('image', imageSchema);
|
||||
|
||||
registerUploadRoutes(this.Upload, this.config, this.router);
|
||||
|
||||
registerCollectionRoutes({
|
||||
model: this.Upload,
|
||||
config: this.config.upload,
|
||||
}, this.router);
|
||||
|
||||
registerUploadRoutes(this.Upload, this.config, this.router);
|
||||
}
|
||||
|
||||
registerGlobals(globals) {
|
||||
|
||||
@@ -12,29 +12,34 @@ function getOutputImageName(sourceImage, size) {
|
||||
}
|
||||
|
||||
module.exports = async function resizeAndSave(config, uploadConfig, file) {
|
||||
const sourceImage = `${config.staticDir}/${file.name}`;
|
||||
/**
|
||||
* Resize images according to image desired width and height and return sizes
|
||||
* @param config
|
||||
* @param uploadConfig
|
||||
* @param file
|
||||
* @returns String[]
|
||||
*/
|
||||
|
||||
const outputSizes = [];
|
||||
|
||||
const sourceImage = `${config.staticDir}/${file.name}`;
|
||||
let sizes;
|
||||
try {
|
||||
const dimensions = await sizeOf(sourceImage);
|
||||
uploadConfig.imageSizes.forEach(async (desiredSize) => {
|
||||
if (desiredSize.width > dimensions.width) {
|
||||
return;
|
||||
}
|
||||
const outputImageName = getOutputImageName(sourceImage, desiredSize);
|
||||
await sharp(sourceImage)
|
||||
.resize(desiredSize.width, desiredSize.height, {
|
||||
position: desiredSize.crop || 'centre',
|
||||
})
|
||||
.toFile(outputImageName);
|
||||
outputSizes.push({
|
||||
name: desiredSize.name,
|
||||
height: desiredSize.height,
|
||||
width: desiredSize.width,
|
||||
sizes = uploadConfig.imageSizes
|
||||
.filter(desiredSize => desiredSize.width < dimensions.width)
|
||||
.map(async (desiredSize) => {
|
||||
const outputImageName = getOutputImageName(sourceImage, desiredSize);
|
||||
await sharp(sourceImage)
|
||||
.resize(desiredSize.width, desiredSize.height, {
|
||||
// would it make sense for this to be set by the uploader?
|
||||
position: desiredSize.crop || 'centre',
|
||||
})
|
||||
.toFile(outputImageName);
|
||||
return { ...desiredSize };
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
console.log('error in resize and save', e.message);
|
||||
}
|
||||
return outputSizes;
|
||||
|
||||
return Promise.all(sizes);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user