Skip to content

Commit

Permalink
Merge pull request #92 from VictoriqueMoe/file-sync
Browse files Browse the repository at this point in the history
sync files that are not in DB
  • Loading branch information
VictoriqueMoe authored Mar 5, 2024
2 parents 20e17fc + 94fc84a commit d0b98a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/services/FileCleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { Constant, Inject, type OnInit, Service } from "@tsed/di";
import { FileRepo } from "../db/repo/FileRepo.js";
import { ScheduleService } from "./ScheduleService.js";
import { FileService } from "./FileService.js";
import { FileUtils } from "../utils/Utils.js";
import { filesDir, FileUtils } from "../utils/Utils.js";
import GlobalEnv from "../model/constants/GlobalEnv.js";
import fs from "node:fs/promises";
import { FileEngine } from "../engine/impl/index.js";
import { FileUploadModel } from "../model/db/FileUpload.model.js";

@Service()
export class FileCleaner implements OnInit {
public constructor(
@Inject() private repo: FileRepo,
@Inject() private scheduleService: ScheduleService,
@Inject() private fileUploadService: FileService,
@Inject() private fileEngine: FileEngine,
) {}

// default to every day at 12am
Expand All @@ -30,6 +34,28 @@ export class FileCleaner implements OnInit {
}

public $onInit(): void {
this.scheduleService.scheduleCronJob(this.cronToRun, this.processFiles, "removeExpiredFiles", this, true);
this.scheduleService.scheduleCronJob(
this.cronToRun,
async () => {
await this.processFiles();
await this.sync();
},
"removeExpiredFiles",
this,
true,
);
}

private async sync(): Promise<void> {
const allFilesFromDb = await this.repo.getAllEntries();
const allFilesFromSystem = await fs.readdir(filesDir);
const deleteFilesPromises = allFilesFromSystem
.filter(fileOnSystem => !this.isFileInDb(allFilesFromDb, fileOnSystem))
.map(fileToDelete => this.fileEngine.deleteFile(fileToDelete));
await Promise.all(deleteFilesPromises);
}

private isFileInDb(fileDbList: FileUploadModel[], fileName: string): boolean {
return !!fileDbList.find(file => file.fullFileNameOnSystem === fileName);
}
}
1 change: 1 addition & 0 deletions src/services/FileUrlService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class FileUrlService {
const fileStream = fs.createWriteStream(destination);
return new Promise((resolve, reject) => {
if (!response.body) {
reject("Response has no body");
return;
}
response.body.pipe(fileStream);
Expand Down

0 comments on commit d0b98a2

Please sign in to comment.