From 694ec7ebe19e9b385da9263739c67acbe5e6b944 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 14 Apr 2019 11:50:31 +0900 Subject: [PATCH] Fix bug --- src/services/drive/add-file.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index 9eccfb125c..54d7f2d13a 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -201,13 +201,20 @@ async function upload(key: string, stream: fs.ReadStream | Buffer, type: string, } async function deleteOldFile(user: IRemoteUser) { - const oldFile = await DriveFiles.createQueryBuilder() - .select('file') - .where('file.id != :avatarId', { avatarId: user.avatarId }) - .andWhere('file.id != :bannerId', { bannerId: user.bannerId }) - .andWhere('file.userId = :userId', { userId: user.id }) - .orderBy('file.id', 'DESC') - .getOne(); + const q = DriveFiles.createQueryBuilder('file') + .where('file.userId = :userId', { userId: user.id }); + + if (user.avatarId) { + q.andWhere('file.id != :avatarId', { avatarId: user.avatarId }); + } + + if (user.bannerId) { + q.andWhere('file.id != :bannerId', { bannerId: user.bannerId }) + } + + q.orderBy('file.id', 'DESC'); + + const oldFile = await q.getOne(); if (oldFile) { delFile(oldFile, true);