From c779fff2d150d92ab3c91410715a5fb2cd7f1247 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 30 Dec 2014 00:43:09 -0800 Subject: [PATCH] GUAC-963: Manage file uploads. --- .../client/controllers/clientController.js | 94 -------- .../app/client/directives/guacClient.js | 92 +------- .../webapp/app/client/types/ManagedClient.js | 31 ++- .../client/types/ManagedFileTransferState.js | 136 +++++++++++ .../app/client/types/ManagedFileUpload.js | 218 ++++++++++++++++++ 5 files changed, 382 insertions(+), 189 deletions(-) create mode 100644 guacamole/src/main/webapp/app/client/types/ManagedFileTransferState.js create mode 100644 guacamole/src/main/webapp/app/client/types/ManagedFileUpload.js diff --git a/guacamole/src/main/webapp/app/client/controllers/clientController.js b/guacamole/src/main/webapp/app/client/controllers/clientController.js index 50320bed1..37bcad99d 100644 --- a/guacamole/src/main/webapp/app/client/controllers/clientController.js +++ b/guacamole/src/main/webapp/app/client/controllers/clientController.js @@ -620,100 +620,6 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams }); }); - // Mapping of upload stream index to notification object - var uploadNotifications = {}; - - // Mapping of upload stream index to notification ID - var uploadNotificationIDs = {}; - - $scope.$on('guacClientFileUploadStart', function handleClientFileUploadStart(event, guacClient, streamIndex, mimetype, filename, length) { - $scope.$apply(function() { - - var notification = { - className : 'upload', - title : 'CLIENT.DIALOG_TITLE_FILE_TRANSFER', - text : filename - }; - - uploadNotifications[streamIndex] = notification; - uploadNotificationIDs[streamIndex] = $scope.addNotification(notification); - - }); - }); - - $scope.$on('guacClientFileUploadProgress', function handleClientFileUploadProgress(event, guacClient, streamIndex, mimetype, filename, length, offset) { - $scope.$apply(function() { - - var notification = uploadNotifications[streamIndex]; - if (notification) - notification.progress = getFileProgress('CLIENT.TEXT_FILE_TRANSFER_PROGRESS', offset, length); - - }); - }); - - $scope.$on('guacClientFileUploadEnd', function handleClientFileUploadEnd(event, guacClient, streamIndex, mimetype, filename, length) { - $scope.$apply(function() { - - var notification = uploadNotifications[streamIndex]; - var notificationID = uploadNotificationIDs[streamIndex]; - - /** - * Close the notification. - */ - var closeNotification = function closeNotification() { - $scope.removeNotification(notificationID); - delete uploadNotifications[streamIndex]; - delete uploadNotificationIDs[streamIndex]; - }; - - // Show that the file has uploaded successfully - if (notificationID && notification) { - delete notification.progress; - notification.actions = [ - { - name : 'CLIENT.ACTION_ACKNOWLEDGE', - callback : closeNotification - } - ]; - } - - }); - }); - - $scope.$on('guacClientFileUploadError', function handleClientFileUploadError(event, guacClient, streamIndex, mimetype, fileName, length, status) { - $scope.$apply(function() { - - var notification = uploadNotifications[streamIndex]; - var notificationID = uploadNotificationIDs[streamIndex]; - - // Determine translation name of error - var errorName = (status in UPLOAD_ERRORS) ? status.toString(16).toUpperCase() : "DEFAULT"; - - /** - * Close the notification. - */ - var closeNotification = function closeNotification() { - $scope.removeNotification(notificationID); - delete uploadNotifications[streamIndex]; - delete uploadNotificationIDs[streamIndex]; - }; - - // Show that the file upload has failed - if (notificationID && notification) { - delete notification.progress; - notification.actions = [ - { - name : 'CLIENT.ACTION_ACKNOWLEDGE', - callback : closeNotification - } - ]; - notification.text = "CLIENT.ERROR_UPLOAD_" + errorName; - notification.className = "upload error"; - } - - }); - }); - // Clean up when view destroyed $scope.$on('$destroy', function clientViewDestroyed() { diff --git a/guacamole/src/main/webapp/app/client/directives/guacClient.js b/guacamole/src/main/webapp/app/client/directives/guacClient.js index 4b24593df..77bd7f164 100644 --- a/guacamole/src/main/webapp/app/client/directives/guacClient.js +++ b/guacamole/src/main/webapp/app/client/directives/guacClient.js @@ -42,8 +42,11 @@ angular.module('client').directive('guacClient', [function guacClient() { templateUrl: 'app/client/templates/guacClient.html', controller: ['$scope', '$injector', '$element', function guacClientController($scope, $injector, $element) { + // Required types + var ManagedClient = $injector.get('ManagedClient'); + // Required services - var $window = $injector.get('$window'); + var $window = $injector.get('$window'); /** * Whether the local, hardware mouse cursor is in use. @@ -407,26 +410,6 @@ angular.module('client').directive('guacClient', [function guacClient() { client.sendKeyEvent(0, keysym); }); - /** - * Converts the given bytes to a base64-encoded string. - * - * @param {Uint8Array} bytes A Uint8Array which contains the data to be - * encoded as base64. - * @return {String} The base64-encoded string. - */ - function getBase64(bytes) { - - var data = ""; - - // Produce binary string from bytes in buffer - for (var i=0; i= bytes.length) { - stream.sendEnd(); - $scope.$emit('guacClientFileUploadProgress', client, stream.index, file.type, file.name, bytes.length, bytes.length); - $scope.$emit('guacClientFileUploadEnd', client, stream.index, file.type, file.name, bytes.length); - } - - // Otherwise, update progress - else - $scope.$emit('guacClientFileUploadProgress', client, stream.index, file.type, file.name, bytes.length, offset); - - }; - - }; - reader.readAsArrayBuffer(file); - - } // Handle and ignore dragenter/dragover displayContainer.addEventListener("dragenter", ignoreEvent, false); @@ -510,12 +431,13 @@ angular.module('client').directive('guacClient', [function guacClient() { e.stopPropagation(); // Ignore file drops if no attached client - if (!client) return; + if (!$scope.client) + return; // Upload each file var files = e.dataTransfer.files; for (var i=0; i= bytes.length) { + stream.sendEnd(); + managedFileUpload.progress = bytes.length; + + // Upload complete + ManagedFileTransferState.setStreamState(managedFileUpload.transferState, + ManagedFileTransferState.StreamState.CLOSED); + + } + + // Otherwise, update progress + else + managedFileUpload.progress = offset; + + }); + + }; // end ack handler + + }; + reader.readAsArrayBuffer(file); + + return managedFileUpload; + + }; + + return ManagedFileUpload; + +}]); \ No newline at end of file