Skip to content

Commit

Permalink
Merge pull request #597 from geonetwork/fix-recognizing-csv-format
Browse files Browse the repository at this point in the history
Fix: Simplify link classifier service
  • Loading branch information
Angi-Kinas authored Aug 28, 2023
2 parents 30a1e9a + 7947804 commit 586e918
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export class DownloadsListComponent {
if (format === 'all') {
return true
}
if (getFileFormat(link) === null) {
return format === 'others'
}
if (format === 'others') {
const knownFormats = FILTER_FORMATS.filter(
(format) => format !== 'all' && format !== 'others'
Expand Down
11 changes: 11 additions & 0 deletions libs/util/shared/src/lib/links/link-classifier.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ describe('LinkClassifierService', () => {
])
})
})
describe('for a WFS link (registered as download)', () => {
it('returns download, data and API usage', () => {
expect(
service.getUsagesForLink({
name: 'mylayer',
type: 'download',
url: new URL('https://my.ogc.server/wfs?abcd'),
})
).toEqual([LinkUsage.DOWNLOAD, LinkUsage.GEODATA])
})
})
describe('for a ESRI REST feature service link', () => {
it('returns download and API usage', () => {
expect(service.getUsagesForLink(LINK_FIXTURES.geodataRest)).toEqual([
Expand Down
35 changes: 11 additions & 24 deletions libs/util/shared/src/lib/links/link-classifier.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@angular/core'
import { DatasetDistribution } from '@geonetwork-ui/common/domain/record'
import { getFileFormat } from './link-utils'
export enum LinkUsage {
API = 'api',
MAP_API = 'mapapi',
Expand Down Expand Up @@ -31,38 +32,24 @@ export class LinkClassifierService {
case 'link':
return [LinkUsage.UNKNOWN]
case 'download': {
switch (link.mimeType) {
case 'application/json':
case 'text/csv':
case 'application/csv':
case 'application/vnd.ms-excel':
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
switch (getFileFormat(link)) {
case 'json':
case 'csv':
case 'excel':
return [LinkUsage.DOWNLOAD, LinkUsage.DATA]
case 'application/geo+json':
case 'application/vnd.geo+json':
case 'geojson':
return [LinkUsage.DOWNLOAD, LinkUsage.GEODATA]
default:
if (link.url.toString().match(/\/wfs/i)) {
return [LinkUsage.DOWNLOAD, LinkUsage.GEODATA]
}
return [LinkUsage.DOWNLOAD]
}

// fallback: look for extension
if (this.hasFileExtension(['json', 'csv', 'xls'], link)) {
return [LinkUsage.DOWNLOAD, LinkUsage.DATA]
}
if (this.hasFileExtension(['geojson', 'wfs'], link)) {
return [LinkUsage.DOWNLOAD, LinkUsage.GEODATA]
}
return [LinkUsage.DOWNLOAD]
}
}
}

hasUsage(link: DatasetDistribution, usage: LinkUsage) {
return this.getUsagesForLink(link).indexOf(usage) > -1
}

private hasFileExtension(extensions: string[], link: DatasetDistribution) {
return (
new RegExp(`[./](${extensions.join('|')})`, 'i').test(link.name) ||
new RegExp(`[./](${extensions.join('|')})`, 'i').test(link.url.toString())
)
}
}
26 changes: 14 additions & 12 deletions libs/util/shared/src/lib/links/link-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export const FORMATS = {
color: '#d98294',
mimeTypes: ['image/svg+xml'],
},
}
} as const

type FileFormat = keyof typeof FORMATS

export function sortPriority(link: DatasetDistribution): number {
const linkFormat = getFileFormat(link)
Expand All @@ -97,26 +99,26 @@ export function sortPriority(link: DatasetDistribution): number {
return 0
}

export function extensionToFormat(extension: string): string {
export function extensionToFormat(extension: string): FileFormat {
for (const format in FORMATS) {
for (const alias of FORMATS[format].extensions) {
if (alias === extension.toLowerCase()) return format
if (alias === extension.toLowerCase()) return format as FileFormat
}
}
return undefined
}

export function getFileFormat(link: DatasetDistribution): string {
export function getFileFormat(link: DatasetDistribution): FileFormat {
if ('mimeType' in link) {
return mimeTypeToFormat(link.mimeType)
}
for (const format in FORMATS) {
for (const alias of FORMATS[format].extensions) {
if (checkFileFormat(link, alias)) return format
if (isFormatInQueryParam(link, alias)) return format
if (checkFileFormat(link, alias)) return format as FileFormat
if (isFormatInQueryParam(link, alias)) return format as FileFormat
}
}
return ''
return null
}

export function isFormatInQueryParam(
Expand All @@ -132,18 +134,18 @@ export function isFormatInQueryParam(
return false
}

export function mimeTypeToFormat(mimeType: string): string {
export function mimeTypeToFormat(mimeType: string): FileFormat {
for (const format in FORMATS) {
for (const mt of FORMATS[format].mimeTypes) {
if (mimeType === mt) return format
if (mimeType === mt) return format as FileFormat
}
}
return undefined
}

export function checkFileFormat(
link: DatasetDistribution,
format: string
format: FileFormat
): boolean {
return (
('name' in link && new RegExp(`[./]${format}`, 'i').test(link.name)) ||
Expand All @@ -152,7 +154,7 @@ export function checkFileFormat(
)
}

export function getBadgeColor(linkFormat: string): string {
export function getBadgeColor(linkFormat: FileFormat): string {
for (const format in FORMATS) {
for (const alias of FORMATS[format].extensions) {
if (new RegExp(`${alias}`, 'i').test(linkFormat))
Expand Down Expand Up @@ -190,6 +192,6 @@ export function getLinkLabel(link: DatasetDistribution): string {
return format ? `${label} (${format})` : label
}

export function getMimeTypeForFormat(format: string): string | null {
export function getMimeTypeForFormat(format: FileFormat): string | null {
return format in FORMATS ? FORMATS[format.toLowerCase()].mimeTypes[0] : null
}

0 comments on commit 586e918

Please sign in to comment.