|
| 1 | +// Browser-specific tools |
| 2 | +( function() { |
| 3 | +"use strict"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Send a batch of files in sequence. The session is left open |
| 7 | + * afterward, which allows for more files to be sent if desired. |
| 8 | + * |
| 9 | + * @param {Zmodem.Session} session - The send session |
| 10 | + * |
| 11 | + * @param {FileList|Array} files - A list of File objects |
| 12 | + * |
| 13 | + * @param {Object} options - Optional, can be: |
| 14 | + * |
| 15 | + * - on_offer_response(File object, Transfer object) |
| 16 | + * Called when an offer response arrives. If the offer is |
| 17 | + * not accepted (i.e., skipped), the 2nd argument will be undefined. |
| 18 | + * |
| 19 | + * - on_progress(File, Transfer, Uint8Array) |
| 20 | + * Called immediately after a chunk of a file is sent. |
| 21 | + * That chunk is represented by the Uint8Array. |
| 22 | + * |
| 23 | + * - on_file_complete(File, Transfer, Uint8Array) |
| 24 | + * Called immediately after the last chunk of a file is sent. |
| 25 | + * That chunk is represented by the Uint8Array. |
| 26 | + * (It’s probably empty.) |
| 27 | + * |
| 28 | + * @return {Promise} A Promise that fulfills when the batch is done. |
| 29 | + * Note that skipped files are not considered an error condition. |
| 30 | + */ |
| 31 | +function send_files(session, files, options) { |
| 32 | + if (!options) options = {}; |
| 33 | + |
| 34 | + var batch = []; |
| 35 | + var total_size = 0; |
| 36 | + for (var f=0; f<files_obj.length; f++) { |
| 37 | + var fobj = files_obj[f]; |
| 38 | + batch.push( { |
| 39 | + obj: fobj, |
| 40 | + name: fobj.name, |
| 41 | + size: fobj.size, |
| 42 | + } ); |
| 43 | + total_size += fobj.size; |
| 44 | + } |
| 45 | + |
| 46 | + var file_idx = 0; |
| 47 | + function promise_callback() { |
| 48 | + var cur_b = batch[file_idx]; |
| 49 | + |
| 50 | + if (!cur_b) return; //batch done! |
| 51 | + |
| 52 | + file_idx++; |
| 53 | + |
| 54 | + return zsession.send_offer(cur_b).then( function after_send_offer(xfer) { |
| 55 | + if (options.on_offer_response) { |
| 56 | + options.on_offer_response(cur_b.obj, xfer); |
| 57 | + } |
| 58 | + |
| 59 | + if (xfer === undefined) { |
| 60 | + return promise_callback(); //skipped |
| 61 | + } |
| 62 | + |
| 63 | + return new Promise( function(res) { |
| 64 | + var reader = new FileReader(); |
| 65 | + |
| 66 | + //This really shouldn’t happen … so let’s |
| 67 | + //blow up if it does. |
| 68 | + reader.onerror = function reader_onerror(e) { |
| 69 | + console.error("file read error", e); |
| 70 | + throw("File read error: " + e); |
| 71 | + }; |
| 72 | + |
| 73 | + var piece; |
| 74 | + reader.onprogress = function reader_onprogress(e) { |
| 75 | + piece = new Uint8Array(e.target.result, xfer.get_offset()) |
| 76 | + xfer.send(piece); |
| 77 | + |
| 78 | + if (options.on_progress) { |
| 79 | + options.on_progress(cur_b.obj, xfer, piece); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + reader.onload = function reader_onload(e) { |
| 84 | + piece = new Uint8Array(e.target.result, xfer, piece) |
| 85 | + xfer.end(piece).then(res).then(promise_callback); |
| 86 | + |
| 87 | + if (options.on_file_complete) { |
| 88 | + options.on_file_complete(cur_b.obj, xfer); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + reader.readAsArrayBuffer(cur_b.obj); |
| 93 | + } ); |
| 94 | + } ); |
| 95 | + } |
| 96 | + |
| 97 | + return promise_callback(); |
| 98 | +} |
| 99 | + |
| 100 | +Zmodem.Browser = { |
| 101 | + send_files: send_files, |
| 102 | +}; |
| 103 | + |
| 104 | +}()); |
0 commit comments