Unhandled error on previous profile image deletion fixed

parent af574442
...@@ -1018,7 +1018,8 @@ module.exports = { ...@@ -1018,7 +1018,8 @@ module.exports = {
try { try {
newAvatarFileDescriptor = uploadedFiles[0].fd; newAvatarFileDescriptor = uploadedFiles[0].fd;
if (student.pic !== sails.config.pictogram.paths.defaultAvatarFileName) { if (student.pic !== sails.config.pictogram.paths.defaultAvatarFileName) {
fs.unlinkSync(path.join(newAvatarDirectory, student.pic)); if (fs.existsSync(path.join(newAvatarDirectory, student.pic)))
fs.unlinkSync(path.join(newAvatarDirectory, student.pic));
} }
student.pic = newAvatarFileName; student.pic = newAvatarFileName;
...@@ -1028,9 +1029,9 @@ module.exports = { ...@@ -1028,9 +1029,9 @@ module.exports = {
} }
res.ok({file: {name: student.pic}}); res.ok({file: {name: student.pic}});
}); });
} catch (updateAvatarError) { } catch (err) {
fs.unlinkSync(newAvatarFileDescriptor); fs.unlinkSync(newAvatarFileDescriptor);
res.serverError("Error when updating profile image in server"); res.serverError("Error when updating profile image in server: " + err);
} }
}); });
}) })
......
...@@ -542,7 +542,8 @@ module.exports = { ...@@ -542,7 +542,8 @@ module.exports = {
newAvatarFileDescriptor = uploadedFiles[0].fd; newAvatarFileDescriptor = uploadedFiles[0].fd;
if (supervisor.pic !== sails.config.pictogram.paths.defaultAvatarFileName) { if (supervisor.pic !== sails.config.pictogram.paths.defaultAvatarFileName) {
sails.log.debug("removing old profile image"); sails.log.debug("removing old profile image");
fs.unlinkSync(path.join(newAvatarDirectory, supervisor.pic)); if (fs.existsSync(path.join(newAvatarDirectory, supervisor.pic)))
fs.unlinkSync(path.join(newAvatarDirectory, supervisor.pic));
} }
supervisor.pic = newAvatarFileName; supervisor.pic = newAvatarFileName;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment