diff --git a/src/lib/datasources/Local.ts b/src/lib/datasources/Local.ts index c1aadb6..bfeb4f6 100644 --- a/src/lib/datasources/Local.ts +++ b/src/lib/datasources/Local.ts @@ -25,6 +25,7 @@ export class Local extends Datasource { await rm(join(process.cwd(), this.path, files[i])); } } + public get(file: string): ReadStream { const full = join(process.cwd(), this.path, file); if (!existsSync(full)) return null; @@ -37,7 +38,9 @@ export class Local extends Datasource { } public async size(file: string): Promise { - const stats = await stat(join(process.cwd(), this.path, file)); + const full = join(process.cwd(), this.path, file); + if (!existsSync(full)) return 0; + const stats = await stat(full); return stats.size; } diff --git a/src/scripts/query-size.ts b/src/scripts/query-size.ts index a13d1f7..655210d 100644 --- a/src/scripts/query-size.ts +++ b/src/scripts/query-size.ts @@ -8,13 +8,42 @@ async function main() { await migrations(); const prisma = new PrismaClient(); + let notFound = false; - const files = await prisma.file.findMany(); + const files = await prisma.file.findMany({ + ...(process.argv.includes('--force-update') + ? undefined + : { + where: { + size: 0, + }, + }), + select: { + id: true, + name: true, + size: true, + }, + }); 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]; + if (!datasource.get(file.name)) { + if (process.argv.includes('--force-delete')) { + console.log(`File ${file.name} does not exist. Deleting...`); + await prisma.file.delete({ + where: { + id: file.id, + }, + }); + continue; + } else { + notFound ? null : (notFound = true); + continue; + } + } + const size = await datasource.size(file.name); if (size === 0) { console.log(`File ${file.name} has a size of 0 bytes. Ignoring...`); @@ -31,7 +60,11 @@ async function main() { } } - console.log('Done.'); + notFound + ? console.log( + 'At least one file has been found to not exist in the datasource but was on the database. To remove these files, run the script with the --force-delete flag.' + ) + : console.log('Done.'); process.exit(0); }