2018-05-04 07:24:08 +03:00
|
|
|
const chalk = require('chalk');
|
|
|
|
const log = require('single-line-log').stdout;
|
2018-05-03 18:08:42 +03:00
|
|
|
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++) {
|
2018-05-04 07:24:08 +03:00
|
|
|
promiseGens.push(() => {
|
|
|
|
const promise = new Promise(async (res, rej) => {
|
|
|
|
const file = await DriveFile.findOne(prev ? {
|
|
|
|
_id: { $gt: prev._id }
|
|
|
|
} : {}, {
|
|
|
|
sort: {
|
|
|
|
_id: 1
|
|
|
|
}
|
|
|
|
});
|
2018-05-03 18:08:42 +03:00
|
|
|
|
2018-05-04 07:24:08 +03:00
|
|
|
prev = file;
|
2018-05-03 18:08:42 +03:00
|
|
|
|
2018-05-04 07:24:08 +03:00
|
|
|
if (file == null) return res();
|
2018-05-03 18:08:42 +03:00
|
|
|
|
2018-05-04 07:24:08 +03:00
|
|
|
log(chalk`scanning: {bold ${file._id}} ...`);
|
2018-05-03 18:08:42 +03:00
|
|
|
|
2018-05-04 07:24:08 +03:00
|
|
|
const attachingUsersCount = await User.count({
|
|
|
|
$or: [{
|
|
|
|
avatarId: file._id
|
|
|
|
}, {
|
|
|
|
bannerId: file._id
|
|
|
|
}]
|
|
|
|
}, { limit: 1 });
|
|
|
|
if (attachingUsersCount !== 0) return res();
|
2018-05-03 18:08:42 +03:00
|
|
|
|
2018-05-04 07:24:08 +03:00
|
|
|
const attachingNotesCount = await Note.count({
|
|
|
|
mediaIds: file._id
|
|
|
|
}, { limit: 1 });
|
|
|
|
if (attachingNotesCount !== 0) return res();
|
2018-05-03 18:08:42 +03:00
|
|
|
|
2018-05-04 07:24:08 +03:00
|
|
|
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();
|
|
|
|
});
|
2018-05-03 18:08:42 +03:00
|
|
|
|
2018-05-04 07:24:08 +03:00
|
|
|
return promise;
|
|
|
|
});
|
2018-05-03 18:08:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return await sequential(promiseGens);
|
|
|
|
}
|
|
|
|
|
2018-05-04 07:24:08 +03:00
|
|
|
main().then().catch(console.error);
|