Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const image = sharp();
source.on('error', err => next(err));
image.on('error', err => source.emit('error', err));
image.on('finish', () => fs.unlink(req.tempfile, () => {})); // eslint-disable-line no-empty-function
// Convert to JPEG? GIFs become still-frames
const convertToJPEG = (
(config.convertGIF && type.mime === 'image/gif') ||
(config.convertPNG && type.mime === 'image/png')
);
if (mimetype !== 'image/jpeg' && convertToJPEG) {
mimetype = 'image/jpeg';
image.background({ r: 0, g: 0, b: 0, alpha: 0 });
image.flatten();
image.toFormat(sharp.format.jpeg);
}
// Respect EXIF orientation headers
if (mimetype === 'image/jpeg') {
image.rotate();
}
// Apply recipe
recipe.process(image, Object.freeze(Object.assign({}, req.params, {
query: req.query,
path: req.path,
originalUrl: req.originalUrl,
})));
// Always apply compression at the end
if (mimetype === 'image/jpeg') {
s.metadata(Meteor.bindEnvironment((err, metadata) => {
if (!metadata) {
metadata = {};
}
s.toFormat(sharp.format.jpeg)
.resize(Math.min(height || 0, metadata.width || Infinity), Math.min(height || 0, metadata.height || Infinity))
.pipe(sharp()
.resize(height, height)
.background('#FFFFFF')
.embed()
)
// Use buffer to get the result in memory then replace the existing file
// There is no option to override a file using this library
.toBuffer()
.then(Meteor.bindEnvironment((outputBuffer) => {
fs.writeFile(tempFilePath, outputBuffer, Meteor.bindEnvironment((err) => {
if (err != null) {
console.error(err);
}
const { size } = fs.lstatSync(tempFilePath);
this.getCollection().direct.update({ _id: file._id }, { $set: { size } });
const mimetype = (({ mime }) => {
// Convert to JPEG? GIFs become still-frames
if (mime !== 'image/jpeg') {
const convertToJPEG = (
(config.convertGIF && mime === 'image/gif') ||
(config.convertPNG && mime === 'image/png')
);
if (convertToJPEG) {
image.background({ r: 0, g: 0, b: 0, alpha: 0 });
image.flatten();
image.toFormat(sharp.format.jpeg);
mime = 'image/jpeg';
}
}
// Respect EXIF orientation headers
if (mime === 'image/jpeg') {
image.rotate();
}
return mime;
})(type);
if (args !== null) {
// Require at least one input file.
// NOTE: check here b/c https://github.com/yargs/yargs/issues/403
if (args.input && args.input.length === 0) {
err = new Error('Not enough arguments following: i, input')
}
// Global Options.
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#toformat
if (args.format !== options.format.default) {
queue.unshift(['format', (sharp) => sharp.toFormat(args.format)])
}
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#heif
const { heif } = sharp.format
if (args.hcompression !== options.hcompression.default || // HEIF-specific.
// Ensure libheif is installed before applying generic options.
(heif.input && heif.input.file && (args.lossless || args.quality))) {
queue.unshift(['heif', (sharp) => {
return sharp.heif({
compression: args.hcompression,
lossless: args.lossless,
quality: args.quality
})
}])
}
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#jpeg
if (
args.chromaSubsampling ||
args.optimise ||
const resizeAndConvertImage = (size: number) =>
sharp(originalFileBuffer)
.resize(size, size, {
withoutEnlargement: true,
fit: sharp.fit.inside,
})
.toFormat(sharp.format.webp)
.toBuffer();
toConvert: true
});
if (opts.replaceFile) {
input.outputs[i] = webpOutput
}
else {
input.outputs.push(webpOutput);
}
}
}
finish = -input.outputs.length + 1;
for (let i in input.outputs) {
if (input.outputs[i].toConvert) {
sharp(input.outputs[i].buffer)
.toFormat(sharp.format.webp)
.webp({
quality,
alphaQuality,
lossless
})
.toBuffer()
.then(function(i) {
return function(buffer) {
input.outputs[i].buffer = buffer;
next();
};
}(i))
.catch(next);
}
else {
next();
.map(filePath => cb =>
optimiseImage(filePath,
isJpeg(filePath) ? sharp.format.jpeg : sharp.format.png,
cb)).value();
let response = await request({
encoding: null,
uri: imageUrl,
resolveWithFullResponse: true,
})
debug('Requested %s. Status: %s', imageUrl, response.statusCode)
if (response.statusCode >= 400) {
return res.sendStatus(response.statusCode)
}
res.status(response.statusCode)
const inputFormat = response.headers['content-type'] || ''
format = format || inputFormat.replace('image/', '')
format = sharp.format.hasOwnProperty(format) ? format : 'jpeg'
res.type(`image/${format}`)
const image = await transform(response.body, {
crop,
cropMaxSize,
gravity,
height,
quality,
width,
})
res.send(image)
} catch (e) {
if (e.statusCode === 404) return next()
next(e)
}
})
isSharpFormat: function(value) {
return sharp.format.hasOwnProperty(value)
},
isGravity: function(value) {