Skip to content

Commit 56ba64b

Browse files
author
Ron Radtke
committed
adding js interface
1 parent 52d41ab commit 56ba64b

File tree

5 files changed

+92
-12
lines changed

5 files changed

+92
-12
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import androidx.annotation.NonNull;
1313
import androidx.core.content.FileProvider;
1414

15+
import com.ReactNativeBlobUtil.Utils.FileDescription;
1516
import com.facebook.react.bridge.ActivityEventListener;
1617
import com.facebook.react.bridge.Callback;
1718
import com.facebook.react.bridge.LifecycleEventListener;
@@ -426,4 +427,14 @@ public void getSDCardApplicationDir(Promise promise) {
426427
ReactNativeBlobUtilFS.getSDCardApplicationDir(this.getReactApplicationContext(), promise);
427428
}
428429

430+
@ReactMethod
431+
public void createMediaFile(ReadableMap filedata, ReactNativeBlobUtilMediaCollection.MediaType mt, Promise promise) {
432+
if (!(filedata.hasKey("name") || filedata.hasKey("partentFolder") || filedata.hasKey("mimeType"))) promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid filedata");
433+
434+
FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("partentFolder"), filedata.getString("mimeType"));
435+
Uri res = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, mt);
436+
if (res != null) promise.resolve(res);
437+
else promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
438+
}
439+
429440
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public enum MediaType {
2121
Download
2222
}
2323

24-
private Uri getMediaUri(MediaType mt) {
24+
private static Uri getMediaUri(MediaType mt) {
2525
Uri res = null;
2626
if (mt == MediaType.Audio) {
2727
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
@@ -50,15 +50,15 @@ private Uri getMediaUri(MediaType mt) {
5050
return res;
5151
}
5252

53-
private String getRelativePath(MediaType mt) {
53+
private static String getRelativePath(MediaType mt) {
5454
if (mt == MediaType.Audio) return Environment.DIRECTORY_MUSIC;
5555
if (mt == MediaType.Video) return Environment.DIRECTORY_MOVIES;
5656
if (mt == MediaType.Image) return Environment.DIRECTORY_PICTURES;
5757
if (mt == MediaType.Download) return Environment.DIRECTORY_DOWNLOADS;
5858
return Environment.DIRECTORY_DOWNLOADS;
5959
}
6060

61-
public Uri createNewMediaFile(FileDescription file, MediaType mt) {
61+
public static Uri createNewMediaFile(FileDescription file, MediaType mt) {
6262
// Add a specific media item.
6363
Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
6464
ContentResolver resolver = appCtx.getContentResolver();
@@ -76,14 +76,18 @@ public Uri createNewMediaFile(FileDescription file, MediaType mt) {
7676

7777
Uri mediauri = getMediaUri(mt);
7878

79-
// Keeps a handle to the new song's URI in case we need to modify it later.
79+
// Keeps a handle to the new file's URI in case we need to modify it later.
8080
return resolver.insert(mediauri, fileDetails);
81-
}
82-
else{
83-
File directory = Environment.getExternalStoragePublicDirectory(relativePath);
84-
if(directory.canWrite()){
85-
File f = new File(relativePath + '/' + file.getFullPath());
86-
}
81+
} else {
82+
File directory = Environment.getExternalStoragePublicDirectory(relativePath);
83+
if (directory.canWrite()) {
84+
File f = new File(relativePath + '/' + file.getFullPath());
85+
if (!f.exists()) {
86+
boolean result = f.mkdirs();
87+
if (result) return Uri.fromFile(f);
88+
}
89+
90+
}
8791
}
8892

8993
return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class FileDescription {
77
public String partentFolder;
88
public String mimeType;
99

10-
FileDescription(String n, String mT, String pF) {
10+
public FileDescription(String n, String mT, String pF) {
1111
name = n;
1212
partentFolder = pF != null ? pF : "";
1313
mimeType = mT;

index.d.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ export default ReactNativeBlobUtil;
1010
interface ReactNativeBlobUtilStatic {
1111
fetch(method: Methods, url: string, headers?: { [key: string]: string }, body?: any
1212
| null): StatefulPromise<FetchBlobResponse>;
13+
1314
base64: { encode(input: string): string; decode(input: string): string };
1415
android: AndroidApi;
1516
ios: IOSApi;
17+
1618
config(options: ReactNativeBlobUtilConfig): ReactNativeBlobUtilStatic;
19+
1720
session(name: string): ReactNativeBlobUtilSession;
21+
1822
fs: FS;
23+
1924
wrap(path: string): string;
25+
2026
net: Net;
2127
polyfill: Polyfill;
2228
// this require external module https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/oboe
@@ -45,10 +51,15 @@ export declare class ReactNativeBlobUtilFetchPolyfill {
4551

4652
export interface ReactNativeBlobUtilFetchRepsonse {
4753
arrayBuffer(): Promise<any[]>;
54+
4855
blob(): Promise<PolyfillBlob>;
56+
4957
json(): Promise<any>;
58+
5059
rawResp(): Promise<FetchBlobResponse>;
60+
5161
text(): Promise<string>;
62+
5263
bodyUsed: boolean;
5364
headers: any;
5465
ok: boolean;
@@ -64,46 +75,58 @@ export interface ReactNativeBlobUtilFetchRepsonse {
6475
*/
6576
export interface FetchBlobResponse {
6677
taskId: string;
78+
6779
/**
6880
* get path of response temp file
6981
* @return File path of temp file.
7082
*/
7183
path(): string;
84+
7285
type: "base64" | "path" | "utf8";
7386
data: any;
87+
7488
/**
7589
* Convert result to javascript ReactNativeBlobUtil object.
7690
* @return Return a promise resolves Blob object.
7791
*/
7892
blob(contentType: string, sliceSize: number): Promise<PolyfillBlob>;
93+
7994
/**
8095
* Convert result to text.
8196
* @return Decoded base64 string.
8297
*/
8398
text(): string | Promise<any>;
99+
84100
/**
85101
* Convert result to JSON object.
86102
* @return Parsed javascript object.
87103
*/
88104
json(): any;
105+
89106
/**
90107
* Return BASE64 string directly.
91108
* @return BASE64 string of response body.
92109
*/
93110
base64(): any;
111+
94112
/**
95113
* Remove cahced file
96114
*/
97115
flush(): void;
116+
98117
respInfo: ReactNativeBlobUtilResponseInfo;
118+
99119
info(): ReactNativeBlobUtilResponseInfo;
120+
100121
session(name: string): ReactNativeBlobUtilSession | null;
122+
101123
/**
102124
* Read file content with given encoding, if the response does not contains
103125
* a file path, show warning message
104126
* @param encode Encode type, should be one of `base64`, `ascrii`, `utf8`.
105127
*/
106128
readFile(encode: Encoding): Promise<any> | null;
129+
107130
/**
108131
* Start read stream from cached file
109132
* @param encode Encode type, should be one of `base64`, `ascrii`, `utf8`.
@@ -113,17 +136,27 @@ export interface FetchBlobResponse {
113136

114137
export interface PolyfillFileReader extends EventTarget {
115138
isRNFBPolyFill: boolean;
139+
116140
onloadstart(e: Event): void;
141+
117142
onprogress(e: Event): void;
143+
118144
onload(e: Event): void;
145+
119146
onabort(e: Event): void;
147+
120148
onerror(e: Event): void;
149+
121150
onloadend(e: Event): void;
122151

123152
abort(): void;
153+
124154
readAsArrayBuffer(b: PolyfillBlob): void;
155+
125156
readAsBinaryString(b: PolyfillBlob): void;
157+
126158
readAsText(b: PolyfillBlob, label?: string): void;
159+
127160
readAsDataURL(b: PolyfillBlob): void;
128161

129162
readyState: number;
@@ -242,6 +275,7 @@ export interface PolyfillXMLHttpRequest extends PolyfillXMLHttpRequestEventTarge
242275
getAllResponseHeaders(): string | null;
243276

244277
onreadystatechange(e: Event): void;
278+
245279
readyState: number;
246280
status: number;
247281
statusText: string;
@@ -270,11 +304,17 @@ export declare namespace PolyfillXMLHttpRequest {
270304

271305
export interface PolyfillXMLHttpRequestEventTarget extends EventTarget {
272306
onabort(e: Event): void;
307+
273308
onerror(e: Event): void;
309+
274310
onload(e: Event): void;
311+
275312
onloadstart(e: Event): void;
313+
276314
onprogress(e: Event): void;
315+
277316
ontimeout(e: Event): void;
317+
278318
onloadend(e: Event): void;
279319
}
280320

@@ -295,6 +335,7 @@ export interface Net {
295335
}
296336

297337
type HashAlgorithm = "md5" | "sha1" | "sha224" | "sha256" | "sha384" | "sha512";
338+
298339
export interface FS {
299340
ReactNativeBlobUtilSession: ReactNativeBlobUtilSession;
300341

@@ -364,6 +405,7 @@ export interface FS {
364405
* @param encoding Encoding of read stream.
365406
*/
366407
readFile(path: string, encoding: Encoding, bufferSize?: number): Promise<any>;
408+
367409
/**
368410
* Check if file exists and if it is a folder.
369411
* @param path Path to check
@@ -391,7 +433,9 @@ export interface FS {
391433
dirs: Dirs;
392434

393435
slice(src: string, dest: string, start: number, end: number): Promise<void>;
436+
394437
asset(path: string): string;
438+
395439
df(): Promise<{ free: number, total: number }>;
396440
}
397441

@@ -414,6 +458,7 @@ export interface ReactNativeBlobUtilWriteStream {
414458
append: boolean;
415459

416460
write(data: string): Promise<void>;
461+
417462
close(): void;
418463
}
419464

@@ -512,7 +557,7 @@ export interface AndroidApi {
512557
actionViewIntent(path: string, mime: string, chooserTitle?: string): Promise<any>;
513558

514559
/**
515-
*
560+
*
516561
* This method brings up OS default file picker and resolves a file URI when the user selected a file.
517562
* However, it does not resolve or reject when user dismiss the file picker via pressing hardware back button,
518563
* but you can still handle this behavior via AppState.
@@ -703,7 +748,9 @@ export interface ReactNativeBlobUtilResponseInfo {
703748

704749
export interface ReactNativeBlobUtilStream {
705750
onData(): void;
751+
706752
onError(): void;
753+
707754
onEnd(): void;
708755
}
709756

@@ -717,3 +764,13 @@ export declare class ReactNativeBlobUtilStat {
717764
path: string;
718765
filename: string;
719766
}
767+
768+
export type Mediatype = "Audio" | "Image" | "Video" | "Download";
769+
export interface MediaCollection {
770+
/**
771+
* Creates a new File in the collection.
772+
* @param filedata
773+
* @param mediatype
774+
*/
775+
createMediafile(filedata: { path: string, parentFolder: string, mimeType: string }, mediatype: Mediatype): Promise<string>;
776+
}

mediacollection.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {NativeModules} from 'react-native'
2+
3+
const ReactNativeBlobUtil: ReactNativeBlobUtilNative = NativeModules.ReactNativeBlobUtil
4+
5+
function createMediafile(filedata: Object<{ path: string, parentFolder: string, mimeType: string }>, mediatype: string): Promise {
6+
if ((!'parentFolder' in filedata)) filedata['parentFolder'] = '';
7+
return ReactNativeBlobUtil.createMediaFile(filedata, mediatype)
8+
}

0 commit comments

Comments
 (0)