Skip to content

Commit 7d91ccb

Browse files
author
Ron Radtke
committed
deleting unused methods
adds untested copyToInternal method
1 parent 7335923 commit 7d91ccb

File tree

5 files changed

+67
-103
lines changed

5 files changed

+67
-103
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.app.DownloadManager;
55
import android.content.ActivityNotFoundException;
66
import android.content.Intent;
7-
import android.content.pm.PackageManager;
87
import android.net.Uri;
98
import android.os.Build;
109
import android.util.SparseArray;
@@ -427,15 +426,20 @@ public void createMediaFile(ReadableMap filedata, String mt, Promise promise) {
427426
}
428427
if (mt == null) promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid mediatype");
429428

430-
FileDescription file = new FileDescription(filedata.getString("name"),filedata.getString("mimeType"), filedata.getString("parentFolder"));
429+
FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
431430
Uri res = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt));
432431
if (res != null) promise.resolve(res.toString());
433432
else promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
434433
}
435434

436435
@ReactMethod
437-
public void writeToMediaFile(String fileUri, String data, Promise promise){
436+
public void writeToMediaFile(String fileUri, String data, Promise promise) {
438437
ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), data, promise);
439438
}
440439

440+
@ReactMethod
441+
public void copyToInternal(String contentUri, String destpath, Promise promise) {
442+
ReactNativeBlobUtilMediaCollection.copyToInternal(Uri.parse(contentUri), destpath, promise);
443+
}
444+
441445
}

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

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -430,105 +430,6 @@ else if (resolved == null) {
430430
}
431431
}
432432

433-
/**
434-
* Create a write stream and store its instance in ReactNativeBlobUtilFS.fileStreams
435-
*
436-
* @param path Target file path
437-
* @param encoding Should be one of `base64`, `utf8`, `ascii`
438-
* @param append Flag represents if the file stream overwrite existing content
439-
* @param callback Callback
440-
*/
441-
void writeStream(String path, String encoding, boolean append, Callback callback) {
442-
try {
443-
File dest = new File(path);
444-
File dir = dest.getParentFile();
445-
446-
if (!dest.exists()) {
447-
if (dir != null && !dir.exists()) {
448-
if (!dir.mkdirs() && !dir.exists()) {
449-
callback.invoke("ENOTDIR", "Failed to create parent directory of '" + path + "'");
450-
return;
451-
}
452-
}
453-
if (!dest.createNewFile()) {
454-
callback.invoke("ENOENT", "File '" + path + "' does not exist and could not be created");
455-
return;
456-
}
457-
} else if (dest.isDirectory()) {
458-
callback.invoke("EISDIR", "Expecting a file but '" + path + "' is a directory");
459-
return;
460-
}
461-
462-
OutputStream fs = new FileOutputStream(path, append);
463-
this.encoding = encoding;
464-
String streamId = UUID.randomUUID().toString();
465-
ReactNativeBlobUtilFS.fileStreams.put(streamId, this);
466-
this.writeStreamInstance = fs;
467-
callback.invoke(null, null, streamId);
468-
} catch (Exception err) {
469-
callback.invoke("EUNSPECIFIED", "Failed to create write stream at path `" + path + "`; " + err.getLocalizedMessage());
470-
}
471-
}
472-
473-
/**
474-
* Write a chunk of data into a file stream.
475-
*
476-
* @param streamId File stream ID
477-
* @param data Data chunk in string format
478-
* @param callback JS context callback
479-
*/
480-
static void writeChunk(String streamId, String data, Callback callback) {
481-
ReactNativeBlobUtilFS fs = fileStreams.get(streamId);
482-
OutputStream stream = fs.writeStreamInstance;
483-
byte[] chunk = ReactNativeBlobUtilUtils.stringToBytes(data, fs.encoding);
484-
try {
485-
stream.write(chunk);
486-
callback.invoke();
487-
} catch (Exception e) {
488-
callback.invoke(e.getLocalizedMessage());
489-
}
490-
}
491-
492-
/**
493-
* Write data using ascii array
494-
*
495-
* @param streamId File stream ID
496-
* @param data Data chunk in ascii array format
497-
* @param callback JS context callback
498-
*/
499-
static void writeArrayChunk(String streamId, ReadableArray data, Callback callback) {
500-
try {
501-
ReactNativeBlobUtilFS fs = fileStreams.get(streamId);
502-
OutputStream stream = fs.writeStreamInstance;
503-
byte[] chunk = new byte[data.size()];
504-
for (int i = 0; i < data.size(); i++) {
505-
chunk[i] = (byte) data.getInt(i);
506-
}
507-
stream.write(chunk);
508-
callback.invoke();
509-
} catch (Exception e) {
510-
callback.invoke(e.getLocalizedMessage());
511-
}
512-
}
513-
514-
/**
515-
* Close file write stream by ID
516-
*
517-
* @param streamId Stream ID
518-
* @param callback JS context callback
519-
*/
520-
static void closeStream(String streamId, Callback callback) {
521-
try {
522-
ReactNativeBlobUtilFS fs = fileStreams.get(streamId);
523-
OutputStream stream = fs.writeStreamInstance;
524-
fileStreams.remove(streamId);
525-
stream.close();
526-
callback.invoke();
527-
} catch (Exception err) {
528-
callback.invoke(err.getLocalizedMessage());
529-
}
530-
}
531-
532433
/**
533434
* Unlink file at path
534435
*

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.FileInputStream;
1717
import java.io.FileOutputStream;
1818
import java.io.IOException;
19+
import java.io.InputStream;
1920
import java.io.OutputStream;
2021

2122
public class ReactNativeBlobUtilMediaCollection {
@@ -171,4 +172,54 @@ public static void writeToMediaFile(Uri fileUri, String data, Promise promise) {
171172
}
172173
}
173174

175+
public static void copyToInternal(Uri contenturi, String destpath, Promise promise) {
176+
Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
177+
ContentResolver resolver = appCtx.getContentResolver();
178+
179+
InputStream in = null;
180+
OutputStream out = null;
181+
182+
if (!new File(destpath).exists()) {
183+
try {
184+
boolean result = new File(destpath).createNewFile();
185+
if (!result) {
186+
promise.reject("ReactNativeBlobUtil.copyToInternal: Destination file at '" + destpath + "' already exists");
187+
return;
188+
}
189+
} catch (IOException ioException) {
190+
promise.reject("ReactNativeBlobUtil.copyToInternal: Could not create file: " + ioException.getLocalizedMessage());
191+
}
192+
}
193+
194+
try {
195+
in = resolver.openInputStream(contenturi);
196+
out = new FileOutputStream(destpath);
197+
198+
byte[] buf = new byte[10240];
199+
int len;
200+
while ((len = in.read(buf)) > 0) {
201+
out.write(buf, 0, len);
202+
}
203+
204+
} catch (IOException e) {
205+
promise.reject("ReactNativeBlobUtil.copyToInternal: Could not write data: " + e.getLocalizedMessage());
206+
} finally {
207+
if (in != null) {
208+
try {
209+
in.close();
210+
} catch (IOException ioException) {
211+
ioException.printStackTrace();
212+
}
213+
}
214+
if (out != null) {
215+
try {
216+
out.close();
217+
} catch (IOException ioException) {
218+
ioException.printStackTrace();
219+
}
220+
}
221+
}
222+
223+
promise.resolve("");
224+
}
174225
}

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ export interface FS {
436436
slice(src: string, dest: string, start: number, end: number): Promise<void>;
437437

438438
asset(path: string): string;
439+
439440
df(): Promise<RNFetchBlobDf>;
440441
}
441442

@@ -790,4 +791,6 @@ export interface MediaCollection {
790791
createMediafile(filedata: { name: string, parentFolder: string, mimeType: string }, mediatype: Mediatype): Promise<string>;
791792

792793
writeToMediafile(uri: string, data: string)
794+
795+
copyToInternal(contenturi: string, destpath: string)
793796
}

mediacollection.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ function writeToMediafile(uri:string, data:string){
1212
return ReactNativeBlobUtil.writeToMediaFile(uri, data);
1313
}
1414

15+
function copyToInternal(contenturi: string, destpath: string) {
16+
return ReactNativeBlobUtil.copyToInternal(contenturi, destpath);
17+
}
18+
1519
export default {
1620
createMediafile,
17-
writeToMediafile
21+
writeToMediafile,
22+
copyToInternal
1823
};

0 commit comments

Comments
 (0)