Skip to content

Commit 7065f4e

Browse files
committed
Handle slice() from content URI using android content resolver
1 parent cf3df55 commit 7065f4e

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -680,37 +680,36 @@ static void ls(String path, Promise promise) {
680680
*/
681681
static void slice(String path, String dest, int start, int end, String encode, Promise promise) {
682682
try {
683-
path = ReactNativeBlobUtilUtils.normalizePath(path);
684683
dest = ReactNativeBlobUtilUtils.normalizePath(dest);
685-
File source = new File(path);
686-
if (source.isDirectory()) {
687-
promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory");
688-
return;
684+
685+
if (!path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_CONTENT)) {
686+
File file = new File(ReactNativeBlobUtilUtils.normalizePath(path));
687+
if (file.isDirectory()) {
688+
promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory");
689+
return;
690+
}
689691
}
690-
if (!source.exists()) {
692+
693+
InputStream in = inputStreamFromPath(path);
694+
if (in == null) {
691695
promise.reject("ENOENT", "No such file '" + path + "'");
692696
return;
693697
}
694-
int size = (int) source.length();
695-
int max = Math.min(size, end);
696-
int expected = max - start;
697-
int now = 0;
698-
FileInputStream in = new FileInputStream(new File(path));
699698
FileOutputStream out = new FileOutputStream(new File(dest));
700699
int skipped = (int) in.skip(start);
701700
if (skipped != start) {
702-
promise.reject("EUNSPECIFIED", "Skipped " + skipped + " instead of the specified " + start + " bytes, size is " + size);
701+
promise.reject("EUNSPECIFIED", "Skipped " + skipped + " instead of the specified " + start + " bytes");
703702
return;
704703
}
705704
byte[] buffer = new byte[10240];
706-
while (now < expected) {
705+
int remain = end - start;
706+
while (remain > 0) {
707707
int read = in.read(buffer, 0, 10240);
708-
int remain = expected - now;
709708
if (read <= 0) {
710709
break;
711710
}
712711
out.write(buffer, 0, (int) Math.min(remain, read));
713-
now += read;
712+
remain -= read;
714713
}
715714
in.close();
716715
out.flush();

0 commit comments

Comments
 (0)