Skip to content

Commit 1f9a176

Browse files
authored
Merge pull request #8 from flatfox-ag/exception_fixes
Synchronized tables, separated requests and network logic and other...
2 parents b41cfb7 + 01f38a4 commit 1f9a176

16 files changed

+737
-639
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ RNFetchBlob
236236
console.log('The file saved to ', res.path())
237237
// Beware that when using a file path as Image source on Android,
238238
// you must prepend "file://"" before the file path
239-
imageView = <Image source={{ uri : Platform.OS === 'android' ? 'file://' + res.path() : '' + res.path() }}/>
239+
imageView = <Image source={{ uri : Platform.OS === 'android' ? 'file://' + res.path() : '' + res.path() }}/>
240240
})
241241
```
242242

android.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,25 @@ function addCompleteDownload(config) {
3838
return Promise.reject('RNFetchBlob.android.addCompleteDownload only supports Android.')
3939
}
4040

41+
function getSDCardDir() {
42+
if(Platform.OS === 'android')
43+
return RNFetchBlob.getSDCardDir()
44+
else
45+
return Promise.reject('RNFetchBlob.android.getSDCardDir only supports Android.')
46+
}
47+
48+
function getSDCardApplicationDir() {
49+
if(Platform.OS === 'android')
50+
return RNFetchBlob.getSDCardApplicationDir()
51+
else
52+
return Promise.reject('RNFetchBlob.android.getSDCardApplicationDir only supports Android.')
53+
}
54+
4155

4256
export default {
4357
actionViewIntent,
4458
getContentIntent,
45-
addCompleteDownload
59+
addCompleteDownload,
60+
getSDCardDir,
61+
getSDCardApplicationDir,
4662
}

android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ public void run() {
100100
RNFetchBlobFS.createFileASCII(path, dataArray, promise);
101101
}
102102
});
103-
104103
}
105104

106105
@ReactMethod
@@ -164,7 +163,6 @@ public void run() {
164163
RNFetchBlobFS.cp(path, dest, callback);
165164
}
166165
});
167-
168166
}
169167

170168
@ReactMethod
@@ -225,7 +223,6 @@ public void run() {
225223
RNFetchBlobFS.writeFile(path, encoding, data, append, promise);
226224
}
227225
});
228-
229226
}
230227

231228
@ReactMethod
@@ -260,7 +257,6 @@ public void run() {
260257
new RNFetchBlobFS(ctx).scanFile(p, m, callback);
261258
}
262259
});
263-
264260
}
265261

266262
@ReactMethod
@@ -331,7 +327,7 @@ public void enableUploadProgressReport(String taskId, int interval, int count) {
331327
@ReactMethod
332328
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
333329
new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
334-
}
330+
}
335331

336332
@ReactMethod
337333
public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
@@ -377,4 +373,13 @@ public void addCompleteDownload (ReadableMap config, Promise promise) {
377373

378374
}
379375

376+
@ReactMethod
377+
public void getSDCardDir(Promise promise) {
378+
RNFetchBlobFS.getSDCardDir(promise);
379+
}
380+
381+
@ReactMethod
382+
public void getSDCardApplicationDir(Promise promise) {
383+
RNFetchBlobFS.getSDCardApplicationDir(this.getReactApplicationContext(), promise);
384+
}
380385
}

android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,38 @@ static Map<String, Object> getSystemfolders(ReactApplicationContext ctx) {
246246
state = Environment.getExternalStorageState();
247247
if (state.equals(Environment.MEDIA_MOUNTED)) {
248248
res.put("SDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath());
249-
res.put("SDCardApplicationDir", ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath());
249+
try {
250+
res.put("SDCardApplicationDir", ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath());
251+
} catch(Exception e) {
252+
res.put("SDCardApplicationDir", "");
253+
}
250254
}
251255
res.put("MainBundleDir", ctx.getApplicationInfo().dataDir);
252256
return res;
253257
}
254258

259+
static public void getSDCardDir(Promise promise) {
260+
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
261+
promise.resolve(Environment.getExternalStorageDirectory().getAbsolutePath());
262+
} else {
263+
promise.reject("RNFetchBlob.getSDCardDir", "External storage not mounted");
264+
}
265+
266+
}
267+
268+
static public void getSDCardApplicationDir(ReactApplicationContext ctx, Promise promise) {
269+
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
270+
try {
271+
final String path = ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath();
272+
promise.resolve(path);
273+
} catch (Exception e) {
274+
promise.reject("RNFetchBlob.getSDCardApplicationDir", e.getLocalizedMessage());
275+
}
276+
} else {
277+
promise.reject("RNFetchBlob.getSDCardApplicationDir", "External storage not mounted");
278+
}
279+
}
280+
255281
/**
256282
* Static method that returns a temp file path
257283
* @param taskId An unique string for identify

fs.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,26 @@ import RNFetchBlobFile from './class/RNFetchBlobFile'
1313
const RNFetchBlob: RNFetchBlobNative = NativeModules.RNFetchBlob
1414

1515
const dirs = {
16-
DocumentDir: RNFetchBlob.DocumentDir,
17-
CacheDir: RNFetchBlob.CacheDir,
18-
PictureDir: RNFetchBlob.PictureDir,
19-
MusicDir: RNFetchBlob.MusicDir,
20-
MovieDir: RNFetchBlob.MovieDir,
21-
DownloadDir: RNFetchBlob.DownloadDir,
22-
DCIMDir: RNFetchBlob.DCIMDir,
23-
SDCardDir: RNFetchBlob.SDCardDir,
24-
SDCardApplicationDir: RNFetchBlob.SDCardApplicationDir,
25-
MainBundleDir: RNFetchBlob.MainBundleDir,
26-
LibraryDir: RNFetchBlob.LibraryDir
16+
DocumentDir : RNFetchBlob.DocumentDir,
17+
CacheDir : RNFetchBlob.CacheDir,
18+
PictureDir : RNFetchBlob.PictureDir,
19+
MusicDir : RNFetchBlob.MusicDir,
20+
MovieDir : RNFetchBlob.MovieDir,
21+
DownloadDir : RNFetchBlob.DownloadDir,
22+
DCIMDir : RNFetchBlob.DCIMDir,
23+
get SDCardDir() {
24+
console.warn('SDCardDir as a constant is deprecated and will be removed in feature release. ' +
25+
'Use RNFetchBlob.android.getSDCardDir():Promise instead.');
26+
return RNFetchBlob.SDCardDir;
27+
},
28+
get SDCardApplicationDir() {
29+
console.warn('SDCardApplicationDir as a constant is deprecated and will be removed in feature release. ' +
30+
'Use RNFetchBlob.android.getSDCardApplicationDir():Promise instead. ' +
31+
'This variable can be empty on error in native code.');
32+
return RNFetchBlob.SDCardApplicationDir;
33+
},
34+
MainBundleDir : RNFetchBlob.MainBundleDir,
35+
LibraryDir : RNFetchBlob.LibraryDir
2736
}
2837

2938
function addCode(code: string, error: Error): Error {

ios/RNFetchBlob.xcodeproj/project.pbxproj

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
8C4801A6200CF71700FED7ED /* RNFetchBlobRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 8C4801A5200CF71700FED7ED /* RNFetchBlobRequest.m */; };
1011
A158F4271D052E49006FFD38 /* RNFetchBlobFS.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F4261D052E49006FFD38 /* RNFetchBlobFS.m */; };
1112
A158F42D1D0535BB006FFD38 /* RNFetchBlobConst.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42C1D0535BB006FFD38 /* RNFetchBlobConst.m */; };
1213
A158F4301D0539DB006FFD38 /* RNFetchBlobNetwork.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */; };
@@ -29,6 +30,8 @@
2930
/* End PBXCopyFilesBuildPhase section */
3031

3132
/* Begin PBXFileReference section */
33+
8C4801A4200CF71700FED7ED /* RNFetchBlobRequest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobRequest.h; sourceTree = "<group>"; };
34+
8C4801A5200CF71700FED7ED /* RNFetchBlobRequest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobRequest.m; sourceTree = "<group>"; };
3235
A158F4261D052E49006FFD38 /* RNFetchBlobFS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobFS.m; sourceTree = "<group>"; };
3336
A158F4281D052E57006FFD38 /* RNFetchBlobFS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobFS.h; sourceTree = "<group>"; };
3437
A158F4291D0534A9006FFD38 /* RNFetchBlobConst.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobConst.h; sourceTree = "<group>"; };
@@ -71,8 +74,10 @@
7174
A1F950181D7E9134002A95A6 /* IOS7Polyfill.h */,
7275
A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */,
7376
A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */,
74-
A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */,
7577
A158F42E1D0539CE006FFD38 /* RNFetchBlobNetwork.h */,
78+
A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */,
79+
8C4801A4200CF71700FED7ED /* RNFetchBlobRequest.h */,
80+
8C4801A5200CF71700FED7ED /* RNFetchBlobRequest.m */,
7681
A158F42C1D0535BB006FFD38 /* RNFetchBlobConst.m */,
7782
A158F4291D0534A9006FFD38 /* RNFetchBlobConst.h */,
7883
A158F4281D052E57006FFD38 /* RNFetchBlobFS.h */,
@@ -149,6 +154,7 @@
149154
buildActionMask = 2147483647;
150155
files = (
151156
A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */,
157+
8C4801A6200CF71700FED7ED /* RNFetchBlobRequest.m in Sources */,
152158
A158F42D1D0535BB006FFD38 /* RNFetchBlobConst.m in Sources */,
153159
A158F4271D052E49006FFD38 /* RNFetchBlobFS.m in Sources */,
154160
A158F4301D0539DB006FFD38 /* RNFetchBlobNetwork.m in Sources */,

ios/RNFetchBlob/RNFetchBlob.h

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
@property (retain) UIDocumentInteractionController * documentController;
4040

4141
+ (RCTBridge *)getRCTBridge;
42-
+ (void) checkExpiredSessions;
4342

4443
@end
4544

ios/RNFetchBlob/RNFetchBlob.m

+16-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ - (dispatch_queue_t) methodQueue {
3838

3939
+ (RCTBridge *)getRCTBridge
4040
{
41-
RCTRootView * rootView = [[UIApplication sharedApplication] keyWindow].rootViewController.view;
41+
RCTRootView * rootView = (RCTRootView*) [[UIApplication sharedApplication] keyWindow].rootViewController.view;
4242
return rootView.bridge;
4343
}
4444

@@ -101,8 +101,12 @@ + (BOOL)requiresMainQueueSetup
101101
// send HTTP request
102102
else
103103
{
104-
RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
105-
[utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
104+
[[RNFetchBlobNetwork sharedInstance] sendRequest:options
105+
contentLength:bodyLength
106+
bridge:self.bridge
107+
taskId:taskId
108+
withRequest:req
109+
callback:callback];
106110
}
107111
}];
108112

@@ -133,8 +137,12 @@ + (BOOL)requiresMainQueueSetup
133137
// send HTTP request
134138
else
135139
{
136-
__block RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
137-
[utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
140+
[[RNFetchBlobNetwork sharedInstance] sendRequest:options
141+
contentLength:bodyLength
142+
bridge:self.bridge
143+
taskId:taskId
144+
withRequest:req
145+
callback:callback];
138146
}
139147
}];
140148
}
@@ -523,7 +531,7 @@ + (BOOL)requiresMainQueueSetup
523531

524532
#pragma mark - net.cancelRequest
525533
RCT_EXPORT_METHOD(cancelRequest:(NSString *)taskId callback:(RCTResponseSenderBlock)callback) {
526-
[RNFetchBlobNetwork cancelRequest:taskId];
534+
[[RNFetchBlobNetwork sharedInstance] cancelRequest:taskId];
527535
callback(@[[NSNull null], taskId]);
528536

529537
}
@@ -533,14 +541,14 @@ + (BOOL)requiresMainQueueSetup
533541
{
534542

535543
RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Download interval:interval count:count];
536-
[RNFetchBlobNetwork enableProgressReport:taskId config:cfg];
544+
[[RNFetchBlobNetwork sharedInstance] enableProgressReport:taskId config:cfg];
537545
}
538546

539547
#pragma mark - net.enableUploadProgressReport
540548
RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId interval:(nonnull NSNumber*)interval count:(nonnull NSNumber*)count)
541549
{
542550
RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Upload interval:interval count:count];
543-
[RNFetchBlobNetwork enableUploadProgress:taskId config:cfg];
551+
[[RNFetchBlobNetwork sharedInstance] enableUploadProgress:taskId config:cfg];
544552
}
545553

546554
#pragma mark - fs.slice

ios/RNFetchBlobConst.m

+28-28
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,38 @@
77
//
88
#import "RNFetchBlobConst.h"
99

10-
extern NSString *const FILE_PREFIX = @"RNFetchBlob-file://";
11-
extern NSString *const ASSET_PREFIX = @"bundle-assets://";
12-
extern NSString *const AL_PREFIX = @"assets-library://";
10+
NSString *const FILE_PREFIX = @"RNFetchBlob-file://";
11+
NSString *const ASSET_PREFIX = @"bundle-assets://";
12+
NSString *const AL_PREFIX = @"assets-library://";
1313

1414
// fetch configs
15-
extern NSString *const CONFIG_USE_TEMP = @"fileCache";
16-
extern NSString *const CONFIG_FILE_PATH = @"path";
17-
extern NSString *const CONFIG_FILE_EXT = @"appendExt";
18-
extern NSString *const CONFIG_TRUSTY = @"trusty";
19-
extern NSString *const CONFIG_INDICATOR = @"indicator";
20-
extern NSString *const CONFIG_KEY = @"key";
21-
extern NSString *const CONFIG_EXTRA_BLOB_CTYPE = @"binaryContentTypes";
15+
NSString *const CONFIG_USE_TEMP = @"fileCache";
16+
NSString *const CONFIG_FILE_PATH = @"path";
17+
NSString *const CONFIG_FILE_EXT = @"appendExt";
18+
NSString *const CONFIG_TRUSTY = @"trusty";
19+
NSString *const CONFIG_INDICATOR = @"indicator";
20+
NSString *const CONFIG_KEY = @"key";
21+
NSString *const CONFIG_EXTRA_BLOB_CTYPE = @"binaryContentTypes";
2222

23-
extern NSString *const EVENT_STATE_CHANGE = @"RNFetchBlobState";
24-
extern NSString *const EVENT_SERVER_PUSH = @"RNFetchBlobServerPush";
25-
extern NSString *const EVENT_PROGRESS = @"RNFetchBlobProgress";
26-
extern NSString *const EVENT_PROGRESS_UPLOAD = @"RNFetchBlobProgress-upload";
27-
extern NSString *const EVENT_EXPIRE = @"RNFetchBlobExpire";
23+
NSString *const EVENT_STATE_CHANGE = @"RNFetchBlobState";
24+
NSString *const EVENT_SERVER_PUSH = @"RNFetchBlobServerPush";
25+
NSString *const EVENT_PROGRESS = @"RNFetchBlobProgress";
26+
NSString *const EVENT_PROGRESS_UPLOAD = @"RNFetchBlobProgress-upload";
27+
NSString *const EVENT_EXPIRE = @"RNFetchBlobExpire";
2828

29-
extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";
30-
extern NSString *const MSG_EVENT_LOG = @"log";
31-
extern NSString *const MSG_EVENT_WARN = @"warn";
32-
extern NSString *const MSG_EVENT_ERROR = @"error";
33-
extern NSString *const FS_EVENT_DATA = @"data";
34-
extern NSString *const FS_EVENT_END = @"end";
35-
extern NSString *const FS_EVENT_WARN = @"warn";
36-
extern NSString *const FS_EVENT_ERROR = @"error";
29+
NSString *const MSG_EVENT = @"RNFetchBlobMessage";
30+
NSString *const MSG_EVENT_LOG = @"log";
31+
NSString *const MSG_EVENT_WARN = @"warn";
32+
NSString *const MSG_EVENT_ERROR = @"error";
33+
NSString *const FS_EVENT_DATA = @"data";
34+
NSString *const FS_EVENT_END = @"end";
35+
NSString *const FS_EVENT_WARN = @"warn";
36+
NSString *const FS_EVENT_ERROR = @"error";
3737

38-
extern NSString *const KEY_REPORT_PROGRESS = @"reportProgress";
39-
extern NSString *const KEY_REPORT_UPLOAD_PROGRESS = @"reportUploadProgress";
38+
NSString *const KEY_REPORT_PROGRESS = @"reportProgress";
39+
NSString *const KEY_REPORT_UPLOAD_PROGRESS = @"reportUploadProgress";
4040

4141
// response type
42-
extern NSString *const RESP_TYPE_BASE64 = @"base64";
43-
extern NSString *const RESP_TYPE_UTF8 = @"utf8";
44-
extern NSString *const RESP_TYPE_PATH = @"path";
42+
NSString *const RESP_TYPE_BASE64 = @"base64";
43+
NSString *const RESP_TYPE_UTF8 = @"utf8";
44+
NSString *const RESP_TYPE_PATH = @"path";

ios/RNFetchBlobFS.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
NSString * streamId;
3535
}
3636

37-
@property (nonatomic) NSOutputStream * outStream;
38-
@property (nonatomic) NSInputStream * inStream;
37+
@property (nonatomic) NSOutputStream * _Nullable outStream;
38+
@property (nonatomic) NSInputStream * _Nullable inStream;
3939
@property (strong, nonatomic) RCTResponseSenderBlock callback;
4040
@property (nonatomic) RCTBridge * bridge;
4141
@property (nonatomic) NSString * encoding;

0 commit comments

Comments
 (0)