react-native-blob-util 0.22.0 → 0.22.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +56 -56
- package/LICENSE +21 -21
- package/Migration.md +41 -41
- package/NuGet.config +11 -11
- package/README.md +1109 -1104
- package/android/gradle.properties +1 -1
- package/android/src/main/AndroidManifest.xml +37 -37
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConfig.java +57 -57
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConst.java +20 -20
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFileTransformer.java +9 -9
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilImpl.java +431 -431
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilProgressConfig.java +39 -39
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java +1014 -1014
- package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilDefaultResp.java +98 -98
- package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilFileResp.java +166 -166
- package/android/src/main/java/com/ReactNativeBlobUtil/Utils/FileDescription.java +19 -19
- package/android/src/main/java/com/ReactNativeBlobUtil/Utils/FileProvider.java +4 -4
- package/android/src/main/java/com/ReactNativeBlobUtil/Utils/MimeType.java +70 -70
- package/android/src/main/java/com/ReactNativeBlobUtil/Utils/PathResolver.java +223 -223
- package/android/src/main/java/com/ReactNativeBlobUtil/Utils/Tls12SocketFactory.java +68 -68
- package/android/src/main/res/values/strings.xml +3 -3
- package/android/src/main/res/xml/provider_paths.xml +12 -12
- package/android.js +58 -58
- package/class/ReactNativeBlobUtilCanceledFetchError.js +9 -9
- package/class/ReactNativeBlobUtilFile.js +5 -5
- package/class/ReactNativeBlobUtilReadStream.js +84 -84
- package/class/ReactNativeBlobUtilSession.js +68 -68
- package/class/ReactNativeBlobUtilWriteStream.js +50 -50
- package/class/StatefulPromise.js +7 -7
- package/codegenSpecs/NativeBlobUtils.js +86 -86
- package/components/Fetch.onPress.js +10 -10
- package/components/Fetch.when.js +15 -15
- package/fetch.js +350 -350
- package/fs.js +480 -480
- package/index.js +64 -64
- package/index.js.flow +192 -192
- package/index.web.js +1 -1
- package/ios/PrivacyInfo.xcprivacy +28 -28
- package/ios/ReactNativeBlobUtil/ReactNativeBlobUtil.h +55 -55
- package/ios/ReactNativeBlobUtil/ReactNativeBlobUtil.mm +992 -992
- package/ios/ReactNativeBlobUtil.xcodeproj/project.pbxproj +405 -405
- package/ios/ReactNativeBlobUtilConst.h +58 -58
- package/ios/ReactNativeBlobUtilConst.mm +47 -47
- package/ios/ReactNativeBlobUtilFS.h +108 -108
- package/ios/ReactNativeBlobUtilFS.mm +1064 -1064
- package/ios/ReactNativeBlobUtilFileTransformer.h +23 -23
- package/ios/ReactNativeBlobUtilFileTransformer.mm +20 -20
- package/ios/ReactNativeBlobUtilNetwork.h +49 -49
- package/ios/ReactNativeBlobUtilNetwork.mm +159 -159
- package/ios/ReactNativeBlobUtilProgress.h +32 -32
- package/ios/ReactNativeBlobUtilProgress.mm +57 -57
- package/ios/ReactNativeBlobUtilReqBuilder.h +37 -37
- package/ios/ReactNativeBlobUtilReqBuilder.mm +292 -292
- package/ios/ReactNativeBlobUtilRequest.h +48 -48
- package/ios/ReactNativeBlobUtilRequest.mm +584 -584
- package/ios.js +61 -61
- package/json-stream.js +42 -42
- package/lib/oboe-browser.js +2703 -2703
- package/mediacollection.js +36 -36
- package/package.json +68 -68
- package/polyfill/Blob.js +362 -362
- package/polyfill/Event.js +11 -11
- package/polyfill/EventTarget.js +78 -78
- package/polyfill/Fetch.js +219 -219
- package/polyfill/File.js +27 -27
- package/polyfill/FileReader.js +91 -91
- package/polyfill/ProgressEvent.js +32 -32
- package/polyfill/XMLHttpRequest.js +466 -466
- package/polyfill/XMLHttpRequestEventTarget.js +126 -126
- package/polyfill/index.js +11 -11
- package/react-native-blob-util.podspec +48 -48
- package/types.js +69 -69
- package/utils/log.js +40 -40
- package/utils/unicode.js +7 -7
- package/utils/uri.js +33 -33
- package/utils/uuid.js +4 -4
- package/windows/ExperimentalFeatures.props +33 -33
- package/windows/README.md +2 -2
- package/windows/ReactNativeBlobUtil/PropertySheet.props +15 -15
- package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.cpp +1836 -1836
- package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.def +3 -3
- package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.h +372 -372
- package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.vcxproj +139 -139
- package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.vcxproj.filters +43 -43
- package/windows/ReactNativeBlobUtil/ReactPackageProvider.cpp +20 -20
- package/windows/ReactNativeBlobUtil/ReactPackageProvider.h +24 -24
- package/windows/ReactNativeBlobUtil/ReactPackageProvider.idl +9 -9
- package/windows/ReactNativeBlobUtil/codegen/.clang-format +1 -1
- package/windows/ReactNativeBlobUtil/codegen/NativeBlobUtilsDataTypes.g.h +42 -42
- package/windows/ReactNativeBlobUtil/codegen/NativeBlobUtilsSpec.g.h +353 -353
- package/windows/ReactNativeBlobUtil/packages.config +3 -3
- package/windows/ReactNativeBlobUtil/packages.lock.json +59 -59
- package/windows/ReactNativeBlobUtil/pch.cpp +1 -1
- package/windows/ReactNativeBlobUtil/pch.h +30 -30
- package/windows/ReactNativeBlobUtil/resource.h +5 -5
- package/windows/ReactNativeBlobUtil/targetver.h +8 -8
- package/windows/ReactNativeBlobUtil.sln +43 -43
package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilDefaultResp.java
CHANGED
|
@@ -1,98 +1,98 @@
|
|
|
1
|
-
package com.ReactNativeBlobUtil.Response;
|
|
2
|
-
|
|
3
|
-
import com.ReactNativeBlobUtil.ReactNativeBlobUtilConst;
|
|
4
|
-
import com.ReactNativeBlobUtil.ReactNativeBlobUtilProgressConfig;
|
|
5
|
-
import com.ReactNativeBlobUtil.ReactNativeBlobUtilReq;
|
|
6
|
-
import com.facebook.react.bridge.Arguments;
|
|
7
|
-
import com.facebook.react.bridge.ReactApplicationContext;
|
|
8
|
-
import com.facebook.react.bridge.WritableMap;
|
|
9
|
-
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
10
|
-
|
|
11
|
-
import java.io.IOException;
|
|
12
|
-
import java.nio.charset.Charset;
|
|
13
|
-
|
|
14
|
-
import okhttp3.MediaType;
|
|
15
|
-
import okhttp3.ResponseBody;
|
|
16
|
-
import okio.Buffer;
|
|
17
|
-
import okio.BufferedSource;
|
|
18
|
-
import okio.Okio;
|
|
19
|
-
import okio.Source;
|
|
20
|
-
import okio.Timeout;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Created by wkh237 on 2016/7/11.
|
|
24
|
-
*/
|
|
25
|
-
public class ReactNativeBlobUtilDefaultResp extends ResponseBody {
|
|
26
|
-
|
|
27
|
-
String mTaskId;
|
|
28
|
-
ReactApplicationContext rctContext;
|
|
29
|
-
ResponseBody originalBody;
|
|
30
|
-
boolean isIncrement = false;
|
|
31
|
-
|
|
32
|
-
public ReactNativeBlobUtilDefaultResp(ReactApplicationContext ctx, String taskId, ResponseBody body, boolean isIncrement) {
|
|
33
|
-
this.rctContext = ctx;
|
|
34
|
-
this.mTaskId = taskId;
|
|
35
|
-
this.originalBody = body;
|
|
36
|
-
this.isIncrement = isIncrement;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
@Override
|
|
40
|
-
public MediaType contentType() {
|
|
41
|
-
return originalBody.contentType();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
@Override
|
|
45
|
-
public long contentLength() {
|
|
46
|
-
return originalBody.contentLength();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
@Override
|
|
50
|
-
public BufferedSource source() {
|
|
51
|
-
return Okio.buffer(new ProgressReportingSource(originalBody.source()));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private class ProgressReportingSource implements Source {
|
|
55
|
-
|
|
56
|
-
BufferedSource mOriginalSource;
|
|
57
|
-
long bytesRead = 0;
|
|
58
|
-
|
|
59
|
-
ProgressReportingSource(BufferedSource originalSource) {
|
|
60
|
-
mOriginalSource = originalSource;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
@Override
|
|
64
|
-
public long read(Buffer sink, long byteCount) throws IOException {
|
|
65
|
-
|
|
66
|
-
long read = mOriginalSource.read(sink, byteCount);
|
|
67
|
-
bytesRead += read > 0 ? read : 0;
|
|
68
|
-
ReactNativeBlobUtilProgressConfig reportConfig = ReactNativeBlobUtilReq.getReportProgress(mTaskId);
|
|
69
|
-
long cLen = contentLength();
|
|
70
|
-
if (reportConfig != null && cLen != 0 && reportConfig.shouldReport(bytesRead / contentLength())) {
|
|
71
|
-
WritableMap args = Arguments.createMap();
|
|
72
|
-
args.putString("taskId", mTaskId);
|
|
73
|
-
args.putString("written", String.valueOf(bytesRead));
|
|
74
|
-
args.putString("total", String.valueOf(contentLength()));
|
|
75
|
-
if (isIncrement) {
|
|
76
|
-
args.putString("chunk", sink.readString(Charset.defaultCharset()));
|
|
77
|
-
} else {
|
|
78
|
-
args.putString("chunk", "");
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
82
|
-
.emit(ReactNativeBlobUtilConst.EVENT_PROGRESS, args);
|
|
83
|
-
}
|
|
84
|
-
return read;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
@Override
|
|
88
|
-
public Timeout timeout() {
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
@Override
|
|
93
|
-
public void close() throws IOException {
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
}
|
|
1
|
+
package com.ReactNativeBlobUtil.Response;
|
|
2
|
+
|
|
3
|
+
import com.ReactNativeBlobUtil.ReactNativeBlobUtilConst;
|
|
4
|
+
import com.ReactNativeBlobUtil.ReactNativeBlobUtilProgressConfig;
|
|
5
|
+
import com.ReactNativeBlobUtil.ReactNativeBlobUtilReq;
|
|
6
|
+
import com.facebook.react.bridge.Arguments;
|
|
7
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
8
|
+
import com.facebook.react.bridge.WritableMap;
|
|
9
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
10
|
+
|
|
11
|
+
import java.io.IOException;
|
|
12
|
+
import java.nio.charset.Charset;
|
|
13
|
+
|
|
14
|
+
import okhttp3.MediaType;
|
|
15
|
+
import okhttp3.ResponseBody;
|
|
16
|
+
import okio.Buffer;
|
|
17
|
+
import okio.BufferedSource;
|
|
18
|
+
import okio.Okio;
|
|
19
|
+
import okio.Source;
|
|
20
|
+
import okio.Timeout;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Created by wkh237 on 2016/7/11.
|
|
24
|
+
*/
|
|
25
|
+
public class ReactNativeBlobUtilDefaultResp extends ResponseBody {
|
|
26
|
+
|
|
27
|
+
String mTaskId;
|
|
28
|
+
ReactApplicationContext rctContext;
|
|
29
|
+
ResponseBody originalBody;
|
|
30
|
+
boolean isIncrement = false;
|
|
31
|
+
|
|
32
|
+
public ReactNativeBlobUtilDefaultResp(ReactApplicationContext ctx, String taskId, ResponseBody body, boolean isIncrement) {
|
|
33
|
+
this.rctContext = ctx;
|
|
34
|
+
this.mTaskId = taskId;
|
|
35
|
+
this.originalBody = body;
|
|
36
|
+
this.isIncrement = isIncrement;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@Override
|
|
40
|
+
public MediaType contentType() {
|
|
41
|
+
return originalBody.contentType();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@Override
|
|
45
|
+
public long contentLength() {
|
|
46
|
+
return originalBody.contentLength();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
@Override
|
|
50
|
+
public BufferedSource source() {
|
|
51
|
+
return Okio.buffer(new ProgressReportingSource(originalBody.source()));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private class ProgressReportingSource implements Source {
|
|
55
|
+
|
|
56
|
+
BufferedSource mOriginalSource;
|
|
57
|
+
long bytesRead = 0;
|
|
58
|
+
|
|
59
|
+
ProgressReportingSource(BufferedSource originalSource) {
|
|
60
|
+
mOriginalSource = originalSource;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
@Override
|
|
64
|
+
public long read(Buffer sink, long byteCount) throws IOException {
|
|
65
|
+
|
|
66
|
+
long read = mOriginalSource.read(sink, byteCount);
|
|
67
|
+
bytesRead += read > 0 ? read : 0;
|
|
68
|
+
ReactNativeBlobUtilProgressConfig reportConfig = ReactNativeBlobUtilReq.getReportProgress(mTaskId);
|
|
69
|
+
long cLen = contentLength();
|
|
70
|
+
if (reportConfig != null && cLen != 0 && reportConfig.shouldReport(bytesRead / contentLength())) {
|
|
71
|
+
WritableMap args = Arguments.createMap();
|
|
72
|
+
args.putString("taskId", mTaskId);
|
|
73
|
+
args.putString("written", String.valueOf(bytesRead));
|
|
74
|
+
args.putString("total", String.valueOf(contentLength()));
|
|
75
|
+
if (isIncrement) {
|
|
76
|
+
args.putString("chunk", sink.readString(Charset.defaultCharset()));
|
|
77
|
+
} else {
|
|
78
|
+
args.putString("chunk", "");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
82
|
+
.emit(ReactNativeBlobUtilConst.EVENT_PROGRESS, args);
|
|
83
|
+
}
|
|
84
|
+
return read;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
@Override
|
|
88
|
+
public Timeout timeout() {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
@Override
|
|
93
|
+
public void close() throws IOException {
|
|
94
|
+
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
}
|
package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilFileResp.java
CHANGED
|
@@ -1,166 +1,166 @@
|
|
|
1
|
-
package com.ReactNativeBlobUtil.Response;
|
|
2
|
-
|
|
3
|
-
import androidx.annotation.NonNull;
|
|
4
|
-
|
|
5
|
-
import com.ReactNativeBlobUtil.ReactNativeBlobUtilConst;
|
|
6
|
-
import com.ReactNativeBlobUtil.ReactNativeBlobUtilProgressConfig;
|
|
7
|
-
import com.ReactNativeBlobUtil.ReactNativeBlobUtilReq;
|
|
8
|
-
import com.facebook.react.bridge.Arguments;
|
|
9
|
-
import com.facebook.react.bridge.ReactApplicationContext;
|
|
10
|
-
import com.facebook.react.bridge.WritableMap;
|
|
11
|
-
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
12
|
-
|
|
13
|
-
import java.io.File;
|
|
14
|
-
import java.io.FileOutputStream;
|
|
15
|
-
import java.io.IOException;
|
|
16
|
-
|
|
17
|
-
import okhttp3.MediaType;
|
|
18
|
-
import okhttp3.ResponseBody;
|
|
19
|
-
import okio.Buffer;
|
|
20
|
-
import okio.BufferedSource;
|
|
21
|
-
import okio.Okio;
|
|
22
|
-
import okio.Source;
|
|
23
|
-
import okio.Timeout;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Created by wkh237 on 2016/7/11.
|
|
27
|
-
*/
|
|
28
|
-
public class ReactNativeBlobUtilFileResp extends ResponseBody {
|
|
29
|
-
|
|
30
|
-
String mTaskId;
|
|
31
|
-
ResponseBody originalBody;
|
|
32
|
-
String mPath;
|
|
33
|
-
long bytesDownloaded = 0;
|
|
34
|
-
ReactApplicationContext rctContext;
|
|
35
|
-
FileOutputStream ofStream;
|
|
36
|
-
boolean isEndMarkerReceived;
|
|
37
|
-
|
|
38
|
-
public ReactNativeBlobUtilFileResp(ResponseBody body) {
|
|
39
|
-
super();
|
|
40
|
-
this.originalBody = body;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public ReactNativeBlobUtilFileResp(ReactApplicationContext ctx, String taskId, ResponseBody body, String path, boolean overwrite) throws IOException {
|
|
44
|
-
super();
|
|
45
|
-
this.rctContext = ctx;
|
|
46
|
-
this.mTaskId = taskId;
|
|
47
|
-
this.originalBody = body;
|
|
48
|
-
assert path != null;
|
|
49
|
-
this.mPath = path;
|
|
50
|
-
this.isEndMarkerReceived = false;
|
|
51
|
-
if (path != null) {
|
|
52
|
-
boolean appendToExistingFile = !overwrite;
|
|
53
|
-
path = path.replace("?append=true", "");
|
|
54
|
-
mPath = path;
|
|
55
|
-
File f = new File(path);
|
|
56
|
-
|
|
57
|
-
File parent = f.getParentFile();
|
|
58
|
-
if (parent != null && !parent.exists() && !parent.mkdirs()) {
|
|
59
|
-
throw new IllegalStateException("Couldn't create dir: " + parent);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (!f.exists())
|
|
63
|
-
f.createNewFile();
|
|
64
|
-
ofStream = new FileOutputStream(new File(path), appendToExistingFile);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
@Override
|
|
69
|
-
public MediaType contentType() {
|
|
70
|
-
return originalBody.contentType();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
@Override
|
|
74
|
-
public long contentLength() {
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
*
|
|
78
|
-
* Okio buffer issue in current version seems to be fixed in the latest versions
|
|
79
|
-
*
|
|
80
|
-
* limiting this was causing the download progress to stop at 2GB size but files still was downloading
|
|
81
|
-
*
|
|
82
|
-
*/
|
|
83
|
-
|
|
84
|
-
// if (originalBody.contentLength() > Integer.MAX_VALUE) {
|
|
85
|
-
// This is a workaround for a bug Okio buffer where it can't handle larger than int.
|
|
86
|
-
// return Integer.MAX_VALUE;
|
|
87
|
-
// }
|
|
88
|
-
return originalBody.contentLength();
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
public boolean isDownloadComplete() {
|
|
92
|
-
return (bytesDownloaded == contentLength()) // Case of non-chunked downloads
|
|
93
|
-
|| (contentLength() == -1 && isEndMarkerReceived); // Case of chunked downloads
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
@Override
|
|
97
|
-
public BufferedSource source() {
|
|
98
|
-
ProgressReportingSource countable = new ProgressReportingSource();
|
|
99
|
-
return Okio.buffer(countable);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
private class ProgressReportingSource implements Source {
|
|
103
|
-
@Override
|
|
104
|
-
public long read(@NonNull Buffer sink, long byteCount) throws IOException {
|
|
105
|
-
try {
|
|
106
|
-
byte[] bytes = new byte[(int) byteCount];
|
|
107
|
-
long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
|
|
108
|
-
bytesDownloaded += read > 0 ? read : 0;
|
|
109
|
-
if (read > 0) {
|
|
110
|
-
ofStream.write(bytes, 0, (int) read);
|
|
111
|
-
} else if (contentLength() == -1 && read == -1) {
|
|
112
|
-
// End marker has been received for chunked download
|
|
113
|
-
isEndMarkerReceived = true;
|
|
114
|
-
}
|
|
115
|
-
ReactNativeBlobUtilProgressConfig reportConfig = ReactNativeBlobUtilReq.getReportProgress(mTaskId);
|
|
116
|
-
|
|
117
|
-
if (contentLength() != 0) {
|
|
118
|
-
|
|
119
|
-
// For non-chunked download, progress is received / total
|
|
120
|
-
// For chunked download, progress can be either 0 (started) or 1 (ended)
|
|
121
|
-
float progress = (contentLength() != -1) ? bytesDownloaded / contentLength() : ((isEndMarkerReceived) ? 1 : 0);
|
|
122
|
-
|
|
123
|
-
if (reportConfig != null && reportConfig.shouldReport(progress /* progress */)) {
|
|
124
|
-
if (contentLength() != -1) {
|
|
125
|
-
// For non-chunked downloads
|
|
126
|
-
reportProgress(mTaskId, bytesDownloaded, contentLength());
|
|
127
|
-
} else {
|
|
128
|
-
// For chunked downloads
|
|
129
|
-
if (!isEndMarkerReceived) {
|
|
130
|
-
reportProgress(mTaskId, 0, contentLength());
|
|
131
|
-
} else {
|
|
132
|
-
reportProgress(mTaskId, bytesDownloaded, bytesDownloaded);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return read;
|
|
140
|
-
} catch (Exception ex) {
|
|
141
|
-
return -1;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
private void reportProgress(String taskId, long bytesDownloaded, long contentLength) {
|
|
146
|
-
WritableMap args = Arguments.createMap();
|
|
147
|
-
args.putString("taskId", taskId);
|
|
148
|
-
args.putString("written", String.valueOf(bytesDownloaded));
|
|
149
|
-
args.putString("total", String.valueOf(contentLength));
|
|
150
|
-
rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
151
|
-
.emit(ReactNativeBlobUtilConst.EVENT_PROGRESS, args);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
@Override
|
|
155
|
-
public Timeout timeout() {
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
@Override
|
|
160
|
-
public void close() throws IOException {
|
|
161
|
-
ofStream.close();
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
}
|
|
1
|
+
package com.ReactNativeBlobUtil.Response;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
|
|
5
|
+
import com.ReactNativeBlobUtil.ReactNativeBlobUtilConst;
|
|
6
|
+
import com.ReactNativeBlobUtil.ReactNativeBlobUtilProgressConfig;
|
|
7
|
+
import com.ReactNativeBlobUtil.ReactNativeBlobUtilReq;
|
|
8
|
+
import com.facebook.react.bridge.Arguments;
|
|
9
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
10
|
+
import com.facebook.react.bridge.WritableMap;
|
|
11
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
12
|
+
|
|
13
|
+
import java.io.File;
|
|
14
|
+
import java.io.FileOutputStream;
|
|
15
|
+
import java.io.IOException;
|
|
16
|
+
|
|
17
|
+
import okhttp3.MediaType;
|
|
18
|
+
import okhttp3.ResponseBody;
|
|
19
|
+
import okio.Buffer;
|
|
20
|
+
import okio.BufferedSource;
|
|
21
|
+
import okio.Okio;
|
|
22
|
+
import okio.Source;
|
|
23
|
+
import okio.Timeout;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Created by wkh237 on 2016/7/11.
|
|
27
|
+
*/
|
|
28
|
+
public class ReactNativeBlobUtilFileResp extends ResponseBody {
|
|
29
|
+
|
|
30
|
+
String mTaskId;
|
|
31
|
+
ResponseBody originalBody;
|
|
32
|
+
String mPath;
|
|
33
|
+
long bytesDownloaded = 0;
|
|
34
|
+
ReactApplicationContext rctContext;
|
|
35
|
+
FileOutputStream ofStream;
|
|
36
|
+
boolean isEndMarkerReceived;
|
|
37
|
+
|
|
38
|
+
public ReactNativeBlobUtilFileResp(ResponseBody body) {
|
|
39
|
+
super();
|
|
40
|
+
this.originalBody = body;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public ReactNativeBlobUtilFileResp(ReactApplicationContext ctx, String taskId, ResponseBody body, String path, boolean overwrite) throws IOException {
|
|
44
|
+
super();
|
|
45
|
+
this.rctContext = ctx;
|
|
46
|
+
this.mTaskId = taskId;
|
|
47
|
+
this.originalBody = body;
|
|
48
|
+
assert path != null;
|
|
49
|
+
this.mPath = path;
|
|
50
|
+
this.isEndMarkerReceived = false;
|
|
51
|
+
if (path != null) {
|
|
52
|
+
boolean appendToExistingFile = !overwrite;
|
|
53
|
+
path = path.replace("?append=true", "");
|
|
54
|
+
mPath = path;
|
|
55
|
+
File f = new File(path);
|
|
56
|
+
|
|
57
|
+
File parent = f.getParentFile();
|
|
58
|
+
if (parent != null && !parent.exists() && !parent.mkdirs()) {
|
|
59
|
+
throw new IllegalStateException("Couldn't create dir: " + parent);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!f.exists())
|
|
63
|
+
f.createNewFile();
|
|
64
|
+
ofStream = new FileOutputStream(new File(path), appendToExistingFile);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@Override
|
|
69
|
+
public MediaType contentType() {
|
|
70
|
+
return originalBody.contentType();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@Override
|
|
74
|
+
public long contentLength() {
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
*
|
|
78
|
+
* Okio buffer issue in current version seems to be fixed in the latest versions
|
|
79
|
+
*
|
|
80
|
+
* limiting this was causing the download progress to stop at 2GB size but files still was downloading
|
|
81
|
+
*
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
// if (originalBody.contentLength() > Integer.MAX_VALUE) {
|
|
85
|
+
// This is a workaround for a bug Okio buffer where it can't handle larger than int.
|
|
86
|
+
// return Integer.MAX_VALUE;
|
|
87
|
+
// }
|
|
88
|
+
return originalBody.contentLength();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public boolean isDownloadComplete() {
|
|
92
|
+
return (bytesDownloaded == contentLength()) // Case of non-chunked downloads
|
|
93
|
+
|| (contentLength() == -1 && isEndMarkerReceived); // Case of chunked downloads
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Override
|
|
97
|
+
public BufferedSource source() {
|
|
98
|
+
ProgressReportingSource countable = new ProgressReportingSource();
|
|
99
|
+
return Okio.buffer(countable);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private class ProgressReportingSource implements Source {
|
|
103
|
+
@Override
|
|
104
|
+
public long read(@NonNull Buffer sink, long byteCount) throws IOException {
|
|
105
|
+
try {
|
|
106
|
+
byte[] bytes = new byte[(int) byteCount];
|
|
107
|
+
long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
|
|
108
|
+
bytesDownloaded += read > 0 ? read : 0;
|
|
109
|
+
if (read > 0) {
|
|
110
|
+
ofStream.write(bytes, 0, (int) read);
|
|
111
|
+
} else if (contentLength() == -1 && read == -1) {
|
|
112
|
+
// End marker has been received for chunked download
|
|
113
|
+
isEndMarkerReceived = true;
|
|
114
|
+
}
|
|
115
|
+
ReactNativeBlobUtilProgressConfig reportConfig = ReactNativeBlobUtilReq.getReportProgress(mTaskId);
|
|
116
|
+
|
|
117
|
+
if (contentLength() != 0) {
|
|
118
|
+
|
|
119
|
+
// For non-chunked download, progress is received / total
|
|
120
|
+
// For chunked download, progress can be either 0 (started) or 1 (ended)
|
|
121
|
+
float progress = (contentLength() != -1) ? bytesDownloaded / contentLength() : ((isEndMarkerReceived) ? 1 : 0);
|
|
122
|
+
|
|
123
|
+
if (reportConfig != null && reportConfig.shouldReport(progress /* progress */)) {
|
|
124
|
+
if (contentLength() != -1) {
|
|
125
|
+
// For non-chunked downloads
|
|
126
|
+
reportProgress(mTaskId, bytesDownloaded, contentLength());
|
|
127
|
+
} else {
|
|
128
|
+
// For chunked downloads
|
|
129
|
+
if (!isEndMarkerReceived) {
|
|
130
|
+
reportProgress(mTaskId, 0, contentLength());
|
|
131
|
+
} else {
|
|
132
|
+
reportProgress(mTaskId, bytesDownloaded, bytesDownloaded);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return read;
|
|
140
|
+
} catch (Exception ex) {
|
|
141
|
+
return -1;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private void reportProgress(String taskId, long bytesDownloaded, long contentLength) {
|
|
146
|
+
WritableMap args = Arguments.createMap();
|
|
147
|
+
args.putString("taskId", taskId);
|
|
148
|
+
args.putString("written", String.valueOf(bytesDownloaded));
|
|
149
|
+
args.putString("total", String.valueOf(contentLength));
|
|
150
|
+
rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
151
|
+
.emit(ReactNativeBlobUtilConst.EVENT_PROGRESS, args);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
@Override
|
|
155
|
+
public Timeout timeout() {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
@Override
|
|
160
|
+
public void close() throws IOException {
|
|
161
|
+
ofStream.close();
|
|
162
|
+
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
package com.ReactNativeBlobUtil.Utils;
|
|
2
|
-
|
|
3
|
-
import android.webkit.MimeTypeMap;
|
|
4
|
-
|
|
5
|
-
public class FileDescription {
|
|
6
|
-
public String name;
|
|
7
|
-
public String partentFolder;
|
|
8
|
-
public String mimeType;
|
|
9
|
-
|
|
10
|
-
public FileDescription(String n, String mT, String pF) {
|
|
11
|
-
name = n;
|
|
12
|
-
partentFolder = pF != null ? pF : "";
|
|
13
|
-
mimeType = mT;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public String getFullPath(){
|
|
17
|
-
return partentFolder + "/" + MimeType.getFullFileName(name, mimeType);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
1
|
+
package com.ReactNativeBlobUtil.Utils;
|
|
2
|
+
|
|
3
|
+
import android.webkit.MimeTypeMap;
|
|
4
|
+
|
|
5
|
+
public class FileDescription {
|
|
6
|
+
public String name;
|
|
7
|
+
public String partentFolder;
|
|
8
|
+
public String mimeType;
|
|
9
|
+
|
|
10
|
+
public FileDescription(String n, String mT, String pF) {
|
|
11
|
+
name = n;
|
|
12
|
+
partentFolder = pF != null ? pF : "";
|
|
13
|
+
mimeType = mT;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public String getFullPath(){
|
|
17
|
+
return partentFolder + "/" + MimeType.getFullFileName(name, mimeType);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package com.ReactNativeBlobUtil.Utils;
|
|
2
|
-
|
|
3
|
-
public class FileProvider extends androidx.core.content.FileProvider {
|
|
4
|
-
}
|
|
1
|
+
package com.ReactNativeBlobUtil.Utils;
|
|
2
|
+
|
|
3
|
+
public class FileProvider extends androidx.core.content.FileProvider {
|
|
4
|
+
}
|