24 lines
981 B
TypeScript
24 lines
981 B
TypeScript
import { stat } from "node:fs/promises";
|
|
import { PrismaClient } from "@prisma/client";
|
|
import { attachmentPath } from "../src/lib/attachments";
|
|
const prisma = new PrismaClient();
|
|
async function main() {
|
|
let count = 0, missing = 0;
|
|
while (true) {
|
|
const batch = await prisma.attachment.findMany({ where: { sizeBytes: null }, select: { id: true }, take: 100 });
|
|
if (!batch.length) break;
|
|
for (const item of batch) {
|
|
let sizeBytes = 0;
|
|
try { sizeBytes = (await stat(attachmentPath(item.id))).size; }
|
|
catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
missing++;
|
|
}
|
|
await prisma.attachment.update({ where: { id: item.id }, data: { sizeBytes } });
|
|
count++;
|
|
}
|
|
}
|
|
console.log(`Attachment sizes recorded: ${count}; missing files: ${missing}.`);
|
|
}
|
|
main().finally(() => prisma.$disconnect()).catch(() => { console.error("Attachment size backfill failed."); process.exitCode = 1; });
|