source: OpenRLabs-Git/web2py/applications/rlabs/static/js/guacamole-common-js/modules/BlobReader.js

main
Last change on this file was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20var Guacamole = Guacamole || {};
21
22/**
23 * A reader which automatically handles the given input stream, assembling all
24 * received blobs into a single blob by appending them to each other in order.
25 * Note that this object will overwrite any installed event handlers on the
26 * given Guacamole.InputStream.
27 *
28 * @constructor
29 * @param {Guacamole.InputStream} stream The stream that data will be read
30 *                                       from.
31 * @param {String} mimetype The mimetype of the blob being built.
32 */
33Guacamole.BlobReader = function(stream, mimetype) {
34
35    /**
36     * Reference to this Guacamole.InputStream.
37     * @private
38     */
39    var guac_reader = this;
40
41    /**
42     * The length of this Guacamole.InputStream in bytes.
43     * @private
44     */
45    var length = 0;
46
47    // Get blob builder
48    var blob_builder;
49    if      (window.BlobBuilder)       blob_builder = new BlobBuilder();
50    else if (window.WebKitBlobBuilder) blob_builder = new WebKitBlobBuilder();
51    else if (window.MozBlobBuilder)    blob_builder = new MozBlobBuilder();
52    else
53        blob_builder = new (function() {
54
55            var blobs = [];
56
57            /** @ignore */
58            this.append = function(data) {
59                blobs.push(new Blob([data], {"type": mimetype}));
60            };
61
62            /** @ignore */
63            this.getBlob = function() {
64                return new Blob(blobs, {"type": mimetype});
65            };
66
67        })();
68
69    // Append received blobs
70    stream.onblob = function(data) {
71
72        // Convert to ArrayBuffer
73        var binary = window.atob(data);
74        var arrayBuffer = new ArrayBuffer(binary.length);
75        var bufferView = new Uint8Array(arrayBuffer);
76
77        for (var i=0; i<binary.length; i++)
78            bufferView[i] = binary.charCodeAt(i);
79
80        blob_builder.append(arrayBuffer);
81        length += arrayBuffer.byteLength;
82
83        // Call handler, if present
84        if (guac_reader.onprogress)
85            guac_reader.onprogress(arrayBuffer.byteLength);
86
87        // Send success response
88        stream.sendAck("OK", 0x0000);
89
90    };
91
92    // Simply call onend when end received
93    stream.onend = function() {
94        if (guac_reader.onend)
95            guac_reader.onend();
96    };
97
98    /**
99     * Returns the current length of this Guacamole.InputStream, in bytes.
100     * @return {Number} The current length of this Guacamole.InputStream.
101     */
102    this.getLength = function() {
103        return length;
104    };
105
106    /**
107     * Returns the contents of this Guacamole.BlobReader as a Blob.
108     * @return {Blob} The contents of this Guacamole.BlobReader.
109     */
110    this.getBlob = function() {
111        return blob_builder.getBlob();
112    };
113
114    /**
115     * Fired once for every blob of data received.
116     *
117     * @event
118     * @param {Number} length The number of bytes received.
119     */
120    this.onprogress = null;
121
122    /**
123     * Fired once this stream is finished and no further data will be written.
124     * @event
125     */
126    this.onend = null;
127
128};
Note: See TracBrowser for help on using the repository browser.