From fca929a8ac97aa8b7b5f8617743119cc3701ce73 Mon Sep 17 00:00:00 2001 From: Angelika Kinas Date: Tue, 14 Nov 2023 11:21:46 +0100 Subject: [PATCH] fix(DH): Link detection with unknown mime type --- .../downloads-list.component.spec.ts | 26 +++++++++++++++++-- .../shared/src/lib/links/link-utils.spec.ts | 11 ++++++++ libs/util/shared/src/lib/links/link-utils.ts | 11 +++++--- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/libs/ui/elements/src/lib/downloads-list/downloads-list.component.spec.ts b/libs/ui/elements/src/lib/downloads-list/downloads-list.component.spec.ts index de6a46d76e..5f224193a6 100644 --- a/libs/ui/elements/src/lib/downloads-list/downloads-list.component.spec.ts +++ b/libs/ui/elements/src/lib/downloads-list/downloads-list.component.spec.ts @@ -11,7 +11,10 @@ import { LinkClassifierService } from '@geonetwork-ui/util/shared' import { LINK_FIXTURES } from '@geonetwork-ui/common/fixtures' import { TranslateModule } from '@ngx-translate/core' import { DownloadsListComponent } from './downloads-list.component' -import { DatasetDistribution } from '@geonetwork-ui/common/domain/record' +import { + DatasetDistribution, + DatasetDownloadDistribution, +} from '@geonetwork-ui/common/domain/record' @Component({ selector: 'gn-ui-download-item', @@ -92,8 +95,27 @@ describe('DownloadsListComponent', () => { fixture.detectChanges() items = de.queryAll(By.directive(MockDownloadItemComponent)) }) - it('contains one link', () => { + it('contains one link in "others" section', () => { + expect(items.length).toBe(1) + expect(component.isLinkOfFormat(component.links[0], 'others')).toBe(true) + }) + }) + describe('when link mime type is unknown', () => { + let items: DebugElement[] + + beforeEach(() => { + component.links = [ + { + ...LINK_FIXTURES.geodataJsonWithMimeType, + mimeType: 'unknown/x-type', + } as DatasetDownloadDistribution, + ] + fixture.detectChanges() + items = de.queryAll(By.directive(MockDownloadItemComponent)) + }) + it('contains one link and mime type is ignored', () => { expect(items.length).toBe(1) + expect(component.isLinkOfFormat(component.links[0], 'json')).toBe(true) }) }) describe('derives color and format from link', () => { diff --git a/libs/util/shared/src/lib/links/link-utils.spec.ts b/libs/util/shared/src/lib/links/link-utils.spec.ts index cebd11e3e9..4c5364073e 100644 --- a/libs/util/shared/src/lib/links/link-utils.spec.ts +++ b/libs/util/shared/src/lib/links/link-utils.spec.ts @@ -9,6 +9,7 @@ import { mimeTypeToFormat, getLinkPriority, } from './link-utils' +import { DatasetDownloadDistribution } from '@geonetwork-ui/common/domain/record' describe('link utils', () => { describe('#getFileFormat', () => { @@ -29,6 +30,16 @@ describe('link utils', () => { ) }) }) + describe('for a geojson FILE link with unrecognized mime type', () => { + it('returns geojson format', () => { + expect( + getFileFormat({ + ...LINK_FIXTURES.geodataJsonWithMimeType, + mimeType: 'unknown', + } as DatasetDownloadDistribution) + ).toEqual('geojson') + }) + }) describe('for a json FILE link', () => { it('returns json format', () => { expect(getFileFormat(LINK_FIXTURES.dataJson)).toEqual('json') diff --git a/libs/util/shared/src/lib/links/link-utils.ts b/libs/util/shared/src/lib/links/link-utils.ts index de8015953d..ab21cce3fb 100644 --- a/libs/util/shared/src/lib/links/link-utils.ts +++ b/libs/util/shared/src/lib/links/link-utils.ts @@ -59,10 +59,10 @@ export const FORMATS = { mimeTypes: ['application/geopackage+sqlite3'], }, zip: { - extensions: ['zip'], + extensions: ['zip', 'tar.gz'], priority: 7, color: '#f2bb3a', - mimeTypes: ['application/zip'], + mimeTypes: ['application/zip', 'application/x-zip'], }, pdf: { extensions: ['pdf'], @@ -113,7 +113,10 @@ export function extensionToFormat(extension: string): FileFormat { export function getFileFormat(link: DatasetDistribution): FileFormat { if ('mimeType' in link) { - return mimeTypeToFormat(link.mimeType) + const mimeTypeFormat = mimeTypeToFormat(link.mimeType) + if (mimeTypeFormat !== null) { + return mimeTypeFormat + } } for (const format in FORMATS) { for (const alias of FORMATS[format].extensions) { @@ -143,7 +146,7 @@ export function mimeTypeToFormat(mimeType: string): FileFormat { if (mimeType === mt) return format as FileFormat } } - return undefined + return null } export function checkFileFormat(