tethys.backend/commands/validate_checksum.ts
Arno Kaimbacher a29865b781
Some checks failed
CI Pipeline / japa-tests (push) Failing after 58s
- renamings to the new naming convetion for adonisjs version 6
- npm updates
2024-04-29 11:25:50 +02:00

71 lines
2.6 KiB
TypeScript

import crypto from 'crypto';
import fs from 'fs';
// import Config from '@ioc:Adonis/Core/Config';
import logger from '@adonisjs/core/services/logger';
import { BaseCommand } from "@adonisjs/core/ace";
import { CommandOptions } from "@adonisjs/core/types/ace";
export default class ValidateChecksum extends BaseCommand {
/**
* Command name is used to run the command
*/
public static commandName = 'validate:checksum';
/**
* Command description is displayed in the "help" output
*/
public static description = '';
static options: CommandOptions = {
loadApp: true,
staysAlive: false,
};
public async run() {
// this.logger.info('Hello world!')
const { default: File } = await import('#app/Models/File');
// const { default: HashValue } = await (await (import ('App/Models/HashValue')));
// query all published files from database:
const files = await File.query()
.whereHas('dataset', (dQuery) => {
dQuery.where('server_state', 'published');
})
.preload('hashvalues');
// const logLevel = Config.get('app.logger.level', 'info');
// console.log(this.logger.)
for (var file of files) {
let hashValue = await file.related('hashvalues').query().pluck('value', 'type');
const filePath = '/storage/app/public/' + file.pathName;
let calculatedMd5FileHash;
try {
calculatedMd5FileHash = await this.checksumFile(filePath, 'md5');
} catch (exception) {
// this.logger.error(exception.message);
logger.error(exception.message);
continue;
}
if (hashValue['md5'] === calculatedMd5FileHash) {
logger.info(`File id ${file.id} OK: stored md5 checksum: ${calculatedMd5FileHash}, same control md5 checksum: ${hashValue['md5']}`);
} else {
logger.error(
`File id ${file.id}: stored md5 checksum: ${calculatedMd5FileHash}, control md5 checksum: ${hashValue['md5']}`,
);
}
}
}
private async checksumFile(path: string, hashName = 'md5'): Promise<string> {
return new Promise((resolve, reject) => {
const hash = crypto.createHash(hashName);
const stream = fs.createReadStream(path);
stream.on('error', (err) => reject(err));
stream.on('data', (chunk) => hash.update(chunk));
stream.on('end', () => resolve(hash.digest('hex')));
});
}
}