From da0ea15f177b6abb19781ab7394e4e825dcc2f97 Mon Sep 17 00:00:00 2001 From: "s.teleshev" Date: Wed, 27 Dec 2023 11:18:35 +0100 Subject: [PATCH] CB-3555 :feat adds format blob to string helpers --- webapp/packages/core-utils/src/blobToHex.ts | 23 ++++++++++++ webapp/packages/core-utils/src/hexToBlob.ts | 8 +++++ webapp/packages/core-utils/src/index.ts | 2 ++ .../TextValue/useAutoFormat.ts | 36 +++++++++++++++---- 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 webapp/packages/core-utils/src/blobToHex.ts create mode 100644 webapp/packages/core-utils/src/hexToBlob.ts diff --git a/webapp/packages/core-utils/src/blobToHex.ts b/webapp/packages/core-utils/src/blobToHex.ts new file mode 100644 index 0000000000..60511f4a62 --- /dev/null +++ b/webapp/packages/core-utils/src/blobToHex.ts @@ -0,0 +1,23 @@ +export function blobToHex(blob: Blob): Promise { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + + reader.onloadend = () => { + if (reader.result instanceof ArrayBuffer) { + const buffer = new Uint8Array(reader.result); + const hex = Array.from(buffer) + .map(byte => byte.toString(16).padStart(2, '0')) + .join(''); + resolve(hex); + } else { + reject(new Error('Failed to read Blob as ArrayBuffer')); + } + }; + + reader.onerror = () => { + reject(new Error('Failed to read Blob')); + }; + + reader.readAsArrayBuffer(blob); + }); +} diff --git a/webapp/packages/core-utils/src/hexToBlob.ts b/webapp/packages/core-utils/src/hexToBlob.ts new file mode 100644 index 0000000000..a8ec380cf7 --- /dev/null +++ b/webapp/packages/core-utils/src/hexToBlob.ts @@ -0,0 +1,8 @@ +export function hexToBlob(hexdata: string): Blob { + const byteArray = new Uint8Array(hexdata.length / 2); + for (let x = 0; x < byteArray.length; x++) { + byteArray[x] = parseInt(hexdata.substring(x * 2, x * 2 + 2), 16); + } + + return new Blob([byteArray], { type: 'application/octet-stream' }); +} diff --git a/webapp/packages/core-utils/src/index.ts b/webapp/packages/core-utils/src/index.ts index 9ccaa2f809..58803da4fb 100644 --- a/webapp/packages/core-utils/src/index.ts +++ b/webapp/packages/core-utils/src/index.ts @@ -10,6 +10,8 @@ export * from './underscore'; export * from './base64ToBlob'; export * from './blobToBase64'; +export * from './blobToHex'; +export * from './hexToBlob'; export * from './bytesToSize'; export * from './cacheValue'; export * from './clsx'; diff --git a/webapp/packages/plugin-data-viewer/src/ValuePanelPresentation/TextValue/useAutoFormat.ts b/webapp/packages/plugin-data-viewer/src/ValuePanelPresentation/TextValue/useAutoFormat.ts index 80a7f8b711..54a66b85d3 100644 --- a/webapp/packages/plugin-data-viewer/src/ValuePanelPresentation/TextValue/useAutoFormat.ts +++ b/webapp/packages/plugin-data-viewer/src/ValuePanelPresentation/TextValue/useAutoFormat.ts @@ -6,23 +6,45 @@ * you may not use this file except in compliance with the License. */ import { useObjectRef } from '@cloudbeaver/core-blocks'; +import { blobToBase64, blobToHex } from '@cloudbeaver/core-utils'; export function useAutoFormat() { return useObjectRef( () => ({ format(type: string, value: string) { + if (typeof value === 'string') { + try { + switch (type) { + case 'application/json': + return JSON.stringify(JSON.parse(value), null, 2); + case 'text/xml': + case 'text/html': + return value; + case 'text/plain': + default: + return value; + } + } catch { + return value; + } + } + + return value; + }, + async formatBlob(type: string, value: Blob) { try { switch (type) { - case 'application/json': - return JSON.stringify(JSON.parse(value), null, 2); - case 'text/xml': - case 'text/html': - return value; + case 'test/base64': + return blobToBase64(value); + case 'text/hex': + return blobToHex(value); default: - return value; + return value.text(); } } catch { - return value; + return value.text().catch(() => { + throw new Error('Failed to read Blob'); + }); } }, }),