new getFSInfo command

This commit is contained in:
Kyle Corbitt 2016-03-18 10:53:00 -07:00 committed by Chris Dell
parent 2196577b66
commit 33d637e42e
4 changed files with 62 additions and 2 deletions

View File

@ -24,6 +24,7 @@ var _unlink = Promise.promisify(RNFSManager.unlink);
var _mkdir = Promise.promisify(RNFSManager.mkdir); var _mkdir = Promise.promisify(RNFSManager.mkdir);
var _downloadFile = Promise.promisify(RNFSManager.downloadFile); var _downloadFile = Promise.promisify(RNFSManager.downloadFile);
var _pathForBundle = Promise.promisify(RNFSManager.pathForBundle); var _pathForBundle = Promise.promisify(RNFSManager.pathForBundle);
var _getFSInfo = Promise.promisify(RNFSManager.getFSInfo);
var convertError = (err) => { var convertError = (err) => {
if (err.isOperational && err.cause) { if (err.isOperational && err.cause) {
@ -139,6 +140,11 @@ var RNFS = {
return _pathForBundle(bundleName); return _pathForBundle(bundleName);
}, },
getFSInfo() {
return _getFSInfo()
.catch(convertError);
},
unlink(filepath) { unlink(filepath) {
return _unlink(filepath) return _unlink(filepath)
.catch(convertError); .catch(convertError);

View File

@ -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. 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 / Demo app
Test app to demostrate the use of the module. Useful for testing and developing the module: Test app to demostrate the use of the module. Useful for testing and developing the module:

View File

@ -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 - (NSNumber *)dateToTimeIntervalNumber:(NSDate *)date
{ {
return @([date timeIntervalSince1970]); return @([date timeIntervalSince1970]);

View File

@ -7,6 +7,7 @@ import java.util.ArrayList;
import android.os.Environment; import android.os.Environment;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.StatFs;
import android.util.Base64; import android.util.Base64;
import android.content.Context; import android.content.Context;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -300,6 +301,26 @@ public class RNFSManager extends ReactContextBaseJavaModule {
// TODO: Not sure what equilivent would be? // 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) { private WritableMap makeErrorPayload(Exception ex) {
WritableMap error = Arguments.createMap(); WritableMap error = Arguments.createMap();
error.putString("message", ex.getMessage()); error.putString("message", ex.getMessage());