From 5b84376dc7e4dd99f0b2915265e18e64ef44634b Mon Sep 17 00:00:00 2001 From: Danil Nikolaev Date: Mon, 16 Feb 2026 19:51:43 +0300 Subject: [PATCH] improve cache storage and cleanup (+ recursively) --- src/commands/openai-gen-image.ts | 8 ++----- src/index.ts | 21 ++++++++-------- src/util/files.ts | 41 +++++++++++++++++--------------- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/commands/openai-gen-image.ts b/src/commands/openai-gen-image.ts index 34e0164..c3771f8 100644 --- a/src/commands/openai-gen-image.ts +++ b/src/commands/openai-gen-image.ts @@ -2,7 +2,7 @@ import {ChatCommand} from "../base/chat-command"; import {Message} from "typescript-telegram-bot-api"; import {Requirements} from "../base/requirements"; import {Requirement} from "../base/requirement"; -import {bot, openAi, photoDir} from "../index"; +import {bot, openAi, photoGenDir} from "../index"; import fs from "node:fs"; import path from "node:path"; import {editMessageText, logError, replyToMessage} from "../util/utils"; @@ -30,11 +30,7 @@ export class OpenAIGenImage extends ChatCommand { const size = "1024x1024"; const fileFullName = `${msg.chat.id}_${msg.message_id}.png`; const getFileLocation = (fn: string) => { - const genRoot = path.join(photoDir, "gen"); - if (!fs.existsSync(genRoot)) { - fs.mkdirSync(genRoot); - } - return path.join(genRoot, fn); + return path.join(photoGenDir, fn); }; waitMessage = await replyToMessage({message: msg, text: "🌈 Генерирую изображение..."}); diff --git a/src/index.ts b/src/index.ts index 6a426a5..a2b4c75 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,6 @@ import {TelegramBot, User} from "typescript-telegram-bot-api"; import {Command} from "./base/command"; import { delay, - ignore, initSystemSpecs, logError, processCallbackQuery, @@ -218,8 +217,10 @@ if (Environment.OPENAI_API_KEY) { ); } -export const photoDir = path.join(Environment.DATA_PATH, "photo"); -export const videoDir = path.join(Environment.DATA_PATH, "video"); +export const cacheDir = path.join(Environment.DATA_PATH, "cache"); +export const photoDir = path.join(cacheDir, "photo"); +export const photoGenDir = path.join(photoDir, "gen"); +export const videoDir = path.join(cacheDir, "video"); let isShuttingDown = false; @@ -249,8 +250,10 @@ async function main() { `DEFAULT_AI_PROVIDER: ${Environment.DEFAULT_AI_PROVIDER}` ); - fs.mkdir(photoDir, ignore); - fs.mkdir(videoDir, ignore); + fs.mkdirSync(cacheDir); + fs.mkdirSync(photoDir); + fs.mkdirSync(photoGenDir); + fs.mkdirSync(videoDir); const now = new Date(); @@ -261,13 +264,11 @@ async function main() { const diff = midnight.getTime() - now.getTime(); console.log("Clearing up videos and photos will be started in " + diff + "ms"); - clearUpFolderFromOldFiles(videoDir); - clearUpFolderFromOldFiles(photoDir); + clearUpFolderFromOldFiles(cacheDir); delay(diff).then(() => { setInterval(() => { - console.log("Started clearing up videos and photos"); - clearUpFolderFromOldFiles(videoDir); - clearUpFolderFromOldFiles(photoDir); + console.log("Started clearing up cache"); + clearUpFolderFromOldFiles(cacheDir); }, 1000 * 60 * 60 * 24); }); diff --git a/src/util/files.ts b/src/util/files.ts index d2a540e..3c080e1 100644 --- a/src/util/files.ts +++ b/src/util/files.ts @@ -2,7 +2,7 @@ import {logError} from "./utils"; import fs from "node:fs"; import path from "node:path"; -export function clearUpFolderFromOldFiles(folder: string) { +export function clearUpFolderFromOldFiles(folder: string, recursive = true) { fs.readdir(folder, (err, files) => { if (err) { logError(err); @@ -11,32 +11,35 @@ export function clearUpFolderFromOldFiles(folder: string) { const filenamesToDelete: string[] = []; - files.forEach((filename, index) => { - fs.stat(path.join(folder, filename), (err, stats) => { - if (err) { - logError(err); + files.forEach(filename => { + const fullPath = path.join(folder, filename); + + try { + const stats = fs.statSync(fullPath); + if (stats.isDirectory() && recursive) { + clearUpFolderFromOldFiles(fullPath, recursive); } else { const then = stats.mtime.getTime() / 1000; const now = Date.now() / 1000; const diff = Math.abs(now - then); const moreThanOneDay = diff >= 60 * 60 * 24; - if (moreThanOneDay) { - filenamesToDelete.push(filename); - } - if (index === files.length - 1) { - console.log("filenamesToDelete", filenamesToDelete); - if (filenamesToDelete.length) { - filenamesToDelete.forEach((filename) => { - const fullPath = path.join(folder, filename); - fs.rm(fullPath, (e) => { - if (e) logError(e); - }); - }); - } + if (stats.isFile() && moreThanOneDay) { + filenamesToDelete.push(fullPath); } } - }); + } catch (e) { + logError(e); + } }); + + console.log("filenamesToDelete", filenamesToDelete); + if (filenamesToDelete.length) { + filenamesToDelete.forEach((filename) => { + fs.rm(filename, (e) => { + if (e) logError(e); + }); + }); + } }); } \ No newline at end of file