Fix profile image upload bug

- Fix bug where the upload failed because a nonexistend profile picture can't be deleted
pull/5/head
Trivernis 5 years ago
parent 9e7150645b
commit df2f440535

@ -112,12 +112,14 @@ export class UploadRoute extends Route {
.toFile(filePath); .toFile(filePath);
fileName = `/${dataDirName}/${fileBasename}`; fileName = `/${dataDirName}/${fileBasename}`;
const user = await User.findByPk(request.session.userId); const user = await User.findByPk(request.session.userId);
if (user.profilePicture) {
const oldProfilePicture = path.join(this.dataDir, path.basename(user.profilePicture)); const oldProfilePicture = path.join(this.dataDir, path.basename(user.profilePicture));
if (await fsx.pathExists(oldProfilePicture)) { if (await fsx.pathExists(oldProfilePicture)) {
await fsx.unlink(oldProfilePicture); await fsx.unlink(oldProfilePicture);
} else { } else {
globals.logger.warn(`Could not delete ${oldProfilePicture}: Not found!`); globals.logger.warn(`Could not delete ${oldProfilePicture}: Not found!`);
} }
}
user.profilePicture = fileName; user.profilePicture = fileName;
await user.save(); await user.save();
success = true; success = true;

Loading…
Cancel
Save