Skip to content

Commit 81f1eda

Browse files
Adding mediaFileWithTransform
1 parent 4d2dc89 commit 81f1eda

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,14 @@ await ReactNativeBlobUtil.MediaCollection.writeToMediafile('content://....', //
696696
);
697697
````
698698

699+
Copies and tranforms data from a file in the apps storage to an existing entry of the Media Store. NOTE: you must set a transformer on the file in order for the transformation to happen (see [Setting a File Transformer](#Setting-A-File-Transformer)).
700+
701+
````js
702+
await ReactNativeBlobUtil.MediaCollection.writeToMediafileWithTransform('content://....', // content uri of the entry in the media storage
703+
localpath // path to the file that should be copied
704+
);
705+
````
706+
699707
#### copyToInternal
700708
Copies an entry form the media storage to the apps internal storage.
701709
````js

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ public void createMediaFile(ReadableMap filedata, String mt, Promise promise) {
438438
}
439439

440440
@ReactMethod
441-
public void writeToMediaFile(String fileUri, String path, Promise promise) {
442-
boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, promise);
441+
public void writeToMediaFile(String fileUri, String path, boolean transformFile, Promise promise) {
442+
boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, transformFile, promise);
443443
if(res) promise.resolve("Success");
444444
}
445445

@@ -478,7 +478,7 @@ public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promi
478478
return;
479479
}
480480

481-
boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, promise);
481+
boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, false, promise);
482482
if(res) promise.resolve(fileuri.toString());
483483
}
484484

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public static Uri createNewMediaFile(FileDescription file, MediaType mt, ReactAp
124124
return null;
125125
}
126126

127-
public static boolean writeToMediaFile(Uri fileUri, String data, Promise promise) {
127+
public static boolean writeToMediaFile(Uri fileUri, String data, boolean transformFile, Promise promise) {
128128
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
129129
try {
130130
Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
@@ -151,16 +151,31 @@ public static boolean writeToMediaFile(Uri fileUri, String data, Promise promise
151151
promise.reject("ENOENT", "No such file ('" + normalizedData + "')");
152152
return false;
153153
}
154-
byte[] buf = new byte[10240];
155-
int read;
154+
156155

157156
FileInputStream fin = new FileInputStream(src);
158157
FileOutputStream out = new FileOutputStream(descr.getFileDescriptor());
159158

160-
while ((read = fin.read(buf)) > 0) {
161-
out.write(buf, 0, read);
159+
if (transformFile) {
160+
// in order to transform file, we must load the entire file onto memory
161+
int length = (int) src.length();
162+
byte[] bytes = new byte[length];
163+
fin.read(bytes);
164+
if (ReactNativeBlobUtilFileTransformer.sharedFileTransformer == null) {
165+
throw new IllegalStateException("Write to media file with transform was specified but the shared file transformer is not set");
166+
}
167+
byte[] transformedBytes = ReactNativeBlobUtilFileTransformer.sharedFileTransformer.onWriteFile(bytes);
168+
out.write(transformedBytes);
169+
} else {
170+
byte[] buf = new byte[10240];
171+
int read;
172+
173+
while ((read = fin.read(buf)) > 0) {
174+
out.write(buf, 0, read);
175+
}
162176
}
163177

178+
164179
fin.close();
165180
out.close();
166181
descr.close();

index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,13 @@ export interface MediaCollection {
830830
*/
831831
writeToMediafile(uri: string, path: string): Promise<string>
832832

833+
/**
834+
* Copies and transforms an existing file to a mediastore file. Make sure FileTransformer is set
835+
* @param uri URI of the destination mediastore file
836+
* @param path Path to the existing file which should be copied
837+
*/
838+
writeToMediafileWithTransform(uri: string, path: string): Promise<string>
839+
833840
/**
834841
* Copies a file from the mediastore to the apps internal storage
835842
* @param contenturi URI of the mediastore file

mediacollection.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ function createMediafile(fd: filedescriptor, mediatype: string): Promise {
99
}
1010

1111
function writeToMediafile(uri: string, path: string) {
12-
return ReactNativeBlobUtil.writeToMediaFile(uri, path);
12+
return ReactNativeBlobUtil.writeToMediaFile(uri, path, false);
13+
}
14+
15+
function writeToMediafileWithTransform(uri: string, path: string) {
16+
return ReactNativeBlobUtil.writeToMediaFile(uri, path, true);
1317
}
1418

1519
function copyToInternal(contenturi: string, destpath: string) {

0 commit comments

Comments
 (0)