Add hashDocument to MessageBus

This commit is contained in:
Sosthene 2025-06-30 22:38:06 +02:00
parent 723322cc0a
commit 095c4efba2

View File

@ -6,6 +6,7 @@ import EventBus from './EventBus';
import User from './User';
import MapUtils from './MapUtils';
import { FileBlob } from '../front/Api/Entities/types';
export default class MessageBus {
private static instance: MessageBus;
@ -564,6 +565,49 @@ export default class MessageBus {
});
}
/**
* Hash a document file using SHA-256
* @param fileBlob - The file blob to hash
* @returns Promise<string> - The SHA-256 hash of the file
*/
public hashDocument(fileBlob: FileBlob, commitedIn: string): Promise<string> {
return new Promise<string>((resolve: (hash: string) => void, reject: (error: string) => void) => {
this.checkToken().then(() => {
const messageId = `HASH_VALUE_${uuidv4()}`;
const unsubscribe = EventBus.getInstance().on('VALUE_HASHED', (responseId: string, hash: string) => {
if (responseId !== messageId) {
return;
}
unsubscribe();
resolve(hash);
});
const unsubscribeError = EventBus.getInstance().on('ERROR_VALUE_HASHED', (responseId: string, error: string) => {
if (responseId !== messageId) {
return;
}
unsubscribeError();
reject(error);
});
const user = User.getInstance();
const accessToken = user.getAccessToken()!;
const label = 'file_blob';
this.sendMessage({
type: 'HASH_VALUE',
accessToken,
commitedIn,
label,
fileBlob,
messageId
});
}).catch(reject);
});
}
private validateToken(): Promise<boolean> {
return new Promise<boolean>((resolve: (isValid: boolean) => void, reject: (error: string) => void) => {
const messageId = `VALIDATE_TOKEN_${uuidv4()}`;
@ -753,6 +797,10 @@ export default class MessageBus {
this.doHandleMessage(message.messageId, 'STATE_VALIDATED', message, (message: any) => message.validatedProcess);
break;
case 'VALUE_HASHED': // HASH_VALUE
this.doHandleMessage(message.messageId, 'VALUE_HASHED', message, (message: any) => message.hash);
break;
case 'ERROR':
console.error('Error:', message);
this.errors[message.messageId] = message.error;