Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ DerivedData
# Pods - for those of you who use CocoaPods
Pods
update-test.sh
.vscode/
android/.gradle/*
android/gradle/*
android/*.iml
android/local.properties
5 changes: 4 additions & 1 deletion FS.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ type ReadDirItem = {
};

type StatResult = {
name: string; // The name of the item
name: ?string; // The name of the item TODO: why is this not documented?
path: string; // The absolute path to the item
size: string; // Size in bytes
mode: number; // UNIX file mode
ctime: number; // Created date
mtime: number; // Last modified date
originalFilepath: string; // In case of content uri this is the pointed file path, otherwise is the same as path
isFile: () => boolean; // Is the file just a file?
isDirectory: () => boolean; // Is the file a directory?
};
Expand Down Expand Up @@ -272,10 +273,12 @@ var RNFS = {
stat(filepath: string): Promise<StatResult> {
return RNFSManager.stat(normalizeFilePath(filepath)).then((result) => {
return {
'path': filepath,
'ctime': new Date(result.ctime * 1000),
'mtime': new Date(result.mtime * 1000),
'size': result.size,
'mode': result.mode,
'originalFilepath': result.originalFilepath,
isFile: () => result.type === RNFSFileTypeRegular,
isDirectory: () => result.type === RNFSFileTypeDirectory,
};
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,17 @@ Node.js style version of `readDir` that returns only the names. Note the lowerca

### `stat(filepath: string): Promise<StatResult>`

Stats an item at `path`.
Stats an item at `filepath`. If the `filepath` is linked to a virtual file, for example Android Content URI, the `originalPath` can be used to find the pointed file path.
The promise resolves with an object with the following properties:

```
type StatResult = {
path: // The same as filepath argument
ctime: date; // The creation date of the file
mtime: date; // The last modified date of the file
size: string; // Size in bytes
mode: number; // UNIX file mode
originalFilepath: string; // ANDROID: In case of content uri this is the pointed file path, otherwise is the same as path
isFile: () => boolean; // Is the file just a file?
isDirectory: () => boolean; // Is the file a directory?
};
Expand Down
22 changes: 20 additions & 2 deletions android/src/main/java/com/rnfs/RNFSManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.StatFs;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.util.Base64;
import android.util.SparseArray;
Expand Down Expand Up @@ -72,6 +74,21 @@ private Uri getFileUri(String filepath) throws IORejectionException {
return uri;
}

private String getOriginalFilepath(String filepath) throws IORejectionException {
Uri uri = getFileUri(filepath);
String originalFilepath = "";
if (uri.getScheme().equals("content")) {
try {
Cursor cursor = reactContext.getContentResolver().query(uri, null, null, null, null);
if (cursor.moveToFirst()) {
originalFilepath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
}
} catch (IllegalArgumentException ignored) {
}
}
return originalFilepath;
}

private InputStream getInputStream(String filepath) throws IORejectionException {
Uri uri = getFileUri(filepath);
InputStream stream;
Expand Down Expand Up @@ -510,16 +527,17 @@ public void setReadable(String filepath, Boolean readable, Boolean ownerOnly, Pr
@ReactMethod
public void stat(String filepath, Promise promise) {
try {
File file = new File(filepath);
String originalFilepath = getOriginalFilepath(filepath);
File file = new File(originalFilepath);

if (!file.exists()) throw new Exception("File does not exist");

WritableMap statMap = Arguments.createMap();

statMap.putInt("ctime", (int) (file.lastModified() / 1000));
statMap.putInt("mtime", (int) (file.lastModified() / 1000));
statMap.putInt("size", (int) file.length());
statMap.putInt("type", file.isDirectory() ? 1 : 0);
statMap.putString("originalFilepath", originalFilepath);

promise.resolve(statMap);
} catch (Exception ex) {
Expand Down