Browse Source

wip commit

Ben Hsieh 8 years ago
parent
commit
03ac81deb4
3 changed files with 35 additions and 13 deletions
  1. 26
    10
      src/polyfill/Blob.js
  2. 6
    0
      test/test-0.8.0.js
  3. 3
    3
      test/test-init.js

+ 26
- 10
src/polyfill/Blob.js View File

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

+ 6
- 0
test/test-0.8.0.js View File

55
     })
55
     })
56
 
56
 
57
 })
57
 })
58
+// 
59
+// describe('automatic response data handing test', (report, done) => {
60
+//
61
+//
62
+//
63
+// })
58
 
64
 
59
 function getASCIIArray(str) {
65
 function getASCIIArray(str) {
60
   let r = []
66
   let r = []

+ 3
- 3
test/test-init.js View File

18
 // test environment variables
18
 // test environment variables
19
 
19
 
20
 prop('FILENAME', `${Platform.OS}-0.8.0-${Date.now()}.png`)
20
 prop('FILENAME', `${Platform.OS}-0.8.0-${Date.now()}.png`)
21
-prop('TEST_SERVER_URL', 'http://192.168.16.70:8123')
22
-prop('TEST_SERVER_URL_SSL', 'https://192.168.1.70:8124')
21
+prop('TEST_SERVER_URL', 'http://192.168.0.11:8123')
22
+prop('TEST_SERVER_URL_SSL', 'https://192.168.0.11:8124')
23
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
23
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
24
 prop('styles', {
24
 prop('styles', {
25
   image : {
25
   image : {
67
 // require('./test-0.7.0')
67
 // require('./test-0.7.0')
68
 require('./test-0.8.0')
68
 require('./test-0.8.0')
69
 // require('./test-fs')
69
 // require('./test-fs')
70
-// require('./test-firebase')
70
+require('./test-firebase')
71
 // require('./test-android')
71
 // require('./test-android')