Browse Source

consolidate functionality, separate js

Josh Bicking 7 years ago
parent
commit
35f471f55f
6 changed files with 124 additions and 91 deletions
  1. 0 2
      package.json
  2. 94 0
      public/javascripts/index.js
  3. 0 2
      routes/index.js
  4. 30 8
      views/index.ejs
  5. 0 33
      views/receive.ejs
  6. 0 46
      views/send.ejs

+ 0 - 2
package.json

@@ -11,8 +11,6 @@
     "debug": "~2.2.0",
     "ejs": "~2.4.1",
     "express": "~4.13.4",
-    "file-saver": "^1.3.3",
-    "fs": "0.0.1-security",
     "morgan": "~1.7.0",
     "peer": "^0.2.8",
     "peerjs": "^0.3.14",

+ 94 - 0
public/javascripts/index.js

@@ -0,0 +1,94 @@
+// Check for the various File API support.
+if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
+  alert('This website requires HTML5, which your browser does not fully support. Please use a supported browser to use this website.');
+}
+
+// Grab a peer ID
+var peer = new Peer({host: 'localhost', port: 9000, path: '/node_modules/peerjs'});
+
+// Send a file
+function send() {
+    // Check that all fields are filled
+    var valid = true;
+    var file_to_send = document.getElementById("file_to_send").files[0];
+    var dest_id = document.getElementById("dest_id").value;
+    if(!file_to_send) {
+        valid = false;
+        alert("Please select a file.");
+    }
+    if(!dest_id) {
+        valid = false;
+        alert("Please provide a Destination ID.");
+    }
+    else if (dest_id === peer.id) {
+        valid = false;
+        alert("You can't send a file to yourself!");
+    }
+    if(!valid){
+        return;
+    }
+
+    // Build blob from file and file type
+    var blob = new Blob([file_to_send], {type: file_to_send.type});
+
+    // Connect to the desired peer
+    var con = peer.connect(dest_id, { label: 'file', reliable: true}); 
+
+    con.on('open', function() {
+        // Send arr of blob and file data
+        con.send({
+            name: document.getElementById("file_to_send").files[0].name,
+            file: blob,
+            type: document.getElementById("file_to_send").files[0].type
+        });
+    });
+}
+
+// Display ID
+peer.on('open', function(id) {
+  document.getElementById("my_id").textContent = peer.id;
+});
+
+// Constantly listen for incoming connections
+peer.on('connection', function(conn) {
+    conn.on('open', function() {
+        conn.on('data', function(data) {
+            var blob = new Blob([data.file], {type: data.type});
+            var url = window.URL.createObjectURL(blob);
+ 
+            var table = document.getElementById("file_table");
+
+            // Remove the "No Files" row
+            var elem = document.getElementById("empty_row");
+            if(elem) {
+                elem.parentNode.removeChild(elem);
+                //var row = table.insertRow(1);
+                //row.innerHTML = "File";
+                //var cell = row.insertCell(0);
+                //cell.innerHTML = "Time received";
+                //cell = row.insertCell(0);
+                //cell.innerHTML = "Source ID";
+                //cell = row.insertCell(0);
+                //cell.innerHTML = "Size";
+
+            }
+
+            var row = table.insertRow(1);
+            var a = document.createElement("a");
+            a.href = url;
+            a.innerHTML = data.name;
+            a.setAttribute("download", data.name);
+            var file_col = row.insertCell(0);
+            file_col.appendChild(a);
+            //int id_col = row.insertCell(0);
+            //var d = new Date();
+            //id_col.innerHTML = d.getHours+':'+d.getMinutes();
+        });
+    });
+});
+
+peer.on('error', function(err) {
+    if (err.type === 'peer-unavailable') {
+        alert("No user with the given ID is currently connected.");
+    }
+});

+ 0 - 2
routes/index.js

@@ -1,8 +1,6 @@
 var express = require('express');
 var router = express.Router();
 
-var FileSaver = require('file-saver');
-
 /* PeerServer */
 var app = express();
 var ExpressPeerServer = require('peer').ExpressPeerServer;

+ 30 - 8
views/index.ejs

@@ -3,17 +3,39 @@
   <head>
     <title>Sendd</title>
     <link rel='stylesheet' href='/stylesheets/style.css' />
+    <script src="http://cdn.peerjs.com/0.3/peer.js"></script>
+    <script type="text/javascript" src="/javascripts/index.js"></script>
   </head>
   <body>
-  <script>
-    // Check for the various File API support.
-    if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
-      alert('The File APIs are not fully supported by your browser. This webapp will not work.');
-    }
-  </script>
     <h1>Sendd</h1>
     <p>Welcome to Sendd, a simple, direct file sender.</p>
-    <a href="/send">Send a file</a>
-    <a href="/receive">Receive a file</a>
+    <h3>Receive a file</h3>
+
+    <table border="1" width="100%" id="file_table">
+    <thead>
+    <tr>
+    <td colspan="4">Files sent to you:</td>
+    </tr>
+    </thead>
+    <tfoot>
+    <tr>
+    <p></p>
+    <td colspan="4">My ID: <strong id="my_id"></strong></td>
+    </tr>
+    </tfoot>
+    <tbody>
+    <tr id="empty_row">
+        <td colspan=4><i>No Files...</i></td>
+    </tr>
+    <tr>
+    </tr>
+    </tbody>
+    </table>
+
+    <h3>Send a file</h3>
+    <input type="file" id="file_to_send"><br/><br/>
+    <label>Destination ID (ask your reciever to send it to you):</label><br/>
+    <input type="text" id="dest_id"></br></br>
+    <button type="button" onclick="send();">Send</button> 
   </body>
 </html>

+ 0 - 33
views/receive.ejs

@@ -1,33 +0,0 @@
-<!DOCTYPE html>
-<html>
-  <head>
-    <title>Sendd - Receive a file</title>
-    <link rel='stylesheet' href='/stylesheets/style.css' />
-    <script src="http://cdn.peerjs.com/0.3/peer.js"></script>
-  </head>
-  <body>
-    <h1>Receive a file</h1>
-    <p>Give the sender the following id:</p>
-    <script>
-        // No API key required when not using cloud server
-        var peer = new Peer({host: 'localhost', port: 9000, path: '/node_modules/peerjs'});
-        peer.on('open', function(id) {
-            document.body.appendChild(document.createTextNode(peer.id));
-        });
-        peer.on('connection', function(conn) {
-            conn.on('data', function(data) {
-                var dataBlob = new Blob([data.file], {type: data.type});
-                var url = window.URL.createObjectURL(dataBlob);
-                var a = document.createElement("a");
-                var linkText = document.createTextNode("Download");
-                a.appendChild(linkText);
-                a.href = url;
-                a.setAttribute("download", data.name);
-                document.body.appendChild(a);
-            });
-        });
-
-    </script>
-  </body>
-</html>
-

+ 0 - 46
views/send.ejs

@@ -1,46 +0,0 @@
-<!DOCTYPE html>
-<html>
-  <head>
-    <title>Sendd - Send a file</title>
-    <link rel='stylesheet' href='/stylesheets/style.css' />
-    <script src="http://cdn.peerjs.com/0.3/peer.js"></script>
-    <script>
-        // No API key required when not using cloud server
-        var peer = new Peer({host: 'localhost', port: 9000, path: '/node_modules/peerjs'});
-        peer.on('open', function(id) {
-            console.log('My peer ID is: ' + id);
-        });
-
-    function send() {
-        // TODO figure out why this isn't calling the bottom stuff
-        var valid = true;
-        if(!document.getElementById("file_to_send").files[0]) {
-            valid = false;
-            alert("Please select a file.");
-        }
-        if(!document.getElementById("dest_id").value) {
-            valid = false;
-            alert("Please provide a Destination ID.");
-        }
-        if(!valid){
-            return;
-        }
-
-        var blob = new Blob([document.getElementById("file_to_send").files[0]], {type: document.getElementById("file_to_send").files[0].type});
-        var con = peer.connect(document.getElementById("dest_id").value); 
-        con.send({
-            name: document.getElementById("file_to_send").files[0].name,
-            file: blob,
-            type: document.getElementById("file_to_send").files[0].type
-        });
-    }
-    </script>
-  </head>
-  <body>
-    <h1>Send a file</h1>
-    <input type="file" id="file_to_send"><br/><br/>
-    <label>Destination ID (ask your reciever to send it to you):</label><br/>
-    <input type="text" id="dest_id"></br></br>
-    <button type="button" onclick="send();">Send</button> 
-  </body>
-</html>