From df2f440535d20c7491576686cc6535d80ac15acb Mon Sep 17 00:00:00 2001 From: Trivernis Date: Wed, 15 Jan 2020 16:38:50 +0100 Subject: [PATCH] Fix profile image upload bug - Fix bug where the upload failed because a nonexistend profile picture can't be deleted --- src/routes/UploadRoute.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/routes/UploadRoute.ts b/src/routes/UploadRoute.ts index 6f526b0..1beab93 100644 --- a/src/routes/UploadRoute.ts +++ b/src/routes/UploadRoute.ts @@ -112,11 +112,13 @@ export class UploadRoute extends Route { .toFile(filePath); fileName = `/${dataDirName}/${fileBasename}`; const user = await User.findByPk(request.session.userId); - const oldProfilePicture = path.join(this.dataDir, path.basename(user.profilePicture)); - if (await fsx.pathExists(oldProfilePicture)) { - await fsx.unlink(oldProfilePicture); - } else { - globals.logger.warn(`Could not delete ${oldProfilePicture}: Not found!`); + if (user.profilePicture) { + const oldProfilePicture = path.join(this.dataDir, path.basename(user.profilePicture)); + if (await fsx.pathExists(oldProfilePicture)) { + await fsx.unlink(oldProfilePicture); + } else { + globals.logger.warn(`Could not delete ${oldProfilePicture}: Not found!`); + } } user.profilePicture = fileName; await user.save();