From daf52dee5e17cd139d2bdc4caa6ed3840ccb4f34 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 3 Jul 2015 13:07:19 -0700 Subject: [PATCH] GUAC-1172: Implement file upload through object streams. --- .../client/controllers/clientController.js | 20 ++++++++++++++ .../webapp/app/client/templates/client.html | 1 + .../webapp/app/client/types/ManagedClient.js | 26 +++++++++++++++++-- .../app/client/types/ManagedFileUpload.js | 19 ++++++++++++-- 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/guacamole/src/main/webapp/app/client/controllers/clientController.js b/guacamole/src/main/webapp/app/client/controllers/clientController.js index fbce5cc37..3082a5709 100644 --- a/guacamole/src/main/webapp/app/client/controllers/clientController.js +++ b/guacamole/src/main/webapp/app/client/controllers/clientController.js @@ -27,6 +27,7 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams function clientController($scope, $routeParams, $injector) { // Required types + var ManagedClient = $injector.get('ManagedClient'); var ManagedClientState = $injector.get('ManagedClientState'); var ManagedFilesystem = $injector.get('ManagedFilesystem'); var ScrollState = $injector.get('ScrollState'); @@ -635,6 +636,25 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams ManagedFilesystem.changeDirectory(filesystem, file); }; + /** + * Begins a file upload through the attached Guacamole client for + * each file in the given FileList. + * + * @param {FileList} files + * The files to upload. + */ + $scope.uploadFiles = function uploadFiles(files) { + + // Ignore file uploads if no attached client + if (!$scope.client) + return; + + // Upload each file + for (var i = 0; i < files.length; i++) + ManagedClient.uploadFile($scope.client, files[i], $scope.filesystemMenuContents); + + }; + // Clean up when view destroyed $scope.$on('$destroy', function clientViewDestroyed() { diff --git a/guacamole/src/main/webapp/app/client/templates/client.html b/guacamole/src/main/webapp/app/client/templates/client.html index 5b2912c26..2254b42ca 100644 --- a/guacamole/src/main/webapp/app/client/templates/client.html +++ b/guacamole/src/main/webapp/app/client/templates/client.html @@ -171,6 +171,7 @@

{{filesystemMenuContents.name}}

+
diff --git a/guacamole/src/main/webapp/app/client/types/ManagedClient.js b/guacamole/src/main/webapp/app/client/types/ManagedClient.js index 2e8a61857..822a80870 100644 --- a/guacamole/src/main/webapp/app/client/types/ManagedClient.js +++ b/guacamole/src/main/webapp/app/client/types/ManagedClient.js @@ -438,9 +438,31 @@ angular.module('client').factory('ManagedClient', ['$rootScope', '$injector', * * @param {File} file * The file to upload. + * + * @param {ManagedFilesystem} [filesystem] + * The filesystem to upload the file to, if any. If not specified, the + * file will be sent as a generic Guacamole file stream. + * + * @param {ManagedFilesystem.File} [directory=filesystem.currentDirectory] + * The directory within the given filesystem to upload the file to. If + * not specified, but a filesystem is given, the current directory of + * that filesystem will be used. */ - ManagedClient.uploadFile = function uploadFile(managedClient, file) { - managedClient.uploads.push(ManagedFileUpload.getInstance(managedClient.client, file)); + ManagedClient.uploadFile = function uploadFile(managedClient, file, filesystem, directory) { + + // Use generic Guacamole file streams by default + var object = null; + var streamName = null; + + // If a filesystem is given, determine the destination object and stream + if (filesystem) { + object = filesystem.object; + streamName = (directory || filesystem.currentDirectory).streamName + '/' + file.name; + } + + // Start and manage file upload + managedClient.uploads.push(ManagedFileUpload.getInstance(managedClient.client, file, object, streamName)); + }; return ManagedClient; diff --git a/guacamole/src/main/webapp/app/client/types/ManagedFileUpload.js b/guacamole/src/main/webapp/app/client/types/ManagedFileUpload.js index 01a946806..6cd51bba9 100644 --- a/guacamole/src/main/webapp/app/client/types/ManagedFileUpload.js +++ b/guacamole/src/main/webapp/app/client/types/ManagedFileUpload.js @@ -124,11 +124,19 @@ angular.module('client').factory('ManagedFileUpload', ['$rootScope', '$injector' * @param {File} file * The file to upload. * + * @param {Object} [object] + * The object to upload the file to, if any, such as a filesystem + * object. + * + * @param {String} [streamName] + * The name of the stream to upload the file to. If an object is given, + * this must be specified. + * * @return {ManagedFileUpload} * A new ManagedFileUpload object which can be used to track the * progress of the upload. */ - ManagedFileUpload.getInstance = function getInstance(client, file) { + ManagedFileUpload.getInstance = function getInstance(client, file, object, streamName) { var managedFileUpload = new ManagedFileUpload(); @@ -137,7 +145,14 @@ angular.module('client').factory('ManagedFileUpload', ['$rootScope', '$injector' reader.onloadend = function fileContentsLoaded() { // Open file for writing - var stream = client.createFileStream(file.type, file.name); + var stream; + if (!object) + stream = client.createFileStream(file.type, file.name); + + // If object/streamName specified, upload to that instead of a file + // stream + else + stream = object.createOutputStream(file.type, streamName); var valid = true; var bytes = new Uint8Array(reader.result);