Skip to content

Commit

Permalink
[data-model] fix: labels for blobs
Browse files Browse the repository at this point in the history
Co-authored-by: elf Pavlik <elf-pavlik@hackers4peace.net>
  • Loading branch information
samurex and elf-pavlik committed Oct 26, 2023
1 parent f1e2582 commit 2d1605e
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions packages/data-model/src/readable/data-instance.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SHAPETREES, buildNamespace, getDescriptionResource } from '@janeirodigital/interop-utils';
import { InteropFactory, ReadableDataRegistration } from '..';
import { ReadableResource } from './resource';

Expand All @@ -14,13 +15,24 @@ export class ReadableDataInstance extends ReadableResource {

children: ChildInfo[];

constructor(public iri: string, public factory: InteropFactory, public descriptionLang?: string) {
constructor(
public iri: string,
public factory: InteropFactory,
public descriptionLang?: string
) {
super(iri, factory);
}

private async bootstrap(): Promise<void> {
await this.fetchData();
await this.buildDataRegistration();
if (!this.isBlob) {
await this.fetchData();
} else {
const descriptionIri = await this.discoverDescriptionResource();
const response = await this.fetch(descriptionIri);
this.dataset = await response.dataset();
}

if (this.descriptionLang) {
await this.dataRegistration?.shapeTree?.getDescription(this.descriptionLang);
this.children = await this.buildChildrenInfo();
Expand All @@ -43,11 +55,13 @@ export class ReadableDataInstance extends ReadableResource {
}

get label(): string | undefined {
let label;
const predicate = this.dataRegistration?.shapeTree?.describesInstance;
if (predicate) {
return this.getObject(predicate).value;
label = this.getObject(predicate)?.value;
}
return undefined;
const NFO = buildNamespace('http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#');
return label || this.getObject(NFO.fileName)?.value;
}

get shapeTree(): { iri: string; label: string } {
Expand All @@ -57,6 +71,11 @@ export class ReadableDataInstance extends ReadableResource {
};
}

// TODO: extract as mixin from other data instance
get isBlob(): boolean {
return this.dataRegistration.shapeTree?.expectsType.value === SHAPETREES.NonRDFResource.value;
}

async buildChildrenInfo(): Promise<ChildInfo[]> {
return Promise.all(
this.dataRegistration!.shapeTree!.references.map(async (reference) => {
Expand All @@ -71,4 +90,15 @@ export class ReadableDataInstance extends ReadableResource {
})
);
}

// TODO: extract as mixin from container
async discoverDescriptionResource(): Promise<string> {
// @ts-ignore
const response = await this.fetch.raw(this.iri, {
method: 'HEAD'
});

// get value of describedby Link
return getDescriptionResource(response.headers.get('Link'));
}
}

0 comments on commit 2d1605e

Please sign in to comment.