forked from joltup/rn-fetch-blob
-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathReactNativeBlobUtilMediaCollection.java
314 lines (274 loc) · 13.3 KB
/
ReactNativeBlobUtilMediaCollection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package com.ReactNativeBlobUtil;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.util.Base64;
import com.ReactNativeBlobUtil.Utils.FileDescription;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableArray;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ReactNativeBlobUtilMediaCollection {
public enum MediaType {
Audio,
Image,
Video,
Download
}
private static Uri getMediaUri(MediaType mt) {
Uri res = null;
if (mt == MediaType.Audio) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
res = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
} else {
res = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
} else if (mt == MediaType.Video) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
res = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
} else {
res = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
}
} else if (mt == MediaType.Image) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
res = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
} else {
res = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
} else if (mt == MediaType.Download) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
res = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
}
}
return res;
}
private static String getRelativePath(MediaType mt, ReactApplicationContext ctx) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (mt == MediaType.Audio) return Environment.DIRECTORY_MUSIC;
if (mt == MediaType.Video) return Environment.DIRECTORY_MOVIES;
if (mt == MediaType.Image) return Environment.DIRECTORY_PICTURES;
if (mt == MediaType.Download) return Environment.DIRECTORY_DOWNLOADS;
return Environment.DIRECTORY_DOWNLOADS;
} else {
if (mt == MediaType.Audio) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyMusicDir").toString();
if (mt == MediaType.Video) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyMovieDir").toString();
if (mt == MediaType.Image) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyPictureDir").toString();
if (mt == MediaType.Download) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyDownloadDir").toString();
return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyDownloadDir").toString();
}
}
public static Uri createNewMediaFile(FileDescription file, MediaType mt, ReactApplicationContext ctx) {
// Add a specific media item.
Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
ContentResolver resolver = appCtx.getContentResolver();
ContentValues fileDetails = new ContentValues();
String relativePath = getRelativePath(mt, ctx);
String mimeType = file.mimeType;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
fileDetails.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis() / 1000);
fileDetails.put(MediaStore.MediaColumns.DATE_MODIFIED, System.currentTimeMillis() / 1000);
fileDetails.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
fileDetails.put(MediaStore.MediaColumns.DISPLAY_NAME, file.name);
fileDetails.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath + '/' + file.partentFolder);
Uri mediauri = getMediaUri(mt);
// Keeps a handle to the new file's URI in case we need to modify it later.
return resolver.insert(mediauri, fileDetails);
} else {
File f = new File(relativePath + file.getFullPath());
if (true) {
if (!f.exists()) {
File parent = f.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
return null;
}
try {
if (f.createNewFile()) ;
{
return Uri.fromFile(f);
}
} catch (IOException ioException) {
return null;
}
} else {
return Uri.fromFile(f);
}
}
}
return null;
}
public static boolean writeToMediaFile(Uri fileUri, String data, boolean transformFile, Promise promise, ReactApplicationContext ctx) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try {
Context appCtx = ctx.getApplicationContext();
ContentResolver resolver = appCtx.getContentResolver();
// set pending doesn't work right now. We would have to requery for the item
//ContentValues contentValues = new ContentValues();
//contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1);
//resolver.update(fileUri, contentValues, null, null);
// write data
OutputStream stream = null;
Uri uri = null;
try {
ParcelFileDescriptor descr;
try {
assert fileUri != null;
descr = appCtx.getContentResolver().openFileDescriptor(fileUri, "w");
assert descr != null;
String normalizedData = ReactNativeBlobUtilUtils.normalizePath(data);
File src = new File(normalizedData);
if (!src.exists()) {
promise.reject("ENOENT", "No such file ('" + normalizedData + "')");
return false;
}
FileInputStream fin = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(descr.getFileDescriptor());
if (transformFile) {
// in order to transform file, we must load the entire file onto memory
int length = (int) src.length();
byte[] bytes = new byte[length];
fin.read(bytes);
if (ReactNativeBlobUtilFileTransformer.sharedFileTransformer == null) {
throw new IllegalStateException("Write to media file with transform was specified but the shared file transformer is not set");
}
byte[] transformedBytes = ReactNativeBlobUtilFileTransformer.sharedFileTransformer.onWriteFile(bytes);
out.write(transformedBytes);
} else {
byte[] buf = new byte[10240];
int read;
while ((read = fin.read(buf)) > 0) {
out.write(buf, 0, read);
}
}
fin.close();
out.close();
descr.close();
} catch (Exception e) {
e.printStackTrace();
promise.reject(new IOException("Failed to get output stream."));
return false;
}
//contentValues.clear();
//contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
//appCtx.getContentResolver().update(fileUri, contentValues, null, null);
stream = resolver.openOutputStream(fileUri);
if (stream == null) {
promise.reject(new IOException("Failed to get output stream."));
return false;
}
} catch (IOException e) {
// Don't leave an orphan entry in the MediaStore
resolver.delete(uri, null, null);
promise.reject(e);
return false;
} finally {
if (stream != null) {
stream.close();
}
}
// remove pending
//contentValues = new ContentValues();
//contentValues.put(MediaStore.MediaColumns.IS_PENDING, 0);
//resolver.update(fileUri, contentValues, null, null);
} catch (IOException e) {
promise.reject("ReactNativeBlobUtil.createMediaFile", "Cannot write to file, file might not exist");
return false;
}
return true;
} else {
return ReactNativeBlobUtilFS.writeFile(ReactNativeBlobUtilUtils.normalizePath(fileUri.toString()), ReactNativeBlobUtilConst.DATA_ENCODE_URI, data, false);
}
}
public static void copyToInternal(Uri contenturi, String destpath, Promise promise) {
Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
ContentResolver resolver = appCtx.getContentResolver();
InputStream in = null;
OutputStream out = null;
File f = new File((destpath));
if (!f.exists()) {
try {
File parent = f.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
promise.reject("ReactNativeBlobUtil.copyToInternal: Cannot create parent folders<'" + destpath);
return;
}
boolean result = f.createNewFile();
if (!result) {
promise.reject("ReactNativeBlobUtil.copyToInternal: Destination file at '" + destpath + "' already exists");
return;
}
} catch (IOException ioException) {
promise.reject("ReactNativeBlobUtil.copyToInternal: Could not create file: " + ioException.getLocalizedMessage());
}
}
try {
in = resolver.openInputStream(contenturi);
out = new FileOutputStream(destpath);
byte[] buf = new byte[10240];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (IOException e) {
promise.reject("ReactNativeBlobUtil.copyToInternal: Could not write data: " + e.getLocalizedMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
promise.resolve("");
}
public static void getBlob(Uri contentUri, String encoding, Promise promise) {
Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
ContentResolver resolver = appCtx.getContentResolver();
try {
InputStream in = resolver.openInputStream(contentUri);
int length = in.available();
byte[] bytes = new byte[length];
int bytesRead = in.read(bytes);
in.close();
if (bytesRead < length) {
promise.reject("EUNSPECIFIED", "Read only " + bytesRead + " bytes of " + length);
return;
}
switch (encoding.toLowerCase()) {
case "base64":
promise.resolve(Base64.encodeToString(bytes, Base64.NO_WRAP));
break;
case "ascii":
WritableArray asciiResult = Arguments.createArray();
for (byte b : bytes) {
asciiResult.pushInt(b);
}
promise.resolve(asciiResult);
break;
default: // covers utf-8
promise.resolve(new String(bytes));
break;
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}