This commit is contained in:
syuilo 2018-05-04 13:24:08 +09:00
parent 9643a87320
commit abfbb068d7
2 changed files with 54 additions and 34 deletions

View file

@ -1,3 +1,5 @@
const chalk = require('chalk');
const log = require('single-line-log').stdout;
const sequential = require('promise-sequential'); const sequential = require('promise-sequential');
const { default: DriveFile, deleteDriveFile } = require('../built/models/drive-file'); const { default: DriveFile, deleteDriveFile } = require('../built/models/drive-file');
const { default: Note } = require('../built/models/note'); const { default: Note } = require('../built/models/note');
@ -12,45 +14,61 @@ async function main() {
let prev; let prev;
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
promiseGens.push(() => new Promise(async (res, rej) => { promiseGens.push(() => {
const file = await DriveFile.findOne(prev ? { const promise = new Promise(async (res, rej) => {
_id: { $gt: prev._id } const file = await DriveFile.findOne(prev ? {
} : {}, { _id: { $gt: prev._id }
sort: { } : {}, {
_id: 1 sort: {
} _id: 1
}
});
prev = file;
if (file == null) return res();
log(chalk`scanning: {bold ${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();
deleteDriveFile(file).then(res).catch(rej);
}); });
prev = file; 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();
});
console.log(`scanning ${file._id}`); return promise;
});
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); return await sequential(promiseGens);
} }
main().then(console.dir).catch(console.error); main().then().catch(console.error);

View file

@ -76,6 +76,7 @@
"@types/request-promise-native": "1.0.14", "@types/request-promise-native": "1.0.14",
"@types/rimraf": "2.0.2", "@types/rimraf": "2.0.2",
"@types/seedrandom": "2.4.27", "@types/seedrandom": "2.4.27",
"@types/single-line-log": "^1.1.0",
"@types/speakeasy": "2.0.2", "@types/speakeasy": "2.0.2",
"@types/tmp": "0.0.33", "@types/tmp": "0.0.33",
"@types/uuid": "3.4.3", "@types/uuid": "3.4.3",
@ -180,6 +181,7 @@
"s-age": "1.1.2", "s-age": "1.1.2",
"sass-loader": "7.0.1", "sass-loader": "7.0.1",
"seedrandom": "2.4.3", "seedrandom": "2.4.3",
"single-line-log": "^1.1.2",
"speakeasy": "2.0.0", "speakeasy": "2.0.0",
"style-loader": "0.21.0", "style-loader": "0.21.0",
"stylus": "0.54.5", "stylus": "0.54.5",