Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Remove extension from title if it's the same as filetype (#1526)
Olga Bulat8c53ceb5e4ce
diff --git a/src/components/VErrorSection/VErrorImage.vue b/src/components/VErrorSection/VErrorImage.vueindex 062da485e..a477a5d2e 100644--- a/src/components/VErrorSection/VErrorImage.vue+++ b/src/components/VErrorSection/VErrorImage.vue@@ -51,6 +51,7 @@ export default defineComponent({let image = errorItem.imageconst errorImage: ErrorImage = {...image,+ originalTitle: image.title,src: require(`~/assets/error_images/${image.file}.jpg`),alt: `error-images.${image.id}`,license: image.license as License,diff --git a/src/models/media.ts b/src/models/media.tsindex 93c793067..f2014281f 100644--- a/src/models/media.ts+++ b/src/models/media.ts@@ -12,6 +12,7 @@ export interface Tag {export interface Media {id: stringtitle: string+ originalTitle: stringcreator?: stringcreator_url?: stringdiff --git a/src/utils/attribution-html.ts b/src/utils/attribution-html.tsindex 00435f6ef..718578187 100644--- a/src/utils/attribution-html.ts+++ b/src/utils/attribution-html.ts@@ -114,7 +114,7 @@ const extLink = (href: string, text: string) =>*/export type AttributableMedia = Pick<Media,- | 'title'+ | 'originalTitle'| 'foreign_landing_url'| 'creator'| 'creator_url'@@ -162,10 +162,10 @@ export const getAttribution = (/* Title */- let title = mediaItem.title || tFn('generic-title')+ let title = mediaItem.originalTitle || tFn('generic-title')if (!isPlaintext && mediaItem.foreign_landing_url)title = extLink(mediaItem.foreign_landing_url, title)- if (mediaItem.title) title = tFn('actual-title', { title })+ if (mediaItem.originalTitle) title = tFn('actual-title', { title })/* License */diff --git a/src/utils/decode-media-data.ts b/src/utils/decode-media-data.tsindex 75a24e336..a65fd3d2d 100644--- a/src/utils/decode-media-data.ts+++ b/src/utils/decode-media-data.ts@@ -1,14 +1,85 @@-import { title } from 'case'+import { title as titleCase } from 'case'import { decodeData as decodeString } from '~/utils/decode-data'import type { Media, Tag } from '~/models/media'+import type { MediaType } from '~/constants/media'+import { AUDIO, IMAGE, MODEL_3D, VIDEO } from '~/constants/media'/*** This interface is a subset of `Media` that types dictionaries sent by the API* being decoded in the `decodeMediaData` function.*/-interface ApiMedia extends Omit<Media, 'frontendMediaType' | 'title'> {+interface ApiMedia+ extends Omit<Media, 'frontendMediaType' | 'title' | 'originalTitle'> {title?: string+ originalTitle?: string+}++const mediaTypeExtensions: Record<MediaType, string[]> = {+ [IMAGE]: ['jpg', 'jpeg', 'png', 'gif', 'svg'],+ [AUDIO]: ['mp3', 'wav', 'ogg', 'flac', 'aac', 'aiff', 'mp32'],+ [VIDEO]: ['mp4', 'webm', 'mkv', 'avi', 'mov', 'wmv', 'flv', 'mpg', 'mpeg'],+ [MODEL_3D]: ['fbx', 'obj', 'stl', 'dae', '3ds', 'blend', 'max', 'obj', 'ply'],+}++const matchers = [/jpe?g$/i, /tiff?$/i, /mp32?$/i]++/**+ * Compares the filetypes, taking into account different versions of the same+ * filetype. For example, `.jpg` and `.jpeg` are considered the same filetype.+ * @param extension - the extension of the file.+ * @param filetype - the type of the file.+ */+const isFiletypeMatching = (extension: string, filetype?: string) => {+ if (filetype === extension) {+ return true+ }+ if (!filetype) return false+ return matchers.some((matcher) =>+ Boolean(filetype.match(matcher) && extension.match(matcher))+ )+}+const extractPartAfterLastDot = (str?: string) => {+ if (!str) {+ return ''+ }+ const parts = str.split('.')+ return parts.length ? parts[parts.length - 1].toLowerCase() : ''+}++/**+ * Strip the extension from title if it matches the filetype of the media.+ * Since not all media records return filetype, we also try to guess the filetype+ * from the url extension.+ */+const stripExtension = (+ title: string,+ mediaType: MediaType,+ media: ApiMedia+) => {+ const filetype = media.filetype ?? extractPartAfterLastDot(media.url)+ const titleParts = title.split('.')+ if (+ mediaTypeExtensions[mediaType].includes(filetype) &&+ isFiletypeMatching(extractPartAfterLastDot(title), filetype)+ ) {+ titleParts.pop()+ }+ return titleParts.join('.')+}+/**+ * Corrects the encoding of the media title, or uses the media type as the title.+ * If the title has a file extension that matches media filetype, it will be stripped.+ */+const mediaTitle = (+ media: ApiMedia,+ mediaType: MediaType+): { title: string; originalTitle: string } => {+ const originalTitle = decodeString(media.title) || titleCase(mediaType)+ return {+ originalTitle,+ title: stripExtension(originalTitle, mediaType, media),+ }}/**@@ -25,8 +96,8 @@ export const decodeMediaData = <T extends Media>(): T =>({...media,+ ...mediaTitle(media, mediaType),frontendMediaType: mediaType,- title: decodeString(media.title) || title(mediaType),creator: decodeString(media.creator),// TODO: remove `?? []`tags: (media.tags ?? ([] as Tag[])).map((tag) => ({diff --git a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-2xl-linux.png b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-2xl-linux.pngindex c242659ba..6e1f1dbb9 100644Binary files a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-2xl-linux.png and b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-2xl-linux.png differdiff --git a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-lg-linux.png b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-lg-linux.pngindex a3658eebf..3c5d66d3f 100644Binary files a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-lg-linux.png and b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-lg-linux.png differdiff --git a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-md-linux.png b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-md-linux.pngindex b12dd8970..2c81b894a 100644Binary files a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-md-linux.png and b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-md-linux.png differdiff --git a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-xl-linux.png b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-xl-linux.pngindex 30828d954..5c5c03cc7 100644Binary files a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-xl-linux.png and b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-desktop-xl-linux.png differdiff --git a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-desktop-UA-sm-linux.png b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-desktop-UA-sm-linux.pngindex d928d6b27..8bff3b796 100644Binary files a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-desktop-UA-sm-linux.png and b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-desktop-UA-sm-linux.png differdiff --git a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-desktop-UA-xs-linux.png b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-desktop-UA-xs-linux.pngindex 6c233f3c7..bbc486992 100644Binary files a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-desktop-UA-xs-linux.png and b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-desktop-UA-xs-linux.png differdiff --git a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-mobile-UA-sm-linux.png b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-mobile-UA-sm-linux.pngindex 6094e8d92..f8e03b7ad 100644Binary files a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-mobile-UA-sm-linux.png and b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-mobile-UA-sm-linux.png differdiff --git a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-mobile-UA-xs-linux.png b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-mobile-UA-xs-linux.pngindex 015de121a..7bff6efe9 100644Binary files a/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-mobile-UA-xs-linux.png and b/test/playwright/visual-regression/components/audio-results.spec.ts-snapshots/audio-results-narrow-viewport-mobile-UA-xs-linux.png differdiff --git a/test/unit/specs/utils/attribution-html.spec.ts b/test/unit/specs/utils/attribution-html.spec.tsindex 0c3fbcb31..01da747a7 100644--- a/test/unit/specs/utils/attribution-html.spec.ts+++ b/test/unit/specs/utils/attribution-html.spec.ts@@ -11,7 +11,7 @@ const i18n = new Vuei18n({})const mediaItem: AttributableMedia = {- title: 'Title',+ originalTitle: 'Title',foreign_landing_url: 'https://foreign.landing/url',creator: 'Creator',creator_url: 'https://creator/url',@@ -44,7 +44,7 @@ describe('getAttribution', () => {)it('uses generic title if not known', () => {- const mediaItemNoTitle = { ...mediaItem, title: '' }+ const mediaItemNoTitle = { ...mediaItem, originalTitle: '' }const attrText = getAttribution(mediaItemNoTitle, i18n, {isPlaintext: true,})diff --git a/test/unit/specs/utils/decode-image-data.spec.js b/test/unit/specs/utils/decode-image-data.spec.jsindex 697f0e0a0..ef28fab23 100644--- a/test/unit/specs/utils/decode-image-data.spec.js+++ b/test/unit/specs/utils/decode-image-data.spec.js@@ -1,16 +1,36 @@import { decodeMediaData } from '~/utils/decode-media-data'import { IMAGE } from '~/constants/media'+const requiredFields = {+ id: 'id',+ url: 'https://example.com/image.jpg',+ foreign_landing_url: 'https://example.com',+ license: 'by',+ license_version: '4.0',+ attribution: 'Attribution',++ category: null,+ provider: 'provider',++ detail_url: 'url',+ related_url: 'url',++ tags: [],+}+describe('decodeImageData', () => {- it('returns empty string for empty string', () => {+ it('decodes symbols correctly', () => {const data = {+ ...requiredFields,creator: 'S\\xe3',title: 'S\\xe9',tags: [{ name: 'ma\\xdf' }],}const expected = {+ ...requiredFields,title: 'Sé',+ originalTitle: 'Sé',creator: 'Sã',tags: [{ name: 'maß' }],frontendMediaType: IMAGE,@@ -18,4 +38,63 @@ describe('decodeImageData', () => {expect(decodeMediaData(data, IMAGE)).toEqual(expected)})++ it('strips the extension if the same as media filetype', () => {+ const data = {+ ...requiredFields,+ creator: 'Creator',+ title: 'Image.JPEG',+ filetype: 'jpg',+ }++ const expected = {+ ...requiredFields,+ title: 'Image',+ originalTitle: 'Image.JPEG',+ creator: 'Creator',+ filetype: 'jpg',+ frontendMediaType: IMAGE,+ }++ expect(decodeMediaData(data, IMAGE)).toEqual(expected)+ })++ it('strips the extension if the same as url extension', () => {+ const data = {+ ...requiredFields,+ url: 'https://example.com/image.jpg',+ creator: 'Creator',+ title: 'Image.JPG',+ }++ const expected = {+ ...requiredFields,+ title: 'Image',+ originalTitle: 'Image.JPG',+ creator: 'Creator',+ frontendMediaType: IMAGE,+ }++ expect(decodeMediaData(data, IMAGE)).toEqual(expected)+ })++ it('does not strip the extension if different from filetype in url extension', () => {+ const data = {+ ...requiredFields,+ url: 'https://example.com/image.png',+ creator: 'Creator',+ title: 'Image.JPG',+ }++ const expected = {+ ...requiredFields,+ url: 'https://example.com/image.png',+ title: 'Image.JPG',+ originalTitle: 'Image.JPG',+ creator: 'Creator',+ frontendMediaType: IMAGE,+ }++ expect(decodeMediaData(data, IMAGE)).toEqual(expected)+ })})