mirror of
https://github.com/status-im/react-native.git
synced 2025-02-26 08:05:34 +00:00
Add RCTLibraryPathForURL in RCTUtil
Reviewed By: fromcelticpark Differential Revision: D6445626 fbshipit-source-id: aa37c87f019eea85d76365b6be919adfafc3c27a
This commit is contained in:
parent
b952365ba6
commit
2fecbf6171
@ -30,7 +30,7 @@ RCT_EXPORT_MODULE()
|
||||
{
|
||||
return
|
||||
[request.URL.scheme caseInsensitiveCompare:@"file"] == NSOrderedSame
|
||||
&& !RCTIsLocalAssetURL(request.URL);
|
||||
&& !RCTIsBundleAssetURL(request.URL);
|
||||
}
|
||||
|
||||
- (NSOperation *)sendRequest:(NSURLRequest *)request
|
||||
|
@ -90,4 +90,17 @@
|
||||
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
|
||||
|
@ -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)
|
||||
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
|
||||
RCT_EXTERN BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL);
|
||||
|
||||
|
@ -596,34 +596,64 @@ NSData *__nullable RCTGzipData(NSData *__nullable input, float level)
|
||||
return output;
|
||||
}
|
||||
|
||||
NSString *__nullable RCTBundlePathForURL(NSURL *__nullable URL)
|
||||
static NSString *RCTRelativePathForURL(NSString *basePath, NSURL *__nullable URL)
|
||||
{
|
||||
if (!URL.fileURL) {
|
||||
// Not a file path
|
||||
return nil;
|
||||
}
|
||||
NSString *path = [NSString stringWithUTF8String:[URL fileSystemRepresentation]];
|
||||
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
|
||||
if (![path hasPrefix:bundlePath]) {
|
||||
if (![path hasPrefix:basePath]) {
|
||||
// Not a bundle-relative file
|
||||
return nil;
|
||||
}
|
||||
path = [path substringFromIndex:bundlePath.length];
|
||||
path = [path substringFromIndex:basePath.length];
|
||||
if ([path hasPrefix:@"/"]) {
|
||||
path = [path substringFromIndex:1];
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
NSString *__nullable RCTLibraryPath(void)
|
||||
{
|
||||
static NSString *libraryPath = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
|
||||
});
|
||||
return libraryPath;
|
||||
}
|
||||
|
||||
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"];
|
||||
}
|
||||
|
||||
BOOL RCTIsBundleAssetURL(NSURL *__nullable imageURL)
|
||||
{
|
||||
return RCTIsImageAssetsPath(RCTBundlePathForURL(imageURL));
|
||||
}
|
||||
|
||||
BOOL RCTIsLibraryAssetURL(NSURL *__nullable imageURL)
|
||||
{
|
||||
return RCTIsImageAssetsPath(RCTLibraryPathForURL(imageURL));
|
||||
}
|
||||
|
||||
BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL)
|
||||
{
|
||||
NSString *name = RCTBundlePathForURL(imageURL);
|
||||
if (!name) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
NSString *extension = [name pathExtension];
|
||||
return [extension isEqualToString:@"png"] || [extension isEqualToString:@"jpg"];
|
||||
return RCTIsBundleAssetURL(imageURL) || RCTIsLibraryAssetURL(imageURL);
|
||||
}
|
||||
|
||||
static NSString *bundleName(NSBundle *bundle)
|
||||
|
Loading…
x
Reference in New Issue
Block a user