Browse Source

UI: decode base64 and return normal value

pull/6090/head
ArtemDzhereleiko 5 years ago
parent
commit
6a91c6418d
  1. 6
      ui-ngx/src/app/core/utils.ts
  2. 19
      ui-ngx/src/app/modules/home/components/event/event-content-dialog.component.ts

6
ui-ngx/src/app/core/utils.ts

@ -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));
}

19
ui-ngx/src/app/modules/home/components/event/event-content-dialog.component.ts

@ -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$) {

Loading…
Cancel
Save