From e22e46088ae3f98a63dd166389e6011bf2a6a684 Mon Sep 17 00:00:00 2001 From: Dmitriy Loktev Date: Fri, 23 Dec 2016 20:06:28 +0300 Subject: [PATCH 1/2] Getting ready for RN@0.40 --- NSArray+Map.m | 2 +- RNFS.xcodeproj/project.pbxproj | 8 +- RNFSManager.h | 4 +- RNFSManager.m | 754 ++++++++++++++++----------------- 4 files changed, 382 insertions(+), 386 deletions(-) diff --git a/NSArray+Map.m b/NSArray+Map.m index d7cf283d..d5af2672 100644 --- a/NSArray+Map.m +++ b/NSArray+Map.m @@ -17,4 +17,4 @@ - (NSArray *)rnfs_mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block return result; } -@end \ No newline at end of file +@end diff --git a/RNFS.xcodeproj/project.pbxproj b/RNFS.xcodeproj/project.pbxproj index 6809a2b4..def02d22 100644 --- a/RNFS.xcodeproj/project.pbxproj +++ b/RNFS.xcodeproj/project.pbxproj @@ -220,10 +220,8 @@ buildSettings = { HEADER_SEARCH_PATHS = ( "$(inherited)", + "$(BUILT_PRODUCTS_DIR)/usr/local/include", /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, - "$(SRCROOT)/../../React/**", - "$(SRCROOT)/../react-native/React/**", - "$(SRCROOT)/node_modules/react-native/React/**", ); OTHER_LDFLAGS = "-ObjC"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -236,10 +234,8 @@ buildSettings = { HEADER_SEARCH_PATHS = ( "$(inherited)", + "$(BUILT_PRODUCTS_DIR)/usr/local/include", /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, - "$(SRCROOT)/../../React/**", - "$(SRCROOT)/../react-native/React/**", - "$(SRCROOT)/node_modules/react-native/React/**", ); OTHER_LDFLAGS = "-ObjC"; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/RNFSManager.h b/RNFSManager.h index c801aaef..3ebc1743 100644 --- a/RNFSManager.h +++ b/RNFSManager.h @@ -6,8 +6,8 @@ // Copyright (c) 2015 Johannes Lumpe. All rights reserved. // -#import "RCTBridgeModule.h" -#import "RCTLog.h" +#import "React/RCTBridgeModule.h" +#import "React/RCTLog.h" @interface RNFSManager : NSObject diff --git a/RNFSManager.m b/RNFSManager.m index 95c282d6..cf8d209c 100644 --- a/RNFSManager.m +++ b/RNFSManager.m @@ -7,11 +7,11 @@ // #import "RNFSManager.h" -#import "RCTBridge.h" +#import "React/RCTBridge.h" #import "NSArray+Map.h" #import "Downloader.h" #import "Uploader.h" -#import "RCTEventDispatcher.h" +#import "React/RCTEventDispatcher.h" #import @interface RNFSManager() @@ -29,66 +29,66 @@ @implementation RNFSManager - (dispatch_queue_t)methodQueue { - return dispatch_queue_create("pe.lum.rnfs", DISPATCH_QUEUE_SERIAL); + return dispatch_queue_create("pe.lum.rnfs", DISPATCH_QUEUE_SERIAL); } RCT_EXPORT_METHOD(readDir:(NSString *)dirPath resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - NSFileManager *fileManager = [NSFileManager defaultManager]; - NSError *error = nil; - - NSArray *contents = [fileManager contentsOfDirectoryAtPath:dirPath error:&error]; - - contents = [contents rnfs_mapObjectsUsingBlock:^id(NSString *obj, NSUInteger idx) { - NSString *path = [dirPath stringByAppendingPathComponent:obj]; - NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:nil]; - - return @{ - @"name": obj, - @"path": path, - @"size": [attributes objectForKey:NSFileSize], - @"type": [attributes objectForKey:NSFileType] - }; - }]; - - if (error) { - return [self reject:reject withError:error]; - } - - resolve(contents); + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSError *error = nil; + + NSArray *contents = [fileManager contentsOfDirectoryAtPath:dirPath error:&error]; + + contents = [contents rnfs_mapObjectsUsingBlock:^id(NSString *obj, NSUInteger idx) { + NSString *path = [dirPath stringByAppendingPathComponent:obj]; + NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:nil]; + + return @{ + @"name": obj, + @"path": path, + @"size": [attributes objectForKey:NSFileSize], + @"type": [attributes objectForKey:NSFileType] + }; + }]; + + if (error) { + return [self reject:reject withError:error]; + } + + resolve(contents); } RCT_EXPORT_METHOD(exists:(NSString *)filepath resolver:(RCTPromiseResolveBlock)resolve rejecter:(__unused RCTPromiseRejectBlock)reject) { - BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filepath]; - - resolve([NSNumber numberWithBool:fileExists]); + BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filepath]; + + resolve([NSNumber numberWithBool:fileExists]); } RCT_EXPORT_METHOD(stat:(NSString *)filepath resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - NSError *error = nil; - NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error]; - - if (error) { - return [self reject:reject withError:error]; - } - - attributes = @{ - @"ctime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileCreationDate]], - @"mtime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileModificationDate]], - @"size": [attributes objectForKey:NSFileSize], - @"type": [attributes objectForKey:NSFileType], - @"mode": @([[NSString stringWithFormat:@"%ld", (long)[(NSNumber *)[attributes objectForKey:NSFilePosixPermissions] integerValue]] integerValue]) - }; - - resolve(attributes); + NSError *error = nil; + NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error]; + + if (error) { + return [self reject:reject withError:error]; + } + + attributes = @{ + @"ctime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileCreationDate]], + @"mtime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileModificationDate]], + @"size": [attributes objectForKey:NSFileSize], + @"type": [attributes objectForKey:NSFileType], + @"mode": @([[NSString stringWithFormat:@"%ld", (long)[(NSNumber *)[attributes objectForKey:NSFilePosixPermissions] integerValue]] integerValue]) + }; + + resolve(attributes); } RCT_EXPORT_METHOD(writeFile:(NSString *)filepath @@ -96,15 +96,15 @@ - (dispatch_queue_t)methodQueue resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Content options:NSDataBase64DecodingIgnoreUnknownCharacters]; - - BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:nil]; - - if (!success) { - return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil); - } - - return resolve(nil); + NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Content options:NSDataBase64DecodingIgnoreUnknownCharacters]; + + BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:nil]; + + if (!success) { + return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil); + } + + return resolve(nil); } RCT_EXPORT_METHOD(appendFile:(NSString *)filepath @@ -112,52 +112,52 @@ - (dispatch_queue_t)methodQueue resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Content options:NSDataBase64DecodingIgnoreUnknownCharacters]; - - NSFileManager *fM = [NSFileManager defaultManager]; - - if (![fM fileExistsAtPath:filepath]) - { - BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:nil]; - - if (!success) { - return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil); - } else { - return resolve(nil); + NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Content options:NSDataBase64DecodingIgnoreUnknownCharacters]; + + NSFileManager *fM = [NSFileManager defaultManager]; + + if (![fM fileExistsAtPath:filepath]) + { + BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:nil]; + + if (!success) { + return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil); + } else { + return resolve(nil); + } + } + + @try { + NSFileHandle *fH = [NSFileHandle fileHandleForUpdatingAtPath:filepath]; + + [fH seekToEndOfFile]; + [fH writeData:data]; + + return resolve(nil); + } @catch (NSException *e) { + return [self reject:reject withError:e]; } - } - - @try { - NSFileHandle *fH = [NSFileHandle fileHandleForUpdatingAtPath:filepath]; - - [fH seekToEndOfFile]; - [fH writeData:data]; - - return resolve(nil); - } @catch (NSException *e) { - return [self reject:reject withError:e]; - } } RCT_EXPORT_METHOD(unlink:(NSString*)filepath resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - NSFileManager *manager = [NSFileManager defaultManager]; - BOOL exists = [manager fileExistsAtPath:filepath isDirectory:false]; - - if (!exists) { - return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil); - } - - NSError *error = nil; - BOOL success = [manager removeItemAtPath:filepath error:&error]; - - if (!success) { - return [self reject:reject withError:error]; - } - - resolve(nil); + NSFileManager *manager = [NSFileManager defaultManager]; + BOOL exists = [manager fileExistsAtPath:filepath isDirectory:false]; + + if (!exists) { + return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil); + } + + NSError *error = nil; + BOOL success = [manager removeItemAtPath:filepath error:&error]; + + if (!success) { + return [self reject:reject withError:error]; + } + + resolve(nil); } RCT_EXPORT_METHOD(mkdir:(NSString *)filepath @@ -165,55 +165,55 @@ - (dispatch_queue_t)methodQueue resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - NSFileManager *manager = [NSFileManager defaultManager]; - - NSError *error = nil; - BOOL success = [manager createDirectoryAtPath:filepath withIntermediateDirectories:YES attributes:nil error:&error]; - - if (!success) { - return [self reject:reject withError:error]; - } - - NSURL *url = [NSURL fileURLWithPath:filepath]; - - if ([[options allKeys] containsObject:@"NSURLIsExcludedFromBackupKey"]) { - NSNumber *value = options[@"NSURLIsExcludedFromBackupKey"]; - success = [url setResourceValue: value forKey: NSURLIsExcludedFromBackupKey error: &error]; - + NSFileManager *manager = [NSFileManager defaultManager]; + + NSError *error = nil; + BOOL success = [manager createDirectoryAtPath:filepath withIntermediateDirectories:YES attributes:nil error:&error]; + if (!success) { - return [self reject:reject withError:error]; + return [self reject:reject withError:error]; } - } - - resolve(nil); + + NSURL *url = [NSURL fileURLWithPath:filepath]; + + if ([[options allKeys] containsObject:@"NSURLIsExcludedFromBackupKey"]) { + NSNumber *value = options[@"NSURLIsExcludedFromBackupKey"]; + success = [url setResourceValue: value forKey: NSURLIsExcludedFromBackupKey error: &error]; + + if (!success) { + return [self reject:reject withError:error]; + } + } + + resolve(nil); } RCT_EXPORT_METHOD(readFile:(NSString *)filepath resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filepath]; - - if (!fileExists) { - return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil); - } - - NSError *error = nil; - - NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error]; - - if (error) { - return [self reject:reject withError:error]; - } - - if ([attributes objectForKey:NSFileType] == NSFileTypeDirectory) { - return reject(@"EISDIR", @"EISDIR: illegal operation on a directory, read", nil); - } - - NSData *content = [[NSFileManager defaultManager] contentsAtPath:filepath]; - NSString *base64Content = [content base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]; - - resolve(base64Content); + BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filepath]; + + if (!fileExists) { + return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil); + } + + NSError *error = nil; + + NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error]; + + if (error) { + return [self reject:reject withError:error]; + } + + if ([attributes objectForKey:NSFileType] == NSFileTypeDirectory) { + return reject(@"EISDIR", @"EISDIR: illegal operation on a directory, read", nil); + } + + NSData *content = [[NSFileManager defaultManager] contentsAtPath:filepath]; + NSString *base64Content = [content base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]; + + resolve(base64Content); } RCT_EXPORT_METHOD(hash:(NSString *)filepath @@ -221,68 +221,68 @@ - (dispatch_queue_t)methodQueue resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filepath]; - - if (!fileExists) { - return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil); - } - - NSError *error = nil; - - NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error]; - - if (error) { - return [self reject:reject withError:error]; - } - - if ([attributes objectForKey:NSFileType] == NSFileTypeDirectory) { - return reject(@"EISDIR", @"EISDIR: illegal operation on a directory, read", nil); - } - - NSData *content = [[NSFileManager defaultManager] contentsAtPath:filepath]; - - NSArray *keys = [NSArray arrayWithObjects:@"md5", @"sha1", @"sha224", @"sha256", @"sha384", @"sha512", nil]; - - NSArray *digestLengths = [NSArray arrayWithObjects: - @CC_MD5_DIGEST_LENGTH, - @CC_SHA1_DIGEST_LENGTH, - @CC_SHA224_DIGEST_LENGTH, - @CC_SHA256_DIGEST_LENGTH, - @CC_SHA384_DIGEST_LENGTH, - @CC_SHA512_DIGEST_LENGTH, - nil]; - - NSDictionary *keysToDigestLengths = [NSDictionary dictionaryWithObjects:digestLengths forKeys:keys]; - - int digestLength = [[keysToDigestLengths objectForKey:algorithm] intValue]; - - if (!digestLength) { - return reject(@"Error", [NSString stringWithFormat:@"Invalid hash algorithm '%@'", algorithm], nil); - } - - unsigned char buffer[digestLength]; - - if ([algorithm isEqualToString:@"md5"]) { - CC_MD5(content.bytes, (CC_LONG)content.length, buffer); - } else if ([algorithm isEqualToString:@"sha1"]) { - CC_SHA1(content.bytes, (CC_LONG)content.length, buffer); - } else if ([algorithm isEqualToString:@"sha224"]) { - CC_SHA224(content.bytes, (CC_LONG)content.length, buffer); - } else if ([algorithm isEqualToString:@"sha256"]) { - CC_SHA256(content.bytes, (CC_LONG)content.length, buffer); - } else if ([algorithm isEqualToString:@"sha384"]) { - CC_SHA384(content.bytes, (CC_LONG)content.length, buffer); - } else if ([algorithm isEqualToString:@"sha512"]) { - CC_SHA512(content.bytes, (CC_LONG)content.length, buffer); - } else { - return reject(@"Error", [NSString stringWithFormat:@"Invalid hash algorithm '%@'", algorithm], nil); - } - - NSMutableString *output = [NSMutableString stringWithCapacity:digestLength * 2]; - for(int i = 0; i < digestLength; i++) - [output appendFormat:@"%02x",buffer[i]]; - - resolve(output); + BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filepath]; + + if (!fileExists) { + return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil); + } + + NSError *error = nil; + + NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error]; + + if (error) { + return [self reject:reject withError:error]; + } + + if ([attributes objectForKey:NSFileType] == NSFileTypeDirectory) { + return reject(@"EISDIR", @"EISDIR: illegal operation on a directory, read", nil); + } + + NSData *content = [[NSFileManager defaultManager] contentsAtPath:filepath]; + + NSArray *keys = [NSArray arrayWithObjects:@"md5", @"sha1", @"sha224", @"sha256", @"sha384", @"sha512", nil]; + + NSArray *digestLengths = [NSArray arrayWithObjects: + @CC_MD5_DIGEST_LENGTH, + @CC_SHA1_DIGEST_LENGTH, + @CC_SHA224_DIGEST_LENGTH, + @CC_SHA256_DIGEST_LENGTH, + @CC_SHA384_DIGEST_LENGTH, + @CC_SHA512_DIGEST_LENGTH, + nil]; + + NSDictionary *keysToDigestLengths = [NSDictionary dictionaryWithObjects:digestLengths forKeys:keys]; + + int digestLength = [[keysToDigestLengths objectForKey:algorithm] intValue]; + + if (!digestLength) { + return reject(@"Error", [NSString stringWithFormat:@"Invalid hash algorithm '%@'", algorithm], nil); + } + + unsigned char buffer[digestLength]; + + if ([algorithm isEqualToString:@"md5"]) { + CC_MD5(content.bytes, (CC_LONG)content.length, buffer); + } else if ([algorithm isEqualToString:@"sha1"]) { + CC_SHA1(content.bytes, (CC_LONG)content.length, buffer); + } else if ([algorithm isEqualToString:@"sha224"]) { + CC_SHA224(content.bytes, (CC_LONG)content.length, buffer); + } else if ([algorithm isEqualToString:@"sha256"]) { + CC_SHA256(content.bytes, (CC_LONG)content.length, buffer); + } else if ([algorithm isEqualToString:@"sha384"]) { + CC_SHA384(content.bytes, (CC_LONG)content.length, buffer); + } else if ([algorithm isEqualToString:@"sha512"]) { + CC_SHA512(content.bytes, (CC_LONG)content.length, buffer); + } else { + return reject(@"Error", [NSString stringWithFormat:@"Invalid hash algorithm '%@'", algorithm], nil); + } + + NSMutableString *output = [NSMutableString stringWithCapacity:digestLength * 2]; + for(int i = 0; i < digestLength; i++) + [output appendFormat:@"%02x",buffer[i]]; + + resolve(output); } RCT_EXPORT_METHOD(moveFile:(NSString *)filepath @@ -290,16 +290,16 @@ - (dispatch_queue_t)methodQueue resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - NSFileManager *manager = [NSFileManager defaultManager]; - - NSError *error = nil; - BOOL success = [manager moveItemAtPath:filepath toPath:destPath error:&error]; - - if (!success) { - return [self reject:reject withError:error]; - } - - resolve(nil); + NSFileManager *manager = [NSFileManager defaultManager]; + + NSError *error = nil; + BOOL success = [manager moveItemAtPath:filepath toPath:destPath error:&error]; + + if (!success) { + return [self reject:reject withError:error]; + } + + resolve(nil); } RCT_EXPORT_METHOD(copyFile:(NSString *)filepath @@ -307,224 +307,224 @@ - (dispatch_queue_t)methodQueue resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - NSFileManager *manager = [NSFileManager defaultManager]; - - NSError *error = nil; - BOOL success = [manager copyItemAtPath:filepath toPath:destPath error:&error]; - - if (!success) { - return [self reject:reject withError:error]; - } - - resolve(nil); + NSFileManager *manager = [NSFileManager defaultManager]; + + NSError *error = nil; + BOOL success = [manager copyItemAtPath:filepath toPath:destPath error:&error]; + + if (!success) { + return [self reject:reject withError:error]; + } + + resolve(nil); } RCT_EXPORT_METHOD(downloadFile:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - RNFSDownloadParams* params = [RNFSDownloadParams alloc]; - - NSNumber* jobId = options[@"jobId"]; - params.fromUrl = options[@"fromUrl"]; - params.toFile = options[@"toFile"]; - NSDictionary* headers = options[@"headers"]; - params.headers = headers; - NSNumber* background = options[@"background"]; - params.background = [background boolValue]; - NSNumber* progressDivider = options[@"progressDivider"]; - params.progressDivider = progressDivider; - - params.completeCallback = ^(NSNumber* statusCode, NSNumber* bytesWritten) { - NSMutableDictionary* result = [[NSMutableDictionary alloc] initWithDictionary: @{@"jobId": jobId}]; - if (statusCode) { - [result setObject:statusCode forKey: @"statusCode"]; - } - if (bytesWritten) { - [result setObject:bytesWritten forKey: @"bytesWritten"]; - } - return resolve(result); - }; - - params.errorCallback = ^(NSError* error) { - return [self reject:reject withError:error]; - }; - - params.beginCallback = ^(NSNumber* statusCode, NSNumber* contentLength, NSDictionary* headers) { - [self.bridge.eventDispatcher sendAppEventWithName:[NSString stringWithFormat:@"DownloadBegin-%@", jobId] - body:@{@"jobId": jobId, - @"statusCode": statusCode, - @"contentLength": contentLength, - @"headers": headers}]; - }; - - params.progressCallback = ^(NSNumber* contentLength, NSNumber* bytesWritten) { - [self.bridge.eventDispatcher sendAppEventWithName:[NSString stringWithFormat:@"DownloadProgress-%@", jobId] - body:@{@"jobId": jobId, - @"contentLength": contentLength, - @"bytesWritten": bytesWritten}]; - }; - - if (!self.downloaders) self.downloaders = [[NSMutableDictionary alloc] init]; - - RNFSDownloader* downloader = [RNFSDownloader alloc]; - - [downloader downloadFile:params]; - - [self.downloaders setValue:downloader forKey:[jobId stringValue]]; + RNFSDownloadParams* params = [RNFSDownloadParams alloc]; + + NSNumber* jobId = options[@"jobId"]; + params.fromUrl = options[@"fromUrl"]; + params.toFile = options[@"toFile"]; + NSDictionary* headers = options[@"headers"]; + params.headers = headers; + NSNumber* background = options[@"background"]; + params.background = [background boolValue]; + NSNumber* progressDivider = options[@"progressDivider"]; + params.progressDivider = progressDivider; + + params.completeCallback = ^(NSNumber* statusCode, NSNumber* bytesWritten) { + NSMutableDictionary* result = [[NSMutableDictionary alloc] initWithDictionary: @{@"jobId": jobId}]; + if (statusCode) { + [result setObject:statusCode forKey: @"statusCode"]; + } + if (bytesWritten) { + [result setObject:bytesWritten forKey: @"bytesWritten"]; + } + return resolve(result); + }; + + params.errorCallback = ^(NSError* error) { + return [self reject:reject withError:error]; + }; + + params.beginCallback = ^(NSNumber* statusCode, NSNumber* contentLength, NSDictionary* headers) { + [self.bridge.eventDispatcher sendAppEventWithName:[NSString stringWithFormat:@"DownloadBegin-%@", jobId] + body:@{@"jobId": jobId, + @"statusCode": statusCode, + @"contentLength": contentLength, + @"headers": headers}]; + }; + + params.progressCallback = ^(NSNumber* contentLength, NSNumber* bytesWritten) { + [self.bridge.eventDispatcher sendAppEventWithName:[NSString stringWithFormat:@"DownloadProgress-%@", jobId] + body:@{@"jobId": jobId, + @"contentLength": contentLength, + @"bytesWritten": bytesWritten}]; + }; + + if (!self.downloaders) self.downloaders = [[NSMutableDictionary alloc] init]; + + RNFSDownloader* downloader = [RNFSDownloader alloc]; + + [downloader downloadFile:params]; + + [self.downloaders setValue:downloader forKey:[jobId stringValue]]; } RCT_EXPORT_METHOD(stopDownload:(nonnull NSNumber *)jobId) { - RNFSDownloader* downloader = [self.downloaders objectForKey:[jobId stringValue]]; - - if (downloader != nil) { - [downloader stopDownload]; - } + RNFSDownloader* downloader = [self.downloaders objectForKey:[jobId stringValue]]; + + if (downloader != nil) { + [downloader stopDownload]; + } } RCT_EXPORT_METHOD(uploadFiles:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - RNFSUploadParams* params = [RNFSUploadParams alloc]; - - NSNumber* jobId = options[@"jobId"]; - params.toUrl = options[@"toUrl"]; - params.files = options[@"files"]; - NSDictionary* headers = options[@"headers"]; - NSDictionary* fields = options[@"fields"]; - NSString* method = options[@"method"]; - params.headers = headers; - params.fields = fields; - params.method = method; - - params.completeCallback = ^(NSString* body, NSURLResponse *resp) { - - NSMutableDictionary* result = [[NSMutableDictionary alloc] initWithDictionary: @{@"jobId": jobId, - @"body": body}]; - if ([resp isKindOfClass:[NSHTTPURLResponse class]]) { - [result setValue:((NSHTTPURLResponse *)resp).allHeaderFields forKey:@"headers"]; - [result setValue:[NSNumber numberWithUnsignedInteger:((NSHTTPURLResponse *)resp).statusCode] forKey:@"statusCode"]; - } - return resolve(result); - }; - - params.errorCallback = ^(NSError* error) { - return [self reject:reject withError:error]; - }; - - params.beginCallback = ^() { - [self.bridge.eventDispatcher sendAppEventWithName:[NSString stringWithFormat:@"UploadBegin-%@", jobId] - body:@{@"jobId": jobId}]; - }; - - params.progressCallback = ^(NSNumber* totalBytesExpectedToSend, NSNumber* totalBytesSent) { - [self.bridge.eventDispatcher sendAppEventWithName:[NSString stringWithFormat:@"UploadProgress-%@", jobId] - body:@{@"jobId": jobId, - @"totalBytesExpectedToSend": totalBytesExpectedToSend, - @"totalBytesSent": totalBytesSent}]; - }; - - if (!self.uploaders) self.uploaders = [[NSMutableDictionary alloc] init]; - - RNFSUploader* uploader = [RNFSUploader alloc]; - - [uploader uploadFiles:params]; - - [self.uploaders setValue:uploader forKey:[jobId stringValue]]; + RNFSUploadParams* params = [RNFSUploadParams alloc]; + + NSNumber* jobId = options[@"jobId"]; + params.toUrl = options[@"toUrl"]; + params.files = options[@"files"]; + NSDictionary* headers = options[@"headers"]; + NSDictionary* fields = options[@"fields"]; + NSString* method = options[@"method"]; + params.headers = headers; + params.fields = fields; + params.method = method; + + params.completeCallback = ^(NSString* body, NSURLResponse *resp) { + + NSMutableDictionary* result = [[NSMutableDictionary alloc] initWithDictionary: @{@"jobId": jobId, + @"body": body}]; + if ([resp isKindOfClass:[NSHTTPURLResponse class]]) { + [result setValue:((NSHTTPURLResponse *)resp).allHeaderFields forKey:@"headers"]; + [result setValue:[NSNumber numberWithUnsignedInteger:((NSHTTPURLResponse *)resp).statusCode] forKey:@"statusCode"]; + } + return resolve(result); + }; + + params.errorCallback = ^(NSError* error) { + return [self reject:reject withError:error]; + }; + + params.beginCallback = ^() { + [self.bridge.eventDispatcher sendAppEventWithName:[NSString stringWithFormat:@"UploadBegin-%@", jobId] + body:@{@"jobId": jobId}]; + }; + + params.progressCallback = ^(NSNumber* totalBytesExpectedToSend, NSNumber* totalBytesSent) { + [self.bridge.eventDispatcher sendAppEventWithName:[NSString stringWithFormat:@"UploadProgress-%@", jobId] + body:@{@"jobId": jobId, + @"totalBytesExpectedToSend": totalBytesExpectedToSend, + @"totalBytesSent": totalBytesSent}]; + }; + + if (!self.uploaders) self.uploaders = [[NSMutableDictionary alloc] init]; + + RNFSUploader* uploader = [RNFSUploader alloc]; + + [uploader uploadFiles:params]; + + [self.uploaders setValue:uploader forKey:[jobId stringValue]]; } RCT_EXPORT_METHOD(stopUpload:(nonnull NSNumber *)jobId) { - RNFSUploader* uploader = [self.uploaders objectForKey:[jobId stringValue]]; - - if (uploader != nil) { - [uploader stopUpload]; - } + RNFSUploader* uploader = [self.uploaders objectForKey:[jobId stringValue]]; + + if (uploader != nil) { + [uploader stopUpload]; + } } RCT_EXPORT_METHOD(pathForBundle:(NSString *)bundleNamed resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - NSString *path = [[NSBundle mainBundle].bundlePath stringByAppendingFormat:@"/%@.bundle", bundleNamed]; - NSBundle *bundle = [NSBundle bundleWithPath:path]; - - if (!bundle) { - bundle = [NSBundle bundleForClass:NSClassFromString(bundleNamed)]; - path = bundle.bundlePath; - } - - if (!bundle.isLoaded) { - [bundle load]; - } - - if (path) { - resolve(path); - } else { - NSError *error = [NSError errorWithDomain:NSPOSIXErrorDomain - code:NSFileNoSuchFileError - userInfo:nil]; - - [self reject:reject withError:error]; - } + NSString *path = [[NSBundle mainBundle].bundlePath stringByAppendingFormat:@"/%@.bundle", bundleNamed]; + NSBundle *bundle = [NSBundle bundleWithPath:path]; + + if (!bundle) { + bundle = [NSBundle bundleForClass:NSClassFromString(bundleNamed)]; + path = bundle.bundlePath; + } + + if (!bundle.isLoaded) { + [bundle load]; + } + + if (path) { + resolve(path); + } else { + NSError *error = [NSError errorWithDomain:NSPOSIXErrorDomain + code:NSFileNoSuchFileError + userInfo:nil]; + + [self reject:reject withError:error]; + } } RCT_EXPORT_METHOD(getFSInfo:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - unsigned long long totalSpace = 0; - unsigned long long totalFreeSpace = 0; - - __autoreleasing NSError *error = nil; - NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); - NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error:&error]; - - if (dictionary) { - NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize]; - NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize]; - totalSpace = [fileSystemSizeInBytes unsignedLongLongValue]; - totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue]; - - resolve(@{ - @"totalSpace": [NSNumber numberWithUnsignedLongLong:totalSpace], - @"freeSpace": [NSNumber numberWithUnsignedLongLong:totalFreeSpace] - }); - } else { - [self reject:reject withError:error]; - } + unsigned long long totalSpace = 0; + unsigned long long totalFreeSpace = 0; + + __autoreleasing NSError *error = nil; + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); + NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error:&error]; + + if (dictionary) { + NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize]; + NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize]; + totalSpace = [fileSystemSizeInBytes unsignedLongLongValue]; + totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue]; + + resolve(@{ + @"totalSpace": [NSNumber numberWithUnsignedLongLong:totalSpace], + @"freeSpace": [NSNumber numberWithUnsignedLongLong:totalFreeSpace] + }); + } else { + [self reject:reject withError:error]; + } } - (NSNumber *)dateToTimeIntervalNumber:(NSDate *)date { - return @([date timeIntervalSince1970]); + return @([date timeIntervalSince1970]); } - (void)reject:(RCTPromiseRejectBlock)reject withError:(NSError *)error { - NSString *codeWithDomain = [NSString stringWithFormat:@"E%@%zd", error.domain.uppercaseString, error.code]; - reject(codeWithDomain, error.localizedDescription, error); + NSString *codeWithDomain = [NSString stringWithFormat:@"E%@%zd", error.domain.uppercaseString, error.code]; + reject(codeWithDomain, error.localizedDescription, error); } - (NSString *)getPathForDirectory:(int)directory { - NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES); - return [paths firstObject]; + NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES); + return [paths firstObject]; } - (NSDictionary *)constantsToExport { - return @{ - @"RNFSMainBundlePath": [[NSBundle mainBundle] bundlePath], - @"RNFSCachesDirectoryPath": [self getPathForDirectory:NSCachesDirectory], - @"RNFSDocumentDirectoryPath": [self getPathForDirectory:NSDocumentDirectory], - @"RNFSExternalDirectoryPath": [NSNull null], - @"RNFSExternalStorageDirectoryPath": [NSNull null], - @"RNFSTemporaryDirectoryPath": NSTemporaryDirectory(), - @"RNFSLibraryDirectoryPath": [self getPathForDirectory:NSLibraryDirectory], - @"RNFSFileTypeRegular": NSFileTypeRegular, - @"RNFSFileTypeDirectory": NSFileTypeDirectory - }; + return @{ + @"RNFSMainBundlePath": [[NSBundle mainBundle] bundlePath], + @"RNFSCachesDirectoryPath": [self getPathForDirectory:NSCachesDirectory], + @"RNFSDocumentDirectoryPath": [self getPathForDirectory:NSDocumentDirectory], + @"RNFSExternalDirectoryPath": [NSNull null], + @"RNFSExternalStorageDirectoryPath": [NSNull null], + @"RNFSTemporaryDirectoryPath": NSTemporaryDirectory(), + @"RNFSLibraryDirectoryPath": [self getPathForDirectory:NSLibraryDirectory], + @"RNFSFileTypeRegular": NSFileTypeRegular, + @"RNFSFileTypeDirectory": NSFileTypeDirectory + }; } @end From f69f172bbd11a7e887de613ecca7fce744c65307 Mon Sep 17 00:00:00 2001 From: Dmitriy Loktev Date: Mon, 26 Dec 2016 17:37:06 +0300 Subject: [PATCH 2/2] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 99c91ea2..6fb0f7d5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-fs", - "version": "2.0.1-rc.2", + "version": "3.0.1-rc.1", "description": "Native filesystem access for react-native", "main": "FS.common.js", "scripts": {