| 2533 | |
| 2534 | exports.removeBlobs = function(data, callback) { |
| 2535 | function _removeBlobs(obj, curKey, containingObject) { |
| 2536 | if (!obj) return obj; |
| 2537 | |
| 2538 | // convert any blob |
| 2539 | if ((global.Blob && obj instanceof Blob) || |
| 2540 | (global.File && obj instanceof File)) { |
| 2541 | pendingBlobs++; |
| 2542 | |
| 2543 | // async filereader |
| 2544 | var fileReader = new FileReader(); |
| 2545 | fileReader.onload = function() { // this.result == arraybuffer |
| 2546 | if (containingObject) { |
| 2547 | containingObject[curKey] = this.result; |
| 2548 | } |
| 2549 | else { |
| 2550 | bloblessData = this.result; |
| 2551 | } |
| 2552 | |
| 2553 | // if nothing pending its callback time |
| 2554 | if(! --pendingBlobs) { |
| 2555 | callback(bloblessData); |
| 2556 | } |
| 2557 | }; |
| 2558 | |
| 2559 | fileReader.readAsArrayBuffer(obj); // blob -> arraybuffer |
| 2560 | } else if (isArray(obj)) { // handle array |
| 2561 | for (var i = 0; i < obj.length; i++) { |
| 2562 | _removeBlobs(obj[i], i, obj); |
| 2563 | } |
| 2564 | } else if (obj && 'object' == typeof obj && !isBuf(obj)) { // and object |
| 2565 | for (var key in obj) { |
| 2566 | _removeBlobs(obj[key], key, obj); |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | var pendingBlobs = 0; |
| 2572 | var bloblessData = data; |