Skip to content

Commit

Permalink
fix: update reading blob value
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfaron committed Jul 30, 2024
1 parent 99b4800 commit 6985bdd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
20 changes: 20 additions & 0 deletions projects/aas-lib/src/lib/aas-tree/aas-tree-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
return new Promise<string>((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),
});
});
}
}
41 changes: 20 additions & 21 deletions projects/aas-lib/src/lib/aas-tree/aas-tree.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
if (!operation || this.state() === 'online') return;
if (!operation || this.state() === 'online') {
return;
}

try {
if (operation) {
Expand Down
2 changes: 1 addition & 1 deletion projects/aas-server/src/app/aas-provider/aas-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6985bdd

Please sign in to comment.