From fd2746c2d01791d0e9cbf0c616d11d52dbe88bde Mon Sep 17 00:00:00 2001 From: Jayvin Hernandez Date: Tue, 11 Apr 2023 18:37:38 -0700 Subject: [PATCH] fix: clearing (#367) + added convenient clear-temp script, no flags yet --- package.json | 3 ++- src/pages/api/admin/clear.ts | 2 +- src/scripts/clear-temp.ts | 28 ++++++++++++++++++++++++++++ tsup.config.ts | 5 +++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/scripts/clear-temp.ts diff --git a/package.json b/package.json index e1a172d..3b56858 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "scripts:list-users": "node --enable-source-maps dist/scripts/list-users", "scripts:set-user": "node --enable-source-maps dist/scripts/set-user", "scripts:clear-zero-byte": "node --enable-source-maps dist/scripts/clear-zero-byte", - "scripts:query-size": "node --enable-source-maps dist/scripts/query-size" + "scripts:query-size": "node --enable-source-maps dist/scripts/query-size", + "scripts:clear-temp": "node --enable-source-maps dist/scripts/clear-temp" }, "dependencies": { "@emotion/react": "^11.10.6", diff --git a/src/pages/api/admin/clear.ts b/src/pages/api/admin/clear.ts index d1d58d8..cfd8231 100644 --- a/src/pages/api/admin/clear.ts +++ b/src/pages/api/admin/clear.ts @@ -6,7 +6,7 @@ const logger = Logger.get('admin'); async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) { try { - const { datasource, orphaned } = req.body; + const { orphaned } = req.body; if (orphaned) { const { count } = await prisma.file.deleteMany({ where: { diff --git a/src/scripts/clear-temp.ts b/src/scripts/clear-temp.ts new file mode 100644 index 0000000..9f8787a --- /dev/null +++ b/src/scripts/clear-temp.ts @@ -0,0 +1,28 @@ +import config from 'lib/config'; +import { readdir, rm } from 'fs/promises'; +import { join } from 'path'; +import { existsSync } from 'fs'; + +async function main() { + const temp = config.core.temp_directory; + + if (!existsSync(temp)) { + console.log('Temp directory does not exist, exiting..'); + process.exit(0); + } + + const files = (await readdir(temp)).filter((x) => x.startsWith('zipline_partial_')); + if (files.length === 0) { + console.log('No partial files found, exiting..'); + process.exit(0); + } else { + for (const file of files) { + console.log(`Deleting ${file}`); + await rm(join(temp, file)); + } + console.log('Done!'); + process.exit(0); + } +} + +main(); diff --git a/tsup.config.ts b/tsup.config.ts index a38ced3..624db0a 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -50,4 +50,9 @@ export default defineConfig([ outDir: 'dist/scripts', ...opts, }, + { + entryPoints: ['src/scripts/clear-temp.ts'], + outDir: 'dist/scripts', + ...opts, + }, ]);