2023-03-03 15:54:28 +00:00
|
|
|
import { BaseCommand } from '@adonisjs/core/build/standalone';
|
|
|
|
import crypto from 'crypto';
|
|
|
|
import fs from 'fs';
|
2023-03-17 15:13:37 +00:00
|
|
|
// import Config from '@ioc:Adonis/Core/Config';
|
2023-06-16 14:44:28 +00:00
|
|
|
import Logger from '@ioc:Adonis/Core/Logger';
|
2023-03-03 15:54:28 +00:00
|
|
|
|
|
|
|
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 = '';
|
|
|
|
|
|
|
|
public static settings = {
|
|
|
|
/**
|
|
|
|
* Set the following value to true, if you want to load the application
|
|
|
|
* before running the command. Don't forget to call `node ace generate:manifest`
|
|
|
|
* afterwards.
|
|
|
|
*/
|
|
|
|
loadApp: true,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the following value to true, if you want this command to keep running until
|
|
|
|
* you manually decide to exit the process. Don't forget to call
|
|
|
|
* `node ace generate:manifest` afterwards.
|
|
|
|
*/
|
|
|
|
stayAlive: 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 files from database:
|
|
|
|
const files = await File.query().preload('hashvalues');
|
|
|
|
|
2023-03-17 15:13:37 +00:00
|
|
|
// const logLevel = Config.get('app.logger.level', 'info');
|
2023-06-16 14:44:28 +00:00
|
|
|
// console.log(this.logger.)
|
2023-03-17 15:13:37 +00:00
|
|
|
|
2023-03-03 15:54:28 +00:00
|
|
|
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) {
|
2023-03-17 15:13:37 +00:00
|
|
|
// this.logger.error(exception.message);
|
|
|
|
Logger.error(exception.message);
|
2023-03-03 15:54:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:13:30 +00:00
|
|
|
if (hashValue['md5'] === calculatedMd5FileHash) {
|
2023-06-16 14:44:28 +00:00
|
|
|
Logger.info(`File id ${file.id}: stored md5 checksum: ${calculatedMd5FileHash}, control md5 checksum: ${hashValue['md5']}`);
|
2023-03-03 15:54:28 +00:00
|
|
|
} else {
|
2023-03-17 15:13:37 +00:00
|
|
|
Logger.error(
|
2023-03-03 15:54:28 +00:00
|
|
|
`File id ${file.id}: stored md5 checksum: ${calculatedMd5FileHash}, control md5 checksum: ${hashValue['md5']}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async checksumFile(path, 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')));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|