Skip to content

Commit

Permalink
Merge pull request #689 from geonetwork/DH-fix-i18n-failure
Browse files Browse the repository at this point in the history
fix(DH): Link detection with unknown mime type
  • Loading branch information
Angi-Kinas authored Nov 14, 2023
2 parents 295eda8 + 50fd05e commit 0cd989d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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', () => {
Expand Down
11 changes: 11 additions & 0 deletions libs/util/shared/src/lib/links/link-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
mimeTypeToFormat,
getLinkPriority,
} from './link-utils'
import { DatasetDownloadDistribution } from '@geonetwork-ui/common/domain/record'

describe('link utils', () => {
describe('#getFileFormat', () => {
Expand All @@ -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')
Expand Down
11 changes: 7 additions & 4 deletions libs/util/shared/src/lib/links/link-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -143,7 +146,7 @@ export function mimeTypeToFormat(mimeType: string): FileFormat {
if (mimeType === mt) return format as FileFormat
}
}
return undefined
return null
}

export function checkFileFormat(
Expand Down

0 comments on commit 0cd989d

Please sign in to comment.