Skip to content

Commit

Permalink
Fix non-image files causing unhandled exception on upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanraftery committed Mar 24, 2020
1 parent 8f69d46 commit 89fd54e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 83 deletions.
177 changes: 96 additions & 81 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,106 @@
'use strict';

/**
* Module dependencies
*/

// Public node modules.
const fs = require('fs');
const path = require('path');
var Jimp = require('jimp');
const Jimp = require('jimp');
const ImageType = require('image-type');

/**
* Local Resize Upload Provider
* Saves uploads to localhost with configured resized images
*/
module.exports = {
provider: 'local-resize',
name: 'Local upload resize',
init: (config) => {
return {
upload: (file) => {
return new Promise((resolve, reject) => {
//resize function
function resize(file,variant){
return Jimp.read(file.buffer)
.then(image => {
//largest
let destImg = path.join(strapi.config.appPath, 'public', `uploads/${variant.prefix}${file.hash}${file.ext}`)
//change to preferences https://www.npmjs.com/package/jimp
if(variant.scaleType!=="resize"){
//cover image
image.cover(variant.maxSize,variant.maxSize)
}else{
//width, height
image.resize(Jimp.AUTO, variant.maxSize)
}
image.quality(variant.quality)
image.write(destImg)
file[variant.attributes] = `/uploads/${variant.prefix}${file.hash}${file.ext}`;
return true
})
.catch(err => {
return reject(err);
});
}
//define variants, props should be defined in plugins/uploads/models/File.settings.json
let variants = [
{
maxSize:900,
scaleType:"resize",
prefix:"large_",
quality:70,
attributes:'url'
},
{
maxSize:600,
scaleType:"cover",
prefix:"thumb_",
quality:70,
attributes:'thumb'
}
]
//resize image in two variants
var promise = resize(file,variants[0]);
promise
.then(function() {
return resize(file,variants[1]);
})
.then(function() {
return resolve()
})
.catch(err => {
return reject(err);
});
});
},
delete: (file) => {
return new Promise((resolve, reject) => {
const filePath = path.join(strapi.config.appPath, 'public', `uploads/baander${file.hash}${file.ext}`);
provider: 'local-resize',
name: 'Local upload resize',

init: () => {
return {
/**
* Upload
* @param file
* @returns {Promise<unknown>}
*/
upload: (file) => {
return new Promise((resolve, reject) => {
function resize(file, variant) {
return Jimp.read(file.buffer)
.then(image => {
let destImg = path.join(strapi.config.appPath, 'public', `uploads/${variant.prefix}${file.hash}${file.ext}`);
if (variant.scaleType !== "resize") {
image.cover(variant.maxSize, variant.maxSize)
} else {
image.resize(Jimp.AUTO, variant.maxSize)
}
image.quality(variant.quality);
image.write(destImg);
file[variant.attributes] = `/uploads/${variant.prefix}${file.hash}${file.ext}`;
return true
})
.catch(err => {
return reject(err);
});
}

// define variants, props should be defined in plugins/uploads/models/File.settings.json
let variants = [
{
maxSize: 1080,
scaleType: "resize",
prefix: "large_",
quality: 70,
attributes: 'url'
},
{
maxSize: 400,
scaleType: "cover",
prefix: "thumb_",
quality: 70,
attributes: 'thumb'
}
];

// skip resizing if file is not an image
if (!ImageType(file.buffer)) {
file.url = `/uploads/${file.hash}${file.ext}`;
return resolve();
}

// resize the images
const promise = resize(file, variants[0]);
promise
.then(function () {
return resize(file, variants[1]);
})
.then(function () {
return resolve()
})
.catch(err => {
return reject(err);
});
});
},

/**
* Delete
* @param file
* @returns {Promise<unknown>}
*/
delete: (file) => {
return new Promise((resolve, reject) => {
const filePath = path.join(strapi.config.appPath, 'public', `uploads/baander${file.hash}${file.ext}`);

if (!fs.existsSync(filePath)) {
return resolve('File doesn\'t exist');
}
// remove file from public/assets folder
fs.unlink(filePath, (err) => {
if (err) {
return reject(err);
if (!fs.existsSync(filePath)) {
return resolve('File doesn\'t exist');
}
// remove file from public/assets folder
fs.unlink(filePath, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
}
resolve();
});
});
}
}
}
}
};
19 changes: 17 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"bundleDependencies": false,
"dependencies": {
"image-type": "^4.1.0",
"jimp": "^0.6.0"
},
"deprecated": false,
Expand Down

0 comments on commit 89fd54e

Please sign in to comment.