Skip to content

Commit

Permalink
Local filenames with colon should be parsed correctly (#35123)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #35123

**Context**

On RN Desktop, images can be copy/pasted into text inputs.
When copy/pasting local files w/ semi-colons in the filename (`/downloads/devices:pre-call-IMG_346C38284B2B-1.jpeg`), RN would throw this error.

{F785684529}

The error was due to no having the correct asset loader due to the local file path being parsed incorrectly.

To parse the url, we convert the string URI to a `NSURL` using a custom convertor fn.
The conversion was matching any `:` and assuming it was a network url scheme:
https://www.internalfb.com/code/archon_react_native_macos/[fde4113acd89fb13ee11636c48b59eac49c21bae]/React/Base/RCTConvert.m?lines=97-111

When an image path with `:` in the name was passed to this, it was assuming it was a valid network url and not file path.

Because this is a local filepath, the image path needs to be a filesystem url. Since this was parsed as network url, it did not have the `file://` prefix and would not pass this check, causing the loader to throw.l
https://www.internalfb.com/code/fbsource/%5B60d9d5e67383%5D/xplat/js/react-native-github/Libraries/Image/RCTImageLoader.mm?lines=220-230

## Changelog

[iOS] [Added] -  Add support for parsing files w/ `:` in filename

Reviewed By: christophpurrer, philIip

Differential Revision: D40729963

fbshipit-source-id: 2f3adcabc8f0f1f22cbfca69c3484e72b1d93d25
  • Loading branch information
shwanton authored and facebook-github-bot committed Nov 3, 2022
1 parent e8d3fb8 commit 714b22b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion React/Base/RCTConvert.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ + (NSURL *)NSURL:(id)json
}

// Check if it has a scheme
if ([path rangeOfString:@":"].location != NSNotFound) {
if ([path rangeOfString:@"://"].location != NSNotFound) {

This comment has been minimized.

Copy link
@one-smiling

one-smiling Feb 26, 2023

base64 will parse failed,return "file:// bundlepath/.../datadata:image/jpg/png/gif;base64,R0lGODdhUAA..."

This comment has been minimized.

Copy link
@sagar-tomar-groww

sagar-tomar-groww Jun 9, 2023

this broke lot of functionalities in our mobile app related to it after upgrade.

NSMutableCharacterSet *urlAllowedCharacterSet = [NSMutableCharacterSet new];
[urlAllowedCharacterSet formUnionWithCharacterSet:[NSCharacterSet URLUserAllowedCharacterSet]];
[urlAllowedCharacterSet formUnionWithCharacterSet:[NSCharacterSet URLPasswordAllowedCharacterSet]];
Expand Down
3 changes: 3 additions & 0 deletions packages/rn-tester/RNTesterUnitTests/RCTConvert_NSURLTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ -(void)test_##name \
TEST_BUNDLE_PATH(imageAt2XPath, @"images/foo@2x.jpg", @"images/foo@2x.jpg")
TEST_BUNDLE_PATH(imageFile, @"foo.jpg", @"foo.jpg")

TEST_BUNDLE_PATH(imageFileWithSemicolon, @"folder/foo:bar-baz.jpg", @"folder/foo:bar-baz.jpg")
TEST_URL(filePathWithSemicolon, @"/folder/foo:bar-baz.jpg", @"file:///folder/foo:bar-baz.jpg")

// User documents
TEST_PATH(
documentsFolder,
Expand Down

0 comments on commit 714b22b

Please sign in to comment.