1 var app = angular.module('Workbench', ['Arvados']);
2 app.controller('UploadToCollection', UploadToCollection);
3 app.directive('arvUuid', arvUuid);
6 // Copy the given uuid into the current $scope.
9 link: function(scope, element, attributes) {
10 scope.uuid = attributes.arvUuid;
15 UploadToCollection.$inject = ['$scope', '$filter', '$q', '$timeout',
16 'ArvadosClient', 'arvadosApiToken'];
17 function UploadToCollection($scope, $filter, $q, $timeout,
18 ArvadosClient, arvadosApiToken) {
21 uploader: new QueueUploader(),
22 addFilesToQueue: function(files) {
23 // Angular binding doesn't work its usual magic for file
24 // inputs, so we need to $scope.$apply() this update.
25 $scope.$apply(function(){
27 // Add these new files after the items already waiting
28 // in the queue -- but before the items that are
29 // 'Done' and have therefore been pushed to the
32 (nItemsTodo < $scope.uploadQueue.length &&
33 $scope.uploadQueue[nItemsTodo].state !== 'Done'); ) {
36 for (i=0; i<files.length; i++) {
37 $scope.uploadQueue.splice(nItemsTodo+i, 0,
38 new FileUploader(files[i]));
46 $scope.uploader.stop();
48 removeFileFromQueue: function(index) {
49 var wasRunning = $scope.uploader.running;
50 $scope.uploadQueue[index].stop();
51 $scope.uploadQueue.splice(index, 1);
55 countInStates: function(want_states) {
57 $.each($scope.uploadQueue, function() {
58 if (want_states.indexOf(this.state) >= 0) {
65 ////////////////////////////////
68 var defaultErrorMessage = 'A network error occurred: either the server was unreachable, or there is a server configuration problem. Please check your browser debug console for a more specific error message (browser security features prevent us from showing the details here).';
70 function SliceReader(_slice) {
75 ////////////////////////////////
79 // Return a promise, which will be resolved with the
80 // requested slice data.
81 _deferred = $.Deferred();
82 _reader = new FileReader();
83 _reader.onload = resolve;
84 _reader.onerror = _deferred.reject;
85 _reader.onprogress = _deferred.notify;
86 _reader.readAsArrayBuffer(_slice.blob);
87 return _deferred.promise();
90 if (that._reader.result.length !== that._slice.size) {
91 // Sometimes we get an onload event even if the read
92 // did not return the desired number of bytes. We
93 // treat that as a fail.
96 "Short read: wanted " + _slice.size +
97 ", received " + _reader.result.length);
100 return _deferred.resolve(_reader.result);
104 function SliceUploader(_label, _data, _dataSize) {
109 ////////////////////////////////
116 // Send data to the Keep proxy. Retry a few times on
117 // fail. Return a promise that will get resolved with
118 // resolve(locator) when the block is accepted by the
120 _deferred = $.Deferred();
121 if (proxyUriBase().match(/^http:/) &&
122 window.location.origin.match(/^https:/)) {
123 // In this case, requests will fail, and no ajax
124 // success/fail handlers will be called (!), which
125 // will leave our status saying "uploading" and the
126 // user waiting for something to happen. Better to
130 err: 'There is a server configuration problem. Proxy ' + proxyUriBase() + ' cannot be used from origin ' + window.location.origin + ' due to the browser\'s mixed-content (https/http) policy.'
135 return _deferred.promise();
141 textStatus: 'stopped',
142 err: 'interrupted at slice '+_label
151 'Authorization': 'OAuth2 '+arvadosApiToken,
152 'Content-Type': 'application/octet-stream',
153 'X-Keep-Desired-Replicas': '2'
156 // Make an xhr that reports upload progress
157 var xhr = $.ajaxSettings.xhr();
159 xhr.upload.onprogress = onSendProgress;
166 _jqxhr.then(onSendResolve, onSendReject);
168 function onSendProgress(xhrProgressEvent) {
169 _deferred.notify(xhrProgressEvent.loaded, _dataSize);
171 function onSendResolve(data, textStatus, jqxhr) {
172 _deferred.resolve(data, _dataSize);
174 function onSendReject(xhr, textStatus, err) {
175 if (++_failCount < _failMax) {
176 // TODO: nice to tell the user that retry is happening.
177 console.log('slice ' + _label + ': ' +
178 textStatus + ', retry ' + _failCount);
182 {xhr: xhr, textStatus: textStatus, err: err});
185 function proxyUriBase() {
186 return ((keepProxy.service_ssl_flag ? 'https' : 'http') +
187 '://' + keepProxy.service_host + ':' +
188 keepProxy.service_port + '/');
192 function FileUploader(file) {
197 state: 'Queued', // Queued, Uploading, Paused, Uploaded, Done
200 stop: stop // User wants to stop.
202 ////////////////////////////////
204 var _currentUploader;
207 var _maxBlobSize = Math.pow(2,26);
209 var _queueTime = Date.now();
213 var _readPos = 0; // number of bytes confirmed uploaded
216 _deferred.reject({textStatus: 'restarted'});
217 _deferred = $.Deferred();
218 that.state = 'Uploading';
219 _startTime = Date.now();
220 _startByte = _readPos;
223 return _deferred.promise().always(function() { _deferred = null; });
227 that.state = 'Paused';
228 _deferred.reject({textStatus: 'stopped', err: 'interrupted'});
230 if (_currentUploader) {
231 _currentUploader.stop();
232 _currentUploader = null;
236 // Ensure this._deferred gets resolved or rejected --
237 // either right here, or when a new promise arranged right
238 // here is fulfilled.
239 _currentSlice = nextSlice();
240 if (!_currentSlice) {
241 // All slices have been uploaded, but the work won't
242 // be truly Done until the target collection has been
243 // updated by the QueueUploader. This state is called:
244 that.state = 'Uploaded';
245 setProgress(_readPos);
246 _currentUploader = null;
247 _deferred.resolve([that]);
250 _currentUploader = new SliceUploader(
254 _currentUploader.go().then(
259 function onUploaderResolve(locator, dataSize) {
260 var sizeHint = (''+locator).split('+')[1];
261 if (!locator || parseInt(sizeHint) !== dataSize) {
262 console.log("onUploaderResolve, but locator '" + locator +
263 "' with size hint '" + sizeHint +
264 "' does not look right for dataSize=" + dataSize);
265 return onUploaderReject({
267 err: "Bad response from slice upload"
270 that.locators.push(locator);
271 _readPos += dataSize;
272 _currentUploader = null;
275 function onUploaderReject(reason) {
276 that.state = 'Paused';
277 setProgress(_readPos);
278 _currentUploader = null;
280 _deferred.reject(reason);
282 function onUploaderProgress(sliceDone, sliceSize) {
283 setProgress(_readPos + sliceDone);
285 function nextSlice() {
288 that.file.size - _readPos);
289 setProgress(_readPos);
293 var blob = that.file.slice(
294 _readPos, _readPos+size,
295 'application/octet-stream; charset=x-user-defined');
296 return {blob: blob, size: size};
298 function setProgress(bytesDone) {
300 if (that.file.size == 0)
303 that.progress = Math.min(100, 100 * bytesDone / that.file.size);
304 if (bytesDone > _startByte) {
305 kBps = (bytesDone - _startByte) /
306 (Date.now() - _startTime);
308 '' + $filter('number')(bytesDone/1024, '0') + ' KiB ' +
309 'at ~' + $filter('number')(kBps, '0') + ' KiB/s')
310 if (that.state === 'Paused') {
311 that.statistics += ', paused';
312 } else if (that.state === 'Uploading') {
313 that.statistics += ', ETA ' +
316 Date.now() + (that.file.size - bytesDone) / kBps),
320 that.statistics = that.state;
322 if (that.state === 'Uploaded') {
323 // 'Uploaded' gets reported as 'finished', which is a
324 // little misleading because the collection hasn't
325 // been updated yet. But FileUploader's portion of the
326 // work (and the time when it makes sense to show
327 // speed and ETA) is finished.
328 that.statistics += ', finished ' +
329 $filter('date')(Date.now(), 'shortTime');
330 _finishTime = Date.now();
337 function QueueUploader() {
339 state: 'Idle', // Idle, Running, Stopped, Failed
345 ////////////////////////////////
347 var _deferred; // the one we promise to go()'s caller
348 var _deferredAppend; // tracks current appendToCollection
350 if (_deferred) return _deferred.promise();
351 if (_deferredAppend) return _deferredAppend.promise();
352 _deferred = $.Deferred();
353 that.state = 'Running';
354 ArvadosClient.apiPromise(
355 'keep_services', 'list',
356 {filters: [['service_type','=','proxy']]}).
357 then(doQueueWithProxy);
359 return _deferred.promise().always(function() { _deferred = null; });
362 that.state = 'Stopped';
364 _deferred.reject({});
366 for (var i=0; i<$scope.uploadQueue.length; i++)
367 $scope.uploadQueue[i].stop();
370 function doQueueWithProxy(data) {
371 keepProxy = data.items[0];
373 that.state = 'Failed';
375 'There seems to be no Keep proxy service available.';
376 _deferred.reject(null, 'error', that.stateReason);
379 return doQueueWork();
381 function doQueueWork() {
382 // If anything is not Done, do it.
383 if ($scope.uploadQueue.length > 0 &&
384 $scope.uploadQueue[0].state !== 'Done') {
386 that.stateReason = null;
387 return $scope.uploadQueue[0].go().
388 then(appendToCollection, null, onQueueProgress).
389 then(doQueueWork, onQueueReject);
391 // Queue work has been stopped. Just update the
397 // If everything is Done, resolve the promise and clean
398 // up. Note this can happen even after the _deferred
399 // promise has been rejected: specifically, when stop() is
400 // called too late to prevent completion of the last
401 // upload. In that case we want to update state to "Idle",
402 // rather than leave it at "Stopped".
405 function onQueueReject(reason) {
407 // Outcome has already been decided (by stop()).
411 that.state = 'Failed';
413 (reason.textStatus || 'Error') +
414 (reason.xhr && reason.xhr.options
415 ? (' (from ' + reason.xhr.options.url + ')')
418 (reason.err || defaultErrorMessage));
419 if (reason.xhr && reason.xhr.responseText)
420 that.stateReason += ' -- ' + reason.xhr.responseText;
421 _deferred.reject(reason);
424 function onQueueResolve() {
426 that.stateReason = 'Done!';
431 function onQueueProgress() {
432 // Ensure updates happen after FileUpload promise callbacks.
433 $timeout(function(){$scope.$apply();});
435 function appendToCollection(uploads) {
436 _deferredAppend = $.Deferred();
437 ArvadosClient.apiPromise(
438 'collections', 'get',
439 { uuid: $scope.uuid }).
440 then(function(collection) {
441 var manifestText = '';
442 $.each(uploads, function(_, upload) {
443 var locators = upload.locators;
444 if (locators.length === 0) {
445 // Every stream must have at least one
446 // data locator, even if it is zero bytes
448 locators = ['d41d8cd98f00b204e9800998ecf8427e+0'];
450 filename = ArvadosClient.uniqueNameForManifest(
451 collection.manifest_text,
452 '.', upload.file.name);
453 collection.manifest_text += '. ' +
455 ' 0:' + upload.file.size.toString() + ':' +
459 return ArvadosClient.apiPromise(
460 'collections', 'update',
464 collection.manifest_text }
468 // Mark the completed upload(s) as Done and push
469 // them to the bottom of the queue.
470 var i, qLen = $scope.uploadQueue.length;
471 for (i=0; i<qLen; i++) {
472 if (uploads.indexOf($scope.uploadQueue[i]) >= 0) {
473 $scope.uploadQueue[i].state = 'Done';
474 $scope.uploadQueue.push.apply(
476 $scope.uploadQueue.splice(i, 1));
482 then(_deferredAppend.resolve,
483 _deferredAppend.reject);
484 return _deferredAppend.promise().
486 _deferredAppend = null;