From e413e7632197edf559081015858c65ff8d11e4d6 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Thu, 9 Jan 2020 17:08:22 +0100 Subject: [PATCH] Add deletion of old profile pictures after uploading a new one --- src/routes/upload.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/routes/upload.ts b/src/routes/upload.ts index e4a34c3..ed957f7 100644 --- a/src/routes/upload.ts +++ b/src/routes/upload.ts @@ -49,6 +49,12 @@ export class UploadRoute extends Route { .toFile(filePath); fileName = `/${dataDirName}/${fileBasename}`; const user = await User.findByPk(req.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!`); + } user.profilePicture = fileName; await user.save(); success = true;