When I send an image to my web server through an http request, it manages to save the original image with its full size. But when I attempt to resize the image, it saves it as 0 bytes. Im using an npm module called sharp to do the image resizing. When I do any resizing, even massive resizing jobs on my local machine, there are no issues. Running this code on my Apache web server causes it to not actually save the images most of the time. It seems completely random which images it actually saves. The only consistent part is that it saves the original image sent through the http request.
In my filesystem there will be the original image EG: abcdefg.jpg, and then abcdefg-350x350.jpg will be 0 bytes.
const results = await temp.mv(path.resolve(folderName + name + '.' + extension))
bluebird.map(sizes, (size) => {
sharp(folderName + name + '.' + extension)
.resize(size.w, size.h, {
fit: 'cover'
})
.toFile(folderName + name + '-' + size.w + 'x' + size.h + '.' + extension)
console.log('Resized image')
})
.then(() => {
console.log('Images were resized')
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'testing') {
console.log('Images resized and going back');
res.status(200).json({'location': '/imageserving/uploads/' + year + '/' + month + '/' + name + '.' + extension, fileIndex: '1'});
}
else if (process.env.NODE_ENV === 'production') {
//possibly use req.hostname
res.status(200).json({'location': 'mysite/uploads/' + year + '/' + month + '/' + name + '.' + extension, fileIndex: '1'});
}
})
Try updating this line:
sharp(folderName + name + '.' + extension)
to this:
return sharp(folderName + name + '.' + extension)
...that should make sure the following then() section is properly chained to the promise and avoids potential race conditions that may be causing the inconsistent behavior.