Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature to send multiple files #64

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions stars-bg.js

Large diffs are not rendered by default.

117 changes: 82 additions & 35 deletions webrepl.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
color: #f0f0f0;
background: #000;
}

.terminal-cursor {
color: #000;
background: #f0f0f0;
Expand Down Expand Up @@ -57,8 +56,14 @@

<div class="file-box">
<strong>Send a file</strong>
<input type="file" id="put-file-select" />
<!-- select files to send -->
<input type="file" id="put-file-select" multiple="multiple"/>
<strong>Destination path</strong>
<!-- destination path input -->
<input type="text" name="set_filepath" id="put_filepath" value="." size="13" />
<!-- sent files list update div -->
<div id="put-file-list"></div>
<!-- send to device button -->
<input type="button" value="Send to device" id="put-file-button" onclick="put_file(); return false;" />
</div>

Expand All @@ -85,9 +90,12 @@
var connected = false;
var binary_state = 0;
var put_file_name = null;
var put_file_path = null;
var put_file_data = null;
var get_file_name = null;
var get_file_data = null;
var put_file_names_list = [];
var put_files_data_list = [];

function calculate_size(win) {
var cols = Math.max(80, Math.min(150, (win.innerWidth - 280) / 7)) | 0;
Expand Down Expand Up @@ -246,6 +254,8 @@
console.log('GET_VER', data);
binary_state = 0;
break;
default:
console.log('default: \nbinary_state ' + binary_state)
}
}
term.write(event.data);
Expand All @@ -271,31 +281,57 @@
}
}

function put_file() {
var dest_fname = put_file_name;
var dest_fsize = put_file_data.length;
var counter = 0;
var delayTime = 0;

// WEBREPL_FILE = "<2sBBQLH64s"
var rec = new Uint8Array(2 + 1 + 1 + 8 + 4 + 2 + 64);
rec[0] = 'W'.charCodeAt(0);
rec[1] = 'A'.charCodeAt(0);
rec[2] = 1; // put
rec[3] = 0;
rec[4] = 0; rec[5] = 0; rec[6] = 0; rec[7] = 0; rec[8] = 0; rec[9] = 0; rec[10] = 0; rec[11] = 0;
rec[12] = dest_fsize & 0xff; rec[13] = (dest_fsize >> 8) & 0xff; rec[14] = (dest_fsize >> 16) & 0xff; rec[15] = (dest_fsize >> 24) & 0xff;
rec[16] = dest_fname.length & 0xff; rec[17] = (dest_fname.length >> 8) & 0xff;
for (var i = 0; i < 64; ++i) {
if (i < dest_fname.length) {
rec[18 + i] = dest_fname.charCodeAt(i);
} else {
rec[18 + i] = 0;
function put_file() {
setTimeout(function() {
put_file_name = put_file_names_list[counter];
put_file_data = put_files_data_list[counter];
delayTime = Math.floor(put_file_data.length/2);
console.log(counter, delayTime)

var dest_fname = put_file_name;
var dest_fpath = document.getElementById('put_filepath').value;
var dest_fsize = put_file_data.length;
var dest_full_fname = dest_fpath + '/' + dest_fname;

// WEBREPL_FILE = "<2sBBQLH64s"
var rec = new Uint8Array(2 + 1 + 1 + 8 + 4 + 2 + 64);
rec[0] = 'W'.charCodeAt(0);
rec[1] = 'A'.charCodeAt(0);
rec[2] = 1; // put
rec[3] = 0;
rec[4] = 0; rec[5] = 0; rec[6] = 0; rec[7] = 0; rec[8] = 0; rec[9] = 0;
rec[10] = 0; rec[11] = 0;
rec[12] = dest_fsize & 0xff;
rec[13] = (dest_fsize >> 8) & 0xff;
rec[14] = (dest_fsize >> 16) & 0xff;
rec[15] = (dest_fsize >> 24) & 0xff;
rec[16] = dest_full_fname.length & 0xff;
rec[17] = (dest_full_fname.length >> 8) & 0xff;
for (var i = 0; i < 64; ++i) {
if (i < dest_full_fname.length) {
rec[18 + i] = dest_full_fname.charCodeAt(i);
} else {
rec[18 + i] = 0;
}
}
// initiate put
binary_state = 11;
update_file_status('Sending ' + put_file_name + '...');
ws.send(rec);

counter++;
if(counter < put_file_names_list.length){
put_file()
}
else if(counter == put_file_names_list.length){
counter = 0
delayTime = 0
}
}

// initiate put
binary_state = 11;
update_file_status('Sending ' + put_file_name + '...');
ws.send(rec);
}, delayTime);
}

function get_file() {
Expand Down Expand Up @@ -344,25 +380,36 @@
// but we only support single file selection at the moment.
var files = evt.target.files;

// Get the file info and load its data.
var f = files[0];
put_file_name = f.name;
var reader = new FileReader();
reader.onload = function(e) {
put_file_data = new Uint8Array(e.target.result);
document.getElementById('put-file-list').innerHTML = '' + escape(put_file_name) + ' - ' + put_file_data.length + ' bytes';
document.getElementById('put-file-button').disabled = false;
};
reader.readAsArrayBuffer(f);
// addition for multiple file support
for (let i = 0; i < files.length; i++) {
// Get the file info and load its data.
var f = files[i];
// put_file_name = f.name;
put_file_names_list.push(f.name);
var reader = new FileReader();
reader.onload = function(e) {
// put_file_data = new Uint8Array(e.target.result);
put_files_data_list.push(new Uint8Array(e.target.result))
// document.getElementById('put-file-list').innerHTML = '' + escape(put_file_name) + ' - ' + put_file_data.length + ' bytes';
document.getElementById('put-file-list').innerHTML = '' + escape(put_file_names_list[i]) + ' - ' + put_files_data_list[i].length + ' bytes';
if(i === files.length-1){
document.getElementById('put-file-button').disabled = false;
}
};
reader.readAsArrayBuffer(f);
}
}

document.getElementById('put-file-select').addEventListener('click', function(){
this.value = null;
document.getElementById('put-file-button').disabled = true;
put_file_names_list = []
put_files_data_list = []
}, false);

document.getElementById('put-file-select').addEventListener('change', handle_put_file_select, false);
document.getElementById('put-file-button').disabled = true;

</script>

</html>
</html>