UI: decode base64 and return normal value

This commit is contained in:
ArtemDzhereleiko 2022-02-11 15:38:12 +02:00
parent 58461c8106
commit 6a91c6418d
2 changed files with 25 additions and 0 deletions

View File

@ -185,6 +185,12 @@ export function objToBase64(obj: any): string {
}));
}
export function base64toString(b64Encoded: string): string {
return decodeURIComponent(atob(b64Encoded).split('').map((c) => {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
export function objToBase64URI(obj: any): string {
return encodeURIComponent(objToBase64(obj));
}

View File

@ -27,6 +27,7 @@ import { getAce } from '@shared/models/ace/ace.models';
import { Observable } from 'rxjs/internal/Observable';
import { beautifyJs } from '@shared/models/beautify.models';
import { of } from 'rxjs';
import { base64toString, isLiteralObject } from '@core/utils';
export interface EventContentDialogData {
content: string;
@ -64,6 +65,14 @@ export class EventContentDialogComponent extends DialogComponent<EventContentDia
this.createEditor(this.eventContentEditorElmRef, this.content);
}
isJson(str) {
try {
return isLiteralObject(JSON.parse(str));
} catch (e) {
return false;
}
}
createEditor(editorElementRef: ElementRef, content: string) {
const editorElement = editorElementRef.nativeElement;
let mode = 'java';
@ -72,6 +81,16 @@ export class EventContentDialogComponent extends DialogComponent<EventContentDia
mode = contentTypesMap.get(this.contentType).code;
if (this.contentType === ContentType.JSON && content) {
content$ = beautifyJs(content, {indent_size: 4});
} else if (this.contentType === ContentType.BINARY && content) {
try {
const decodedData = base64toString(content);
if (this.isJson(decodedData)) {
mode = 'json';
content$ = beautifyJs(decodedData, {indent_size: 4});
} else {
content$ = of(decodedData);
}
} catch (e) {}
}
}
if (!content$) {