|
@@ -25,7 +25,7 @@ export default class Blob {
|
25
|
25
|
|
26
|
26
|
_ref:string = null;
|
27
|
27
|
_blobCreated:boolean = false;
|
28
|
|
- _onCreated:() => void;
|
|
28
|
+ _onCreated:Array<any> = [];
|
29
|
29
|
|
30
|
30
|
static Instances:any = {}
|
31
|
31
|
|
|
@@ -35,86 +35,44 @@ export default class Blob {
|
35
|
35
|
this.cacheName = getBlobName()
|
36
|
36
|
this.isRNFetchBlobPolyfill = true
|
37
|
37
|
this.type = mime
|
38
|
|
- log.verbose('Blob constructor called' , data, 'mime', mime)
|
39
|
|
-
|
40
|
|
- if(typeof data === 'string') {
|
41
|
|
- // content from file
|
42
|
|
- if(data.startsWith('RNFetchBlob-file://')) {
|
43
|
|
- this._ref = data
|
44
|
|
- this._blobCreated = true
|
45
|
|
- if(typeof this._onCreated === 'function')
|
46
|
|
- this._onCreated(this)
|
47
|
|
- }
|
48
|
|
- // content from variable need create file
|
49
|
|
- else {
|
50
|
|
- log.verbose('create Blob cache file ..')
|
51
|
|
- this._ref = RNFetchBlob.wrap(blobCacheDir + this.cacheName)
|
52
|
|
- let encoding = 'utf8'
|
53
|
|
- if(typeof data === 'string' && String(mime).match('application/octet') )
|
54
|
|
- encoding = 'base64'
|
55
|
|
- else if(Array.isArray(data))
|
56
|
|
- encoding = 'ascii'
|
57
|
|
-
|
58
|
|
- this.init(data, encoding)
|
59
|
|
- .then(() => {
|
60
|
|
- log.verbose('init executed ')
|
61
|
|
- if(typeof this._onCreated === 'function')
|
62
|
|
- this._onCreated(this)
|
63
|
|
- })
|
64
|
|
- .catch((err) => {
|
65
|
|
- log.error('RNFetchBlob cannot create Blob', err)
|
66
|
|
- })
|
67
|
|
- }
|
|
38
|
+ log.verbose('Blob constructor called', 'mime', mime)
|
|
39
|
+ this._ref = blobCacheDir + this.cacheName
|
|
40
|
+ let p = null
|
|
41
|
+ // content from file
|
|
42
|
+ if(typeof data === 'string' && data.startsWith('RNFetchBlob-file://')) {
|
|
43
|
+ log.verbose('create Blob cache file from file path')
|
|
44
|
+ this._ref = data
|
|
45
|
+ p = Promise.resolve()
|
68
|
46
|
}
|
69
|
|
- // TODO : handle mixed blob array
|
|
47
|
+ // content from variable need create file
|
|
48
|
+ else if(typeof data === 'string') {
|
|
49
|
+ log.verbose('create Blob cache file from string')
|
|
50
|
+ let encoding = 'utf8'
|
|
51
|
+ if(String(mime).match('application/octet'))
|
|
52
|
+ encoding = 'base64'
|
|
53
|
+ else if(Array.isArray(data))
|
|
54
|
+ encoding = 'ascii'
|
|
55
|
+ // create cache file
|
|
56
|
+ p = fs.writeFile(this._ref, data, encoding)
|
|
57
|
+
|
|
58
|
+ }
|
|
59
|
+ // when input is an array of mixed data types, create a file cache
|
70
|
60
|
else if(Array.isArray(data)) {
|
71
|
|
- this._ref = RNFetchBlob.wrap(blobCacheDir + this.cacheName)
|
72
|
|
- createMixedBlobData(this._ref, data)
|
73
|
|
- .then(() => {
|
74
|
|
- if(typeof this._onCreated === 'function')
|
75
|
|
- this._onCreated(this)
|
76
|
|
- })
|
|
61
|
+ log.verbose('create Blob cache file from mixed array', data)
|
|
62
|
+ p = createMixedBlobData(this._ref, data)
|
77
|
63
|
}
|
|
64
|
+ p && p.then(() => {
|
|
65
|
+ this._invokeOnCreateEvent()
|
|
66
|
+ })
|
|
67
|
+ .catch((err) => {
|
|
68
|
+ log.error('RNFetchBlob cannot create Blob : '+ this._ref)
|
|
69
|
+ })
|
78
|
70
|
|
79
|
71
|
}
|
80
|
72
|
|
81
|
73
|
onCreated(fn:() => void) {
|
82
|
|
- log.verbose('register blob onCreated')
|
83
|
|
- if(this._blobCreated)
|
84
|
|
- fn()
|
85
|
|
- else
|
86
|
|
- this._onCreated = fn
|
87
|
|
- }
|
88
|
|
-
|
89
|
|
- /**
|
90
|
|
- * Create blob file cache
|
91
|
|
- * @nonstandard
|
92
|
|
- * @param {string | Array} data Data to create Blob file
|
93
|
|
- * @param {'base64' | 'utf8' | 'ascii'} encoding RNFetchBlob fs encoding
|
94
|
|
- * @return {Promise}
|
95
|
|
- */
|
96
|
|
- init(data, encoding):Promise {
|
97
|
|
- return new Promise((resolve, reject) => {
|
98
|
|
- fs.exists(blobCacheDir)
|
99
|
|
- .then((exist) => {
|
100
|
|
- log.verbose('blob cache folder exist', blobCacheDir, exist)
|
101
|
|
- let path = String(this._ref).replace('RNFetchBlob-file://', '')
|
102
|
|
- log.verbose('create cache file', path)
|
103
|
|
- if(!exist)
|
104
|
|
- return fs.mkdir(blobCacheDir)
|
105
|
|
- .then(() => fs.createFile(path, data, encoding))
|
106
|
|
- else
|
107
|
|
- return fs.createFile(path, data, encoding)
|
108
|
|
- })
|
109
|
|
- .then(() => {
|
110
|
|
- this._blobCreated = true
|
111
|
|
- resolve()
|
112
|
|
- })
|
113
|
|
- .catch((err) => {
|
114
|
|
- reject(err)
|
115
|
|
- })
|
116
|
|
- })
|
117
|
|
-
|
|
74
|
+ log.verbose('register blob onCreated', this._onCreated.length)
|
|
75
|
+ this._onCreated.push(fn)
|
118
|
76
|
}
|
119
|
77
|
|
120
|
78
|
/**
|
|
@@ -146,6 +104,21 @@ export default class Blob {
|
146
|
104
|
return fs.unlink(this._ref)
|
147
|
105
|
}
|
148
|
106
|
|
|
107
|
+ clearCache() {
|
|
108
|
+
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ _invokeOnCreateEvent() {
|
|
112
|
+ log.verbose('invoke create event')
|
|
113
|
+ this._blobCreated = true
|
|
114
|
+ let fns = this._onCreated
|
|
115
|
+ for(let i in fns) {
|
|
116
|
+ if(typeof fns[i] === 'function')
|
|
117
|
+ fns[i](this)
|
|
118
|
+ }
|
|
119
|
+ delete this._onCreated
|
|
120
|
+ }
|
|
121
|
+
|
149
|
122
|
}
|
150
|
123
|
|
151
|
124
|
/**
|
|
@@ -164,15 +137,21 @@ function getBlobName() {
|
164
|
137
|
* @return {Promise}
|
165
|
138
|
*/
|
166
|
139
|
function createMixedBlobData(ref, dataArray) {
|
167
|
|
- let p = fs.createFile(ref, '')
|
|
140
|
+ let p = fs.writeFile(ref, '')
|
|
141
|
+ let args = []
|
168
|
142
|
for(let i in dataArray) {
|
169
|
143
|
let part = dataArray[i]
|
170
|
144
|
if(part instanceof Blob)
|
171
|
|
- p.then(() => fs.appendFile(ref, part.getRNFetchBlobRef()), 'uri')
|
|
145
|
+ args.push([ref, part.getRNFetchBlobRef(), 'uri'])
|
|
146
|
+ else if(typeof part === 'string')
|
|
147
|
+ args.push([ref, part, 'utf8'])
|
172
|
148
|
else if (Array.isArray(part))
|
173
|
|
- p.then(() => fs.appendFile(ref), part, 'ascii')
|
174
|
|
- else
|
175
|
|
- p.then(() => fs.appendFile(ref), part, 'utf8')
|
|
149
|
+ args.push([ref, part, 'ascii'])
|
176
|
150
|
}
|
177
|
|
- return p
|
|
151
|
+ return p.then(() => {
|
|
152
|
+ let promises = args.map((p) => {
|
|
153
|
+ return fs.appendFile.call(this, ...p)
|
|
154
|
+ })
|
|
155
|
+ return Promise.all(promises)
|
|
156
|
+ })
|
178
|
157
|
}
|