Skip to content

Commit 976734b

Browse files
author
Ron Radtke
committed
adding copyToMediaStore
1 parent 70d26fb commit 976734b

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,30 @@ public void copyToInternal(String contentUri, String destpath, Promise promise)
447447
}
448448

449449
@ReactMethod
450-
public void getBlob(String contentUri, String encoding, Promise promise) {
450+
public void getBlob(String contentUri, String encoding, Promise promise) {
451451
ReactNativeBlobUtilMediaCollection.getBlob(Uri.parse(contentUri), encoding, promise);
452452
}
453453

454+
@ReactMethod
455+
public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promise promise) {
456+
if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) {
457+
promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid filedata: " + filedata.toString());
458+
return;
459+
}
460+
if (mt == null) {
461+
promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid mediatype");
462+
return;
463+
}
464+
465+
FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
466+
Uri fileuri = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt));
467+
468+
if (fileuri == null) {
469+
promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
470+
return;
471+
}
472+
473+
ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, promise);
474+
}
475+
454476
}

index.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
export const ReactNativeBlobUtil: ReactNativeBlobUtilStatic;
77
export type ReactNativeBlobUtil = ReactNativeBlobUtilStatic;
88
export default ReactNativeBlobUtil;
9+
import {filedescriptor} from './types';
910

1011
interface ReactNativeBlobUtilStatic {
1112
fetch(method: Methods, url: string, headers?: { [key: string]: string }, body?: any
@@ -783,12 +784,21 @@ export declare class ReactNativeBlobUtilStat {
783784
export type Mediatype = "Audio" | "Image" | "Video" | "Download";
784785

785786
export interface MediaCollection {
787+
/**
788+
* Creates a new File in the collection.
789+
* @param filedata descriptor for the media store entry
790+
* @param mediatype
791+
* @param path path of the file being copied
792+
*/
793+
copyToMediaStore(filedata: filedescriptor, mediatype: Mediatype, path: string): Promise<string>;
794+
795+
786796
/**
787797
* Creates a new File in the collection.
788798
* @param filedata
789799
* @param mediatype
790800
*/
791-
createMediafile(filedata: { name: string, parentFolder: string, mimeType: string }, mediatype: Mediatype): Promise<string>;
801+
createMediafile(filedata: filedescriptor, mediatype: Mediatype): Promise<string>;
792802

793803
/**
794804
* Copies an existing file to a mediastore file

mediacollection.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {NativeModules} from 'react-native';
2-
import type {ReactNativeBlobUtilNative} from "types";
2+
import type {ReactNativeBlobUtilNative, filedescriptor} from "types";
33

44
const ReactNativeBlobUtil: ReactNativeBlobUtilNative = NativeModules.ReactNativeBlobUtil;
55

6-
function createMediafile(filedata: Object<{ path: string, parentFolder: string, mimeType: string }>, mediatype: string): Promise {
7-
if ((!'parentFolder' in filedata)) filedata['parentFolder'] = '';
8-
return ReactNativeBlobUtil.createMediaFile(filedata, mediatype);
6+
function createMediafile(fd: filedescriptor, mediatype: string): Promise {
7+
if ((!'parentFolder' in fd)) fd['parentFolder'] = '';
8+
return ReactNativeBlobUtil.createMediaFile(fd, mediatype);
99
}
1010

1111
function writeToMediafile(uri: string, path: string) {
@@ -20,9 +20,14 @@ function getBlob(contenturi: string, encoding: string) {
2020
return ReactNativeBlobUtil.getBlob(contenturi, encoding);
2121
}
2222

23+
function copyToMediaStore(fd: filedescriptor, mediatype: string, path: string) {
24+
return ReactNativeBlobUtil.copyToMediaStore(fd, mediatype, path);
25+
}
26+
2327
export default {
2428
createMediafile,
2529
writeToMediafile,
2630
copyToInternal,
27-
getBlob
31+
getBlob,
32+
copyToMediaStore
2833
};

scripts/prelink.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ try {
6767
'\033[95mreact-native-blob-util\033[97m link \033[91mFAILED \033[97m\nCould not automatically link package :'+
6868
err.stack +
6969
'please follow the instructions to manually link the library : ' +
70-
'\033[4mhttps://github.com/joltup/react-native-blob-util/wiki/Manually-Link-Package\n')
70+
'\033[4mhttps://github.com/RonRadtke/react-native-blob-util/wiki/Manually-Link-Package\n')
7171
}

types.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ export type ReactNativeBlobUtilStream = {
6464
_onEnd : () => void,
6565
_onError : () => void,
6666
}
67+
68+
69+
export type filedescriptor = { path: string, parentFolder: string, mimeType: string }

0 commit comments

Comments
 (0)