Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 23eee76

Browse files
committed
Fix IOS ALAsset upload issue #45
1 parent 271d9f5 commit 23eee76

File tree

9 files changed

+330
-202
lines changed

9 files changed

+330
-202
lines changed

src/ios/RNFetchBlob.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
A158F4301D0539DB006FFD38 /* RNFetchBlobNetwork.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */; };
1313
A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */ = {isa = PBXBuildFile; fileRef = A15C30131CD25C330074CB35 /* RNFetchBlob.m */; };
1414
A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */ = {isa = PBXBuildFile; fileRef = A15C30111CD25C330074CB35 /* RNFetchBlob.h */; };
15+
A1AAE2991D300E4D0051D11C /* RNFetchBlobReqBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */; };
1516
/* End PBXBuildFile section */
1617

1718
/* Begin PBXCopyFilesBuildPhase section */
@@ -36,6 +37,8 @@
3637
A15C300E1CD25C330074CB35 /* libRNFetchBlob.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNFetchBlob.a; sourceTree = BUILT_PRODUCTS_DIR; };
3738
A15C30111CD25C330074CB35 /* RNFetchBlob.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = RNFetchBlob.h; path = RNFetchBlob/RNFetchBlob.h; sourceTree = "<group>"; };
3839
A15C30131CD25C330074CB35 /* RNFetchBlob.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNFetchBlob.m; path = RNFetchBlob/RNFetchBlob.m; sourceTree = "<group>"; };
40+
A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobReqBuilder.h; sourceTree = "<group>"; };
41+
A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobReqBuilder.m; sourceTree = "<group>"; };
3942
/* End PBXFileReference section */
4043

4144
/* Begin PBXFrameworksBuildPhase section */
@@ -59,6 +62,8 @@
5962
A15C30051CD25C330074CB35 = {
6063
isa = PBXGroup;
6164
children = (
65+
A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */,
66+
A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */,
6267
A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */,
6368
A158F42E1D0539CE006FFD38 /* RNFetchBlobNetwork.h */,
6469
A158F42C1D0535BB006FFD38 /* RNFetchBlobConst.m */,
@@ -140,6 +145,7 @@
140145
A158F42D1D0535BB006FFD38 /* RNFetchBlobConst.m in Sources */,
141146
A158F4271D052E49006FFD38 /* RNFetchBlobFS.m in Sources */,
142147
A158F4301D0539DB006FFD38 /* RNFetchBlobNetwork.m in Sources */,
148+
A1AAE2991D300E4D0051D11C /* RNFetchBlobReqBuilder.m in Sources */,
143149
A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */,
144150
);
145151
runOnlyForDeploymentPostprocessing = 0;

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 11 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import "RNFetchBlobFS.h"
1313
#import "RNFetchBlobNetwork.h"
1414
#import "RNFetchBlobConst.h"
15+
#import "RNFetchBlobReqBuilder.h"
1516

1617

1718
////////////////////////////////////////
@@ -61,78 +62,14 @@ - (NSDictionary *)constantsToExport
6162
form:(NSArray *)form
6263
callback:(RCTResponseSenderBlock)callback)
6364
{
64-
NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
65-
// send request
66-
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
67-
initWithURL:[NSURL
68-
URLWithString: encodedUrl]];
6965

70-
NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[ RNFetchBlobNetwork normalizeHeaders:headers]];
71-
72-
73-
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
74-
NSNumber * timeStampObj = [NSNumber numberWithDouble: timeStamp];
75-
76-
// generate boundary
77-
NSString * boundary = [NSString stringWithFormat:@"RNFetchBlob%d", timeStampObj];
78-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
79-
NSMutableData * postData = [[NSMutableData alloc] init];
80-
// if method is POST or PUT, convert data string format
81-
if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"]) {
82-
83-
// combine multipart/form-data body
84-
for(id field in form) {
85-
NSString * name = [field valueForKey:@"name"];
86-
NSString * content = [field valueForKey:@"data"];
87-
// field is a text field
88-
if([field valueForKey:@"filename"] == nil || content == [NSNull null]) {
89-
[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
90-
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
91-
[postData appendData:[[NSString stringWithFormat:@"Content-Type: text/plain\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
92-
[postData appendData:[[NSString stringWithFormat:@"%@\r\n", content] dataUsingEncoding:NSUTF8StringEncoding]];
93-
}
94-
// field contains a file
95-
else {
96-
NSMutableData * blobData;
97-
if(content != nil) {
98-
if([content hasPrefix:self.filePathPrefix]) {
99-
NSString * orgPath = [content substringFromIndex:[self.filePathPrefix length]];
100-
orgPath = [RNFetchBlobFS getPathOfAsset:orgPath];
101-
blobData = [[NSData alloc] initWithContentsOfFile:orgPath];
102-
}
103-
else
104-
blobData = [[NSData alloc] initWithBase64EncodedString:content options:0];
105-
}
106-
NSString * filename = [field valueForKey:@"filename"];
107-
[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
108-
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename] dataUsingEncoding:NSUTF8StringEncoding]];
109-
[postData appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
110-
[postData appendData:blobData];
111-
[postData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
112-
}
113-
114-
}
115-
116-
// close form data
117-
[postData appendData: [[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
118-
[request setHTTPBody:postData];
119-
// set content-length
120-
[mheaders setValue:[NSString stringWithFormat:@"%d",[postData length]] forKey:@"Content-Length"];
121-
[mheaders setValue:[NSString stringWithFormat:@"100-continue",[postData length]] forKey:@"Expect"];
122-
// appaned boundary to content-type
123-
[mheaders setValue:[NSString stringWithFormat:@"multipart/form-data; charset=utf-8; boundary=%@", boundary] forKey:@"content-type"];
124-
125-
}
126-
127-
[request setHTTPMethod: method];
128-
[request setAllHTTPHeaderFields:mheaders];
129-
130-
66+
[RNFetchBlobReqBuilder buildMultipartRequest:options taskId:taskId method:method url:url headers:headers form:form onComplete:^(NSURLRequest *req) {
13167
// send HTTP request
13268
RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
133-
[utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request callback:callback];
69+
[utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:req callback:callback];
13470
utils = nil;
135-
});
71+
}];
72+
13673
}
13774

13875
// Fetch blob data request
@@ -143,46 +80,13 @@ - (NSDictionary *)constantsToExport
14380
headers:(NSDictionary *)headers
14481
body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
14582
{
146-
NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
147-
// send request
148-
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
149-
initWithURL:[NSURL
150-
URLWithString: encodedUrl]];
151-
152-
NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[RNFetchBlobNetwork normalizeHeaders:headers]];
153-
// move heavy task to another thread
154-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
155-
NSMutableData * blobData;
156-
// if method is POST or PUT, convert data string format
157-
if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"]) {
158-
// generate octet-stream body
159-
if(body != nil) {
160-
161-
// when body is a string contains file path prefix, try load file from the path
162-
if([body hasPrefix:self.filePathPrefix]) {
163-
NSString * orgPath = [body substringFromIndex:[self.filePathPrefix length]];
164-
orgPath = [RNFetchBlobFS getPathOfAsset:orgPath];
165-
[request setHTTPBodyStream: [NSInputStream inputStreamWithFileAtPath:orgPath ]];
166-
}
167-
// otherwise convert it as BASE64 data string
168-
else {
169-
blobData = [[NSData alloc] initWithBase64EncodedString:body options:0];
170-
[request setHTTPBody:blobData];
171-
}
172-
173-
[mheaders setValue:@"application/octet-stream" forKey:@"content-type"];
174-
175-
}
176-
}
177-
178-
[request setHTTPMethod: method];
179-
[request setAllHTTPHeaderFields:mheaders];
180-
83+
[RNFetchBlobReqBuilder buildOctetRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req) {
18184
// send HTTP request
18285
RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
183-
[utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request callback:callback];
86+
[utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:req callback:callback];
18487
utils = nil;
185-
});
88+
}];
89+
18690
}
18791

18892
RCT_EXPORT_METHOD(createFile:(NSString *)path data:(NSString *)data encoding:(NSString *)encoding callback:(RCTResponseSenderBlock)callback) {
@@ -419,7 +323,7 @@ - (NSDictionary *)constantsToExport
419323

420324
RCT_EXPORT_METHOD(readFile:(NSString *)path encoding:(NSString *)encoding resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
421325

422-
[RNFetchBlobFS readFile:path encoding:encoding resolver:resolve rejecter:reject];
326+
[RNFetchBlobFS readFile:path encoding:encoding resolver:resolve rejecter:reject onComplete:nil];
423327
})
424328

425329
RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding bufferSize:(int)bufferSize) {
@@ -432,14 +336,7 @@ - (NSDictionary *)constantsToExport
432336
bufferSize = 4096;
433337
}
434338
// read asset stream
435-
if([path hasPrefix:@"assets-library://"])
436-
{
437-
438-
}
439-
else
440-
{
441-
[fileStream readWithPath:path useEncoding:encoding bufferSize:bufferSize];
442-
}
339+
[fileStream readWithPath:path useEncoding:encoding bufferSize:bufferSize];
443340
}
444341

445342
RCT_EXPORT_METHOD(getEnvironmentDirs:(RCTResponseSenderBlock) callback) {

src/ios/RNFetchBlobConst.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ extern NSString *const MSG_EVENT;
1616
extern NSString *const MSG_EVENT_LOG;
1717
extern NSString *const MSG_EVENT_WARN;
1818
extern NSString *const MSG_EVENT_ERROR;
19+
1920
extern NSString *const FILE_PREFIX;
21+
extern NSString *const ASSET_PREFIX;
22+
extern NSString *const AL_PREFIX;
2023

2124
// config
2225
extern NSString *const CONFIG_USE_TEMP;

src/ios/RNFetchBlobConst.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#import "RNFetchBlobConst.h"
99

1010
extern NSString *const FILE_PREFIX = @"RNFetchBlob-file://";
11+
extern NSString *const ASSET_PREFIX = @"bundle-assets://";
12+
extern NSString *const AL_PREFIX = @"assets-library://";
13+
14+
1115

1216
// fetch configs
1317
extern NSString *const CONFIG_USE_TEMP = @"fileCache";

src/ios/RNFetchBlobFS.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#import <Foundation/Foundation.h>
1313
#import "RCTBridgeModule.h"
14+
@import AssetsLibrary;
1415

1516
@interface RNFetchBlobFS : NSObject <NSStreamDelegate> {
1617
NSOutputStream * outStream;
@@ -42,6 +43,8 @@
4243
+ (NSString *) getCacheDir;
4344
+ (NSString *) getDocumentDir;
4445
+ (NSString *) getTempPath:(NSString*)taskId withExtension:(NSString *)ext;
46+
+ (NSString *) getPathOfAsset:(NSString *)assetURI;
47+
+ (void) getPathFromUri:(NSString *)uri completionHandler:(void(^)(NSString * path, ALAssetRepresentation *asset)) onComplete;
4548

4649
// fs methods
4750
+ (RNFetchBlobFS *) getFileStreams;
@@ -50,9 +53,8 @@
5053
+ (BOOL) exists:(NSString *) path;
5154
+ (void) writeFileArray:(NSString *)path data:(NSArray *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
5255
+ (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
53-
+ (void) readFile:(NSString *)path encoding:(NSString *)encoding resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
56+
+ (void) readFile:(NSString *)path encoding:(NSString *)encoding resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject onComplete:(void (^)(NSData * content))onComplete;
5457
+ (void) readAssetFile:(NSData *)assetUrl completionBlock:(void(^)(NSData * content))completionBlock failBlock:(void(^)(NSError * err))failBlock;
55-
+ (NSString *) getPathOfAsset:(NSString *)assetURI;
5658

5759
// constructor
5860
- (id) init;

0 commit comments

Comments
 (0)