Respected sir,
I am using sharp for the first time and really it is a fast and accurate and fulfilling my work so thanks for it, but it is giving me an awkward error and i do not know how to resolve it .
Issue : when i select image let 1.jpg for the first time after restating my node server it gives me an error 'Input file contains unsupported image format' though it supports .jpg format then again i select the same image again and again same issue and after 3-4 try it accepts the same image and stored perfectly fine with resize in my folder.
I am not getting why i am getting this issue and this is not an error cause it is accepting the same image and sometimes does not support same image.
sometimes it accepts the same image in first time and sometimes 10-12 try would be there i am using it in my live web application and i am just loving it now but with this issue i can not run it more.
Any help would be great.
code :
sharp(filePath)
.resize(null,400)
.toBuffer()
.then( data => {
fileSystem.writeFileSync(statusPath+staImageName, data);
})
.catch( error => {
});
@lovell can you please help me in this ?
Hello, if this code works sometimes but not others, then my best guess would be that some logic in your other code not shown here is concurrently writing to or updating the contents of filePath at the same time as attempting to process the file.
sharp(req.files.txtImage.tempFilePath)
.resize(null,400)
.toBuffer()
.then( data => {
fileSystem.writeFileSync(statusPath+staImageName, data);
insertQuery.staColor = "";
insertQuery.staImage = staImageName;
fileSystem.readdir(tempImagePath, function(error, files){
if(error){
console.log('statusRoute | Error While Deleting Temp Image'+ error);
req.flash('error', 'Error While Deleting Temp Image');
res.redirect('/status');
}else{
for (var file of files) {
fileSystem.unlink((tempImagePath+file), function(error,result) {
if (error){
console.log('statusRoute | Error While Deleting Temp Image'+ error);
req.flash('error', 'Error While Deleting Temp Image');
res.redirect('/status');
}else{
}
});
}
addStatus();
}
});
})
.catch( error => {
console.log ('Type'+req.files.txtImage.mimetype);
console.log('statusRoute | Error In Uploading Status Image'+ error);
req.flash('error', 'Error While Uploading Status Image');
res.redirect('/status');
});
This is the full code sir i am using tempFilePath to reduce the size and write it again to destination folder and after it successful write i am deleting the tempFilePath automatically . can you please help after this code to me.
Thanks @lovell
It looks like there's a possible race condition in your code. The logic that is deleting all the files in tempImagePath can occur whilst one or more of those same files, referenced by req.files.txtImage.tempFilePath, is still being processed.
This sort of question isn't directly about sharp and is more suited to a site like StackOverflow.
@lovell I have searched a lot about it on stackoverflow and all help pages after all i asked it to you but i think you are right too.I need to check code again.
Thanks for the help sir it means a lot when a person like you reply to new one thank you so much sir.
Most helpful comment
@lovell I have searched a lot about it on stackoverflow and all help pages after all i asked it to you but i think you are right too.I need to check code again.
Thanks for the help sir it means a lot when a person like you reply to new one thank you so much sir.