Pārlūkot izejas kodu

Add mixed data handler #44

Ben Hsieh 8 gadus atpakaļ
vecāks
revīzija
a1d59f5ebc
1 mainītis faili ar 32 papildinājumiem un 16 dzēšanām
  1. 32
    16
      src/polyfill/Blob.js

+ 32
- 16
src/polyfill/Blob.js Parādīt failu

@@ -42,6 +42,8 @@ export default class Blob {
42 42
       if(data.startsWith('RNFetchBlob-file://')) {
43 43
         this._ref = data
44 44
         this._blobCreated = true
45
+        if(typeof this._onCreated === 'function')
46
+          this._onCreated(this)
45 47
       }
46 48
       // content from variable need create file
47 49
       else {
@@ -54,23 +56,26 @@ export default class Blob {
54 56
           encoding = 'ascii'
55 57
 
56 58
         this.init(data, encoding)
57
-          .then(() => {
58
-            log.verbose('init executed ')
59
-            if(typeof this._onCreated === 'function')
60
-              this._onCreated(this)
61
-          })
62
-          .catch((err) => {
63
-            log.error('RNFetchBlob cannot create Blob', err)
64
-          })
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
+            })
65 67
       }
66 68
     }
67 69
     // TODO : handle mixed blob array
68 70
     else if(Array.isArray(data)) {
69
-
70
-    }
71
-    else {
72
-      log.verbose('TODO: else')
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
+        })
73 77
     }
78
+
74 79
   }
75 80
 
76 81
   onCreated(fn:() => void) {
@@ -153,10 +158,21 @@ function getBlobName() {
153 158
 
154 159
 /**
155 160
  * Create a file according to given array. The element in array can be a number,
156
- * Blob, string.
161
+ * Blob, String, Array.
162
+ * @param  {string} ref File path reference
157 163
  * @param  {Array} dataArray An array contains different types of data.
158
- * @return {string}      The blob file reference
164
+ * @return {Promise}
159 165
  */
160
-function createMixedBlobData(dataArray) {
161
-  // TODO : mixed blob data creator
166
+function createMixedBlobData(ref, dataArray) {
167
+  let p = fs.createFile(ref, '')
168
+  for(let i in dataArray) {
169
+    let part = dataArray[i]
170
+    if(part instanceof Blob)
171
+      p.then(() => fs.appendFile(ref, part.getRNFetchBlobRef()), 'uri')
172
+    else if (Array.isArray(part))
173
+      p.then(() => fs.appendFile(ref), part, 'ascii')
174
+    else
175
+      p.then(() => fs.appendFile(ref), part, 'utf8')
176
+  }
177
+  return p
162 178
 }