瀏覽代碼

Blob polyfill wip commit

Ben Hsieh 8 年之前
父節點
當前提交
09c9d1b9e9
共有 1 個文件被更改,包括 66 次插入19 次删除
  1. 66
    19
      src/polyfill/Blob.js

+ 66
- 19
src/polyfill/Blob.js 查看文件

@@ -2,47 +2,94 @@
2 2
 // Use of this source code is governed by a MIT-style license that can be
3 3
 // found in the LICENSE file.
4 4
 
5
+import RNFetchBlob from '../index.js'
5 6
 import fs from '../fs.js'
6 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 15
 export default class Blob {
9 16
 
10 17
   cacheName:string;
11 18
   type:string;
12 19
   size:number;
13 20
 
21
+  _ref:string = null;
22
+  _blobCreated:boolean = false;
23
+  _onCreated:() => void;
24
+
25
+  static Instances:any = {}
26
+
14 27
   // legacy constructor
15 28
   constructor(data:any, mime:?string) {
16 29
     this.cacheName = getBlobName()
17 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 32
       // content from file
26 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 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 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,7 +101,7 @@ export default class Blob {
54 101
    */
55 102
   slice(start:?number, end:?number, encoding:?string):Blob {
56 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,7 +110,7 @@ export default class Blob {
63 110
    * @return {Promise}
64 111
    */
65 112
   close() {
66
-    fs.unlink(this.cacheName)
113
+    return fs.unlink(this._ref)
67 114
   }
68 115
 
69 116