index-localhost.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // This is the default config (localhost). This file does nothing on its
  2. // own, and should not be modified. The index.js file is updated by sed
  3. // when the container is run.
  4. // Check for the various File API support.
  5. if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
  6. alert('This website requires HTML5, which your browser does not fully support. Please use a supported browser to use this website.');
  7. }
  8. // Grab a peer ID
  9. var peer = new Peer({host: 'localhost', port: 9000, path: '/node_modules/peerjs'});
  10. // Send a file
  11. function send() {
  12. // Check that all fields are filled
  13. var valid = true;
  14. var file_to_send = document.getElementById("file_to_send").files[0];
  15. var dest_id = document.getElementById("dest_id").value;
  16. if(!file_to_send) {
  17. valid = false;
  18. alert("Please select a file.");
  19. }
  20. if(!dest_id) {
  21. valid = false;
  22. alert("Please provide a Destination ID.");
  23. }
  24. else if (dest_id === peer.id) {
  25. valid = false;
  26. alert("You can't send a file to yourself!");
  27. }
  28. if(!valid){
  29. return;
  30. }
  31. // Build blob from file and file type
  32. var blob = new Blob([file_to_send], {type: file_to_send.type});
  33. // Connect to the desired peer
  34. var con = peer.connect(dest_id, { label: 'file', reliable: true});
  35. con.on('open', function() {
  36. // Send arr of blob and file data
  37. con.send({
  38. name: document.getElementById("file_to_send").files[0].name,
  39. file: blob,
  40. type: document.getElementById("file_to_send").files[0].type
  41. });
  42. });
  43. }
  44. // Display ID
  45. peer.on('open', function(id) {
  46. document.getElementById("my_id").textContent = peer.id;
  47. });
  48. // Constantly listen for incoming connections
  49. peer.on('connection', function(conn) {
  50. conn.on('open', function() {
  51. conn.on('data', function(data) {
  52. var blob = new Blob([data.file], {type: data.type});
  53. var url = window.URL.createObjectURL(blob);
  54. var table = document.getElementById("file_table");
  55. // Remove the "No Files" row
  56. var elem = document.getElementById("empty_row");
  57. if(elem) {
  58. elem.parentNode.removeChild(elem);
  59. //var row = table.insertRow(1);
  60. //row.innerHTML = "File";
  61. //var cell = row.insertCell(0);
  62. //cell.innerHTML = "Time received";
  63. //cell = row.insertCell(0);
  64. //cell.innerHTML = "Source ID";
  65. //cell = row.insertCell(0);
  66. //cell.innerHTML = "Size";
  67. }
  68. var row = table.insertRow(1);
  69. var a = document.createElement("a");
  70. a.href = url;
  71. a.innerHTML = data.name;
  72. a.setAttribute("download", data.name);
  73. var file_col = row.insertCell(0);
  74. file_col.appendChild(a);
  75. //int id_col = row.insertCell(0);
  76. //var d = new Date();
  77. //id_col.innerHTML = d.getHours+':'+d.getMinutes();
  78. });
  79. });
  80. });
  81. peer.on('error', function(err) {
  82. if (err.type === 'peer-unavailable') {
  83. alert("No user with the given ID is currently connected.");
  84. }
  85. });