2023-01-13 05:40:33 +01:00
|
|
|
import type Bull from "bull";
|
2019-02-20 17:30:21 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
import { queueLogger } from "../../logger.js";
|
|
|
|
import { deleteFileSync } from "@/services/drive/delete-file.js";
|
|
|
|
import { Users, DriveFiles } from "@/models/index.js";
|
|
|
|
import { MoreThan } from "typeorm";
|
|
|
|
import type { DbUserJobData } from "@/queue/types.js";
|
2019-02-20 17:30:21 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const logger = queueLogger.createSubLogger("delete-drive-files");
|
2019-02-20 17:30:21 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export async function deleteDriveFiles(
|
|
|
|
job: Bull.Job<DbUserJobData>,
|
|
|
|
done: any,
|
|
|
|
): Promise<void> {
|
2019-04-07 14:50:36 +02:00
|
|
|
logger.info(`Deleting drive files of ${job.data.user.id} ...`);
|
2019-02-20 17:30:21 +01:00
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const user = await Users.findOneBy({ id: job.data.user.id });
|
2019-04-12 18:43:22 +02:00
|
|
|
if (user == null) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
2019-02-20 17:30:21 +01:00
|
|
|
|
|
|
|
let deletedCount = 0;
|
|
|
|
let cursor: any = null;
|
|
|
|
|
2019-04-14 10:18:17 +02:00
|
|
|
while (true) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const files = await DriveFiles.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {}),
|
2019-04-07 14:50:36 +02:00
|
|
|
},
|
|
|
|
take: 100,
|
|
|
|
order: {
|
2021-12-09 15:58:30 +01:00
|
|
|
id: 1,
|
|
|
|
},
|
2019-02-20 17:30:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (files.length === 0) {
|
2019-03-07 15:07:21 +01:00
|
|
|
job.progress(100);
|
2019-02-20 17:30:21 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
cursor = files[files.length - 1].id;
|
2019-02-20 17:30:21 +01:00
|
|
|
|
|
|
|
for (const file of files) {
|
2019-07-01 14:12:14 +02:00
|
|
|
await deleteFileSync(file);
|
2019-02-20 17:30:21 +01:00
|
|
|
deletedCount++;
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const total = await DriveFiles.countBy({
|
2019-04-07 14:50:36 +02:00
|
|
|
userId: user.id,
|
2019-02-20 17:30:21 +01:00
|
|
|
});
|
|
|
|
|
2019-03-07 15:07:21 +01:00
|
|
|
job.progress(deletedCount / total);
|
2019-02-20 17:30:21 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
logger.succ(
|
|
|
|
`All drive files (${deletedCount}) of ${user.id} has been deleted.`,
|
|
|
|
);
|
2019-02-20 17:30:21 +01:00
|
|
|
done();
|
|
|
|
}
|