From c9a57fe8febc9c7193a396ab2219813207410291 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 4 May 2018 00:08:42 +0900 Subject: [PATCH] =?UTF-8?q?=E6=8A=95=E7=A8=BF=E3=82=84=E3=83=A1=E3=83=83?= =?UTF-8?q?=E3=82=BB=E3=83=BC=E3=82=B8=E3=81=AA=E3=81=A9=E3=81=AB=E6=B7=BB?= =?UTF-8?q?=E4=BB=98=E3=81=95=E3=82=8C=E3=81=A6=E3=81=84=E3=81=AA=E3=81=84?= =?UTF-8?q?=E3=83=89=E3=83=A9=E3=82=A4=E3=83=96=E3=81=AE=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=A4=E3=83=AB=E3=82=92=E3=81=99=E3=81=B9=E3=81=A6=E5=89=8A?= =?UTF-8?q?=E9=99=A4=E3=81=99=E3=82=8B=E3=83=84=E3=83=BC=E3=83=AB=E3=82=92?= =?UTF-8?q?=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/clean-drive.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 57 insertions(+) create mode 100644 cli/clean-drive.js diff --git a/cli/clean-drive.js b/cli/clean-drive.js new file mode 100644 index 0000000000..f0c23b3b29 --- /dev/null +++ b/cli/clean-drive.js @@ -0,0 +1,56 @@ +const sequential = require('promise-sequential'); +const { default: DriveFile, deleteDriveFile } = require('../built/models/drive-file'); +const { default: Note } = require('../built/models/note'); +const { default: MessagingMessage } = require('../built/models/messaging-message'); +const { default: User } = require('../built/models/user'); + +async function main() { + const promiseGens = []; + + const count = await DriveFile.count({}); + + let prev; + + for (let i = 0; i < count; i++) { + promiseGens.push(() => new Promise(async (res, rej) => { + const file = await DriveFile.findOne(prev ? { + _id: { $gt: prev._id } + } : {}, { + sort: { + _id: 1 + } + }); + + prev = file; + + console.log(`scanning ${file._id}`); + + const attachingUsersCount = await User.count({ + $or: [{ + avatarId: file._id + }, { + bannerId: file._id + }] + }, { limit: 1 }); + if (attachingUsersCount !== 0) return res(); + + const attachingNotesCount = await Note.count({ + mediaIds: file._id + }, { limit: 1 }); + if (attachingNotesCount !== 0) return res(); + + const attachingMessagesCount = await MessagingMessage.count({ + fileId: file._id + }, { limit: 1 }); + if (attachingMessagesCount !== 0) return res(); + + console.log(`deleting ${file._id}`); + + deleteDriveFile(file).then(res).catch(rej); + })); + } + + return await sequential(promiseGens); +} + +main().then(console.dir).catch(console.error); diff --git a/package.json b/package.json index 1c6b41e4c2..6ba6bd298e 100644 --- a/package.json +++ b/package.json @@ -165,6 +165,7 @@ "os-utils": "0.0.14", "progress-bar-webpack-plugin": "1.11.0", "prominence": "0.2.0", + "promise-sequential": "^1.1.1", "pug": "2.0.3", "punycode": "2.1.0", "qrcode": "1.2.0",