improve cache storage and cleanup (+ recursively)

This commit is contained in:
2026-02-16 19:51:43 +03:00
parent 69ae37e05e
commit 5b84376dc7
3 changed files with 35 additions and 35 deletions
+2 -6
View File
@@ -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: "🌈 Генерирую изображение..."});
+11 -10
View File
@@ -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);
});
+18 -15
View File
@@ -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) {
if (stats.isFile() && moreThanOneDay) {
filenamesToDelete.push(fullPath);
}
}
} catch (e) {
logError(e);
}
});
console.log("filenamesToDelete", filenamesToDelete);
if (filenamesToDelete.length) {
filenamesToDelete.forEach((filename) => {
const fullPath = path.join(folder, filename);
fs.rm(fullPath, (e) => {
fs.rm(filename, (e) => {
if (e) logError(e);
});
});
}
}
}
});
});
});
}