From d55dbc6f53d5a759e07d534dadbcb327a194ffdf Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 25 Jan 2024 01:48:00 +0100 Subject: [PATCH 1/2] fix: Remove unreachable code Signed-off-by: Ferdinand Thiessen --- lib/index.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 969fd21..74a6092 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -163,8 +163,9 @@ export const imagePath = (app: string, file: string) => { */ export const generateFilePath = (app: string, type: string, file: string) => { const isCore = window?.OC?.coreApps?.indexOf(app) !== -1 + const isPHP = file.slice(-3) === 'php' let link = getRootUrl() - if (file.substring(file.length - 3) === 'php' && !isCore) { + if (isPHP && !isCore) { link += `/index.php/apps/${app}` if (type) { link += `/${encodeURI(type)}` @@ -172,7 +173,7 @@ export const generateFilePath = (app: string, type: string, file: string) => { if (file !== 'index.php') { link += `/${file}` } - } else if (file.substring(file.length - 3) !== 'php' && !isCore) { + } else if (!isPHP && !isCore) { link = getAppRootUrl(app) if (type) { link += '/' + type + '/' @@ -187,9 +188,6 @@ export const generateFilePath = (app: string, type: string, file: string) => { } else { link += '/' } - if (!isCore) { - link += 'apps/' - } if (app !== '') { app += '/' link += app From e0becaf7481dd4dc3b5692a26a9760c7a953c3f4 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 25 Jan 2024 01:54:02 +0100 Subject: [PATCH 2/2] fix: Optimize and modernize code Signed-off-by: Ferdinand Thiessen --- __tests__/paths.spec.ts | 14 ++++++++++++++ lib/index.ts | 19 ++++++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/__tests__/paths.spec.ts b/__tests__/paths.spec.ts index e93cc78..2941781 100644 --- a/__tests__/paths.spec.ts +++ b/__tests__/paths.spec.ts @@ -42,6 +42,20 @@ describe('Path generation', () => { beforeEach(() => { window._oc_webroot = '' window._oc_appswebroots = { forms: '/apps-extra/forms' } + + window.OC = { + coreApps: ['', 'admin', 'log', 'core/search', 'core', '3rdparty'], + } + }) + + test('missing core apps global variable', () => { + delete window.OC.coreApps + expect(generateFilePath('forms', '', 'file.js')).toBe('/apps-extra/forms/file.js') + }) + + test('missing OC global variable (unit tests)', () => { + delete window.OC + expect(generateFilePath('forms', '', 'file.js')).toBe('/apps-extra/forms/file.js') }) test('non core PHP index file', () => { diff --git a/lib/index.ts b/lib/index.ts index 74a6092..1c08b76 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -162,7 +162,7 @@ export const imagePath = (app: string, file: string) => { * @return {string} URL with webroot for a file in an app */ export const generateFilePath = (app: string, type: string, file: string) => { - const isCore = window?.OC?.coreApps?.indexOf(app) !== -1 + const isCore = window?.OC?.coreApps?.includes(app) ?? false const isPHP = file.slice(-3) === 'php' let link = getRootUrl() if (isPHP && !isCore) { @@ -176,26 +176,23 @@ export const generateFilePath = (app: string, type: string, file: string) => { } else if (!isPHP && !isCore) { link = getAppRootUrl(app) if (type) { - link += '/' + type + '/' + link += `/${type}/` } - if (link.substring(link.length - 1) !== '/') { + if (link.at(-1) !== '/') { link += '/' } link += file } else { if ((app === 'settings' || app === 'core' || app === 'search') && type === 'ajax') { - link += '/index.php/' - } else { - link += '/' + link += '/index.php' } - if (app !== '') { - app += '/' - link += app + if (app) { + link += `/${app}` } if (type) { - link += type + '/' + link += `/${type}` } - link += file + link += `/${file}` } return link }