Skip to content

Commit

Permalink
Documentation and better naming for tryResolveUrlFromApiRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
kidroca committed Jan 25, 2023
1 parent 482e13f commit 72a2950
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down
8 changes: 5 additions & 3 deletions src/libs/fileDownload/getAttachmentDetails.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 72a2950

Please sign in to comment.