|
@@ -10,17 +10,29 @@ import {
|
10
|
10
|
DeviceEventEmitter,
|
11
|
11
|
NativeAppEventEmitter,
|
12
|
12
|
Platform,
|
|
13
|
+ AsyncStorage,
|
13
|
14
|
} from 'react-native'
|
14
|
15
|
import type {
|
15
|
16
|
RNFetchBlobNative,
|
16
|
17
|
RNFetchBlobConfig,
|
17
|
18
|
RNFetchBlobStream
|
18
|
19
|
} from './types'
|
|
20
|
+import fs from './fs'
|
19
|
21
|
import base64 from 'base-64'
|
|
22
|
+const {
|
|
23
|
+ RNFetchBlobSession,
|
|
24
|
+ getSystemDirs,
|
|
25
|
+ readStream,
|
|
26
|
+ unlink,
|
|
27
|
+ mkdir,
|
|
28
|
+ session,
|
|
29
|
+ ls,
|
|
30
|
+ mv,
|
|
31
|
+ cp
|
|
32
|
+} = fs
|
20
|
33
|
|
21
|
34
|
const emitter = DeviceEventEmitter
|
22
|
35
|
const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
|
23
|
|
-const pathPrefix = Platform.OS === 'android' ? 'file://' : ''
|
24
|
36
|
|
25
|
37
|
// register message channel event handler.
|
26
|
38
|
emitter.addListener("RNFetchBlobMessage", (e) => {
|
|
@@ -44,26 +56,6 @@ if(!RNFetchBlob || !RNFetchBlob.fetchBlobForm || !RNFetchBlob.fetchBlob) {
|
44
|
56
|
)
|
45
|
57
|
}
|
46
|
58
|
|
47
|
|
-/**
|
48
|
|
- * Get path of system directories.
|
49
|
|
- * @return {object} Map contains PictureDir, MovieDir, DocumentDir, CacheDir,
|
50
|
|
- * MusicDir, and DCIMDir, some directory might not be supported by platform.
|
51
|
|
- */
|
52
|
|
-function getSystemDirs() {
|
53
|
|
- return new Promise((resolve, reject) => {
|
54
|
|
- try {
|
55
|
|
- RNFetchBlob.getEnvironmentDirs((...dirs) => {
|
56
|
|
- let [PictureDir, MovieDir, DocumentDir, CacheDir, MusicDir, DCIMDir] = [...dirs]
|
57
|
|
- console.log({PictureDir, MovieDir, DocumentDir, CacheDir, MusicDir, DCIMDir})
|
58
|
|
- resolve({PictureDir, MovieDir, DocumentDir, CacheDir, MusicDir, DCIMDir})
|
59
|
|
- })
|
60
|
|
- } catch(err) {
|
61
|
|
- reject(err)
|
62
|
|
- }
|
63
|
|
- })
|
64
|
|
-
|
65
|
|
-}
|
66
|
|
-
|
67
|
59
|
/**
|
68
|
60
|
* Calling this method will inject configurations into followed `fetch` method.
|
69
|
61
|
* @param {RNFetchBlobConfig} options
|
|
@@ -117,17 +109,21 @@ function fetch(...args:any):Promise {
|
117
|
109
|
})
|
118
|
110
|
|
119
|
111
|
let req = RNFetchBlob[nativeMethodName]
|
120
|
|
- req(options, taskId, method, url, headers || {}, body, (err, ...data) => {
|
|
112
|
+ req(options, taskId, method, url, headers || {}, body, (err, data) => {
|
121
|
113
|
|
122
|
114
|
// task done, remove event listener
|
123
|
115
|
subscription.remove()
|
124
|
116
|
if(err)
|
125
|
|
- reject(new Error(err, ...data))
|
|
117
|
+ reject(new Error(err, data))
|
126
|
118
|
else {
|
127
|
119
|
let respType = 'base64'
|
128
|
|
- if(options.path || options.fileCache)
|
|
120
|
+ // response data is saved to storage
|
|
121
|
+ if(options.path || options.fileCache) {
|
129
|
122
|
respType = 'path'
|
130
|
|
- resolve(new FetchBlobResponse(taskId, respType, ...data))
|
|
123
|
+ if(options.session)
|
|
124
|
+ session(options.session).add(data)
|
|
125
|
+ }
|
|
126
|
+ resolve(new FetchBlobResponse(taskId, respType, data))
|
131
|
127
|
}
|
132
|
128
|
})
|
133
|
129
|
|
|
@@ -144,73 +140,6 @@ function fetch(...args:any):Promise {
|
144
|
140
|
|
145
|
141
|
}
|
146
|
142
|
|
147
|
|
-/**
|
148
|
|
- * Create file stream from file at `path`.
|
149
|
|
- * @param {String} path The file path.
|
150
|
|
- * @param {String} encoding Data encoding, should be one of `base64`, `utf8`, `ascii`
|
151
|
|
- * @param {String} bufferSize Size of stream buffer.
|
152
|
|
- * @return {RNFetchBlobStream} RNFetchBlobStream stream instance.
|
153
|
|
- */
|
154
|
|
-function openReadStream(
|
155
|
|
- path:string,
|
156
|
|
- encoding:'utf8' | 'ascii' | 'base64',
|
157
|
|
- bufferSize?:?number
|
158
|
|
-):RNFetchBlobStream {
|
159
|
|
-
|
160
|
|
- if(!path)
|
161
|
|
- throw Error('RNFetchBlob could not open file stream with empty `path`')
|
162
|
|
-
|
163
|
|
- let stream:RNFetchBlobStream = {
|
164
|
|
- onData : function(fn) {
|
165
|
|
- this._onData = fn
|
166
|
|
- },
|
167
|
|
- onError : function(fn) {
|
168
|
|
- this._onError = fn
|
169
|
|
- },
|
170
|
|
- onEnd : function(fn) {
|
171
|
|
- this._onEnd = fn
|
172
|
|
- },
|
173
|
|
- }
|
174
|
|
-
|
175
|
|
- // register for file stream event
|
176
|
|
- let subscription = emitter.addListener(`RNFetchBlobStream+${path}`, (e) => {
|
177
|
|
-
|
178
|
|
- let {event, detail} = e
|
179
|
|
- if(stream._onData && event === 'data')
|
180
|
|
- stream._onData(detail)
|
181
|
|
- else if (stream._onEnd && event === 'end') {
|
182
|
|
- stream._onEnd(detail)
|
183
|
|
- }
|
184
|
|
- else {
|
185
|
|
- stream._onError(detail)
|
186
|
|
- }
|
187
|
|
- // when stream closed or error, remove event handler
|
188
|
|
- if (event === 'error' || event === 'end') {
|
189
|
|
- subscription.remove()
|
190
|
|
- }
|
191
|
|
- })
|
192
|
|
-
|
193
|
|
- RNFetchBlob.readStream(path, encoding, bufferSize || 0)
|
194
|
|
- return stream
|
195
|
|
-
|
196
|
|
-}
|
197
|
|
-
|
198
|
|
-/**
|
199
|
|
- * Remove file at path.
|
200
|
|
- * @param {string} path:string Path of target file.
|
201
|
|
- * @return {Promise}
|
202
|
|
- */
|
203
|
|
-function unlink(path:string):Promise {
|
204
|
|
- return new Promise((resolve, reject) => {
|
205
|
|
- RNFetchBlob.unlink(path, (err) => {
|
206
|
|
- if(err)
|
207
|
|
- reject(err)
|
208
|
|
- else
|
209
|
|
- resolve()
|
210
|
|
- })
|
211
|
|
- })
|
212
|
|
-}
|
213
|
|
-
|
214
|
143
|
/**
|
215
|
144
|
* RNFetchBlob response object class.
|
216
|
145
|
*/
|
|
@@ -280,6 +209,14 @@ class FetchBlobResponse {
|
280
|
209
|
return this.data
|
281
|
210
|
return null
|
282
|
211
|
}
|
|
212
|
+ this.session = (name) => {
|
|
213
|
+ if(this.type === 'path')
|
|
214
|
+ return session(name).add(this.data)
|
|
215
|
+ else {
|
|
216
|
+ console.warn('only file paths can be add into session.')
|
|
217
|
+ return null
|
|
218
|
+ }
|
|
219
|
+ }
|
283
|
220
|
/**
|
284
|
221
|
* Start read stream from cached file
|
285
|
222
|
* @param {String} encoding Encode type, should be one of `base64`, `ascrii`, `utf8`.
|
|
@@ -288,7 +225,7 @@ class FetchBlobResponse {
|
288
|
225
|
*/
|
289
|
226
|
this.readStream = (encode: 'base64' | 'utf8' | 'ascii'):RNFetchBlobStream | null => {
|
290
|
227
|
if(this.type === 'path') {
|
291
|
|
- return openReadStream(this.data, encode)
|
|
228
|
+ return readStream(this.data, encode)
|
292
|
229
|
}
|
293
|
230
|
else {
|
294
|
231
|
console.warn('RNFetchblob', 'this response data does not contains any available stream')
|
|
@@ -307,5 +244,5 @@ function getUUID() {
|
307
|
244
|
}
|
308
|
245
|
|
309
|
246
|
export default {
|
310
|
|
- fetch, base64, config, getSystemDirs, readStream, unlink
|
|
247
|
+ fetch, base64, config, getSystemDirs, readStream, unlink, session, ls, mkdir, mv, cp
|
311
|
248
|
}
|