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,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
   }

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

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

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

@@ -18,8 +18,8 @@ const { Assert, Comparer, Info, prop } = RNTest
18 18
 // test environment variables
19 19
 
20 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 23
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
24 24
 prop('styles', {
25 25
   image : {
@@ -67,5 +67,5 @@ describe('GET image from server', (report, done) => {
67 67
 // require('./test-0.7.0')
68 68
 require('./test-0.8.0')
69 69
 // require('./test-fs')
70
-// require('./test-firebase')
70
+require('./test-firebase')
71 71
 // require('./test-android')