feat: clear zero byte files script
This commit is contained in:
parent
e804d0b31e
commit
9b57fb280b
3 changed files with 77 additions and 7 deletions
15
package.json
15
package.json
|
@ -14,18 +14,18 @@
|
||||||
"migrate:dev": "prisma migrate dev --create-only",
|
"migrate:dev": "prisma migrate dev --create-only",
|
||||||
"start": "node dist",
|
"start": "node dist",
|
||||||
"lint": "next lint",
|
"lint": "next lint",
|
||||||
"docker:run": "docker-compose up -d",
|
"docker:up": "docker-compose up",
|
||||||
"docker:down": "docker-compose down",
|
"docker:down": "docker-compose down",
|
||||||
"docker:build-dev": "docker-compose --file docker-compose.dev.yml up --build",
|
"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",
|
"docker:down-dev": "docker-compose --file docker-compose.dev.yml down",
|
||||||
"scripts:read-config": "node dist/scripts/read-config",
|
"scripts:read-config": "node --enable-source-maps dist/scripts/read-config",
|
||||||
"scripts:import-dir": "node dist/scripts/import-dir",
|
"scripts:import-dir": "node --enable-source-maps dist/scripts/import-dir",
|
||||||
"scripts:list-users": "node dist/scripts/list-users",
|
"scripts:list-users": "node --enable-source-maps dist/scripts/list-users",
|
||||||
"scripts:set-user": "node dist/scripts/set-user"
|
"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": {
|
"dependencies": {
|
||||||
"@dicedtomato/mantine-data-grid": "0.0.23",
|
|
||||||
"@emotion/react": "^11.10.5",
|
"@emotion/react": "^11.10.5",
|
||||||
"@emotion/server": "^11.10.0",
|
"@emotion/server": "^11.10.0",
|
||||||
"@mantine/core": "^5.9.2",
|
"@mantine/core": "^5.9.2",
|
||||||
|
@ -54,6 +54,7 @@
|
||||||
"fflate": "^0.7.4",
|
"fflate": "^0.7.4",
|
||||||
"find-my-way": "^7.3.1",
|
"find-my-way": "^7.3.1",
|
||||||
"katex": "^0.16.4",
|
"katex": "^0.16.4",
|
||||||
|
"mantine-datatable": "^1.8.6",
|
||||||
"minio": "^7.0.32",
|
"minio": "^7.0.32",
|
||||||
"ms": "canary",
|
"ms": "canary",
|
||||||
"multer": "^1.4.5-lts.1",
|
"multer": "^1.4.5-lts.1",
|
||||||
|
|
64
src/scripts/clear-zero-byte.ts
Normal file
64
src/scripts/clear-zero-byte.ts
Normal file
|
@ -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();
|
|
@ -34,4 +34,9 @@ export default defineConfig([
|
||||||
outDir: 'dist/scripts',
|
outDir: 'dist/scripts',
|
||||||
...opts,
|
...opts,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
entryPoints: ['src/scripts/clear-zero-byte.ts'],
|
||||||
|
outDir: 'dist/scripts',
|
||||||
|
...opts,
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
|
|
Loading…
Reference in a new issue