diff --git a/package.json b/package.json index 81632a7..1480ece 100644 --- a/package.json +++ b/package.json @@ -14,18 +14,18 @@ "migrate:dev": "prisma migrate dev --create-only", "start": "node dist", "lint": "next lint", - "docker:run": "docker-compose up -d", + "docker:up": "docker-compose up", "docker:down": "docker-compose down", "docker:build-dev": "docker-compose --file docker-compose.dev.yml up --build", - "docker:run-dev": "docker-compose --file docker-compose.dev.yml up", + "docker:up-dev": "docker-compose --file docker-compose.dev.yml up", "docker:down-dev": "docker-compose --file docker-compose.dev.yml down", - "scripts:read-config": "node dist/scripts/read-config", - "scripts:import-dir": "node dist/scripts/import-dir", - "scripts:list-users": "node dist/scripts/list-users", - "scripts:set-user": "node dist/scripts/set-user" + "scripts:read-config": "node --enable-source-maps dist/scripts/read-config", + "scripts:import-dir": "node --enable-source-maps dist/scripts/import-dir", + "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" }, "dependencies": { - "@dicedtomato/mantine-data-grid": "0.0.23", "@emotion/react": "^11.10.5", "@emotion/server": "^11.10.0", "@mantine/core": "^5.9.2", @@ -54,6 +54,7 @@ "fflate": "^0.7.4", "find-my-way": "^7.3.1", "katex": "^0.16.4", + "mantine-datatable": "^1.8.6", "minio": "^7.0.32", "ms": "canary", "multer": "^1.4.5-lts.1", diff --git a/src/scripts/clear-zero-byte.ts b/src/scripts/clear-zero-byte.ts new file mode 100644 index 0000000..2ac7d4d --- /dev/null +++ b/src/scripts/clear-zero-byte.ts @@ -0,0 +1,64 @@ +import { PrismaClient } from '@prisma/client'; +import { readdir, readFile } from 'fs/promises'; +import { join } from 'path'; +import config from 'lib/config'; +import datasource from 'lib/datasource'; +import { guess } from 'lib/mimes'; +import { migrations } from 'server/util'; + +async function main() { + process.env.DATABASE_URL = config.core.database_url; + await migrations(); + + const prisma = new PrismaClient(); + + const files = await prisma.file.findMany(); + + const toDelete = []; + + for (let i = 0; i !== files.length; ++i) { + const file = files[i]; + const size = await datasource.size(file.name); + if (size === 0) { + toDelete.push(file.name); + } + } + + if (toDelete.length === 0) { + console.log('No files to delete.'); + process.exit(0); + } + + process.stdout.write(`Found ${toDelete.length} files to delete. Continue? (y/N) `); + const answer: Buffer = await new Promise((resolve) => { + process.stdin.resume(); + process.stdin.setEncoding('utf8'); + process.stdin.on('data', (text) => { + resolve(text); + }); + }); + + if (answer.toString().trim().toLowerCase() !== 'y') { + console.log('Aborting.'); + process.exit(0); + } + + const { count } = await prisma.file.deleteMany({ + where: { + name: { + in: toDelete, + }, + }, + }); + console.log(`Deleted ${count} files from the database.`); + + for (let i = 0; i !== toDelete.length; ++i) { + await datasource.delete(toDelete[i]); + } + + console.log(`Deleted ${toDelete.length} files from the storage.`); + + process.exit(0); +} + +main(); diff --git a/tsup.config.ts b/tsup.config.ts index b581ea9..33a2755 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -34,4 +34,9 @@ export default defineConfig([ outDir: 'dist/scripts', ...opts, }, + { + entryPoints: ['src/scripts/clear-zero-byte.ts'], + outDir: 'dist/scripts', + ...opts, + }, ]);