Skip to content

Commit cf3df55

Browse files
committed
Handle hash() from content URI using android content resolver
1 parent 4be9547 commit cf3df55

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -846,31 +846,27 @@ static void hash(String path, String algorithm, Promise promise) {
846846
return;
847847
}
848848

849-
path = ReactNativeBlobUtilUtils.normalizePath(path);
850-
851-
File file = new File(path);
852-
853-
if (file.isDirectory()) {
854-
promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory");
855-
return;
849+
if (!path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_CONTENT)) {
850+
File file = new File(ReactNativeBlobUtilUtils.normalizePath(path));
851+
if (file.isDirectory()) {
852+
promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory");
853+
return;
854+
}
856855
}
857856

858-
if (!file.exists()) {
857+
MessageDigest md = MessageDigest.getInstance(algorithms.get(algorithm));
858+
859+
InputStream inputStream = inputStreamFromPath(path);
860+
if (inputStream == null) {
859861
promise.reject("ENOENT", "No such file '" + path + "'");
860862
return;
861863
}
862-
863-
MessageDigest md = MessageDigest.getInstance(algorithms.get(algorithm));
864-
865-
FileInputStream inputStream = new FileInputStream(path);
866864
int chunkSize = 4096 * 256; // 1Mb
867865
byte[] buffer = new byte[chunkSize];
868866

869-
if (file.length() != 0) {
870-
int bytesRead;
871-
while ((bytesRead = inputStream.read(buffer)) != -1) {
872-
md.update(buffer, 0, bytesRead);
873-
}
867+
int bytesRead;
868+
while ((bytesRead = inputStream.read(buffer)) != -1) {
869+
md.update(buffer, 0, bytesRead);
874870
}
875871

876872
StringBuilder hexString = new StringBuilder();

0 commit comments

Comments
 (0)