From 317c932c2a26280bf37ed3d3bf9b879413590f5a Mon Sep 17 00:00:00 2001 From: C4illin Date: Fri, 20 Sep 2024 13:24:18 +0200 Subject: [PATCH] feat: add option to customize how often files are automatically deleted --- README.md | 1 + src/index.tsx | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e7435f1..32fd94a 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ services: - JWT_SECRET=aLongAndSecretStringUsedToSignTheJSONWebToken1234 # will use randomUUID() by default - HTTP_ALLOWED=false # setting this to true is unsafe, only set this to true locally - ALLOW_UNAUTHENTICATED=false # allows anyone to use the service without logging in, only set this to true locally + - AUTO_DELETE_EVERY_N_HOURS=24 # checks every n hours for files older then n hours and deletes them, set to 0 to disable volumes: - convertx:/app/data ``` diff --git a/src/index.tsx b/src/index.tsx index 094c487..3538941 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -33,6 +33,7 @@ const HTTP_ALLOWED = process.env.HTTP_ALLOWED?.toLowerCase() === "true" || false; const ALLOW_UNAUTHENTICATED = process.env.ALLOW_UNAUTHENTICATED?.toLowerCase() === "true" || false; +const AUTO_DELETE_EVERY_N_HOURS = process.env.AUTO_DELETE_EVERY_N_HOURS ? Number(process.env.AUTO_DELETE_EVERY_N_HOURS) : 24; // fileNames: fileNames, // filesToConvert: fileNames.length, @@ -1263,12 +1264,10 @@ console.log( ); const clearJobs = () => { - // clear all jobs older than 24 hours - // get all files older than 24 hours const jobs = db .query("SELECT * FROM jobs WHERE date_created < ?") .as(Jobs) - .all(new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()); + .all(new Date(Date.now() - AUTO_DELETE_EVERY_N_HOURS * 60 * 60 * 1000).toISOString()); for (const job of jobs) { // delete the directories @@ -1279,7 +1278,9 @@ const clearJobs = () => { db.query("DELETE FROM jobs WHERE id = ?").run(job.id); } - // run every 24 hours - setTimeout(clearJobs, 24 * 60 * 60 * 1000); + setTimeout(clearJobs, AUTO_DELETE_EVERY_N_HOURS * 60 * 60 * 1000); }; -clearJobs(); + +if (AUTO_DELETE_EVERY_N_HOURS > 0) { + clearJobs(); +}