From 2c933a58cedf32c6cfa9979b2583f8cc9acbb356 Mon Sep 17 00:00:00 2001 From: Martina Bustacchini Date: Tue, 24 Dec 2024 12:32:49 +0100 Subject: [PATCH] fix: getFileViewFormat regex inconsistencies with different filenames that included @@download/**, added anti-regression tests --- RELEASE.md | 6 +++ src/helpers/files.js | 11 ++++- src/helpers/files.test.js | 90 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 src/helpers/files.test.js diff --git a/RELEASE.md b/RELEASE.md index 21301d372..e91c9776f 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -41,6 +41,12 @@ - ... --> +## Versione X.X.X (dd/mm/yyyy) + +### Fix + +- Risolto un problema riguardante la visualizzazione delle estensioni dei file quando si utilizza un link ad un documento nei blocchi di testo. + ## Versione 11.26.0 (20/12/2024) ### Migliorie diff --git a/src/helpers/files.js b/src/helpers/files.js index 328aea1e7..823241626 100644 --- a/src/helpers/files.js +++ b/src/helpers/files.js @@ -119,11 +119,18 @@ export const FILE_EXTENSIONS = { icon: { lib: '', name: faFileOdp, svg_format: true }, format_name: 'ODP', }, + pdf: { + icon: { lib: 'far', name: 'file-pdf' }, + format_name: 'PDF', + }, }; export const getFileViewFormat = (file) => { - const regexEx = /(?:\.([^.]+))?$/; - const fileExtension = regexEx.exec(file.filename)[1]; + const cleanedFilename = (file.filename || '').split(/\/[@?#]/)[0]; + const regexEx = /\.([a-zA-Z0-9]+)$/; + + const match = regexEx.exec(cleanedFilename); + const fileExtension = match ? match[1] : null; const typeOfContent = file['content-type'] ?? file['mime_type']; const viewFormat = { diff --git a/src/helpers/files.test.js b/src/helpers/files.test.js new file mode 100644 index 000000000..7a50fe70c --- /dev/null +++ b/src/helpers/files.test.js @@ -0,0 +1,90 @@ +import { getFileViewFormat, FILE_EXTENSIONS, FILE_FORMATS } from './files'; + +describe('getFileViewFormat', () => { + test('should correctly extract the extension and match the icon for a valid PDF URL with multiple segments after the extension', () => { + const file = { + filename: + 'https://example.com/amministrazione/documenti-e-dati/modulistica/area-servizi-educativi-e-scolastici/intestazione-ripartizione-rette/modulo_intestazione_ripartizione_rette_nona_784_9690.pdf/@@download/file_principale', + 'content-type': 'application/pdf', + }; + + const result = getFileViewFormat(file); + + expect(result.icon).toEqual(FILE_EXTENSIONS.pdf.icon); // Verifica che l'icona sia corretta + expect(result.label).toEqual(FILE_EXTENSIONS.pdf.format_name); // Verifica che il formato sia corretto + }); + + test('should correctly handle a PDF URL without multiple segments after the extension', () => { + const file = { + filename: + 'https://example.com/amministrazione/documenti-e-dati/modulistica/area-servizi-educativi-e-scolastici/intestazione-ripartizione-rette/modulo_intestazione_ripartizione_rette_nona_784_9690.pdf/@@download/filehttps://example.com/amministrazione/documenti-e-dati/modulistica/area-servizi-educativi-e-scolastici/intestazione-ripartizione-rette/modulo_intestazione_ripartizione_rette_nona_784_9690.pdf', + 'content-type': 'application/pdf', + }; + + const result = getFileViewFormat(file); + + expect(result.icon).toEqual(FILE_EXTENSIONS.pdf.icon); + expect(result.label).toEqual(FILE_EXTENSIONS.pdf.format_name); + }); + + test('should return correct format for a non-PDF file extension', () => { + const file = { + filename: 'https://example.com/file/example.xsd', + 'content-type': 'application/xml', + }; + + const result = getFileViewFormat(file); + + expect(result.icon).toEqual(FILE_EXTENSIONS.xsd.icon); + expect(result.label).toEqual(FILE_EXTENSIONS.xsd.format_name); + }); + + test('should return correct format for a non-PDF content-type', () => { + const file = { + filename: 'https://example.com/file/example.xml', + 'content-type': 'application/xml', + }; + + const result = getFileViewFormat(file); + + expect(result.icon).toEqual(FILE_FORMATS['application/xml'].icon); + expect(result.label).toEqual(FILE_FORMATS['application/xml'].format_name); + }); + + test('should return null format when extension or content-type is not found', () => { + const file = { + filename: 'https://example.com/file/unknown.xyz', + 'content-type': 'application/unknown', + }; + + const result = getFileViewFormat(file); + + expect(result.icon).toBeNull(); + expect(result.label).toBeNull(); + }); + + test('should handle file URLs without an extension', () => { + const file = { + filename: 'https://example.com/file/no-extension/', + 'content-type': 'text/plain', + }; + + const result = getFileViewFormat(file); + + expect(result.icon).toEqual(FILE_FORMATS['text/plain'].icon); + expect(result.label).toEqual(FILE_FORMATS['text/plain'].format_name); + }); + + test('should handle case where URL is .pdf but content-type is text/plain', () => { + const file = { + filename: 'https://example.com/file/example.pdf', + 'content-type': 'text/plain', + }; + + const result = getFileViewFormat(file); + + // La funzione dovrebbe comunque dare priorità all'estensione ".pdf" rispetto al content-type "text/plain" + expect(result.icon).toEqual(FILE_EXTENSIONS.pdf.icon); // Verifica che l'icona PDF venga restituita + expect(result.label).toEqual(FILE_EXTENSIONS.pdf.format_name); // Verifica che il formato PDF venga restituito + }); +});