Skip to content

Commit 45f84a6

Browse files
committed
Use FileReaderAsync for Firefox compatability.
1 parent eab8728 commit 45f84a6

File tree

3 files changed

+102
-102
lines changed

3 files changed

+102
-102
lines changed

app/azureBlobUploadWorker.js

Lines changed: 100 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
(function(){
2-
3-
if ("performance" in self === false) {
4-
self.performance = {};
5-
}
6-
7-
Date.now = (Date.now || function () { // thanks IE8
8-
return new Date().getTime();
9-
});
10-
11-
if ("now" in self.performance === false) {
12-
var nowOffset = Date.now();
13-
14-
if (performance.timing && performance.timing.navigationStart) {
15-
nowOffset = performance.timing.navigationStart;
1+
(function () {
2+
3+
if ("performance" in self === false) {
4+
self.performance = {};
5+
}
6+
7+
Date.now = (Date.now || function () { // thanks IE8
8+
return new Date().getTime();
9+
});
10+
11+
if ("now" in self.performance === false) {
12+
var nowOffset = Date.now();
13+
14+
if (performance.timing && performance.timing.navigationStart) {
15+
nowOffset = performance.timing.navigationStart;
16+
}
17+
18+
self.performance.now = function now() {
19+
return Date.now() - nowOffset;
20+
};
1621
}
17-
18-
self.performance.now = function now() {
19-
return Date.now() - nowOffset;
20-
};
21-
}
2222
})();
2323

24-
(function () {
24+
(function() {
2525
var arrayBufferUtils = (function() {
2626
function arraybuffer2WordArray(arrayBuffer) {
2727
return CryptoJS.lib.WordArray.create(arrayBuffer);
@@ -39,7 +39,7 @@
3939
}
4040

4141
IterativeMd5.prototype.append = function(arrayBuffer) {
42-
if(this._md5 === null) {
42+
if (this._md5 === null) {
4343
this._md5 = CryptoJS.algo.MD5.create();
4444
}
4545

@@ -63,7 +63,7 @@
6363
debug: function(message) {
6464
self.postMessage({ type: 'log', logType: 'debug', message: message });
6565
},
66-
error: function (message) {
66+
error: function(message) {
6767
self.postMessage({ type: 'log', logType: 'error', message: message });
6868
}
6969
};
@@ -138,10 +138,10 @@
138138
function readNextSetOfBlocks(config) {
139139
var numberOfBlocksToRead = 10;
140140

141-
var done = config.done || function () { };
142-
var doneOne = config.doneOne || function () { };
143-
var doneHalf = config.doneHalf || function () { };
144-
var alreadyReading = config.alreadyReading || function () { $log.debug('Already reading next set of blocks!'); };
141+
var done = config.done || function() {};
142+
var doneOne = config.doneOne || function() {};
143+
var doneHalf = config.doneHalf || function() {};
144+
var alreadyReading = config.alreadyReading || function() { $log.debug('Already reading next set of blocks!'); };
145145

146146
if (config.state.readingNextSetOfBlocks) {
147147
alreadyReading();
@@ -150,7 +150,7 @@
150150

151151
config.state.readingNextSetOfBlocks = true;
152152

153-
var fileReader = new FileReader();
153+
var fileReader = new FileReaderSync();
154154
var skip = state.blocksReadIndex;
155155
var numberOfBlocks = state.numberOfBlocks;
156156

@@ -166,47 +166,46 @@
166166
var blocksToRead = config.state.blocks.slice(skip, end);
167167
var currentIndex = 0;
168168

169-
var readNextBlock = function () {
169+
var readNextBlock = function() {
170170
var fileContent = config.state.file.slice(blocksToRead[currentIndex].pointer, blocksToRead[currentIndex].end);
171-
fileReader.readAsArrayBuffer(fileContent);
171+
var result = fileReader.readAsArrayBuffer(fileContent);
172+
loaded(result);
172173
};
173174

174-
fileReader.onload = function (e) {
175-
if (e.target.readyState === FileReader.DONE && !config.state.cancelled) {
176-
var currentBlock = blocksToRead[currentIndex];
177-
$log.debug('Read block ' + currentBlock.blockId);
175+
var loaded = function(result) {
176+
var currentBlock = blocksToRead[currentIndex];
177+
$log.debug('Read block ' + currentBlock.blockId);
178178

179-
currentBlock.read = true;
180-
currentBlock.data = new Uint8Array(e.target.result);
179+
currentBlock.read = true;
180+
currentBlock.data = result;
181181

182-
// Calculate block MD5
183-
var blockMd5Start = performance.now();
184-
currentBlock.md5 = arrayBufferUtils.getArrayBufferMd5(currentBlock.data);
185-
var blockMd5End = performance.now();
186-
$log.debug("Call to getArrayBufferMd5 for block " + currentBlock.blockId + " took " + (blockMd5End - blockMd5Start) + " milliseconds.");
182+
// Calculate block MD5
183+
var blockMd5Start = performance.now();
184+
currentBlock.md5 = arrayBufferUtils.getArrayBufferMd5(currentBlock.data);
185+
var blockMd5End = performance.now();
186+
$log.debug("Call to getArrayBufferMd5 for block " + currentBlock.blockId + " took " + (blockMd5End - blockMd5Start) + " milliseconds.");
187187

188-
// Iterate file MD5
189-
if (config.state.calculateFileMd5) {
190-
config.state.fileMd5.append(currentBlock.data);
191-
}
188+
// Iterate file MD5
189+
if (config.state.calculateFileMd5) {
190+
config.state.fileMd5.append(currentBlock.data);
191+
}
192192

193-
// Useful to keep things fast
194-
if (currentIndex === 0) {
195-
doneOne();
196-
}
193+
// Useful to keep things fast
194+
if (currentIndex === 0) {
195+
doneOne();
196+
}
197197

198-
if (currentIndex === (numberOfBlocksToRead / 2 - 1)) {
199-
doneHalf();
200-
}
198+
if (currentIndex === (numberOfBlocksToRead / 2 - 1)) {
199+
doneHalf();
200+
}
201201

202-
++currentIndex;
203-
if (currentIndex < blocksToRead.length) {
204-
readNextBlock();
205-
} else {
206-
done();
207-
config.state.blocksReadIndex = config.state.blocksReadIndex + currentIndex;
208-
config.state.readingNextSetOfBlocks = false;
209-
}
202+
++currentIndex;
203+
if (currentIndex < blocksToRead.length) {
204+
readNextBlock();
205+
} else {
206+
done();
207+
config.state.blocksReadIndex = config.state.blocksReadIndex + currentIndex;
208+
config.state.readingNextSetOfBlocks = false;
210209
}
211210
};
212211

@@ -216,8 +215,8 @@
216215
function blockUploading(block) {
217216
block.uploading = true;
218217

219-
var getReadAndUnprocessed = function () {
220-
return state.blocks.filter(function (b) {
218+
var getReadAndUnprocessed = function() {
219+
return state.blocks.filter(function(b) {
221220
return b.read === true && b.uploading === false && b.resolved === false;
222221
});
223222
};
@@ -252,18 +251,18 @@
252251
'x-ms-blob-type': 'BlockBlob',
253252
'Content-Type': state.file.type,
254253
'Content-MD5': block.md5.toString(CryptoJS.enc.Base64)
255-
}).success(function (result, req) {
254+
}).success(function(result, req) {
256255
$log.debug('Put block successfully ' + block.blockId);
257256

258257
// Clear data
259258
block.data = null;
260259
block.uploading = false;
261260

262261
deferred.resolve({
263-
requestLength: requestData.length,
262+
requestLength: block.size,
264263
data: result,
265264
});
266-
}).error(function (result, req) {
265+
}).error(function(result, req) {
267266
$log.error('Put block error');
268267
$log.error(data);
269268

@@ -297,6 +296,7 @@
297296
blockId: blockId,
298297
blockIdBase64: btoa(blockId),
299298
pointer: pointer,
299+
size: (end - pointer),
300300
end: end,
301301
resolved: false,
302302
read: false,
@@ -311,21 +311,21 @@
311311

312312
var currentlyProcessing = [];
313313

314-
var addToCurrentlyProcessing = function (block, action) {
314+
var addToCurrentlyProcessing = function(block, action) {
315315
currentlyProcessing.push({ action: action, block: block });
316316
};
317317

318-
var removeFromCurrentlyProcessing = function (action) {
318+
var removeFromCurrentlyProcessing = function(action) {
319319
currentlyProcessing.splice(currentlyProcessing.indexOf(_.findWhere(currentlyProcessing, { action: action })), 1);
320320
};
321321

322-
var getUnresolved = function () {
323-
return state.blocks.filter(function (b) {
322+
var getUnresolved = function() {
323+
return state.blocks.filter(function(b) {
324324
return b.resolved === false && !_.findWhere(currentlyProcessing, { block: b });
325325
});
326326
};
327327

328-
var removeProcessedAction = function (action, result) {
328+
var removeProcessedAction = function(action, result) {
329329
action.resolved = true;
330330

331331
state.bytesUploaded += result.requestLength;
@@ -343,7 +343,7 @@
343343
}
344344
};
345345

346-
var processRejectedAction = function (block, action, rejectReason) {
346+
var processRejectedAction = function(block, action, rejectReason) {
347347
// Remove from currently processing
348348
removeFromCurrentlyProcessing(action);
349349

@@ -356,16 +356,16 @@
356356
//if (state.error) state.error(data, status, headers, config);
357357
};
358358

359-
var addNextAction = function () {
359+
var addNextAction = function() {
360360
var unresolved = getUnresolved();
361361
if (_.any(unresolved)) {
362362
var block = unresolved[0];
363363
var action = uploadBlock(block);
364364

365-
action.then(function (result) {
365+
action.then(function(result) {
366366
block.resolved = true;
367367
removeProcessedAction(action, result);
368-
}, function (rejectReason) {
368+
}, function(rejectReason) {
369369
block.resolved = false;
370370
processRejectedAction(block, action, rejectReason);
371371
});
@@ -381,17 +381,17 @@
381381
// Get first set of blocks and kick off the upload process.
382382
readNextSetOfBlocks({
383383
state: state,
384-
done: function () {
384+
done: function() {
385385
for (var j = 0; j < 8; j++) {
386-
if(state.blocks[j]) {
386+
if (state.blocks[j]) {
387387
addNextAction(state.blocks[j]);
388388
}
389389
}
390390
}
391391
});
392392

393393
return {
394-
cancel: function () {
394+
cancel: function() {
395395
state.cancelled = true;
396396
}
397397
};
@@ -401,7 +401,7 @@
401401
var uri = state.blobUri + '&comp=blocklist';
402402

403403
var requestBody = '<?xml version="1.0" encoding="utf-8"?><BlockList>';
404-
state.blocks.forEach(function (block) {
404+
state.blocks.forEach(function(block) {
405405
requestBody += '<Latest>' + block.blockIdBase64 + '</Latest>';
406406
});
407407
requestBody += '</BlockList>';
@@ -430,15 +430,15 @@
430430
}
431431

432432
function importAllScripts(libPath) {
433-
var addTrailingSlash = function (str) {
433+
var addTrailingSlash = function(str) {
434434
var lastChar = str.substr(-1);
435435
if (lastChar !== '/') {
436-
str = str + '/';
436+
str = str + '/';
437437
}
438438
return str;
439439
};
440440

441-
var addLib = function (f) {
441+
var addLib = function(f) {
442442
importScripts(addTrailingSlash(libPath) + f);
443443
};
444444

@@ -454,27 +454,27 @@
454454
self.postMessage({ type: 'ready' });
455455
}
456456

457-
self.onmessage = function (e) {
457+
self.onmessage = function(e) {
458458

459459
switch (e.data.type) {
460-
case 'file':
461-
setFile(e.data.file);
462-
break;
463-
case 'config':
464-
// Setup state
465-
setConfig(e.data.config);
466-
467-
// Load scripts first
468-
importAllScripts(e.data.config.libPath);
469-
470-
// Notify when ready for an upload
471-
notifyReady();
472-
break;
473-
case 'upload':
474-
upload();
475-
break;
476-
default:
477-
throw new Error("Don't know what to do with message of type " + e.data.type);
460+
case 'file':
461+
setFile(e.data.file);
462+
break;
463+
case 'config':
464+
// Setup state
465+
setConfig(e.data.config);
466+
467+
// Load scripts first
468+
importAllScripts(e.data.config.libPath);
469+
470+
// Notify when ready for an upload
471+
notifyReady();
472+
break;
473+
case 'upload':
474+
upload();
475+
break;
476+
default:
477+
throw new Error("Don't know what to do with message of type " + e.data.type);
478478
}
479479
};
480480
})();

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-azure-blob",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"dependencies": {
55
"angular": "latest",
66
//"jquery": "latest",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-azure-blob",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Angular Azure blob upload helper",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)