From 33d637e42e17900b41133d05bfbacd31d1a1ac4f Mon Sep 17 00:00:00 2001 From: Kyle Corbitt Date: Fri, 18 Mar 2016 10:53:00 -0700 Subject: [PATCH] new getFSInfo command --- FS.common.js | 8 +++++- README.md | 9 ++++++- RNFSManager.m | 26 +++++++++++++++++++ .../src/main/java/com/rnfs/RNFSManager.java | 21 +++++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/FS.common.js b/FS.common.js index 074cdf4..b68804d 100644 --- a/FS.common.js +++ b/FS.common.js @@ -24,6 +24,7 @@ var _unlink = Promise.promisify(RNFSManager.unlink); var _mkdir = Promise.promisify(RNFSManager.mkdir); var _downloadFile = Promise.promisify(RNFSManager.downloadFile); var _pathForBundle = Promise.promisify(RNFSManager.pathForBundle); +var _getFSInfo = Promise.promisify(RNFSManager.getFSInfo); var convertError = (err) => { if (err.isOperational && err.cause) { @@ -83,7 +84,7 @@ var RNFS = { }) .catch(convertError); }, - + exists(filepath) { return _exists(filepath) .catch(convertError); @@ -139,6 +140,11 @@ var RNFS = { return _pathForBundle(bundleName); }, + getFSInfo() { + return _getFSInfo() + .catch(convertError); + }, + unlink(filepath) { return _unlink(filepath) .catch(convertError); diff --git a/README.md b/README.md index 6df133d..941754f 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ dependencies { ``` * register module (in MainActivity.java) - + * For react-native below 0.19.0 (use `cat ./node_modules/react-native/package.json | grep version`) ```java @@ -271,6 +271,13 @@ Percentage can be computed easily by dividing `bytesWritten` by `contentLength`. Abort the current download job with this ID. The partial file will remain on the filesystem. +### `promise getFSInfo()` + +Returns an object with the following properties: + +`totalSpace` (`Number`): The total amount of storage space on the device (in bytes). +`freeSpace` (`Number`): The amount of available storage space on the device (in bytes). + ## Test / Demo app Test app to demostrate the use of the module. Useful for testing and developing the module: diff --git a/RNFSManager.m b/RNFSManager.m index ca8cfcb..35780b1 100644 --- a/RNFSManager.m +++ b/RNFSManager.m @@ -254,6 +254,32 @@ RCT_EXPORT_METHOD(pathForBundle:(NSString *)bundleNamed } } +RCT_EXPORT_METHOD(getFSInfo:(RCTResponseSenderBlock)callback) +{ + 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]; + + callback(@[[NSNull null], + @{ + @"totalSpace": [NSNumber numberWithUnsignedLongLong:totalSpace], + @"freeSpace": [NSNumber numberWithUnsignedLongLong:totalFreeSpace] + } + ]); + } else { + callback(@[error, [NSNull null]]); + } +} + - (NSNumber *)dateToTimeIntervalNumber:(NSDate *)date { return @([date timeIntervalSince1970]); diff --git a/android/src/main/java/com/rnfs/RNFSManager.java b/android/src/main/java/com/rnfs/RNFSManager.java index aeca38e..1d7f774 100644 --- a/android/src/main/java/com/rnfs/RNFSManager.java +++ b/android/src/main/java/com/rnfs/RNFSManager.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import android.os.Environment; import android.os.AsyncTask; +import android.os.StatFs; import android.util.Base64; import android.content.Context; import android.support.annotation.Nullable; @@ -300,6 +301,26 @@ public class RNFSManager extends ReactContextBaseJavaModule { // TODO: Not sure what equilivent would be? } + @ReactMethod + public void getFSInfo(Callback callback) { + File path = Environment.getDataDirectory(); + StatFs stat = new StatFs(path.getPath()); + long totalSpace; + long freeSpace; + if (android.os.Build.VERSION.SDK_INT >= 18) { + totalSpace = stat.getTotalBytes(); + freeSpace = stat.getFreeBytes(); + } else { + long blockSize = stat.getBlockSize(); + totalSpace = blockSize * stat.getBlockCount(); + freeSpace = blockSize * stat.getAvailableBlocks(); + } + HashMap info = new HashMap(); + info.put("totalSpace", totalSpace); + info.put("freeSpace", freeSpace); + callback.invoke(null, info); + } + private WritableMap makeErrorPayload(Exception ex) { WritableMap error = Arguments.createMap(); error.putString("message", ex.getMessage());