Skip to content

Commit d659a3d

Browse files
author
Andrea Tosatto
committed
Android only: support for originalFilapath with stat (content uri resolution)
1 parent 9b130ad commit d659a3d

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

FS.common.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var getJobId = () => {
2525
return jobId;
2626
};
2727

28-
var normalizeFilePath = (path: string) => (path.startsWith('file://') ? path.slice(7) : path);
28+
var normalizeFilePath = (path: string) => (path.startsWith('file://') && !path.startsWith('content://') ? path.slice(7) : path);
2929

3030
type MkdirOptions = {
3131
NSURLIsExcludedFromBackupKey?: boolean; // iOS only
@@ -42,12 +42,13 @@ type ReadDirItem = {
4242
};
4343

4444
type StatResult = {
45-
name: string; // The name of the item
45+
name: ?string; // The name of the item
4646
path: string; // The absolute path to the item
4747
size: string; // Size in bytes
4848
mode: number; // UNIX file mode
4949
ctime: number; // Created date
5050
mtime: number; // Last modified date
51+
originalFilepath: ?string; // In case of content uri this is the pointed file path, otherwise is the same as path
5152
isFile: () => boolean; // Is the file just a file?
5253
isDirectory: () => boolean; // Is the file a directory?
5354
};
@@ -272,10 +273,12 @@ var RNFS = {
272273
stat(filepath: string): Promise<StatResult> {
273274
return RNFSManager.stat(normalizeFilePath(filepath)).then((result) => {
274275
return {
276+
'path': filepath,
275277
'ctime': new Date(result.ctime * 1000),
276278
'mtime': new Date(result.mtime * 1000),
277279
'size': result.size,
278280
'mode': result.mode,
281+
'originalFilepath': result.originalFilepath,
279282
isFile: () => result.type === RNFSFileTypeRegular,
280283
isDirectory: () => result.type === RNFSFileTypeDirectory,
281284
};

android/src/main/java/com/rnfs/RNFSManager.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import android.content.res.AssetFileDescriptor;
44
import android.content.res.AssetManager;
5+
import android.database.Cursor;
56
import android.net.Uri;
67
import android.os.Environment;
78
import android.os.StatFs;
9+
import android.provider.MediaStore;
810
import android.support.annotation.Nullable;
911
import android.util.Base64;
1012
import android.util.SparseArray;
@@ -72,6 +74,21 @@ private Uri getFileUri(String filepath) throws IORejectionException {
7274
return uri;
7375
}
7476

77+
private String getOriginalFilepath(String filepath) throws IORejectionException {
78+
Uri uri = getFileUri(filepath);
79+
String originalFilepath = "";
80+
if (uri.getScheme().equals("content")) {
81+
try {
82+
Cursor cursor = reactContext.getContentResolver().query(uri, null, null, null, null);
83+
if (cursor.moveToFirst()) {
84+
originalFilepath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
85+
}
86+
} catch (IllegalArgumentException ignored) {
87+
}
88+
}
89+
return originalFilepath;
90+
}
91+
7592
private InputStream getInputStream(String filepath) throws IORejectionException {
7693
Uri uri = getFileUri(filepath);
7794
InputStream stream;
@@ -510,16 +527,17 @@ public void setReadable(String filepath, Boolean readable, Boolean ownerOnly, Pr
510527
@ReactMethod
511528
public void stat(String filepath, Promise promise) {
512529
try {
513-
File file = new File(filepath);
530+
String originalFilepath = getOriginalFilepath(filepath);
531+
File file = new File(originalFilepath);
514532

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

517535
WritableMap statMap = Arguments.createMap();
518-
519536
statMap.putInt("ctime", (int) (file.lastModified() / 1000));
520537
statMap.putInt("mtime", (int) (file.lastModified() / 1000));
521538
statMap.putInt("size", (int) file.length());
522539
statMap.putInt("type", file.isDirectory() ? 1 : 0);
540+
statMap.putString("originalFilepath", originalFilepath);
523541

524542
promise.resolve(statMap);
525543
} catch (Exception ex) {

0 commit comments

Comments
 (0)