From 26f7b35fc6b83abdcdd2f12e739f9cad0c892f1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Wed, 13 Nov 2024 17:42:21 +0100 Subject: [PATCH] Fix 60879 --- app/components/document/index.tsx | 2 +- app/components/files/file_info.tsx | 2 +- app/components/markdown/markdown_image/index.tsx | 4 ++-- app/components/markdown/markdown_table_image/index.tsx | 3 ++- .../components/bookmark_file/bookmark_file.tsx | 2 +- app/screens/channel_bookmark/index.tsx | 4 ++-- app/utils/file/file_picker/index.ts | 3 ++- app/utils/url/index.ts | 9 +++++++++ 8 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/components/document/index.tsx b/app/components/document/index.tsx index 66755634484..43ef3faf694 100644 --- a/app/components/document/index.tsx +++ b/app/components/document/index.tsx @@ -116,7 +116,7 @@ const Document = forwardRef(({canDownloadFiles, chil setPreview(true); setStatusBarColor('dark-content'); FileViewer.open(path!.replace('file://', ''), { - displayName: decodeURIComponent(file.name), + displayName: file.name, onDismiss: onDonePreviewingFile, showOpenWithDialog: true, showAppsSuggestions: true, diff --git a/app/components/files/file_info.tsx b/app/components/files/file_info.tsx index 50f965fbb91..b8e4933a55f 100644 --- a/app/components/files/file_info.tsx +++ b/app/components/files/file_info.tsx @@ -79,7 +79,7 @@ const FileInfo = ({disabled, file, channelName, showDate, onPress}: FileInfoProp ellipsizeMode='tail' style={style.fileName} > - {decodeURIComponent(file.name.trim())} + {file.name.trim()} {channelName && diff --git a/app/components/markdown/markdown_image/index.tsx b/app/components/markdown/markdown_image/index.tsx index db4a679927e..94fe31201f7 100644 --- a/app/components/markdown/markdown_image/index.tsx +++ b/app/components/markdown/markdown_image/index.tsx @@ -29,7 +29,7 @@ import {bottomSheetSnapPoint} from '@utils/helpers'; import {calculateDimensions, getViewPortWidth, isGifTooLarge} from '@utils/images'; import {getMarkdownImageSize} from '@utils/markdown'; import {changeOpacity, makeStyleSheetFromTheme} from '@utils/theme'; -import {normalizeProtocol, tryOpenURL} from '@utils/url'; +import {normalizeProtocol, safeDecodeURIComponent, tryOpenURL} from '@utils/url'; import type {GalleryItemType} from '@typings/screens/gallery'; @@ -88,7 +88,7 @@ const MarkdownImage = ({ const uri = source.startsWith('/') ? serverUrl + source : source; const fileInfo = useMemo(() => { - const link = decodeURIComponent(uri); + const link = safeDecodeURIComponent(uri); let filename = parseUrl(link.substr(link.lastIndexOf('/'))).pathname.replace('/', ''); let extension = metadata?.format || filename.split('.').pop(); if (extension === filename) { diff --git a/app/components/markdown/markdown_table_image/index.tsx b/app/components/markdown/markdown_table_image/index.tsx index 042887befee..9a1d5684b48 100644 --- a/app/components/markdown/markdown_table_image/index.tsx +++ b/app/components/markdown/markdown_table_image/index.tsx @@ -13,6 +13,7 @@ import {useGalleryItem} from '@hooks/gallery'; import {fileToGalleryItem, openGalleryAtIndex} from '@utils/gallery'; import {generateId} from '@utils/general'; import {calculateDimensions, isGifTooLarge} from '@utils/images'; +import {safeDecodeURIComponent} from '@utils/url'; import type {GalleryItemType} from '@typings/screens/gallery'; @@ -57,7 +58,7 @@ const MarkTableImage = ({disabled, imagesMetadata, location, postId, serverURL, const getFileInfo = () => { const height = metadata?.height || 0; const width = metadata?.width || 0; - const link = decodeURIComponent(getImageSource()); + const link = safeDecodeURIComponent(getImageSource()); let filename = parseUrl(link.substr(link.lastIndexOf('/'))).pathname.replace('/', ''); let extension = filename.split('.').pop(); diff --git a/app/screens/channel_bookmark/components/bookmark_file/bookmark_file.tsx b/app/screens/channel_bookmark/components/bookmark_file/bookmark_file.tsx index 4fa463492ec..cf5d670bca1 100644 --- a/app/screens/channel_bookmark/components/bookmark_file/bookmark_file.tsx +++ b/app/screens/channel_bookmark/components/bookmark_file/bookmark_file.tsx @@ -308,7 +308,7 @@ const BookmarkFile = ({channelId, close, disabled, initialFile, maxFileSize, set ellipsizeMode='tail' style={styles.filename} > - {decodeURIComponent(file.name.trim())} + {file.name.trim()} {info} diff --git a/app/screens/channel_bookmark/index.tsx b/app/screens/channel_bookmark/index.tsx index 81f310494ad..18501669b53 100644 --- a/app/screens/channel_bookmark/index.tsx +++ b/app/screens/channel_bookmark/index.tsx @@ -159,13 +159,13 @@ const ChannelBookmarkAddOrEdit = ({ ...(bookmark || emptyBookmark), owner_id: ownerId, channel_id: channelId, - display_name: decodeURIComponent(f.name), + display_name: f.name, type: 'file', file_id: f.id, }; setBookmarkToSave(b); setFile(f); - }, [bookmark, channelId, ownerId]); + }, [bookmark, channelId, ownerId, setBookmarkToSave]); const setBookmarkDisplayName = useCallback((displayName: string) => { if (bookmark) { diff --git a/app/utils/file/file_picker/index.ts b/app/utils/file/file_picker/index.ts index 3daec52fed3..2e008733f22 100644 --- a/app/utils/file/file_picker/index.ts +++ b/app/utils/file/file_picker/index.ts @@ -11,6 +11,7 @@ import Permissions from 'react-native-permissions'; import {dismissBottomSheet} from '@screens/navigation'; import {extractFileInfo, lookupMimeType} from '@utils/file'; import {logWarning} from '@utils/log'; +import {safeDecodeURIComponent} from '@utils/url'; import type {IntlShape} from 'react-intl'; @@ -128,7 +129,7 @@ export default class FilePickerUtil { const type = file.type || lookupMimeType(uri); let fileName = file.fileName; if (type.includes('video/') && uri) { - fileName = decodeURIComponent(uri.split('\\').pop()?.split('/').pop() || ''); + fileName = safeDecodeURIComponent(uri.split('\\').pop()?.split('/').pop() || ''); } if (uri) { diff --git a/app/utils/url/index.ts b/app/utils/url/index.ts index def29fcbe4d..a38be92d284 100644 --- a/app/utils/url/index.ts +++ b/app/utils/url/index.ts @@ -252,3 +252,12 @@ export function extractFilenameFromUrl(url: string) { const uri = urlParse(url); return uri.pathname.split('/').pop(); } + +export function safeDecodeURIComponent(v: string) { + try { + const result = decodeURIComponent(v); + return result; + } catch { + return v; + } +}