Skip to content

Commit

Permalink
Add RCTLibraryPathForURL in RCTUtil
Browse files Browse the repository at this point in the history
Reviewed By: fromcelticpark

Differential Revision: D6445626

fbshipit-source-id: aa37c87f019eea85d76365b6be919adfafc3c27a
  • Loading branch information
Yujie Liu authored and facebook-github-bot committed Dec 14, 2017
1 parent b952365 commit 2fecbf6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Libraries/Network/RCTFileRequestHandler.m
Expand Up @@ -30,7 +30,7 @@ - (BOOL)canHandleRequest:(NSURLRequest *)request
{ {
return return
[request.URL.scheme caseInsensitiveCompare:@"file"] == NSOrderedSame [request.URL.scheme caseInsensitiveCompare:@"file"] == NSOrderedSame
&& !RCTIsLocalAssetURL(request.URL); && !RCTIsBundleAssetURL(request.URL);
} }


- (NSOperation *)sendRequest:(NSURLRequest *)request - (NSOperation *)sendRequest:(NSURLRequest *)request
Expand Down
13 changes: 13 additions & 0 deletions RNTester/RNTesterUnitTests/RCTURLUtilsTests.m
Expand Up @@ -90,4 +90,17 @@ - (void)testNilURLAppendQueryParam
XCTAssertNil(result); XCTAssertNil(result);
} }


- (void)testIsLocalAssetsURLParam
{
NSString *libraryAssetsPath = [RCTLibraryPath() stringByAppendingPathComponent:@"assets/foo.png"];
NSURL *libraryAssetsURL = [NSURL fileURLWithPath:libraryAssetsPath];
XCTAssertTrue(RCTIsLocalAssetURL(libraryAssetsURL));
NSString *bundleAssetsPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"assets/foo.png"];
NSURL *bundleAssetsURL = [NSURL fileURLWithPath:bundleAssetsPath];
XCTAssertTrue(RCTIsLocalAssetURL(bundleAssetsURL));
NSString *otherAssetsPath = @"/assets/foo.png";
NSURL *otherAssetsURL = [NSURL fileURLWithPath:otherAssetsPath];
XCTAssertFalse(RCTIsLocalAssetURL(otherAssetsURL));
}

@end @end
13 changes: 13 additions & 0 deletions React/Base/RCTUtils.h
Expand Up @@ -115,6 +115,19 @@ RCT_EXTERN NSData *__nullable RCTGzipData(NSData *__nullable data, float level);
// (or nil, if the URL does not specify a path within the main bundle) // (or nil, if the URL does not specify a path within the main bundle)
RCT_EXTERN NSString *__nullable RCTBundlePathForURL(NSURL *__nullable URL); RCT_EXTERN NSString *__nullable RCTBundlePathForURL(NSURL *__nullable URL);


// Returns the Path of Library directory
RCT_EXTERN NSString *__nullable RCTLibraryPath(void);

// Returns the relative path within the library for an absolute URL
// (or nil, if the URL does not specify a path within the Library directory)
RCT_EXTERN NSString *__nullable RCTLibraryPathForURL(NSURL *__nullable URL);

// Determines if a given image URL refers to a image in bundle
RCT_EXTERN BOOL RCTIsBundleAssetURL(NSURL *__nullable imageURL);

// Determines if a given image URL refers to a image in library
RCT_EXTERN BOOL RCTIsLibraryAssetURL(NSURL *__nullable imageURL);

// Determines if a given image URL refers to a local image // Determines if a given image URL refers to a local image
RCT_EXTERN BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL); RCT_EXTERN BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL);


Expand Down
50 changes: 40 additions & 10 deletions React/Base/RCTUtils.m
Expand Up @@ -596,36 +596,66 @@ BOOL RCTIsGzippedData(NSData *__nullable data)
return output; return output;
} }


NSString *__nullable RCTBundlePathForURL(NSURL *__nullable URL) static NSString *RCTRelativePathForURL(NSString *basePath, NSURL *__nullable URL)
{ {
if (!URL.fileURL) { if (!URL.fileURL) {
// Not a file path // Not a file path
return nil; return nil;
} }
NSString *path = [NSString stringWithUTF8String:[URL fileSystemRepresentation]]; NSString *path = [NSString stringWithUTF8String:[URL fileSystemRepresentation]];
NSString *bundlePath = [[NSBundle mainBundle] resourcePath]; if (![path hasPrefix:basePath]) {
if (![path hasPrefix:bundlePath]) {
// Not a bundle-relative file // Not a bundle-relative file
return nil; return nil;
} }
path = [path substringFromIndex:bundlePath.length]; path = [path substringFromIndex:basePath.length];
if ([path hasPrefix:@"/"]) { if ([path hasPrefix:@"/"]) {
path = [path substringFromIndex:1]; path = [path substringFromIndex:1];
} }
return path; return path;
} }


BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL) NSString *__nullable RCTLibraryPath(void)
{ {
NSString *name = RCTBundlePathForURL(imageURL); static NSString *libraryPath = nil;
if (!name) { static dispatch_once_t onceToken;
return NO; dispatch_once(&onceToken, ^{
} libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
});
return libraryPath;
}


NSString *extension = [name pathExtension]; NSString *__nullable RCTBundlePathForURL(NSURL *__nullable URL)
{
return RCTRelativePathForURL([[NSBundle mainBundle] resourcePath], URL);

}

NSString *__nullable RCTLibraryPathForURL(NSURL *__nullable URL)
{
return RCTRelativePathForURL(RCTLibraryPath(), URL);
}

static BOOL RCTIsImageAssetsPath(NSString *path)
{
NSString *extension = [path pathExtension];
return [extension isEqualToString:@"png"] || [extension isEqualToString:@"jpg"]; return [extension isEqualToString:@"png"] || [extension isEqualToString:@"jpg"];
} }


BOOL RCTIsBundleAssetURL(NSURL *__nullable imageURL)
{
return RCTIsImageAssetsPath(RCTBundlePathForURL(imageURL));
}

BOOL RCTIsLibraryAssetURL(NSURL *__nullable imageURL)
{
return RCTIsImageAssetsPath(RCTLibraryPathForURL(imageURL));
}

BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL)
{
return RCTIsBundleAssetURL(imageURL) || RCTIsLibraryAssetURL(imageURL);
}

static NSString *bundleName(NSBundle *bundle) static NSString *bundleName(NSBundle *bundle)
{ {
NSString *name = bundle.infoDictionary[@"CFBundleName"]; NSString *name = bundle.infoDictionary[@"CFBundleName"];
Expand Down

0 comments on commit 2fecbf6

Please sign in to comment.