summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-05-04 13:24:08 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-05-04 13:24:08 +0900
commitabfbb068d7c48cdd2b26f35da7233d8ae789998a (patch)
tree2cafa02c3303a7148b68fc0b6fb7853be8fdb239 /cli
parentFix (diff)
downloadsharkey-abfbb068d7c48cdd2b26f35da7233d8ae789998a.tar.gz
sharkey-abfbb068d7c48cdd2b26f35da7233d8ae789998a.tar.bz2
sharkey-abfbb068d7c48cdd2b26f35da7233d8ae789998a.zip
:v:
Diffstat (limited to 'cli')
-rw-r--r--cli/clean-drive.js78
1 files changed, 48 insertions, 30 deletions
diff --git a/cli/clean-drive.js b/cli/clean-drive.js
index f0c23b3b29..14eb002795 100644
--- a/cli/clean-drive.js
+++ b/cli/clean-drive.js
@@ -1,3 +1,5 @@
+const chalk = require('chalk');
+const log = require('single-line-log').stdout;
const sequential = require('promise-sequential');
const { default: DriveFile, deleteDriveFile } = require('../built/models/drive-file');
const { default: Note } = require('../built/models/note');
@@ -12,45 +14,61 @@ async function main() {
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
- }
- });
+ promiseGens.push(() => {
+ const promise = new Promise(async (res, rej) => {
+ const file = await DriveFile.findOne(prev ? {
+ _id: { $gt: prev._id }
+ } : {}, {
+ sort: {
+ _id: 1
+ }
+ });
- prev = file;
+ prev = file;
- console.log(`scanning ${file._id}`);
+ if (file == null) return res();
- const attachingUsersCount = await User.count({
- $or: [{
- avatarId: file._id
- }, {
- bannerId: file._id
- }]
- }, { limit: 1 });
- if (attachingUsersCount !== 0) return res();
+ log(chalk`scanning: {bold ${file._id}} ...`);
- const attachingNotesCount = await Note.count({
- mediaIds: file._id
- }, { limit: 1 });
- if (attachingNotesCount !== 0) return res();
+ const attachingUsersCount = await User.count({
+ $or: [{
+ avatarId: file._id
+ }, {
+ bannerId: file._id
+ }]
+ }, { limit: 1 });
+ if (attachingUsersCount !== 0) return res();
- const attachingMessagesCount = await MessagingMessage.count({
- fileId: file._id
- }, { limit: 1 });
- if (attachingMessagesCount !== 0) return res();
+ const attachingNotesCount = await Note.count({
+ mediaIds: file._id
+ }, { limit: 1 });
+ if (attachingNotesCount !== 0) return res();
- console.log(`deleting ${file._id}`);
+ const attachingMessagesCount = await MessagingMessage.count({
+ fileId: file._id
+ }, { limit: 1 });
+ if (attachingMessagesCount !== 0) return res();
+
+ deleteDriveFile(file).then(res).catch(rej);
+ });
+
+ promise.then(x => {
+ if (prev) {
+ if (x == null) {
+ log(chalk`{green skipped: {bold ${prev._id}}}`);
+ } else {
+ log(chalk`{red deleted: {bold ${prev._id}}}`);
+ }
+ }
+ log.clear();
+ console.log();
+ });
- deleteDriveFile(file).then(res).catch(rej);
- }));
+ return promise;
+ });
}
return await sequential(promiseGens);
}
-main().then(console.dir).catch(console.error);
+main().then().catch(console.error);