elijahnicpon commented on code in PR #746:
URL: https://github.com/apache/guacamole-client/pull/746#discussion_r932750593
##########
guacamole/src/main/frontend/src/app/rest/services/tunnelService.js:
##########
@@ -301,51 +310,99 @@ angular.module('rest').factory('tunnelService',
['$injector',
+ '/' + encodeURIComponent(sanitizeFilename(file.name))
+ '?token=' +
encodeURIComponent(authenticationService.getCurrentToken());
- var xhr = new XMLHttpRequest();
-
- // Invoke provided callback if upload tracking is supported
- if (progressCallback && xhr.upload) {
- xhr.upload.addEventListener('progress', function updateProgress(e)
{
- progressCallback(e.loaded);
- });
- }
-
- // Resolve/reject promise once upload has stopped
- xhr.onreadystatechange = function uploadStatusChanged() {
-
- // Ignore state changes prior to completion
- if (xhr.readyState !== 4)
- return;
-
- // Resolve if HTTP status code indicates success
- if (xhr.status >= 200 && xhr.status < 300)
- deferred.resolve();
-
- // Parse and reject with resulting JSON error
- else if (xhr.getResponseHeader('Content-Type') ===
'application/json')
- deferred.reject(new Error(angular.fromJson(xhr.responseText)));
-
- // Warn of lack of permission of a proxy rejects the upload
- else if (xhr.status >= 400 && xhr.status < 500)
- deferred.reject(new Error({
- 'type' : Error.Type.STREAM_ERROR,
- 'statusCode' : Guacamole.Status.Code.CLIENT_FORBIDDEN,
- 'message' : 'HTTP ' + xhr.status
- }));
-
- // Assume internal error for all other cases
- else
- deferred.reject(new Error({
- 'type' : Error.Type.STREAM_ERROR,
- 'statusCode' : Guacamole.Status.Code.INTERNAL_ERROR,
- 'message' : 'HTTP ' + xhr.status
- }));
+ /**
+ * Creates a chunk of the inputted file to be uploaded.
+ *
+ * @param {Number} offset
+ * The byte at which to begin the chunk.
+ *
+ * @return {File}
+ * The file chunk created by this function.
+ */
+ const createChunk = (offset) => {
+ var chunkEnd = Math.min(offset + CHUNK_SIZE, file.size);
+ const chunk = file.slice(offset, chunkEnd);
+ return chunk;
+ };
+
+ /**
+ * POSTs the inputted chunks and recursively calls uploadHandler()
+ * until the upload is complete.
+ *
+ * @param {File} chunk
+ * The chunk to be uploaded to the stream.
+ *
+ * @param {Number} offset
+ * The byte at which the inputted chunk begins.
+ */
+ const uploadChunk = (chunk, offset) => {
+ var xhr = new XMLHttpRequest();
+ xhr.open('POST', url, true);
+
+ // Invoke provided callback if upload tracking is supported.
+ if (progressCallback && xhr.upload) {
+ xhr.upload.addEventListener('progress', function
updateProgress(e) {
+ progressCallback(e.loaded + offset);
+ });
+ };
+
+ // Continue to next chunk, resolve, or reject promise as
appropriate
+ // once upload has stopped
+ xhr.onreadystatechange = function uploadStatusChanged() {
+
+ // Ignore state changes prior to completion.
+ if (xhr.readyState !== 4)
+ return;
+
+ // Resolve if last chunk or begin next chunk if HTTP status
+ // code indicates success.
+ if (xhr.status >= 200 && xhr.status < 300) {
+ offset += CHUNK_SIZE;
+
+ if (offset < file.size)
+ uploadHandler(offset);
+ else
+ deferred.resolve();
+ }
+
+ // Parse and reject with resulting JSON error
+ else if (xhr.getResponseHeader('Content-Type') ===
'application/json')
+ deferred.reject(new
Error(angular.fromJson(xhr.responseText)));
+
+ // Warn of lack of permission of a proxy rejects the upload
+ else if (xhr.status >= 400 && xhr.status < 500)
+ deferred.reject(new Error({
+ 'type': Error.Type.STREAM_ERROR,
+ 'statusCode': Guacamole.Status.Code.CLIENT_FORBIDDEN,
+ 'message': 'HTTP ' + xhr.status
+ }));
+
+ // Assume internal error for all other cases
+ else
+ deferred.reject(new Error({
+ 'type': Error.Type.STREAM_ERROR,
+ 'statusCode': Guacamole.Status.Code.INTERNAL_ERROR,
+ 'message': 'HTTP ' + xhr.status
+ }));
+
+ };
+
+ // Perform upload
+ xhr.send(chunk);
+
+ };
+ /**
+ * Handler for the upload process.
Review Comment:
Gotcha. Updated it to:
Handles the recursive upload process. Each time it is called, a chunk is
made with createChunk(), starting at the offset parameter. The chunk is then
sent by uploadChunk(), which recursively calls this handler until the upload
process is either completed and the promise is resolved, or fails and the
promise is rejected.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]