mirror of
https://github.com/status-im/react-native.git
synced 2025-02-26 16:10:58 +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
|
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
|
||||||
|
@ -90,4 +90,17 @@
|
|||||||
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
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
@ -596,34 +596,64 @@ NSData *__nullable RCTGzipData(NSData *__nullable input, float level)
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL)
|
||||||
{
|
{
|
||||||
NSString *name = RCTBundlePathForURL(imageURL);
|
return RCTIsBundleAssetURL(imageURL) || RCTIsLibraryAssetURL(imageURL);
|
||||||
if (!name) {
|
|
||||||
return NO;
|
|
||||||
}
|
|
||||||
|
|
||||||
NSString *extension = [name pathExtension];
|
|
||||||
return [extension isEqualToString:@"png"] || [extension isEqualToString:@"jpg"];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static NSString *bundleName(NSBundle *bundle)
|
static NSString *bundleName(NSBundle *bundle)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user