react-native-blob-util
Advanced tools
Comparing version 0.14.1 to 0.15.0
@@ -6,3 +6,3 @@ import {ReactNativeBlobUtilResponseInfo, ReactNativeBlobUtilStream} from "../types"; | ||
import URIUtil from "../utils/uri"; | ||
import base64 from "base-64"; | ||
/** | ||
@@ -9,0 +9,0 @@ * ReactNativeBlobUtil response object class. |
45
fs.js
@@ -163,6 +163,19 @@ // Copyright 2016 wkh237@github. All rights reserved. | ||
} | ||
return ReactNativeBlobUtil.readFile(path, encoding); | ||
return ReactNativeBlobUtil.readFile(path, encoding, false); | ||
} | ||
/** | ||
* Reads the file, then transforms it before returning the content | ||
* @param {string} path Path of the file. | ||
* @param {'base64' | 'utf8' | 'ascii'} encoding Encoding of read stream. | ||
* @return {Promise<Array<number> | string>} | ||
*/ | ||
function readFileWithTransform(path: string, encoding: string = 'utf8'): Promise<any> { | ||
if (typeof path !== 'string') { | ||
return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" '))) | ||
} | ||
return ReactNativeBlobUtil.readFile(path, encoding, true); | ||
} | ||
/** | ||
* Write data to file. | ||
@@ -190,6 +203,30 @@ * @param {string} path Path of the file. | ||
else | ||
return ReactNativeBlobUtil.writeFile(path, encoding, data, false); | ||
return ReactNativeBlobUtil.writeFile(path, encoding, data, false, false); | ||
} | ||
} | ||
/** | ||
* Transforms the data and then writes to the file. | ||
* @param {string} path Path of the file. | ||
* @param {string | number[]} data Data to write to the file. | ||
* @param {string} encoding Encoding of data (Optional). | ||
* @return {Promise} | ||
*/ | ||
function writeFileWithTransform(path: string, data: string | Array<number>, encoding: ?string = 'utf8'): Promise { | ||
if (typeof path !== 'string') { | ||
return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" '))) | ||
} | ||
if (encoding.toLocaleLowerCase() === 'ascii') { | ||
return Promise.reject(addCode('EINVAL', new TypeError('ascii is not supported for converted files'))) | ||
} | ||
else { | ||
if (typeof data !== 'string') { | ||
return Promise.reject(addCode('EINVAL', new TypeError(`"data" must be a String when encoding is "utf8" or "base64", but it is "${typeof data}"`))) | ||
} | ||
else | ||
return ReactNativeBlobUtil.writeFile(path, encoding, data, true, false) | ||
} | ||
} | ||
function appendFile(path: string, data: string | Array<number>, encoding?: string = 'utf8'): Promise<number> { | ||
@@ -211,3 +248,3 @@ if (typeof path !== 'string') { | ||
else | ||
return ReactNativeBlobUtil.writeFile(path, encoding, data, true); | ||
return ReactNativeBlobUtil.writeFile(path, encoding, data, false, true); | ||
} | ||
@@ -421,2 +458,4 @@ } | ||
writeFile, | ||
writeFileWithTransform, | ||
readFileWithTransform, | ||
appendFile, | ||
@@ -423,0 +462,0 @@ pathForAppGroup, |
@@ -402,2 +402,10 @@ // Type definitions for react-native-fetch-blob 0.10 | ||
/** | ||
* Processes the data and then writes to the file. | ||
* @param path Path of the file. | ||
* @param data Data to write to the file. | ||
* @param encoding Encoding of data (Optional). | ||
*/ | ||
writeFileWithTransform(path: string, data: string | number[], encoding?: Encoding): Promise<void>; | ||
appendFile(path: string, data: string | number[], encoding?: Encoding | "uri"): Promise<number>; | ||
@@ -413,2 +421,9 @@ | ||
/** | ||
* Reads from a file and then processes the data before returning | ||
* @param path Path of the file. | ||
* @param encoding Encoding of read stream. | ||
*/ | ||
readFileWithTransform(path: string, encoding: Encoding, bufferSize?: number): Promise<any>; | ||
/** | ||
* Check if file exists and if it is a folder. | ||
@@ -704,2 +719,9 @@ * @param path Path to check | ||
/** | ||
* Set this property to true if you want the data to be processed before it gets written onto disk. | ||
* This only has effect if the FileTransformer has been registered and the library is configured to write | ||
* response onto disk. | ||
*/ | ||
transformFile?: boolean; | ||
/** | ||
* Set this property to change temp file extension that created by fetch response data. | ||
@@ -815,2 +837,9 @@ */ | ||
/** | ||
* Copies and transforms an existing file to a mediastore file. Make sure FileTransformer is set | ||
* @param uri URI of the destination mediastore file | ||
* @param path Path to the existing file which should be copied | ||
*/ | ||
writeToMediafileWithTransform(uri: string, path: string): Promise<string> | ||
/** | ||
* Copies a file from the mediastore to the apps internal storage | ||
@@ -817,0 +846,0 @@ * @param contenturi URI of the mediastore file |
@@ -12,5 +12,9 @@ import {NativeModules} from 'react-native'; | ||
function writeToMediafile(uri: string, path: string) { | ||
return ReactNativeBlobUtil.writeToMediaFile(uri, path); | ||
return ReactNativeBlobUtil.writeToMediaFile(uri, path, false); | ||
} | ||
function writeToMediafileWithTransform(uri: string, path: string) { | ||
return ReactNativeBlobUtil.writeToMediaFile(uri, path, true); | ||
} | ||
function copyToInternal(contenturi: string, destpath: string) { | ||
@@ -31,2 +35,3 @@ return ReactNativeBlobUtil.copyToInternal(contenturi, destpath); | ||
writeToMediafile, | ||
writeToMediafileWithTransform, | ||
copyToInternal, | ||
@@ -33,0 +38,0 @@ getBlob, |
{ | ||
"name": "react-native-blob-util", | ||
"version": "0.14.1", | ||
"version": "0.15.0", | ||
"description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -62,2 +62,3 @@ import Log from '../utils/log.js'; | ||
let progressHandler, uploadHandler, cancelHandler; | ||
let scopedTask = null; | ||
let statefulPromise = promise | ||
@@ -67,2 +68,3 @@ .then((body) => { | ||
.fetch(options.method, url, options.headers, body); | ||
scopedTask = task; | ||
if (progressHandler) | ||
@@ -92,4 +94,4 @@ task.progress(progressHandler); | ||
cancelHandler = true; | ||
if (task.cancel) | ||
task.cancel(); | ||
if (scopedTask && scopedTask.cancel) | ||
scopedTask.cancel(); | ||
}; | ||
@@ -96,0 +98,0 @@ |
@@ -306,2 +306,22 @@ # react-native-blob-util | ||
**Use File Transformer** | ||
If you need to perform any processing on the bytes prior to it being written into storage (e.g. if you want it to be encrypted) then you can use `transform` option. NOTE: you will need to set a transformer on the libray (see [Setting a File Transformer](#Setting-A-File-Transformer)) | ||
```js | ||
ReactNativeBlobUtil | ||
.config({ | ||
// response data will be saved to this path if it has access right. | ||
path: dirs.DocumentDir + '/path-to-file.anything', | ||
transform: true | ||
}) | ||
.fetch('GET', 'http://www.example.com/file/example.zip', { | ||
//some headers .. | ||
}) | ||
.then((res) => { | ||
// the path should be dirs.DocumentDir + 'path-to-file.anything' | ||
console.log('The file saved to ', res.path()) | ||
}) | ||
``` | ||
#### Upload example : Dropbox [files-upload](https://www.dropbox.com/developers/documentation/http/documentation#files-upload) API | ||
@@ -680,2 +700,10 @@ | ||
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)). | ||
````js | ||
await ReactNativeBlobUtil.MediaCollection.writeToMediafileWithTransform('content://....', // content uri of the entry in the media storage | ||
localpath // path to the file that should be copied | ||
); | ||
```` | ||
#### copyToInternal | ||
@@ -702,4 +730,6 @@ Copies an entry form the media storage to the apps internal storage. | ||
- [writeFile (0.6.0)](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#writefilepathstring-contentstring--array-encodingstring-appendbooleanpromise) | ||
- writeFileWithTransform | ||
- [appendFile (0.6.0) ](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#appendfilepathstring-contentstring--arraynumber-encodingstring-promisenumber) | ||
- [readFile (0.6.0)](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#readfilepath-encodingpromise) | ||
- readFileWithTransform | ||
- [readStream](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#readstreampath-encoding-buffersize-interval-promisernfbreadstream) | ||
@@ -909,2 +939,6 @@ - [hash (0.10.9)](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#hashpath-algorithm-promise) | ||
### Transform Files | ||
Sometimes you may need the files to be transformed after reading from storage or before writing into storage (eg encryption/decyrption). In order to perform the transformations, use `readFileWithTransform` and `writeFileWithTransform`. 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)). | ||
## Web API Polyfills | ||
@@ -917,2 +951,44 @@ | ||
## Setting A File Transformer | ||
Setting a file transformer will allow you to specify how data should be transformed whenever the library is writing into storage or reading from storage. A use case for this is if you want the files handled by this library to be encrypted. | ||
If you want to use a file transformer, you must implement an interface defined in: | ||
[ReactNativeBlobUtilFileTransformer.h (iOS)](/ios/ReactNativeBlobUtilFileTransformer.h) | ||
[ReactNativeBlobUtilFileTransformer.java (Android)](/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFileTransformer.java) | ||
Then you set the File Transformer during app startup | ||
Android: | ||
```java | ||
public class MainApplication extends Application implements ReactApplication { | ||
... | ||
@Override | ||
public void onCreate() { | ||
... | ||
ReactNativeBlobUtilFileTransformer.sharedFileTransformer = new MyCustomEncryptor(); | ||
... | ||
} | ||
``` | ||
iOS: | ||
```m | ||
@implementation AppDelegate | ||
... | ||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | ||
{ | ||
... | ||
[ReactNativeBlobUtilFileTransformer setFileTransformer: MyCustomEncryptor.new]; | ||
... | ||
} | ||
``` | ||
Here are the places where the transformer would apply | ||
- Reading a file from the file system | ||
- Writing a file into the file system | ||
- Http response is downloaded to storage directly | ||
## Performance Tips | ||
@@ -919,0 +995,0 @@ |
@@ -6,2 +6,3 @@ | ||
fileCache : bool, | ||
transformFile: boolean; | ||
path : string, | ||
@@ -8,0 +9,0 @@ appendExt : string, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
786767
133
10241
1030