mirror of
https://github.com/status-im/react-native-fs.git
synced 2025-02-28 15:00:29 +00:00
Merge branch 'master' of https://github.com/kitolog/react-native-fs into kitolog-master
This commit is contained in:
commit
132fb4d6a0
@ -15,6 +15,7 @@ typedef void (^ProgressCallback)(NSNumber*, NSNumber*);
|
||||
@property (copy) BeginCallback beginCallback; // Download has started (headers received)
|
||||
@property (copy) ProgressCallback progressCallback; // Download is progressing
|
||||
@property bool background; // Whether to continue download when app is in background
|
||||
@property (copy) NSNumber* progressDivider;
|
||||
|
||||
|
||||
@end
|
||||
|
22
Downloader.m
22
Downloader.m
@ -11,6 +11,7 @@
|
||||
@property (retain) NSURLSession* session;
|
||||
@property (retain) NSURLSessionTask* task;
|
||||
@property (retain) NSNumber* statusCode;
|
||||
@property (retain) NSNumber* lastProgressValue;
|
||||
@property (retain) NSNumber* contentLength;
|
||||
@property (retain) NSNumber* bytesWritten;
|
||||
|
||||
@ -65,8 +66,25 @@
|
||||
}
|
||||
|
||||
if ([_statusCode isEqualToNumber:[NSNumber numberWithInt:200]]) {
|
||||
_bytesWritten = @(totalBytesWritten);
|
||||
return _params.progressCallback(_contentLength, _bytesWritten);
|
||||
[_fileHandle writeData:data];
|
||||
|
||||
_bytesWritten = [NSNumber numberWithLong:[_bytesWritten longValue] + data.length];
|
||||
|
||||
if (_params.progressDivider <= 1) {
|
||||
return _params.progressCallback(_contentLength, _bytesWritten);
|
||||
} else {
|
||||
double doubleBytesWritten = (double)[_bytesWritten longValue];
|
||||
double doubleContentLength = (double)[_contentLength longValue];
|
||||
double doublePercents = doubleBytesWritten / doubleContentLength * 100;
|
||||
NSNumber* progress = [NSNumber numberWithUnsignedInt: floor(doublePercents)];
|
||||
if ([progress unsignedIntValue] % [_params.progressDivider integerValue] == 0) {
|
||||
if (([progress unsignedIntValue] != [_lastProgressValue unsignedIntValue]) || ([_bytesWritten unsignedIntegerValue] == [_contentLength longValue])) {
|
||||
NSLog(@"---Progress callback EMIT--- %zu", [progress unsignedIntValue]);
|
||||
_lastProgressValue = [NSNumber numberWithUnsignedInt:[progress unsignedIntValue]];
|
||||
return _params.progressCallback(_contentLength, _bytesWritten);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ var _moveFile = Promise.promisify(RNFSManager.moveFile);
|
||||
var _unlink = Promise.promisify(RNFSManager.unlink);
|
||||
var _mkdir = Promise.promisify(RNFSManager.mkdir);
|
||||
var _downloadFile = Promise.promisify(RNFSManager.downloadFile);
|
||||
var _uploadFiles = RNFSManager.uploadFiles ? Promise.promisify(RNFSManager.uploadFiles) : function () { return Promise.reject('Not implemented on Android') };
|
||||
var _uploadFiles = RNFSManager.uploadFiles ? Promise.promisify(RNFSManager.uploadFiles) : function () { return Promise.reject('Not implemented on Android'); };
|
||||
var _pathForBundle = Promise.promisify(RNFSManager.pathForBundle);
|
||||
var _getFSInfo = Promise.promisify(RNFSManager.getFSInfo);
|
||||
|
||||
@ -173,6 +173,7 @@ var RNFS = {
|
||||
if (typeof options.toFile !== 'string') throw new Error('downloadFile: Invalid value for property `toFile`');
|
||||
if (options.headers && typeof options.headers !== 'object') throw new Error('downloadFile: Invalid value for property `headers`');
|
||||
if (options.background && typeof options.background !== 'boolean') throw new Error('downloadFile: Invalid value for property `background`');
|
||||
if (options.progressDivider && typeof options.progressDivider !== 'number') throw new Error('downloadFile: Invalid value for property `progressDivider`');
|
||||
|
||||
var jobId = getJobId();
|
||||
var subscriptions = [];
|
||||
@ -191,6 +192,7 @@ var RNFS = {
|
||||
toFile: options.toFile,
|
||||
headers: options.headers || {},
|
||||
background: !!options.background,
|
||||
progressDivider: options.progressDivider || 1
|
||||
};
|
||||
|
||||
return _downloadFile(bridgeOptions)
|
||||
|
@ -316,6 +316,7 @@ Create a directory at `filepath`. Automatically creates parents and does not thr
|
||||
toFile (String) - Local filesystem path to save the file to
|
||||
headers (Object) - (Optional) An object of headers to be passed to the server
|
||||
background (Boolean) - (Optional) See below
|
||||
progressDivider (Number) - (Optional) See below
|
||||
begin (Function) - (Optional) See below
|
||||
progress (Function) - (Optional) See below
|
||||
}
|
||||
@ -335,7 +336,11 @@ If `options.progress` is provided, it will be invoked continuously and passed a
|
||||
`contentLength` (`Number`) - The total size in bytes of the download resource
|
||||
`bytesWritten` (`Number`) - The number of bytes written to the file so far
|
||||
|
||||
Percentage can be computed easily by dividing `bytesWritten` by `contentLength`.
|
||||
If `options.progressDivider`(`Number`) is provided, it will return progress events that divided by progressDivider
|
||||
|
||||
For example, if `progressDivider` = 10, you will receive only ten callbacks for this values of progress: 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
|
||||
Use it for performance issues.
|
||||
If `progressDivider` = 1, you will receive all `progressCallback` calls, default value is 1.
|
||||
|
||||
(IOS only): `options.background` (`Boolean`) - Whether to continue downloads when the app is not focused (default: `false`)
|
||||
This option is currently only available for iOS, and you must [enable
|
||||
|
@ -186,6 +186,8 @@ RCT_EXPORT_METHOD(downloadFile:(NSDictionary *)options
|
||||
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,
|
||||
|
@ -22,6 +22,7 @@ public class DownloadParams {
|
||||
public URL src;
|
||||
public File dest;
|
||||
public ReadableMap headers;
|
||||
public float progressDivider;
|
||||
public OnTaskCompleted onTaskCompleted;
|
||||
public OnDownloadBegin onDownloadBegin;
|
||||
public OnDownloadProgress onDownloadProgress;
|
||||
|
@ -13,6 +13,8 @@ import java.net.HttpURLConnection;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
||||
@ -101,6 +103,7 @@ public class Downloader extends AsyncTask<DownloadParams, int[], DownloadResult>
|
||||
byte data[] = new byte[8 * 1024];
|
||||
int total = 0;
|
||||
int count;
|
||||
double lastProgressValue = 0;
|
||||
|
||||
while ((count = input.read(data)) != -1) {
|
||||
if (mAbort.get()) {
|
||||
@ -108,7 +111,18 @@ public class Downloader extends AsyncTask<DownloadParams, int[], DownloadResult>
|
||||
}
|
||||
|
||||
total += count;
|
||||
publishProgress(new int[] { lengthOfFile, total });
|
||||
if (param.progressDivider <= 1) {
|
||||
publishProgress(new int[]{lengthOfFile, total});
|
||||
} else {
|
||||
double progress = Math.round(((double) total * 100) / lengthOfFile);
|
||||
if (progress % param.progressDivider == 0) {
|
||||
if ((progress != lastProgressValue) || (total == lengthOfFile)) {
|
||||
Log.d("Downloader", "EMIT: " + String.valueOf(progress) + ", TOTAL:" + String.valueOf(total));
|
||||
lastProgressValue = progress;
|
||||
publishProgress(new int[]{lengthOfFile, total});
|
||||
}
|
||||
}
|
||||
}
|
||||
output.write(data, 0, count);
|
||||
}
|
||||
|
||||
|
@ -226,12 +226,14 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
URL url = new URL(options.getString("fromUrl"));
|
||||
final int jobId = options.getInt("jobId");
|
||||
ReadableMap headers = options.getMap("headers");
|
||||
int progressDivider = options.getInt("progressDivider");
|
||||
|
||||
DownloadParams params = new DownloadParams();
|
||||
|
||||
params.src = url;
|
||||
params.dest = file;
|
||||
params.headers = headers;
|
||||
params.progressDivider = progressDivider;
|
||||
|
||||
params.onTaskCompleted = new DownloadParams.OnTaskCompleted() {
|
||||
public void onTaskCompleted(DownloadResult res) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-native-fs",
|
||||
"version": "1.3.0",
|
||||
"version": "1.3.1",
|
||||
"description": "Native filesystem access for react-native",
|
||||
"main": "FS.common.js",
|
||||
"scripts": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user