2023-03-03 15:54:28 +00:00
|
|
|
import crypto from 'crypto';
|
|
|
|
import fs from 'fs';
|
2023-03-17 15:13:37 +00:00
|
|
|
// import Config from '@ioc:Adonis/Core/Config';
|
2024-03-14 19:25:27 +00:00
|
|
|
import logger from '@adonisjs/core/services/logger';
|
|
|
|
import { BaseCommand } from "@adonisjs/core/ace";
|
|
|
|
import { CommandOptions } from "@adonisjs/core/types/ace";
|
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 = '';
|
2024-03-14 19:25:27 +00:00
|
|
|
static options: CommandOptions = {
|
|
|
|
loadApp: true,
|
|
|
|
staysAlive: false,
|
|
|
|
};
|
2023-03-03 15:54:28 +00:00
|
|
|
|
|
|
|
public async run() {
|
|
|
|
// this.logger.info('Hello world!')
|
2024-04-30 09:50:50 +00:00
|
|
|
const { default: File } = await import('#models/file');
|
2023-03-03 15:54:28 +00:00
|
|
|
// const { default: HashValue } = await (await (import ('App/Models/HashValue')));
|
|
|
|
|
2024-02-02 13:00:54 +00:00
|
|
|
// query all published files from database:
|
|
|
|
const files = await File.query()
|
|
|
|
.whereHas('dataset', (dQuery) => {
|
|
|
|
dQuery.where('server_state', 'published');
|
|
|
|
})
|
|
|
|
.preload('hashvalues');
|
2023-03-03 15:54:28 +00:00
|
|
|
|
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);
|
2024-03-14 19:25:27 +00:00
|
|
|
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) {
|
2024-03-14 19:25:27 +00:00
|
|
|
logger.info(`File id ${file.id} OK: stored md5 checksum: ${calculatedMd5FileHash}, same control md5 checksum: ${hashValue['md5']}`);
|
2023-03-03 15:54:28 +00:00
|
|
|
} else {
|
2024-03-14 19:25:27 +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']}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
private async checksumFile(path: string, hashName = 'md5'): Promise<string> {
|
2023-03-03 15:54:28 +00:00
|
|
|
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')));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|