From 38a0578011896cfcf217713d34f285cd381ad72c Mon Sep 17 00:00:00 2001 From: "Jason C. Leach" Date: Thu, 15 Jun 2023 01:10:58 -0700 Subject: [PATCH] fix: check if URL already encoded (#1485) Signed-off-by: Jason C. Leach --- packages/react-native/src/ReactNativeFileSystem.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/react-native/src/ReactNativeFileSystem.ts b/packages/react-native/src/ReactNativeFileSystem.ts index 7c2b8b239c..27c7a4e25e 100644 --- a/packages/react-native/src/ReactNativeFileSystem.ts +++ b/packages/react-native/src/ReactNativeFileSystem.ts @@ -66,11 +66,10 @@ export class ReactNativeFileSystem implements FileSystem { // Make sure parent directories exist await RNFS.mkdir(getDirFromFilePath(path)) - // Some characters in the URL might be invalid for - // the native os to handle. We need to encode the URL. - const encodedFromUrl = encodeURI(url) + const fromUrl = this.encodeUriIfRequired(url) + const { promise } = RNFS.downloadFile({ - fromUrl: encodedFromUrl, + fromUrl, toFile: path, }) @@ -92,4 +91,10 @@ export class ReactNativeFileSystem implements FileSystem { } } } + + private encodeUriIfRequired(uri: string) { + // Some characters in the URL might be invalid for + // the native os to handle. Only encode if necessary. + return uri === decodeURI(uri) ? encodeURI(uri) : uri + } }