From 6985bdd7b1cf714910fc5f91a92cb54e32ece52f Mon Sep 17 00:00:00 2001 From: Ralf Aron Date: Tue, 30 Jul 2024 21:35:13 +0200 Subject: [PATCH] fix: update reading blob value --- .../src/lib/aas-tree/aas-tree-api.service.ts | 20 +++++++++ .../src/lib/aas-tree/aas-tree.component.ts | 41 +++++++++---------- .../src/app/aas-provider/aas-provider.ts | 2 +- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/projects/aas-lib/src/lib/aas-tree/aas-tree-api.service.ts b/projects/aas-lib/src/lib/aas-tree/aas-tree-api.service.ts index 08c5e2cc..35d79401 100644 --- a/projects/aas-lib/src/lib/aas-tree/aas-tree-api.service.ts +++ b/projects/aas-lib/src/lib/aas-tree/aas-tree-api.service.ts @@ -52,4 +52,24 @@ export class AASTreeApiService { }); }); } + + /** + * Reads the value of an data element like `File` or `Blob`. + * @param endpoint The endpoint name + * @param id The AAS identifier. + * @param smId The submodel identifier. + * @param path The idShort path of the submodel element. + * @returns The Base64 encoded value. + */ + public getValueAsync(endpoint: string, id: string, smId: string, path: string): Promise { + return new Promise((result, reject) => { + let data: string; + const url = `/api/v1/containers/${encodeBase64Url(endpoint)}/documents/${encodeBase64Url(id)}/submodels/${encodeBase64Url(smId)}/submodel-elements/${path}/value`; + this.http.get<{ value: string }>(url).subscribe({ + next: value => (data = value.value), + complete: () => result(data), + error: error => reject(error), + }); + }); + } } diff --git a/projects/aas-lib/src/lib/aas-tree/aas-tree.component.ts b/projects/aas-lib/src/lib/aas-tree/aas-tree.component.ts index 1b08e1e4..068752eb 100644 --- a/projects/aas-lib/src/lib/aas-tree/aas-tree.component.ts +++ b/projects/aas-lib/src/lib/aas-tree/aas-tree.component.ts @@ -276,32 +276,31 @@ export class AASTreeComponent implements OnInit, OnDestroy { name += extension; } - if (blob.value) { - if (blob.contentType.startsWith('image/')) { - await this.showImageAsync(name, `data:${blob.contentType};base64,${blob.value}`); - } else if (blob.contentType.startsWith('video/')) { - await this.showVideoAsync(name, `data:${blob.contentType};base64,${blob.value}`); - } - } else { - const endpoint = encodeBase64Url(document.endpoint); - const id = encodeBase64Url(document.id); - const smId = encodeBase64Url(blob.parent.keys[0].value); - const path = getIdShortPath(blob); - const url = `/api/v1/containers/${endpoint}/documents/${id}/submodels/${smId}/submodel-elements/${path}/value`; - if (blob.contentType.startsWith('image/')) { - await this.showImageAsync(name, url); - } else if (blob.contentType.startsWith('video/')) { - await this.showVideoAsync(name, url); - } else if (blob.contentType.endsWith('/pdf')) { - this.window.open(url); - } else if (blob) { - await this.downloadFileAsync(name, url); + if (!blob.value) { + try { + blob.value = await this.api.getValueAsync( + document.endpoint, + document.id, + blob.parent.keys[0].value, + getIdShortPath(blob), + ); + } catch (error) { + this.notify.error(error); + return; } } + + if (blob.contentType.startsWith('image/')) { + await this.showImageAsync(name, `data:${blob.contentType};base64,${blob.value}`); + } else if (blob.contentType.startsWith('video/')) { + await this.showVideoAsync(name, `data:${blob.contentType};base64,${blob.value}`); + } } public async openOperation(operation: aas.Operation | undefined): Promise { - if (!operation || this.state() === 'online') return; + if (!operation || this.state() === 'online') { + return; + } try { if (operation) { diff --git a/projects/aas-server/src/app/aas-provider/aas-provider.ts b/projects/aas-server/src/app/aas-provider/aas-provider.ts index 31667997..2ecad17f 100644 --- a/projects/aas-server/src/app/aas-provider/aas-provider.ts +++ b/projects/aas-server/src/app/aas-provider/aas-provider.ts @@ -185,7 +185,7 @@ export class AASProvider { } else if (dataElement.modelType === 'Blob') { const value = await resource.getBlobValueAsync(document.content, smId, path); const readable = new Readable(); - readable.push(value); + readable.push(JSON.stringify({ value })); readable.push(null); stream = readable; } else {