Skip to content

Commit

Permalink
fix(storage,ios): Fix issue with ph:// (Photos) paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Salakar committed Oct 24, 2019
1 parent 5d60639 commit cbced41
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions packages/app/ios/RNFBApp/RNFBUtilsModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*
*/

#import <Firebase/Firebase.h>
#import <React/RCTUtils.h>

#import "RNFBApp/RNFBSharedUtils.h"
Expand All @@ -39,56 +38,54 @@ + (BOOL)requiresMainQueueSetup {
#pragma mark Firebase Utils Methods

+ (BOOL)isRemoteAsset:(NSString *)localFilePath {
return [localFilePath hasPrefix:@"assets-library://"] ||
[localFilePath hasPrefix:@"ph://"];
return [localFilePath hasPrefix:@"assets-library://"] || [localFilePath hasPrefix:@"ph://"];
}

+ (BOOL)unused_isHeic:(NSString *)localFilePath {
return [[localFilePath pathExtension] caseInsensitiveCompare:@"heic"] ==
NSOrderedSame;
return [[localFilePath pathExtension] caseInsensitiveCompare:@"heic"] == NSOrderedSame;
}

+ (NSString *)valueForKey:(NSString *)key fromQueryItems:(NSArray *)queryItems {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", key];
NSURLQueryItem *queryItem =
[[queryItems filteredArrayUsingPredicate:predicate] firstObject];
NSURLQueryItem *queryItem = [[queryItems filteredArrayUsingPredicate:predicate] firstObject];
return queryItem.value;
}

+ (PHAsset *)fetchAssetForPath:(NSString *)localFilePath {
PHAsset *asset;

if ([localFilePath hasPrefix:@"assets-library://"]) {
NSURL *localFile = [[NSURL alloc] initWithString:localFilePath];
asset = [[PHAsset fetchAssetsWithALAssetURLs:@[ localFile ]
options:nil] firstObject];
if ([localFilePath hasPrefix:@"assets-library://"] || [localFilePath hasPrefix:@"ph://"]) {
if ([localFilePath hasPrefix:@"assets-library://"]) {
NSURL *localFile = [[NSURL alloc] initWithString:localFilePath];
asset = [[PHAsset fetchAssetsWithALAssetURLs:@[localFile] options:nil] firstObject];
} else {
NSString *assetId = [localFilePath substringFromIndex:@"ph://".length];
asset = [[PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil] firstObject];
}
} else {
NSURLComponents *components =
[NSURLComponents componentsWithString:localFilePath];
NSURLComponents *components = [NSURLComponents componentsWithString:localFilePath];
NSArray *queryItems = components.queryItems;
NSString *assetId = [self valueForKey:@"id" fromQueryItems:queryItems];
asset = [[PHAsset fetchAssetsWithLocalIdentifiers:@[ assetId ]
options:nil] firstObject];
asset = [[PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil] firstObject];
}

return asset;
}

- (NSString *)getPathForDirectory:(int)directory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(
(NSSearchPathDirectory)directory, NSUserDomainMask, YES);
NSArray *paths = NSSearchPathForDirectoriesInDomains((NSSearchPathDirectory) directory, NSUserDomainMask, YES);
return [paths firstObject];
}

- (NSDictionary *)constantsToExport {
NSMutableDictionary *constants = [@{
@"MAIN_BUNDLE" : [[NSBundle mainBundle] bundlePath],
@"CACHES_DIRECTORY" : [self getPathForDirectory:NSCachesDirectory],
@"DOCUMENT_DIRECTORY" : [self getPathForDirectory:NSDocumentDirectory],
@"PICTURES_DIRECTORY" : [self getPathForDirectory:NSPicturesDirectory],
@"MOVIES_DIRECTORY" : [self getPathForDirectory:NSMoviesDirectory],
@"TEMP_DIRECTORY" : NSTemporaryDirectory(),
@"LIBRARY_DIRECTORY" : [self getPathForDirectory:NSLibraryDirectory],
@"MAIN_BUNDLE": [[NSBundle mainBundle] bundlePath],
@"CACHES_DIRECTORY": [self getPathForDirectory:NSCachesDirectory],
@"DOCUMENT_DIRECTORY": [self getPathForDirectory:NSDocumentDirectory],
@"PICTURES_DIRECTORY": [self getPathForDirectory:NSPicturesDirectory],
@"MOVIES_DIRECTORY": [self getPathForDirectory:NSMoviesDirectory],
@"TEMP_DIRECTORY": NSTemporaryDirectory(),
@"LIBRARY_DIRECTORY": [self getPathForDirectory:NSLibraryDirectory],
} mutableCopy];

return constants;
Expand Down

0 comments on commit cbced41

Please sign in to comment.