From 72a2950a04df5b4a4f118a8b2d38854fc51d5ff1 Mon Sep 17 00:00:00 2001 From: Peter Velkov Date: Wed, 25 Jan 2023 13:35:46 +0200 Subject: [PATCH] Documentation and better naming for `tryResolveUrlFromApiRoot` --- .../HTMLEngineProvider/HTMLRenderers/ImageRenderer.js | 8 +++++--- src/libs/fileDownload/getAttachmentDetails.js | 8 +++++--- ...aceSourceOrigin.js => tryResolveUrlFromApiRoot.js} | 11 +++++++---- 3 files changed, 17 insertions(+), 10 deletions(-) rename src/libs/{replaceSourceOrigin.js => tryResolveUrlFromApiRoot.js} (58%) diff --git a/src/components/HTMLEngineProvider/HTMLRenderers/ImageRenderer.js b/src/components/HTMLEngineProvider/HTMLRenderers/ImageRenderer.js index 357a0733f6a2..537913b500f5 100644 --- a/src/components/HTMLEngineProvider/HTMLRenderers/ImageRenderer.js +++ b/src/components/HTMLEngineProvider/HTMLRenderers/ImageRenderer.js @@ -6,7 +6,7 @@ import ThumbnailImage from '../../ThumbnailImage'; import PressableWithoutFocus from '../../PressableWithoutFocus'; import CONST from '../../../CONST'; import {ShowContextMenuContext, showContextMenuForReport} from '../../ShowContextMenuContext'; -import replaceSourceOrigin from '../../../libs/replaceSourceOrigin'; +import tryResolveUrlFromApiRoot from '../../../libs/tryResolveUrlFromApiRoot'; const ImageRenderer = (props) => { const htmlAttribs = props.tnode.attributes; @@ -30,8 +30,10 @@ const ImageRenderer = (props) => { // const isAttachment = Boolean(htmlAttribs[CONST.ATTACHMENT_SOURCE_ATTRIBUTE]); const originalFileName = htmlAttribs['data-name']; - const previewSource = replaceSourceOrigin(htmlAttribs.src); - const source = replaceSourceOrigin(isAttachment + + // Files created/uploaded/hosted by App should resolve from API ROOT. Other URLs aren't modified + const previewSource = tryResolveUrlFromApiRoot(htmlAttribs.src); + const source = tryResolveUrlFromApiRoot(isAttachment ? htmlAttribs[CONST.ATTACHMENT_SOURCE_ATTRIBUTE] : htmlAttribs.src); diff --git a/src/libs/fileDownload/getAttachmentDetails.js b/src/libs/fileDownload/getAttachmentDetails.js index 39da70fe739f..8105a78c4799 100644 --- a/src/libs/fileDownload/getAttachmentDetails.js +++ b/src/libs/fileDownload/getAttachmentDetails.js @@ -1,5 +1,5 @@ import CONST from '../../CONST'; -import replaceSourceOrigin from '../replaceSourceOrigin'; +import tryResolveUrlFromApiRoot from '../tryResolveUrlFromApiRoot'; /** * Extract the thumbnail URL, source URL and the original filename from the HTML. @@ -19,8 +19,10 @@ export default function getAttachmentName(html) { originalFileName: null, }; } - const sourceURL = replaceSourceOrigin(html.match(SOURCE_REGEX)[1]); - const imageURL = IS_IMAGE_TAG && replaceSourceOrigin(html.match(PREVIEW_SOURCE_REGEX)[1]); + + // Files created/uploaded/hosted by App should resolve from API ROOT. Other URLs aren't modified + const sourceURL = tryResolveUrlFromApiRoot(html.match(SOURCE_REGEX)[1]); + const imageURL = IS_IMAGE_TAG && tryResolveUrlFromApiRoot(html.match(PREVIEW_SOURCE_REGEX)[1]); const previewSourceURL = IS_IMAGE_TAG ? imageURL : sourceURL; const originalFileName = html.match(ORIGINAL_FILENAME_REGEX)[1]; diff --git a/src/libs/replaceSourceOrigin.js b/src/libs/tryResolveUrlFromApiRoot.js similarity index 58% rename from src/libs/replaceSourceOrigin.js rename to src/libs/tryResolveUrlFromApiRoot.js index 277e7b5f213c..7797d3446459 100644 --- a/src/libs/replaceSourceOrigin.js +++ b/src/libs/tryResolveUrlFromApiRoot.js @@ -9,13 +9,16 @@ const ORIGINS_TO_REPLACE = ['/+', Config.EXPENSIFY.EXPENSIFY_URL, Config.EXPENSI const ORIGIN_PATTERN = new RegExp(`^(${ORIGINS_TO_REPLACE.join('|')})`); /** - * Updates URLs, so they are accessed relative to URL_API_ROOT - * Matches: absolute, prod or staging URLs - * Unmatched URLs aren't modified + * When possible resolve sources relative to API ROOT + * Updates applicable URLs, so they are accessed relative to URL_API_ROOT + * - Absolute URLs like `/{path}`, become: `https://{API_ROOT}/{path}` + * - Similarly for prod or staging URLs we replace the `https://www.expensify` + * or `https://staging.expensify` part, with `https://{API_ROOT}` + * - Unmatched URLs are returned with no modifications * * @param {String} url * @returns {String} */ -export default function replaceSourceOrigin(url) { +export default function tryResolveUrlFromApiRoot(url) { return url.replace(ORIGIN_PATTERN, Config.EXPENSIFY.URL_API_ROOT); }