improve cache storage and cleanup (+ recursively)
This commit is contained in:
@@ -2,7 +2,7 @@ import {ChatCommand} from "../base/chat-command";
|
|||||||
import {Message} from "typescript-telegram-bot-api";
|
import {Message} from "typescript-telegram-bot-api";
|
||||||
import {Requirements} from "../base/requirements";
|
import {Requirements} from "../base/requirements";
|
||||||
import {Requirement} from "../base/requirement";
|
import {Requirement} from "../base/requirement";
|
||||||
import {bot, openAi, photoDir} from "../index";
|
import {bot, openAi, photoGenDir} from "../index";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import {editMessageText, logError, replyToMessage} from "../util/utils";
|
import {editMessageText, logError, replyToMessage} from "../util/utils";
|
||||||
@@ -30,11 +30,7 @@ export class OpenAIGenImage extends ChatCommand {
|
|||||||
const size = "1024x1024";
|
const size = "1024x1024";
|
||||||
const fileFullName = `${msg.chat.id}_${msg.message_id}.png`;
|
const fileFullName = `${msg.chat.id}_${msg.message_id}.png`;
|
||||||
const getFileLocation = (fn: string) => {
|
const getFileLocation = (fn: string) => {
|
||||||
const genRoot = path.join(photoDir, "gen");
|
return path.join(photoGenDir, fn);
|
||||||
if (!fs.existsSync(genRoot)) {
|
|
||||||
fs.mkdirSync(genRoot);
|
|
||||||
}
|
|
||||||
return path.join(genRoot, fn);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
waitMessage = await replyToMessage({message: msg, text: "🌈 Генерирую изображение..."});
|
waitMessage = await replyToMessage({message: msg, text: "🌈 Генерирую изображение..."});
|
||||||
|
|||||||
+11
-10
@@ -4,7 +4,6 @@ import {TelegramBot, User} from "typescript-telegram-bot-api";
|
|||||||
import {Command} from "./base/command";
|
import {Command} from "./base/command";
|
||||||
import {
|
import {
|
||||||
delay,
|
delay,
|
||||||
ignore,
|
|
||||||
initSystemSpecs,
|
initSystemSpecs,
|
||||||
logError,
|
logError,
|
||||||
processCallbackQuery,
|
processCallbackQuery,
|
||||||
@@ -218,8 +217,10 @@ if (Environment.OPENAI_API_KEY) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const photoDir = path.join(Environment.DATA_PATH, "photo");
|
export const cacheDir = path.join(Environment.DATA_PATH, "cache");
|
||||||
export const videoDir = path.join(Environment.DATA_PATH, "video");
|
export const photoDir = path.join(cacheDir, "photo");
|
||||||
|
export const photoGenDir = path.join(photoDir, "gen");
|
||||||
|
export const videoDir = path.join(cacheDir, "video");
|
||||||
|
|
||||||
let isShuttingDown = false;
|
let isShuttingDown = false;
|
||||||
|
|
||||||
@@ -249,8 +250,10 @@ async function main() {
|
|||||||
`DEFAULT_AI_PROVIDER: ${Environment.DEFAULT_AI_PROVIDER}`
|
`DEFAULT_AI_PROVIDER: ${Environment.DEFAULT_AI_PROVIDER}`
|
||||||
);
|
);
|
||||||
|
|
||||||
fs.mkdir(photoDir, ignore);
|
fs.mkdirSync(cacheDir);
|
||||||
fs.mkdir(videoDir, ignore);
|
fs.mkdirSync(photoDir);
|
||||||
|
fs.mkdirSync(photoGenDir);
|
||||||
|
fs.mkdirSync(videoDir);
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
|
||||||
@@ -261,13 +264,11 @@ async function main() {
|
|||||||
const diff = midnight.getTime() - now.getTime();
|
const diff = midnight.getTime() - now.getTime();
|
||||||
console.log("Clearing up videos and photos will be started in " + diff + "ms");
|
console.log("Clearing up videos and photos will be started in " + diff + "ms");
|
||||||
|
|
||||||
clearUpFolderFromOldFiles(videoDir);
|
clearUpFolderFromOldFiles(cacheDir);
|
||||||
clearUpFolderFromOldFiles(photoDir);
|
|
||||||
delay(diff).then(() => {
|
delay(diff).then(() => {
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
console.log("Started clearing up videos and photos");
|
console.log("Started clearing up cache");
|
||||||
clearUpFolderFromOldFiles(videoDir);
|
clearUpFolderFromOldFiles(cacheDir);
|
||||||
clearUpFolderFromOldFiles(photoDir);
|
|
||||||
}, 1000 * 60 * 60 * 24);
|
}, 1000 * 60 * 60 * 24);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+18
-15
@@ -2,7 +2,7 @@ import {logError} from "./utils";
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
|
||||||
export function clearUpFolderFromOldFiles(folder: string) {
|
export function clearUpFolderFromOldFiles(folder: string, recursive = true) {
|
||||||
fs.readdir(folder, (err, files) => {
|
fs.readdir(folder, (err, files) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
logError(err);
|
logError(err);
|
||||||
@@ -11,32 +11,35 @@ export function clearUpFolderFromOldFiles(folder: string) {
|
|||||||
|
|
||||||
const filenamesToDelete: string[] = [];
|
const filenamesToDelete: string[] = [];
|
||||||
|
|
||||||
files.forEach((filename, index) => {
|
files.forEach(filename => {
|
||||||
fs.stat(path.join(folder, filename), (err, stats) => {
|
const fullPath = path.join(folder, filename);
|
||||||
if (err) {
|
|
||||||
logError(err);
|
try {
|
||||||
|
const stats = fs.statSync(fullPath);
|
||||||
|
if (stats.isDirectory() && recursive) {
|
||||||
|
clearUpFolderFromOldFiles(fullPath, recursive);
|
||||||
} else {
|
} else {
|
||||||
const then = stats.mtime.getTime() / 1000;
|
const then = stats.mtime.getTime() / 1000;
|
||||||
const now = Date.now() / 1000;
|
const now = Date.now() / 1000;
|
||||||
const diff = Math.abs(now - then);
|
const diff = Math.abs(now - then);
|
||||||
const moreThanOneDay = diff >= 60 * 60 * 24;
|
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);
|
console.log("filenamesToDelete", filenamesToDelete);
|
||||||
if (filenamesToDelete.length) {
|
if (filenamesToDelete.length) {
|
||||||
filenamesToDelete.forEach((filename) => {
|
filenamesToDelete.forEach((filename) => {
|
||||||
const fullPath = path.join(folder, filename);
|
fs.rm(filename, (e) => {
|
||||||
fs.rm(fullPath, (e) => {
|
|
||||||
if (e) logError(e);
|
if (e) logError(e);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user