Browse Source

Blob polyfill wip commit

Ben Hsieh 8 years ago
parent
commit
09c9d1b9e9
1 changed files with 66 additions and 19 deletions
  1. 66
    19
      src/polyfill/Blob.js

+ 66
- 19
src/polyfill/Blob.js View File

2
 // Use of this source code is governed by a MIT-style license that can be
2
 // Use of this source code is governed by a MIT-style license that can be
3
 // found in the LICENSE file.
3
 // found in the LICENSE file.
4
 
4
 
5
+import RNFetchBlob from '../index.js'
5
 import fs from '../fs.js'
6
 import fs from '../fs.js'
6
 import getUUID from '../utils/uuid'
7
 import getUUID from '../utils/uuid'
7
 
8
 
9
+const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blob/'
10
+
11
+/**
12
+ * A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
13
+ * Response object attain fron RNFetchBlob.fetch.
14
+ */
8
 export default class Blob {
15
 export default class Blob {
9
 
16
 
10
   cacheName:string;
17
   cacheName:string;
11
   type:string;
18
   type:string;
12
   size:number;
19
   size:number;
13
 
20
 
21
+  _ref:string = null;
22
+  _blobCreated:boolean = false;
23
+  _onCreated:() => void;
24
+
25
+  static Instances:any = {}
26
+
14
   // legacy constructor
27
   // legacy constructor
15
   constructor(data:any, mime:?string) {
28
   constructor(data:any, mime:?string) {
16
     this.cacheName = getBlobName()
29
     this.cacheName = getBlobName()
17
     this.type = mime
30
     this.type = mime
18
-
19
-    let encode = 'utf8'
20
-    // plain text content
21
-    if(mime === 'text/plain') {
22
-      this.size = data ? data.length : 0
23
-    }
24
-    else if(typeof data === 'string') {
31
+    if(typeof data === 'string') {
25
       // content from file
32
       // content from file
26
       if(data.startsWith('RNFetchBlob-file://')) {
33
       if(data.startsWith('RNFetchBlob-file://')) {
27
-        encode = 'uri'
34
+        this._ref = data
35
+        this._blobCreated = true
28
       }
36
       }
29
-      // BASE64 encoded
37
+      // content from variable need create file
30
       else {
38
       else {
31
-        encode = 'base64'
39
+        this._ref = RNFetchBlob.wrap(blobCacheDir + this.cacheName)
40
+        let encoding = 'utf8'
41
+        if(typeof data === 'string' && String(mime).match('application/octet') )
42
+          encoding = 'base64'
43
+        else if(Array.isArray(data))
44
+          encoding = 'ascii'
45
+
46
+        this.init(data, encoding)
47
+          .then(() => {
48
+            if(typeof this._onCreated === 'function')
49
+              this._onCreated()
50
+            _blobCreated = true
51
+          })
52
+          .catch((err) => {
53
+            console.log('RNFetchBlob cannot create Blob', err)
54
+          })
32
       }
55
       }
33
     }
56
     }
34
-    // create cache entry for Blob object
35
-    fs.createFile(this.cacheName, data, encode)
57
+    else {
58
+      console.log('TODO')
59
+    }
60
+  }
61
+
62
+  onCreated(fn:() => void) {
63
+    console.log('register blob onCreated', fn)
64
+    if(this._blobCreated)
65
+      fn()
66
+    else
67
+      this._onCreated = fn
36
   }
68
   }
37
 
69
 
38
   /**
70
   /**
39
-   * Write data to Blob object
40
-   * @param  {string | Array} data Data that will write to Blob object
41
-   * @param  {string} encoding Encoding of data to be written
71
+   * Create blob file cache
72
+   * @nonstandard
73
+   * @param  {string | Array} data Data to create Blob file
74
+   * @param  {'base64' | 'utf8' | 'ascii'} encoding RNFetchBlob fs encoding
42
    * @return {Promise}
75
    * @return {Promise}
43
    */
76
    */
44
-  write(data:string | Array<number>, encoding:'base64' | 'utf8' | 'ascii' | 'uri'):Promise {
45
-    return fs.write(this.cacheName, data, encoding)
77
+  init(data, encoding):Promise {
78
+    console.log('blob init called')
79
+    return fs.exists(blobCacheDir).then((exist) => {
80
+      if(!exist)
81
+        return fs.mkdir(blobCacheDir).then(() => fs.createFile(this._ref, data, encoding))
82
+      else
83
+        return fs.createFile(this._ref, data, encoding)
84
+    })
85
+  }
86
+
87
+  /**
88
+   * @nonstandard
89
+   * @return {string} Blob file reference which can be consumed by RNFetchBlob fs
90
+   */
91
+  getRNFetchBlobRef() {
92
+    return this._ref
46
   }
93
   }
47
 
94
 
48
   /**
95
   /**
54
    */
101
    */
55
   slice(start:?number, end:?number, encoding:?string):Blob {
102
   slice(start:?number, end:?number, encoding:?string):Blob {
56
     console.log('slice called')
103
     console.log('slice called')
57
-    return fs.slice(this.cacheName, getBlobName(), contentType, start, end)
104
+    // return fs.slice(this.cacheName, getBlobName(), contentType, start, end)
58
   }
105
   }
59
 
106
 
60
   /**
107
   /**
63
    * @return {Promise}
110
    * @return {Promise}
64
    */
111
    */
65
   close() {
112
   close() {
66
-    fs.unlink(this.cacheName)
113
+    return fs.unlink(this._ref)
67
   }
114
   }
68
 
115
 
69
 
116