(feat): clearing up both photo and video after 1 day and on process' start
This commit is contained in:
+7
-4
@@ -72,7 +72,6 @@ import {YouTubeDownload} from "./commands/youtube-download";
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import {setInterval} from "node:timers";
|
import {setInterval} from "node:timers";
|
||||||
import {clearUpVideoFolder} from "./util/files";
|
|
||||||
import {OpenAI} from "openai";
|
import {OpenAI} from "openai";
|
||||||
import {OpenAIChat} from "./commands/openai-chat";
|
import {OpenAIChat} from "./commands/openai-chat";
|
||||||
import {OpenAIListModels} from "./commands/openai-list-models";
|
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 {OpenAISetModel} from "./commands/openai-set-model";
|
||||||
import {Info} from "./commands/info";
|
import {Info} from "./commands/info";
|
||||||
import {OpenAIGenImage} from "./commands/openai-gen-image";
|
import {OpenAIGenImage} from "./commands/openai-gen-image";
|
||||||
|
import {clearUpFolderFromOldFiles} from "./util/files";
|
||||||
|
|
||||||
process.setUncaughtExceptionCaptureCallback(logError);
|
process.setUncaughtExceptionCaptureCallback(logError);
|
||||||
|
|
||||||
@@ -239,12 +239,15 @@ async function main() {
|
|||||||
midnight.setDate(now.getDate() + 1);
|
midnight.setDate(now.getDate() + 1);
|
||||||
|
|
||||||
const diff = midnight.getTime() - now.getTime();
|
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(() => {
|
delay(diff).then(() => {
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
console.log("Started clearing up videos");
|
console.log("Started clearing up videos and photos");
|
||||||
clearUpVideoFolder();
|
clearUpFolderFromOldFiles(videoDir);
|
||||||
|
clearUpFolderFromOldFiles(photoDir);
|
||||||
}, 1000 * 60 * 60 * 24);
|
}, 1000 * 60 * 60 * 24);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+7
-6
@@ -1,10 +1,9 @@
|
|||||||
import {logError} from "./utils";
|
import {logError} from "./utils";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import {videoDir} from "../index";
|
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
|
||||||
export function clearUpVideoFolder() {
|
export function clearUpFolderFromOldFiles(folder: string) {
|
||||||
fs.readdir(videoDir, (err, files) => {
|
fs.readdir(folder, (err, files) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
logError(err);
|
logError(err);
|
||||||
return;
|
return;
|
||||||
@@ -13,7 +12,7 @@ export function clearUpVideoFolder() {
|
|||||||
const filenamesToDelete: string[] = [];
|
const filenamesToDelete: string[] = [];
|
||||||
|
|
||||||
files.forEach((filename, index) => {
|
files.forEach((filename, index) => {
|
||||||
fs.stat(path.join(videoDir, filename), (err, stats) => {
|
fs.stat(path.join(folder, filename), (err, stats) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
logError(err);
|
logError(err);
|
||||||
} else {
|
} else {
|
||||||
@@ -29,8 +28,10 @@ export function clearUpVideoFolder() {
|
|||||||
console.log("filenamesToDelete", filenamesToDelete);
|
console.log("filenamesToDelete", filenamesToDelete);
|
||||||
if (filenamesToDelete.length) {
|
if (filenamesToDelete.length) {
|
||||||
filenamesToDelete.forEach((filename) => {
|
filenamesToDelete.forEach((filename) => {
|
||||||
const fullPath = path.join(videoDir, filename);
|
const fullPath = path.join(folder, filename);
|
||||||
fs.rm(fullPath, logError);
|
fs.rm(fullPath, (e) => {
|
||||||
|
if (e) logError(e);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user