GUACAMOLE-55: Refactor FileWriter to BlobWriter.

This commit is contained in:
Michael Jumper
2016-06-23 00:24:38 -07:00
parent e8232315ce
commit c87f7444cc

View File

@@ -21,19 +21,19 @@ var Guacamole = Guacamole || {};
/** /**
* A writer which automatically writes to the given output stream with the * A writer which automatically writes to the given output stream with the
* contents of a local files, supplied as standard File objects. * contents of provided Blob objects.
* *
* @constructor * @constructor
* @param {Guacamole.OutputStream} stream * @param {Guacamole.OutputStream} stream
* The stream that data will be written to. * The stream that data will be written to.
*/ */
Guacamole.FileWriter = function FileWriter(stream) { Guacamole.BlobWriter = function BlobWriter(stream) {
/** /**
* Reference to this Guacamole.FileWriter. * Reference to this Guacamole.BlobWriter.
* *
* @private * @private
* @type {Guacamole.FileWriter} * @type {Guacamole.BlobWriter}
*/ */
var guacWriter = this; var guacWriter = this;
@@ -100,19 +100,19 @@ Guacamole.FileWriter = function FileWriter(stream) {
}; };
/** /**
* Sends the contents of the given file over the underlying stream. * Sends the contents of the given blob over the underlying stream.
* *
* @param {File} file * @param {Blob} blob
* The file to send. * The blob to send.
*/ */
this.sendFile = function sendFile(file) { this.sendBlob = function sendBlob(blob) {
var offset = 0; var offset = 0;
var reader = new FileReader(); var reader = new FileReader();
/** /**
* Reads the next chunk of the file provided to * Reads the next chunk of the blob provided to
* [sendFile()]{@link Guacamole.FileWriter#sendFile}. The chunk itself * [sendBlob()]{@link Guacamole.BlobWriter#sendBlob}. The chunk itself
* is read asynchronously, and will not be available until * is read asynchronously, and will not be available until
* reader.onload fires. * reader.onload fires.
* *
@@ -121,11 +121,11 @@ Guacamole.FileWriter = function FileWriter(stream) {
var readNextChunk = function readNextChunk() { var readNextChunk = function readNextChunk() {
// If no further chunks remain, inform of completion and stop // If no further chunks remain, inform of completion and stop
if (offset >= file.size) { if (offset >= blob.size) {
// Fire completion event for completed file // Fire completion event for completed blob
if (guacWriter.oncomplete) if (guacWriter.oncomplete)
guacWriter.oncomplete(file); guacWriter.oncomplete(blob);
// No further chunks to read // No further chunks to read
return; return;
@@ -133,10 +133,10 @@ Guacamole.FileWriter = function FileWriter(stream) {
} }
// Obtain reference to next chunk as a new blob // Obtain reference to next chunk as a new blob
var chunk = slice(file, offset, offset + arrayBufferWriter.blobLength); var chunk = slice(blob, offset, offset + arrayBufferWriter.blobLength);
offset += arrayBufferWriter.blobLength; offset += arrayBufferWriter.blobLength;
// Attempt to read the file contents represented by the blob into // Attempt to read the blob contents represented by the blob into
// a new array buffer // a new array buffer
reader.readAsArrayBuffer(chunk); reader.readAsArrayBuffer(chunk);
@@ -159,9 +159,9 @@ Guacamole.FileWriter = function FileWriter(stream) {
if (status.isError()) if (status.isError())
return; return;
// Inform of file upload progress via progress events // Inform of blob upload progress via progress events
if (guacWriter.onprogress) if (guacWriter.onprogress)
guacWriter.onprogress(file, offset - arrayBufferWriter.blobLength); guacWriter.onprogress(blob, offset - arrayBufferWriter.blobLength);
// Queue the next chunk for reading // Queue the next chunk for reading
readNextChunk(); readNextChunk();
@@ -175,7 +175,7 @@ Guacamole.FileWriter = function FileWriter(stream) {
// Fire error event, including the context of the error // Fire error event, including the context of the error
if (guacWriter.onerror) if (guacWriter.onerror)
guacWriter.onerror(file, offset, reader.error); guacWriter.onerror(blob, offset, reader.error);
}; };
@@ -202,16 +202,16 @@ Guacamole.FileWriter = function FileWriter(stream) {
this.onack = null; this.onack = null;
/** /**
* Fired when an error occurs reading a file passed to * Fired when an error occurs reading a blob passed to
* [sendFile()]{@link Guacamole.FileWriter#sendFile}. The file transfer for * [sendBlob()]{@link Guacamole.BlobWriter#sendBlob}. The transfer for the
* the given file will cease, but the stream will remain open. * the given blob will cease, but the stream will remain open.
* *
* @event * @event
* @param {File} file * @param {Blob} blob
* The file that was being read when the error occurred. * The blob that was being read when the error occurred.
* *
* @param {Number} offset * @param {Number} offset
* The offset of the failed read attempt within the file, in bytes. * The offset of the failed read attempt within the blob, in bytes.
* *
* @param {DOMError} error * @param {DOMError} error
* The error that occurred. * The error that occurred.
@@ -219,12 +219,12 @@ Guacamole.FileWriter = function FileWriter(stream) {
this.onerror = null; this.onerror = null;
/** /**
* Fired for each successfully-read chunk of file data as a file is being * Fired for each successfully-read chunk of data as a blob is being sent
* sent via [sendFile()]{@link Guacamole.FileWriter#sendFile}. * via [sendBlob()]{@link Guacamole.BlobWriter#sendBlob}.
* *
* @event * @event
* @param {File} file * @param {Blob} blob
* The file that is being read. * The blob that is being read.
* *
* @param {Number} offset * @param {Number} offset
* The offset of the read that just succeeded. * The offset of the read that just succeeded.
@@ -232,13 +232,13 @@ Guacamole.FileWriter = function FileWriter(stream) {
this.onprogress = null; this.onprogress = null;
/** /**
* Fired when a file passed to * Fired when a blob passed to
* [sendFile()]{@link Guacamole.FileWriter#sendFile} has finished being * [sendBlob()]{@link Guacamole.BlobWriter#sendBlob} has finished being
* sent. * sent.
* *
* @event * @event
* @param {File} file * @param {Blob} blob
* The file that was sent. * The blob that was sent.
*/ */
this.oncomplete = null; this.oncomplete = null;