fix: query-size (#363)

* feat: add flags for querying & delete option

* return 0 for no file

* include size
This commit is contained in:
Jayvin Hernandez 2023-04-05 20:25:19 -07:00 committed by GitHub
parent 5ded128263
commit 89a28bf50b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 3 deletions

View file

@ -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<number> {
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;
}

View file

@ -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);
}