-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix non-image files causing unhandled exception on upload
- Loading branch information
1 parent
8f69d46
commit 89fd54e
Showing
3 changed files
with
114 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters