Skip to content

Commit 4ceca95

Browse files
Ron RadtkeRon Radtke
authored andcommitted
fixing the issue for corrupted files (when using file cache)
1 parent 110c929 commit 4ceca95

File tree

2 files changed

+70
-58
lines changed

2 files changed

+70
-58
lines changed

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

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static void writeFile(String path, String encoding, String data, final boolean a
7070

7171
// write data from a file
7272
if (encoding.equalsIgnoreCase(ReactNativeBlobUtilConst.DATA_ENCODE_URI)) {
73-
String normalizedData = normalizePath(data);
73+
String normalizedData = ReactNativeBlobUtilUtils.normalizePath(data);
7474
File src = new File(normalizedData);
7575
if (!src.exists()) {
7676
promise.reject("ENOENT", "No such file '" + path + "' " + "('" + normalizedData + "')");
@@ -97,7 +97,7 @@ static void writeFile(String path, String encoding, String data, final boolean a
9797
}
9898
}
9999
} else {
100-
byte[] bytes = stringToBytes(data, encoding);
100+
byte[] bytes = ReactNativeBlobUtilUtils.stringToBytes(data, encoding);
101101
FileOutputStream fout = new FileOutputStream(f, append);
102102
try {
103103
fout.write(bytes);
@@ -167,7 +167,7 @@ static void writeFile(String path, ReadableArray data, final boolean append, fin
167167
* @param promise JS promise
168168
*/
169169
static void readFile(String path, String encoding, final Promise promise) {
170-
String resolved = normalizePath(path);
170+
String resolved = ReactNativeBlobUtilUtils.normalizePath(path);
171171
if (resolved != null)
172172
path = resolved;
173173
try {
@@ -337,7 +337,7 @@ static String getTmpPath(String taskId) {
337337
* @param bufferSize Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
338338
*/
339339
void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
340-
String resolved = normalizePath(path);
340+
String resolved = ReactNativeBlobUtilUtils.normalizePath(path);
341341
if (resolved != null)
342342
path = resolved;
343343

@@ -480,7 +480,7 @@ void writeStream(String path, String encoding, boolean append, Callback callback
480480
static void writeChunk(String streamId, String data, Callback callback) {
481481
ReactNativeBlobUtilFS fs = fileStreams.get(streamId);
482482
OutputStream stream = fs.writeStreamInstance;
483-
byte[] chunk = ReactNativeBlobUtilFS.stringToBytes(data, fs.encoding);
483+
byte[] chunk = ReactNativeBlobUtilUtils.stringToBytes(data, fs.encoding);
484484
try {
485485
stream.write(chunk);
486486
callback.invoke();
@@ -537,7 +537,7 @@ static void closeStream(String streamId, Callback callback) {
537537
*/
538538
static void unlink(String path, Callback callback) {
539539
try {
540-
String normalizedPath = normalizePath(path);
540+
String normalizedPath = ReactNativeBlobUtilUtils.normalizePath(path);
541541
ReactNativeBlobUtilFS.deleteRecursive(new File(normalizedPath));
542542
callback.invoke(null, true);
543543
} catch (Exception err) {
@@ -595,7 +595,7 @@ static void mkdir(String path, Promise promise) {
595595
* @param callback JS context callback
596596
*/
597597
static void cp(String path, String dest, Callback callback) {
598-
path = normalizePath(path);
598+
path = ReactNativeBlobUtilUtils.normalizePath(path);
599599
InputStream in = null;
600600
OutputStream out = null;
601601
String message = "";
@@ -700,7 +700,7 @@ static void exists(String path, Callback callback) {
700700
callback.invoke(false, false);
701701
}
702702
} else {
703-
path = normalizePath(path);
703+
path = ReactNativeBlobUtilUtils.normalizePath(path);
704704
if (path != null) {
705705
boolean exist = new File(path).exists();
706706
boolean isDir = new File(path).isDirectory();
@@ -719,7 +719,7 @@ static void exists(String path, Callback callback) {
719719
*/
720720
static void ls(String path, Promise promise) {
721721
try {
722-
path = normalizePath(path);
722+
path = ReactNativeBlobUtilUtils.normalizePath(path);
723723
File src = new File(path);
724724
if (!src.exists()) {
725725
promise.reject("ENOENT", "No such file '" + path + "'");
@@ -754,7 +754,7 @@ static void ls(String path, Promise promise) {
754754
*/
755755
static void slice(String path, String dest, int start, int end, String encode, Promise promise) {
756756
try {
757-
path = normalizePath(path);
757+
path = ReactNativeBlobUtilUtils.normalizePath(path);
758758
File source = new File(path);
759759
if (source.isDirectory()) {
760760
promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory");
@@ -796,7 +796,7 @@ static void slice(String path, String dest, int start, int end, String encode, P
796796
}
797797

798798
static void lstat(String path, final Callback callback) {
799-
path = normalizePath(path);
799+
path = ReactNativeBlobUtilUtils.normalizePath(path);
800800

801801
new AsyncTask<String, Integer, Integer>() {
802802
@Override
@@ -835,7 +835,7 @@ protected Integer doInBackground(String... args) {
835835
*/
836836
static void stat(String path, Callback callback) {
837837
try {
838-
path = normalizePath(path);
838+
path = ReactNativeBlobUtilUtils.normalizePath(path);
839839
WritableMap result = statFile(path);
840840
if (result == null)
841841
callback.invoke("failed to stat path `" + path + "` because it does not exist or it is not a folder", null);
@@ -854,7 +854,7 @@ static void stat(String path, Callback callback) {
854854
*/
855855
static WritableMap statFile(String path) {
856856
try {
857-
path = normalizePath(path);
857+
path = ReactNativeBlobUtilUtils.normalizePath(path);
858858
WritableMap stat = Arguments.createMap();
859859
if (isAsset(path)) {
860860
String name = path.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, "");
@@ -990,7 +990,7 @@ static void createFile(String path, String data, String encoding, Promise promis
990990
return;
991991
}
992992
OutputStream ostream = new FileOutputStream(dest);
993-
ostream.write(ReactNativeBlobUtilFS.stringToBytes(data, encoding));
993+
ostream.write(ReactNativeBlobUtilUtils.stringToBytes(data, encoding));
994994
}
995995
promise.resolve(path);
996996
} catch (Exception err) {
@@ -1085,25 +1085,6 @@ protected Integer doInBackground(ReadableArray... paths) {
10851085
task.execute(paths);
10861086
}
10871087

1088-
/**
1089-
* String to byte converter method
1090-
*
1091-
* @param data Raw data in string format
1092-
* @param encoding Decoder name
1093-
* @return Converted data byte array
1094-
*/
1095-
private static byte[] stringToBytes(String data, String encoding) {
1096-
if (encoding.equalsIgnoreCase("ascii")) {
1097-
return data.getBytes(Charset.forName("US-ASCII"));
1098-
} else if (encoding.toLowerCase().contains("base64")) {
1099-
return Base64.decode(data, Base64.NO_WRAP);
1100-
1101-
} else if (encoding.equalsIgnoreCase("utf8")) {
1102-
return data.getBytes(Charset.forName("UTF-8"));
1103-
}
1104-
return data.getBytes(Charset.forName("US-ASCII"));
1105-
}
1106-
11071088
/**
11081089
* Private method for emit read stream event.
11091090
*
@@ -1174,26 +1155,4 @@ static boolean isAsset(String path) {
11741155
return path != null && path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET);
11751156
}
11761157

1177-
/**
1178-
* Normalize the path, remove URI scheme (xxx://) so that we can handle it.
1179-
*
1180-
* @param path URI string.
1181-
* @return Normalized string
1182-
*/
1183-
static String normalizePath(String path) {
1184-
if (path == null)
1185-
return null;
1186-
if (!path.matches("\\w+\\:.*"))
1187-
return path;
1188-
if (path.startsWith("file://")) {
1189-
return path.replace("file://", "");
1190-
}
1191-
1192-
Uri uri = Uri.parse(path);
1193-
if (path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET)) {
1194-
return path;
1195-
} else
1196-
return PathResolver.getRealPathFromURI(ReactNativeBlobUtil.RCTContext, uri);
1197-
}
1198-
11991158
}

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import android.net.Uri;
77
import android.os.Build;
88
import android.os.Environment;
9+
import android.os.ParcelFileDescriptor;
910
import android.provider.MediaStore;
1011

1112
import com.ReactNativeBlobUtil.Utils.FileDescription;
1213
import com.facebook.react.bridge.Promise;
1314

1415
import java.io.File;
16+
import java.io.FileInputStream;
17+
import java.io.FileOutputStream;
1518
import java.io.IOException;
1619
import java.io.OutputStream;
1720

@@ -107,9 +110,59 @@ public static void writeToMediaFile(Uri fileUri, String data, Promise promise) {
107110
resolver.update(fileUri, contentValues, null, null);
108111

109112
// write data
110-
OutputStream outputStream = resolver.openOutputStream(fileUri);
111-
outputStream.write(data.getBytes());
112-
outputStream.close();
113+
//OutputStream outputStream = resolver.openOutputStream(fileUri);
114+
//outputStream.write(data.getBytes());
115+
//outputStream.close();
116+
117+
OutputStream stream = null;
118+
Uri uri = null;
119+
120+
try {
121+
ParcelFileDescriptor descr;
122+
try {
123+
assert fileUri != null;
124+
descr = appCtx.getContentResolver().openFileDescriptor(fileUri, "w");
125+
assert descr != null;
126+
String normalizedData = ReactNativeBlobUtilUtils.normalizePath(data);
127+
File src = new File(normalizedData);
128+
if (!src.exists()) {
129+
promise.reject("ENOENT", "No such file ('" + normalizedData + "')");
130+
return;
131+
}
132+
byte[] buf = new byte[10240];
133+
int read;
134+
int written = 0;
135+
136+
FileInputStream fin = new FileInputStream(src);
137+
FileOutputStream out = new FileOutputStream(descr.getFileDescriptor());
138+
139+
while ((read = fin.read(buf)) > 0) {
140+
out.write(buf, 0, read);
141+
}
142+
143+
fin.close();
144+
out.close();
145+
descr.close();
146+
} catch (Exception e) {
147+
e.printStackTrace();
148+
}
149+
150+
contentValues.clear();
151+
contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
152+
appCtx.getContentResolver().update(fileUri, contentValues, null, null);
153+
stream = resolver.openOutputStream(fileUri);
154+
if (stream == null) {
155+
throw new IOException("Failed to get output stream.");
156+
}
157+
} catch (IOException e) {
158+
// Don't leave an orphan entry in the MediaStore
159+
resolver.delete(uri, null, null);
160+
throw e;
161+
} finally {
162+
if (stream != null) {
163+
stream.close();
164+
}
165+
}
113166

114167
// remove pending
115168
contentValues = new ContentValues();

0 commit comments

Comments
 (0)