Skip to content

Commit 4d2dc89

Browse files
Adding documentation to README.md
1 parent 55abe45 commit 4d2dc89

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,26 @@ ReactNativeBlobUtil
303303

304304
**These files won't be removed automatically, please refer to [Cache File Management](#user-content-cache-file-management)**
305305

306+
**Use File Transformer**
307+
308+
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))
309+
310+
```js
311+
ReactNativeBlobUtil
312+
.config({
313+
// response data will be saved to this path if it has access right.
314+
path: dirs.DocumentDir + '/path-to-file.anything',
315+
transform: true
316+
})
317+
.fetch('GET', 'http://www.example.com/file/example.zip', {
318+
//some headers ..
319+
})
320+
.then((res) => {
321+
// the path should be dirs.DocumentDir + 'path-to-file.anything'
322+
console.log('The file saved to ', res.path())
323+
})
324+
```
325+
306326
#### Upload example : Dropbox [files-upload](https://www.dropbox.com/developers/documentation/http/documentation#files-upload) API
307327

308328
`react-native-blob-util` will convert the base64 string in `body` to binary format using native API, this process is done in a separated thread so that it won't block your GUI.
@@ -697,8 +717,10 @@ File Access APIs
697717
- [dirs](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#dirs)
698718
- [createFile](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#createfilepath-data-encodingpromise)
699719
- [writeFile (0.6.0)](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#writefilepathstring-contentstring--array-encodingstring-appendbooleanpromise)
720+
- writeFileWithTransform
700721
- [appendFile (0.6.0) ](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#appendfilepathstring-contentstring--arraynumber-encodingstring-promisenumber)
701722
- [readFile (0.6.0)](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#readfilepath-encodingpromise)
723+
- readFileWithTransform
702724
- [readStream](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#readstreampath-encoding-buffersize-interval-promisernfbreadstream)
703725
- [hash (0.10.9)](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#hashpath-algorithm-promise)
704726
- [writeStream](https://github.com/RonRadtke/react-native-blob-util/wiki/File-System-Access-API#writestreampathstring-encodingstringpromise)
@@ -903,13 +925,59 @@ ReactNativeBlobUtil.config({
903925
})
904926
```
905927

928+
### Transform Files
929+
930+
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)).
931+
906932
## Web API Polyfills
907933

908934
After `0.8.0` we've made some [Web API polyfills](https://github.com/RonRadtke/react-native-blob-util/wiki/Web-API-Polyfills-(experimental)) that makes some browser-based library available in RN.
909935

910936
- Blob
911937
- XMLHttpRequest (Use our implementation if you're going to use it with Blob)
912938

939+
940+
## Setting A File Transformer
941+
942+
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.
943+
944+
If you want to use a file transformer, you must implement an interface defined in:
945+
946+
[ReactNativeBlobUtilFileTransformer.h (iOS)](/ios/ReactNativeBlobUtilFileTransformer.h)
947+
948+
[ReactNativeBlobUtilFileTransformer.java (Android)](/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFileTransformer.java)
949+
950+
Then you set the File Transformer during app startup
951+
952+
Android:
953+
```java
954+
public class MainApplication extends Application implements ReactApplication {
955+
...
956+
@Override
957+
public void onCreate() {
958+
...
959+
ReactNativeBlobUtilFileTransformer.sharedFileTransformer = new MyCustomEncryptor();
960+
...
961+
}
962+
```
963+
964+
iOS:
965+
```m
966+
@implementation AppDelegate
967+
...
968+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
969+
{
970+
...
971+
[ReactNativeBlobUtilFileTransformer setFileTransformer: MyCustomEncryptor.new];
972+
...
973+
}
974+
```
975+
976+
Here are the places where the transformer would apply
977+
- Reading a file from the file system
978+
- Writing a file into the file system
979+
- Http response is downloaded to storage directly
980+
913981
## Performance Tips
914982

915983
**Read Stream and Progress Event Overhead**

0 commit comments

Comments
 (0)