|
@@ -8,7 +8,7 @@ import getUUID from '../utils/uuid'
|
8
|
8
|
import Log from '../utils/log.js'
|
9
|
9
|
|
10
|
10
|
const log = new Log('Blob')
|
11
|
|
-const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blob/'
|
|
11
|
+const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blobs/'
|
12
|
12
|
|
13
|
13
|
log.level(3)
|
14
|
14
|
|
|
@@ -42,28 +42,35 @@ export default class Blob {
|
42
|
42
|
* given `mime`. However, the blob creation is asynchronously, to register
|
43
|
43
|
* event `onCreated` is need to ensure the Blob is creadted.
|
44
|
44
|
* @param {any} data Content of Blob object
|
45
|
|
- * @param {string} mime Content type of Blob object, `text/plain` by default
|
|
45
|
+ * @param {any} mime Content type settings of Blob object, `text/plain`
|
|
46
|
+ * by default
|
46
|
47
|
*/
|
47
|
|
- constructor(data:any, mime='text/plain':?string) {
|
48
|
|
-
|
|
48
|
+ constructor(data:any, cType:any) {
|
|
49
|
+ cType = cType || {}
|
49
|
50
|
this.cacheName = getBlobName()
|
50
|
51
|
this.isRNFetchBlobPolyfill = true
|
51
|
|
- this.type = mime
|
52
|
|
- log.verbose('Blob constructor called', 'mime', mime, 'type', typeof data, 'length', data.length)
|
|
52
|
+ this.type = cType.type || 'text/plain'
|
|
53
|
+ log.verbose('Blob constructor called', 'mime', this.type, 'type', typeof data, 'length', data.length)
|
53
|
54
|
this._ref = blobCacheDir + this.cacheName
|
54
|
55
|
let p = null
|
|
56
|
+ if(data instanceof Blob) {
|
|
57
|
+ log.verbose('create Blob cache file from Blob object')
|
|
58
|
+ this._ref = data.getRNFetchBlobRef()
|
|
59
|
+ p = fs.stat(String(this._ref).replace('RNFetchBlob-file://'))
|
|
60
|
+ .then((stat) => Promise.resolve(stat.size))
|
|
61
|
+ }
|
55
|
62
|
// if the data is a string starts with `RNFetchBlob-file://`, append the
|
56
|
63
|
// Blob data from file path
|
57
|
|
- if(typeof data === 'string' && data.startsWith('RNFetchBlob-file://')) {
|
|
64
|
+ else if(typeof data === 'string' && data.startsWith('RNFetchBlob-file://')) {
|
58
|
65
|
log.verbose('create Blob cache file from file path')
|
59
|
66
|
this._ref = data
|
60
|
|
- p = fs.stat(data.replace('RNFetchBlob-file://'))
|
|
67
|
+ p = fs.stat(String(this._ref).replace('RNFetchBlob-file://'))
|
61
|
68
|
.then((stat) => Promise.resolve(stat.size))
|
62
|
69
|
}
|
63
|
70
|
// content from variable need create file
|
64
|
71
|
else if(typeof data === 'string') {
|
65
|
72
|
let encoding = 'utf8'
|
66
|
|
- mime = String(mime)
|
|
73
|
+ let mime = String(this.type)
|
67
|
74
|
// when content type contains application/octet* or *;base64, RNFetchBlob
|
68
|
75
|
// fs will treat it as BASE64 encoded string binary data
|
69
|
76
|
if(/(application\/octet|\;base64)/i.test(mime))
|
|
@@ -76,6 +83,10 @@ export default class Blob {
|
76
|
83
|
.then((size) => Promise.resolve(size))
|
77
|
84
|
|
78
|
85
|
}
|
|
86
|
+ // TODO : ArrayBuffer support
|
|
87
|
+ // else if (data instanceof ArrayBuffer ) {
|
|
88
|
+ //
|
|
89
|
+ // }
|
79
|
90
|
// when input is an array of mixed data types, create a file cache
|
80
|
91
|
else if(Array.isArray(data)) {
|
81
|
92
|
log.verbose('create Blob cache file from mixed array', data)
|
|
@@ -83,7 +94,8 @@ export default class Blob {
|
83
|
94
|
}
|
84
|
95
|
else {
|
85
|
96
|
data = data.toString()
|
86
|
|
- p = Promise.resolve(data.length)
|
|
97
|
+ p = fs.writeFile(this._ref, data, 'utf8')
|
|
98
|
+ .then((size) => Promise.resolve(size))
|
87
|
99
|
}
|
88
|
100
|
p && p.then((size) => {
|
89
|
101
|
this.size = size
|
|
@@ -176,6 +188,10 @@ function createMixedBlobData(ref, dataArray) {
|
176
|
188
|
args.push([ref, part.getRNFetchBlobRef(), 'uri'])
|
177
|
189
|
else if(typeof part === 'string')
|
178
|
190
|
args.push([ref, part, 'utf8'])
|
|
191
|
+ // TODO : ArrayBuffer
|
|
192
|
+ // else if (part instanceof ArrayBuffer) {
|
|
193
|
+ //
|
|
194
|
+ // }
|
179
|
195
|
else if (Array.isArray(part))
|
180
|
196
|
args.push([ref, part, 'ascii'])
|
181
|
197
|
}
|