Skip to content

Commit 20799fa

Browse files
author
Rick Terrill
committed
Encrypting m4a and mp4 files on download
1 parent 821eeb0 commit 20799fa

File tree

5 files changed

+472
-314
lines changed

5 files changed

+472
-314
lines changed

android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

100644100755
Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@
2828
import java.util.concurrent.LinkedBlockingQueue;
2929
import java.util.concurrent.ThreadPoolExecutor;
3030
import java.util.concurrent.TimeUnit;
31+
import java.util.Arrays;
3132

3233
import static android.app.Activity.RESULT_OK;
3334
import static com.RNFetchBlob.RNFetchBlobConst.GET_CONTENT_INTENT;
3435

36+
import android.util.Log;
37+
3538
public class RNFetchBlob extends ReactContextBaseJavaModule {
3639

40+
private static final String TAG = "RNFetchBlob";
41+
3742
// Cookies
3843
private final ForwardingCookieHandler mCookieHandler;
3944
private final CookieJarContainer mCookieJarContainer;
@@ -47,6 +52,9 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
4752
static public boolean ActionViewVisible = false;
4853
static HashMap<Integer, Promise> promiseTable = new HashMap<>();
4954

55+
// Added by Rick Terrill 11-2017
56+
public static byte[] appSalt;
57+
5058
public RNFetchBlob(ReactApplicationContext reactContext) {
5159

5260
super(reactContext);
@@ -60,7 +68,7 @@ public RNFetchBlob(ReactApplicationContext reactContext) {
6068
reactContext.addActivityEventListener(new ActivityEventListener() {
6169
@Override
6270
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
63-
if(requestCode == GET_CONTENT_INTENT && resultCode == RESULT_OK) {
71+
if (requestCode == GET_CONTENT_INTENT && resultCode == RESULT_OK) {
6472
Uri d = data.getData();
6573
promiseTable.get(GET_CONTENT_INTENT).resolve(d.toString());
6674
promiseTable.remove(GET_CONTENT_INTENT);
@@ -84,6 +92,14 @@ public Map<String, Object> getConstants() {
8492
return RNFetchBlobFS.getSystemfolders(this.getReactApplicationContext());
8593
}
8694

95+
// Added by Rick Terrill 11-2017
96+
@ReactMethod
97+
public void setSalt(String salt) {
98+
Log.d(TAG, "setting app salt: " + salt);
99+
RNFetchBlob.appSalt = Arrays.copyOfRange(salt.getBytes(), 0, 8);
100+
Log.d(TAG, "setting app salt: " + Arrays.toString(RNFetchBlob.appSalt));
101+
}
102+
87103
@ReactMethod
88104
public void createFile(final String path, final String content, final String encode, final Callback callback) {
89105
threadPool.execute(new Runnable() {
@@ -98,16 +114,15 @@ public void run() {
98114
@ReactMethod
99115
public void actionViewIntent(String path, String mime, final Promise promise) {
100116
try {
101-
Intent intent= new Intent(Intent.ACTION_VIEW)
102-
.setDataAndType(Uri.parse("file://" + path), mime);
117+
Intent intent = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.parse("file://" + path), mime);
103118
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
104119
this.getReactApplicationContext().startActivity(intent);
105120
ActionViewVisible = true;
106121

107122
final LifecycleEventListener listener = new LifecycleEventListener() {
108123
@Override
109124
public void onHostResume() {
110-
if(ActionViewVisible)
125+
if (ActionViewVisible)
111126
promise.resolve(null);
112127
RCTContext.removeLifecycleEventListener(this);
113128
}
@@ -123,7 +138,7 @@ public void onHostDestroy() {
123138
}
124139
};
125140
RCTContext.addLifecycleEventListener(listener);
126-
} catch(Exception ex) {
141+
} catch (Exception ex) {
127142
promise.reject(ex.getLocalizedMessage());
128143
}
129144
}
@@ -211,7 +226,8 @@ public void run() {
211226
}
212227

213228
@ReactMethod
214-
public void writeFileArray(final String path, final ReadableArray data, final boolean append, final Promise promise) {
229+
public void writeFileArray(final String path, final ReadableArray data, final boolean append,
230+
final Promise promise) {
215231
threadPool.execute(new Runnable() {
216232
@Override
217233
public void run() {
@@ -221,7 +237,8 @@ public void run() {
221237
}
222238

223239
@ReactMethod
224-
public void writeFile(final String path, final String encoding, final String data, final boolean append, final Promise promise) {
240+
public void writeFile(final String path, final String encoding, final String data, final boolean append,
241+
final Promise promise) {
225242
threadPool.execute(new Runnable() {
226243
@Override
227244
public void run() {
@@ -248,13 +265,13 @@ public void scanFile(final ReadableArray pairs, final Callback callback) {
248265
@Override
249266
public void run() {
250267
int size = pairs.size();
251-
String [] p = new String[size];
252-
String [] m = new String[size];
253-
for(int i=0;i<size;i++) {
268+
String[] p = new String[size];
269+
String[] m = new String[size];
270+
for (int i = 0; i < size; i++) {
254271
ReadableMap pair = pairs.getMap(i);
255-
if(pair.hasKey("path")) {
272+
if (pair.hasKey("path")) {
256273
p[i] = pair.getString("path");
257-
if(pair.hasKey("mime"))
274+
if (pair.hasKey("mime"))
258275
m[i] = pair.getString("mime");
259276
else
260277
m[i] = null;
@@ -272,7 +289,8 @@ public void run() {
272289
* @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8`
273290
* @param bufferSize Stream buffer size, default to 4096 or 4095(base64).
274291
*/
275-
public void readStream(final String path, final String encoding, final int bufferSize, final int tick, final String streamId) {
292+
public void readStream(final String path, final String encoding, final int bufferSize, final int tick,
293+
final String streamId) {
276294
final ReactApplicationContext ctx = this.getReactApplicationContext();
277295
fsThreadPool.execute(new Runnable() {
278296
@Override
@@ -300,7 +318,8 @@ public void slice(String src, String dest, int start, int end, Promise promise)
300318

301319
@ReactMethod
302320
public void enableProgressReport(String taskId, int interval, int count) {
303-
RNFetchBlobProgressConfig config = new RNFetchBlobProgressConfig(true, interval, count, RNFetchBlobProgressConfig.ReportType.Download);
321+
RNFetchBlobProgressConfig config = new RNFetchBlobProgressConfig(true, interval, count,
322+
RNFetchBlobProgressConfig.ReportType.Download);
304323
RNFetchBlobReq.progressReport.put(taskId, config);
305324
}
306325

@@ -314,27 +333,29 @@ public void run() {
314333
});
315334
}
316335

317-
318336
@ReactMethod
319337
public void enableUploadProgressReport(String taskId, int interval, int count) {
320-
RNFetchBlobProgressConfig config = new RNFetchBlobProgressConfig(true, interval, count, RNFetchBlobProgressConfig.ReportType.Upload);
338+
RNFetchBlobProgressConfig config = new RNFetchBlobProgressConfig(true, interval, count,
339+
RNFetchBlobProgressConfig.ReportType.Upload);
321340
RNFetchBlobReq.uploadProgressReport.put(taskId, config);
322341
}
323342

324343
@ReactMethod
325-
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
344+
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers,
345+
String body, final Callback callback) {
326346
new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
327-
}
347+
}
328348

329349
@ReactMethod
330-
public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
350+
public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers,
351+
ReadableArray body, final Callback callback) {
331352
new RNFetchBlobReq(options, taskId, method, url, headers, null, body, mClient, callback).run();
332353
}
333354

334355
@ReactMethod
335356
public void getContentIntent(String mime, Promise promise) {
336357
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
337-
if(mime != null)
358+
if (mime != null)
338359
i.setType(mime);
339360
else
340361
i.setType("*/*");
@@ -344,27 +365,23 @@ public void getContentIntent(String mime, Promise promise) {
344365
}
345366

346367
@ReactMethod
347-
public void addCompleteDownload (ReadableMap config, Promise promise) {
348-
DownloadManager dm = (DownloadManager) RNFetchBlob.RCTContext.getSystemService(RNFetchBlob.RCTContext.DOWNLOAD_SERVICE);
368+
public void addCompleteDownload(ReadableMap config, Promise promise) {
369+
DownloadManager dm = (DownloadManager) RNFetchBlob.RCTContext
370+
.getSystemService(RNFetchBlob.RCTContext.DOWNLOAD_SERVICE);
349371
String path = RNFetchBlobFS.normalizePath(config.getString("path"));
350-
if(path == null) {
351-
promise.reject("RNFetchblob.addCompleteDownload can not resolve URI:" + config.getString("path"), "RNFetchblob.addCompleteDownload can not resolve URI:" + path);
372+
if (path == null) {
373+
promise.reject("RNFetchblob.addCompleteDownload can not resolve URI:" + config.getString("path"),
374+
"RNFetchblob.addCompleteDownload can not resolve URI:" + path);
352375
return;
353376
}
354377
try {
355378
WritableMap stat = RNFetchBlobFS.statFile(path);
356-
dm.addCompletedDownload(
357-
config.hasKey("title") ? config.getString("title") : "",
358-
config.hasKey("description") ? config.getString("description") : "",
359-
true,
360-
config.hasKey("mime") ? config.getString("mime") : null,
361-
path,
362-
Long.valueOf(stat.getString("size")),
363-
config.hasKey("showNotification") && config.getBoolean("showNotification")
364-
);
379+
dm.addCompletedDownload(config.hasKey("title") ? config.getString("title") : "",
380+
config.hasKey("description") ? config.getString("description") : "", true,
381+
config.hasKey("mime") ? config.getString("mime") : null, path, Long.valueOf(stat.getString("size")),
382+
config.hasKey("showNotification") && config.getBoolean("showNotification"));
365383
promise.resolve(null);
366-
}
367-
catch(Exception ex) {
384+
} catch (Exception ex) {
368385
promise.reject("RNFetchblob.addCompleteDownload failed", ex.getStackTrace().toString());
369386
}
370387

0 commit comments

Comments
 (0)