From 6ea9905c2536b0ad63c65024413155ac2ba0dd89 Mon Sep 17 00:00:00 2001 From: Danil Nikolaev Date: Thu, 12 Feb 2026 18:22:37 +0300 Subject: [PATCH] (feat): clearing up both photo and video after 1 day and on process' start --- src/index.ts | 11 +++++++---- src/util/files.ts | 13 +++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index cb3f4ab..a290c3d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -72,7 +72,6 @@ import {YouTubeDownload} from "./commands/youtube-download"; import fs from "node:fs"; import path from "node:path"; import {setInterval} from "node:timers"; -import {clearUpVideoFolder} from "./util/files"; import {OpenAI} from "openai"; import {OpenAIChat} from "./commands/openai-chat"; import {OpenAIListModels} from "./commands/openai-list-models"; @@ -80,6 +79,7 @@ import {OpenAIGetModel} from "./commands/openai-get-model"; import {OpenAISetModel} from "./commands/openai-set-model"; import {Info} from "./commands/info"; import {OpenAIGenImage} from "./commands/openai-gen-image"; +import {clearUpFolderFromOldFiles} from "./util/files"; process.setUncaughtExceptionCaptureCallback(logError); @@ -239,12 +239,15 @@ async function main() { midnight.setDate(now.getDate() + 1); const diff = midnight.getTime() - now.getTime(); - console.log("Clearing up videos will be started in " + diff + "ms"); + console.log("Clearing up videos and photos will be started in " + diff + "ms"); + clearUpFolderFromOldFiles(videoDir); + clearUpFolderFromOldFiles(photoDir); delay(diff).then(() => { setInterval(() => { - console.log("Started clearing up videos"); - clearUpVideoFolder(); + console.log("Started clearing up videos and photos"); + clearUpFolderFromOldFiles(videoDir); + clearUpFolderFromOldFiles(photoDir); }, 1000 * 60 * 60 * 24); }); diff --git a/src/util/files.ts b/src/util/files.ts index a52bdc5..d2a540e 100644 --- a/src/util/files.ts +++ b/src/util/files.ts @@ -1,10 +1,9 @@ import {logError} from "./utils"; import fs from "node:fs"; -import {videoDir} from "../index"; import path from "node:path"; -export function clearUpVideoFolder() { - fs.readdir(videoDir, (err, files) => { +export function clearUpFolderFromOldFiles(folder: string) { + fs.readdir(folder, (err, files) => { if (err) { logError(err); return; @@ -13,7 +12,7 @@ export function clearUpVideoFolder() { const filenamesToDelete: string[] = []; files.forEach((filename, index) => { - fs.stat(path.join(videoDir, filename), (err, stats) => { + fs.stat(path.join(folder, filename), (err, stats) => { if (err) { logError(err); } else { @@ -29,8 +28,10 @@ export function clearUpVideoFolder() { console.log("filenamesToDelete", filenamesToDelete); if (filenamesToDelete.length) { filenamesToDelete.forEach((filename) => { - const fullPath = path.join(videoDir, filename); - fs.rm(fullPath, logError); + const fullPath = path.join(folder, filename); + fs.rm(fullPath, (e) => { + if (e) logError(e); + }); }); } }