Skip to content

Commit 9d26d04

Browse files
committed
Useful callback data
1 parent 76a5bec commit 9d26d04

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ angular.module('appx').controller('UploadController', [
5151
path: '/dist/',
5252
libPath: '/libs/',
5353
blobUri: xxx,
54-
file: files[0],
55-
process: function cb(){},
56-
complete: function cb() {},
54+
file: files,
55+
process: function cb(file, data) {},
56+
complete: function cb(file, data) {},
5757
error: function cb() {},
5858
blockSize: 1024, // optional
5959
calculateFileMd5: false // optional, false by default

app/azureBlobUploader.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = function Uploader(config) {
4545

4646
var state = {};
4747
var file = null;
48-
48+
4949
this.setFile = function (f) {
5050
file = f;
5151
};
@@ -58,13 +58,13 @@ module.exports = function Uploader(config) {
5858
log({ type: 'log', logType: 'error', message: message });
5959
}
6060
};
61-
61+
6262
this.logger = logger;
6363

6464
function error(data, status) {
6565
this.log({ type: 'error', fatal: true, data: data, status: status });
6666
}
67-
67+
6868
function Block(index, blockId, pointer, end) {
6969
this.index = index;
7070
this.blockId = blockId;
@@ -79,6 +79,7 @@ module.exports = function Uploader(config) {
7979
this.md5 = null;
8080
this.uploading = false;
8181
this.retries = 0;
82+
this.startedAt = null;
8283

8384
this.incrementRetry = function () {
8485
this.retries++;
@@ -172,6 +173,7 @@ module.exports = function Uploader(config) {
172173

173174
function blockUploading(block) {
174175
block.uploading = true;
176+
block.startedAt = Date.now();
175177

176178
var getReadAndUnprocessed = function () {
177179
return state.blocks.filter(function (b) {
@@ -231,7 +233,7 @@ module.exports = function Uploader(config) {
231233

232234
return deferred.promise;
233235
}
234-
236+
235237
function commitBlockList() {
236238
var uri = state.blobUri + '&comp=blocklist';
237239

@@ -244,14 +246,13 @@ module.exports = function Uploader(config) {
244246
atomic.put(uri, requestBody, {
245247
'x-ms-blob-content-type': state.file.type,
246248
}).success(function (data, req) {
247-
complete({ data: data, md5: state.fileMd5.finalize().toString(CryptoJS.enc.Base64) });
249+
complete({ data: data, md5: state.fileMd5.finalize().toString(CryptoJS.enc.Base64), startedAt: state.startedAt });
248250
logger.debug('Upload took ' + (performance.now() - state.startedUpload) + 'ms');
249-
})
250-
.error(function (data, req) {
251-
logger.error('Put block list error ' + req.status);
252-
logger.error(data);
253-
error(data, req.status);
254-
});
251+
}).error(function (data, req) {
252+
logger.error('Put block list error ' + req.status);
253+
logger.error(data);
254+
error(data, req.status);
255+
});
255256
}
256257

257258
function pad(number, length) {
@@ -312,12 +313,16 @@ module.exports = function Uploader(config) {
312313
cancelled: false,
313314
calculateFileMd5: config.calculateFileMd5 || false,
314315
fileMd5: arrayBufferUtils.getArrayBufferMd5Iterative(),
315-
readingNextSetOfBlocks: false
316+
readingNextSetOfBlocks: false,
317+
percentComplete: null,
318+
startedAt: null
316319
};
317320
};
318321

319322
this.upload = function () {
320323
state.blocks = [];
324+
state.startedAt = Date.now();
325+
321326
var numberOfBlocks = state.numberOfBlocks;
322327
var index = 0;
323328
var totalFileSize = state.fileSize;
@@ -354,15 +359,23 @@ module.exports = function Uploader(config) {
354359
});
355360
};
356361

357-
var removeProcessedAction = function (action, result) {
362+
var removeProcessedAction = function (block, action, result) {
358363
action.resolved = true;
359364

360365
state.bytesUploaded += result.requestLength;
361366
state.totalBytesRemaining -= result.requestLength;
362367

363368
var percentComplete = ((parseFloat(state.bytesUploaded) / parseFloat(state.file.size)) * 100).toFixed(2);
364369

365-
progress({ percentComplete: percentComplete, result: result });
370+
progress({
371+
result: result,
372+
previousPercentComplete: state.percentComplete,
373+
percentComplete: percentComplete,
374+
startedAt: block.startedAt,
375+
blockSize: block.size
376+
});
377+
378+
state.percentComplete = percentComplete;
366379

367380
removeFromCurrentlyProcessing(action);
368381

@@ -396,7 +409,7 @@ module.exports = function Uploader(config) {
396409

397410
action.then(function (result) {
398411
block.resolved = true;
399-
removeProcessedAction(action, result);
412+
removeProcessedAction(block, action, result);
400413
}, function (rejectReason) {
401414
block.resolved = false;
402415
processRejectedAction(block, action, rejectReason);
@@ -422,7 +435,7 @@ module.exports = function Uploader(config) {
422435
}
423436
});
424437
};
425-
438+
426439
this.getState = function () {
427440
return _.clone(state);
428441
};

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.8",
3+
"version": "0.0.9",
44
"dependencies": {
55
"angular": "latest",
66
"underscore": "latest",

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
path: '/app/',
3030
libPath: '/libs/',
3131
blobUri: 'xxx',
32-
file: files[0],
32+
files: files,
3333
process: function cb(){},
3434
complete: function cb() {},
3535
error: function cb() {},

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.8",
3+
"version": "0.0.9",
44
"description": "Angular Azure blob upload helper",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)