Skip to content

Commit

Permalink
feat(backend): Add cron for clean up unused files
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Dec 31, 2024
1 parent d44935e commit 3912040
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { core_files } from '@/database/schema/files';
import { core_users } from '@/database/schema/users';
import { FilesHelperService } from '@/helpers/files/files-helper.service';
import { InternalDatabaseService } from '@/utils/database/internal_database.service';
import {
ForbiddenException,
Expand All @@ -10,7 +12,10 @@ import { User } from 'vitnode-shared/user.dto';

@Injectable()
export class DeleteUsersMembersAdminService {
constructor(private readonly databaseService: InternalDatabaseService) {}
constructor(
private readonly databaseService: InternalDatabaseService,
private readonly filesService: FilesHelperService,
) {}

async delete({
id,
Expand Down Expand Up @@ -45,6 +50,24 @@ export class DeleteUsersMembersAdminService {
throw new ForbiddenException('DELETE_ADMIN');
}

// Delete all files of the user
const files = await this.databaseService.db.query.core_files.findMany({
where: (table, { eq }) => eq(table.user_id, user.id),
});
await Promise.all(
files.map(async file => {
await Promise.all([
this.filesService.delete({
dir_folder: file.dir_folder,
file_name: file.file_name,
}),
this.databaseService.db
.delete(core_files)
.where(eq(core_files.id, file.id)),
]);
}),
);

await this.databaseService.db
.delete(core_users)
.where(eq(core_users.id, id));
Expand Down
45 changes: 45 additions & 0 deletions packages/backend/src/core/files/clean.core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { core_files, core_files_using } from '@/database/schema/files';
import { FilesHelperService } from '@/helpers/files/files-helper.service';
import { InternalDatabaseService } from '@/utils/database/internal_database.service';
import { Injectable } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { and, eq, isNull, lte } from 'drizzle-orm';

@Injectable()
export class FilesCron {
constructor(
private readonly databaseService: InternalDatabaseService,
private readonly filesService: FilesHelperService,
) {}

@Cron(CronExpression.EVERY_6_HOURS, {
name: 'core_clear_unused_files',
})
async clearUnusedFiles() {
const twelveHoursAgo = new Date(Date.now() - 12 * 60 * 60 * 1000);
const findFiles = await this.databaseService.db
.select()
.from(core_files)
.leftJoin(core_files_using, eq(core_files.id, core_files_using.file_id))
.where(
and(
isNull(core_files_using.file_id),
lte(core_files.created_at, twelveHoursAgo),
),
);

await Promise.all(
findFiles.map(async file => {
await Promise.all([
this.filesService.delete({
dir_folder: file.core_files.dir_folder,
file_name: file.core_files.file_name,
}),
this.databaseService.db
.delete(core_files)
.where(eq(core_files.id, file.core_files.id)),
]);
}),
);
}
}
3 changes: 2 additions & 1 deletion packages/backend/src/core/files/files.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Module } from '@nestjs/common';

import { FilesCron } from './clean.core';
import { FilesController } from './files.controller';
import { DeleteFilesService } from './services/delete.service';
import { UploadFilesService } from './services/upload.service';

@Module({
providers: [UploadFilesService, DeleteFilesService],
providers: [UploadFilesService, DeleteFilesService, FilesCron],
controllers: [FilesController],
})
export class FilesModule {}

0 comments on commit 3912040

Please sign in to comment.