mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-07 05:31:22 +00:00
GUACAMOLE-44: Use tunnel REST service for file uploads.
This commit is contained in:
@@ -519,7 +519,7 @@ angular.module('client').factory('ManagedClient', ['$rootScope', '$injector',
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Start and manage file upload
|
// Start and manage file upload
|
||||||
managedClient.uploads.push(ManagedFileUpload.getInstance(managedClient.client, file, object, streamName));
|
managedClient.uploads.push(ManagedFileUpload.getInstance(managedClient, file, object, streamName));
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -24,10 +24,12 @@ angular.module('client').factory('ManagedFileUpload', ['$rootScope', '$injector'
|
|||||||
function defineManagedFileUpload($rootScope, $injector) {
|
function defineManagedFileUpload($rootScope, $injector) {
|
||||||
|
|
||||||
// Required types
|
// Required types
|
||||||
|
var Error = $injector.get('Error');
|
||||||
var ManagedFileTransferState = $injector.get('ManagedFileTransferState');
|
var ManagedFileTransferState = $injector.get('ManagedFileTransferState');
|
||||||
|
|
||||||
// Required services
|
// Required services
|
||||||
var $window = $injector.get('$window');
|
var $window = $injector.get('$window');
|
||||||
|
var tunnelService = $injector.get('tunnelService');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Object which serves as a surrogate interface, encapsulating a Guacamole
|
* Object which serves as a surrogate interface, encapsulating a Guacamole
|
||||||
@@ -105,8 +107,8 @@ angular.module('client').factory('ManagedFileUpload', ['$rootScope', '$injector'
|
|||||||
* Creates a new ManagedFileUpload which uploads the given file to the
|
* Creates a new ManagedFileUpload which uploads the given file to the
|
||||||
* server through the given Guacamole client.
|
* server through the given Guacamole client.
|
||||||
*
|
*
|
||||||
* @param {Guacamole.Client} client
|
* @param {ManagedClient} managedClient
|
||||||
* The Guacamole client through which the file is to be uploaded.
|
* The ManagedClient through which the file is to be uploaded.
|
||||||
*
|
*
|
||||||
* @param {File} file
|
* @param {File} file
|
||||||
* The file to upload.
|
* The file to upload.
|
||||||
@@ -123,10 +125,13 @@ angular.module('client').factory('ManagedFileUpload', ['$rootScope', '$injector'
|
|||||||
* A new ManagedFileUpload object which can be used to track the
|
* A new ManagedFileUpload object which can be used to track the
|
||||||
* progress of the upload.
|
* progress of the upload.
|
||||||
*/
|
*/
|
||||||
ManagedFileUpload.getInstance = function getInstance(client, file, object, streamName) {
|
ManagedFileUpload.getInstance = function getInstance(managedClient, file, object, streamName) {
|
||||||
|
|
||||||
var managedFileUpload = new ManagedFileUpload();
|
var managedFileUpload = new ManagedFileUpload();
|
||||||
var streamAcknowledged = false;
|
|
||||||
|
// Pull Guacamole.Tunnel and Guacamole.Client from given ManagedClient
|
||||||
|
var client = managedClient.client;
|
||||||
|
var tunnel = managedClient.tunnel;
|
||||||
|
|
||||||
// Open file for writing
|
// Open file for writing
|
||||||
var stream;
|
var stream;
|
||||||
@@ -153,10 +158,8 @@ angular.module('client').factory('ManagedFileUpload', ['$rootScope', '$injector'
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var writer = new Guacamole.FileWriter(stream);
|
// Upload file once stream is acknowledged
|
||||||
|
stream.onack = function beginUpload(status) {
|
||||||
// Begin upload when stream is acknowledged, notify of any errors
|
|
||||||
writer.onack = function ackReceived(status) {
|
|
||||||
|
|
||||||
// Notify of any errors from the Guacamole server
|
// Notify of any errors from the Guacamole server
|
||||||
if (status.isError()) {
|
if (status.isError()) {
|
||||||
@@ -165,50 +168,48 @@ angular.module('client').factory('ManagedFileUpload', ['$rootScope', '$injector'
|
|||||||
ManagedFileTransferState.StreamState.ERROR,
|
ManagedFileTransferState.StreamState.ERROR,
|
||||||
status.code);
|
status.code);
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Begin sending the requested file once stream is acknowledged
|
// Begin upload
|
||||||
else if (!streamAcknowledged) {
|
tunnelService.uploadToStream(tunnel.uuid, stream, file, function uploadContinuing(length) {
|
||||||
writer.sendFile(file);
|
$rootScope.$apply(function uploadStreamProgress() {
|
||||||
streamAcknowledged = true;
|
managedFileUpload.progress = length;
|
||||||
}
|
});
|
||||||
|
})
|
||||||
|
|
||||||
};
|
// Notify if upload succeeds
|
||||||
|
.then(function uploadSuccessful() {
|
||||||
|
|
||||||
// Abort and notify if the file cannot be read
|
// Upload complete
|
||||||
writer.onerror = function fileReadError(file, offset, error) {
|
managedFileUpload.progress = file.size;
|
||||||
|
ManagedFileTransferState.setStreamState(managedFileUpload.transferState,
|
||||||
|
ManagedFileTransferState.StreamState.CLOSED);
|
||||||
|
|
||||||
// Abort transfer
|
// Notify of upload completion
|
||||||
writer.sendEnd();
|
$rootScope.$broadcast('guacUploadComplete', file.name);
|
||||||
|
|
||||||
// Upload failed
|
},
|
||||||
ManagedFileTransferState.setStreamState(managedFileUpload.transferState,
|
|
||||||
ManagedFileTransferState.StreamState.ERROR);
|
|
||||||
|
|
||||||
};
|
// Notify if upload fails
|
||||||
|
function uploadFailed(error) {
|
||||||
|
|
||||||
// Notify of upload progress
|
// Use provide status code if the error is coming from the stream
|
||||||
writer.onprogress = function uploadProgressing(file, length) {
|
if (error.type === Error.Type.STREAM_ERROR)
|
||||||
|
ManagedFileTransferState.setStreamState(managedFileUpload.transferState,
|
||||||
|
ManagedFileTransferState.StreamState.ERROR,
|
||||||
|
error.statusCode);
|
||||||
|
|
||||||
|
// Fail with internal error for all other causes
|
||||||
|
else
|
||||||
|
ManagedFileTransferState.setStreamState(managedFileUpload.transferState,
|
||||||
|
ManagedFileTransferState.StreamState.ERROR,
|
||||||
|
Guacamole.Status.Code.INTERNAL_ERROR);
|
||||||
|
|
||||||
$rootScope.$apply(function uploadStreamProgress() {
|
|
||||||
managedFileUpload.progress = length;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
// Ignore all further acks
|
||||||
|
stream.onack = null;
|
||||||
// Clean up and notify when upload completes
|
|
||||||
writer.oncomplete = function uploadComplete(file) {
|
|
||||||
|
|
||||||
// If at end, stop upload
|
|
||||||
writer.sendEnd();
|
|
||||||
managedFileUpload.progress = file.size;
|
|
||||||
|
|
||||||
// Upload complete
|
|
||||||
ManagedFileTransferState.setStreamState(managedFileUpload.transferState,
|
|
||||||
ManagedFileTransferState.StreamState.CLOSED);
|
|
||||||
|
|
||||||
// Notify of upload completion
|
|
||||||
$rootScope.$broadcast('guacUploadComplete', file.name);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user