From c7df4a578bf865739641cb68ee543325b0449e17 Mon Sep 17 00:00:00 2001 From: dicedtomato <35403473+diced@users.noreply.github.com> Date: Sat, 4 Mar 2023 04:36:06 +0000 Subject: [PATCH] feat: script to add file sizes --- package.json | 3 ++- src/scripts/query-size.ts | 38 ++++++++++++++++++++++++++++++++++++++ tsconfig.json | 6 +----- tsup.config.ts | 5 +++++ 4 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 src/scripts/query-size.ts diff --git a/package.json b/package.json index 2a5436f3..aa6775e7 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ "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" + "scripts:clear-zero-byte": "node --enable-source-maps dist/scripts/clear-zero-byte", + "scripts:query-size": "node --enable-source-maps dist/scripts/query-size" }, "dependencies": { "@emotion/react": "^11.10.6", diff --git a/src/scripts/query-size.ts b/src/scripts/query-size.ts new file mode 100644 index 00000000..a13d1f7a --- /dev/null +++ b/src/scripts/query-size.ts @@ -0,0 +1,38 @@ +import { PrismaClient } from '@prisma/client'; +import config from 'lib/config'; +import datasource from 'lib/datasource'; +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(); + + console.log(`The script will attempt to query the size of ${files.length} files.`); + + for (let i = 0; i !== files.length; ++i) { + const file = files[i]; + const size = await datasource.size(file.name); + if (size === 0) { + console.log(`File ${file.name} has a size of 0 bytes. Ignoring...`); + } else { + console.log(`File ${file.name} has a size of ${size} bytes. Updating...`); + await prisma.file.update({ + where: { + id: file.id, + }, + data: { + size, + }, + }); + } + } + + console.log('Done.'); + process.exit(0); +} + +main(); diff --git a/tsconfig.json b/tsconfig.json index 726cd7c2..32adce98 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,10 +25,6 @@ }, "incremental": true }, - "include": [ - "next-env.d.ts", - "**/*.ts", - "**/*.tsx", - ], + "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], "exclude": ["node_modules", "dist", ".yarn", ".next"] } diff --git a/tsup.config.ts b/tsup.config.ts index 33a2755d..9d8b6612 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -39,4 +39,9 @@ export default defineConfig([ outDir: 'dist/scripts', ...opts, }, + { + entryPoints: ['src/scripts/query-size.ts'], + outDir: 'dist/scripts', + ...opts, + }, ]);