This commit is contained in:
syuilo 2018-05-04 17:27:14 +09:00
parent 479d76c0d4
commit b6907c78e8
2 changed files with 50 additions and 31 deletions

View file

@ -3,8 +3,6 @@ const log = require('single-line-log').stdout;
const sequential = require('promise-sequential');
const { default: DriveFile, DriveFileChunk } = require('../built/models/drive-file');
const { default: DriveFileThumbnail, DriveFileThumbnailChunk } = require('../built/models/drive-file-thumbnail');
const { default: Note } = require('../built/models/note');
const { default: MessagingMessage } = require('../built/models/messaging-message');
const { default: User } = require('../built/models/user');
const q = {
@ -33,21 +31,37 @@ async function main() {
prev = file;
if (file == null) return res([i, null]);
function skip() {
res([i, file, false]);
}
if (file == null) return skip();
log(chalk`{gray ${i}} scanning {bold ${file._id}} ${file.filename} ...`);
const attachingUsersCount = await User.count({
$or: [{
avatarId: file._id
}, {
bannerId: file._id
}]
}, { limit: 1 });
if (attachingUsersCount !== 0) return skip();
Promise.all([
// チャンクをすべて削除
await DriveFileChunk.remove({
DriveFileChunk.remove({
files_id: file._id
});
}),
await DriveFile.update({ _id: file._id }, {
DriveFile.update({ _id: file._id }, {
$set: {
'metadata.deletedAt': new Date(),
'metadata.isExpired': true
}
});
res([i, file]);
})
]).then(() => {
res([i, file, true]);
//#region サムネイルもあれば削除
const thumbnail = await DriveFileThumbnail.findOne({
@ -63,11 +77,16 @@ async function main() {
}
//#endregion
});
});
promise.then(([i, file]) => {
if (file) {
console.log(chalk`{gray ${i}} {green done: {bold ${file._id}} ${file.filename}}`);
promise.then(([i, file, deleted]) => {
if (deleted) {
log(chalk`{gray ${i}} {red deleted: {bold ${file._id}} ${file.filename}}`);
} else {
log(chalk`{gray ${i}} {green skipped: {bold ${file._id}} ${file.filename}}`);
}
log.clear();
console.log();
});
return promise;

View file

@ -16,10 +16,6 @@ async function main() {
for (let i = 0; i < count; i++) {
promiseGens.push(() => {
const promise = new Promise(async (res, rej) => {
function skip() {
res([i, file, false]);
}
const file = await DriveFile.findOne(prev ? {
_id: { $lt: prev._id }
} : {}, {
@ -30,6 +26,10 @@ async function main() {
prev = file;
function skip() {
res([i, file, false]);
}
if (file == null) return skip();
log(chalk`{gray ${i}} scanning {bold ${file._id}} ${file.filename} ...`);