Browse Source

Add basic functionality

Still has a problem with connecting to the reciever with the Send
button.
Josh Bicking 10 năm trước cách đây
mục cha
commit
f76e4757b0
11 tập tin đã thay đổi với 349 bổ sung và 14 xóa
  1. 30 0
      .gitignore
  2. 60 0
      README.md
  3. 90 0
      bin/www
  4. 16 14
      package.json
  5. 8 0
      public/stylesheets/style.css
  6. 35 0
      routes/index.js
  7. 9 0
      routes/users.js
  8. 3 0
      views/error.ejs
  9. 19 0
      views/index.ejs
  10. 33 0
      views/receive.ejs
  11. 46 0
      views/send.ejs

+ 30 - 0
.gitignore

@@ -0,0 +1,30 @@
+# Logs
+logs
+*.log
+
+# Runtime data
+pids
+*.pid
+*.seed
+
+# Directory for instrumented libs generated by jscoverage/JSCover
+lib-cov
+
+# Coverage directory used by tools like istanbul
+coverage
+
+# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
+.grunt
+
+# node-waf configuration
+.lock-wscript
+
+# Compiled binary addons (http://nodejs.org/api/addons.html)
+build/Release
+
+# Dependency directory
+# https://docs.npmjs.com/cli/shrinkwrap#caveats
+node_modules
+
+# Debug log from npm
+npm-debug.log

+ 60 - 0
README.md

@@ -0,0 +1,60 @@
+var express = require('express');
+var path = require('path');
+var favicon = require('serve-favicon');
+var logger = require('morgan');
+var cookieParser = require('cookie-parser');
+var bodyParser = require('body-parser');
+
+var routes = require('./routes/index');
+var users = require('./routes/users');
+
+var app = express();
+
+// view engine setup
+app.set('views', path.join(__dirname, 'views'));
+app.set('view engine', 'ejs');
+
+// uncomment after placing your favicon in /public
+//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
+app.use(logger('dev'));
+app.use(bodyParser.json());
+app.use(bodyParser.urlencoded({ extended: false }));
+app.use(cookieParser());
+app.use(express.static(path.join(__dirname, 'public')));
+
+app.use('/', routes);
+app.use('/users', users);
+
+// catch 404 and forward to error handler
+app.use(function(req, res, next) {
+  var err = new Error('Not Found');
+  err.status = 404;
+  next(err);
+});
+
+// error handlers
+
+// development error handler
+// will print stacktrace
+if (app.get('env') === 'development') {
+  app.use(function(err, req, res, next) {
+    res.status(err.status || 500);
+    res.render('error', {
+      message: err.message,
+      error: err
+    });
+  });
+}
+
+// production error handler
+// no stacktraces leaked to user
+app.use(function(err, req, res, next) {
+  res.status(err.status || 500);
+  res.render('error', {
+    message: err.message,
+    error: {}
+  });
+});
+
+
+module.exports = app;

+ 90 - 0
bin/www

@@ -0,0 +1,90 @@
+#!/usr/bin/env node
+
+/**
+ * Module dependencies.
+ */
+
+var app = require('../app');
+var debug = require('debug')('sendd:server');
+var http = require('http');
+
+/**
+ * Get port from environment and store in Express.
+ */
+
+var port = normalizePort(process.env.PORT || '3000');
+app.set('port', port);
+
+/**
+ * Create HTTP server.
+ */
+
+var server = http.createServer(app);
+
+/**
+ * Listen on provided port, on all network interfaces.
+ */
+
+server.listen(port);
+server.on('error', onError);
+server.on('listening', onListening);
+
+/**
+ * Normalize a port into a number, string, or false.
+ */
+
+function normalizePort(val) {
+  var port = parseInt(val, 10);
+
+  if (isNaN(port)) {
+    // named pipe
+    return val;
+  }
+
+  if (port >= 0) {
+    // port number
+    return port;
+  }
+
+  return false;
+}
+
+/**
+ * Event listener for HTTP server "error" event.
+ */
+
+function onError(error) {
+  if (error.syscall !== 'listen') {
+    throw error;
+  }
+
+  var bind = typeof port === 'string'
+    ? 'Pipe ' + port
+    : 'Port ' + port;
+
+  // handle specific listen errors with friendly messages
+  switch (error.code) {
+    case 'EACCES':
+      console.error(bind + ' requires elevated privileges');
+      process.exit(1);
+      break;
+    case 'EADDRINUSE':
+      console.error(bind + ' is already in use');
+      process.exit(1);
+      break;
+    default:
+      throw error;
+  }
+}
+
+/**
+ * Event listener for HTTP server "listening" event.
+ */
+
+function onListening() {
+  var addr = server.address();
+  var bind = typeof addr === 'string'
+    ? 'pipe ' + addr
+    : 'port ' + addr.port;
+  debug('Listening on ' + bind);
+}

+ 16 - 14
package.json

@@ -1,19 +1,21 @@
 {
   "name": "sendd",
-  "version": "1.0.0",
-  "description": "direct file sender",
-  "main": "index.js",
+  "version": "0.0.0",
+  "private": true,
   "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1"
+    "start": "node ./bin/www"
   },
-  "repository": {
-    "type": "git",
-    "url": "git+https://github.com/Josh1147582/sendd.git"
-  },
-  "author": "Josh Bicking",
-  "license": "GPL-3.0",
-  "bugs": {
-    "url": "https://github.com/Josh1147582/sendd/issues"
-  },
-  "homepage": "https://github.com/Josh1147582/sendd#readme"
+  "dependencies": {
+    "body-parser": "~1.15.1",
+    "cookie-parser": "~1.4.3",
+    "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",
+    "serve-favicon": "~2.3.0"
+  }
 }

+ 8 - 0
public/stylesheets/style.css

@@ -0,0 +1,8 @@
+body {
+  padding: 50px;
+  font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+}
+
+a {
+  color: #00B7FF;
+}

+ 35 - 0
routes/index.js

@@ -0,0 +1,35 @@
+var express = require('express');
+var router = express.Router();
+
+var FileSaver = require('file-saver');
+
+/* PeerServer */
+var app = express();
+var ExpressPeerServer = require('peer').ExpressPeerServer;
+
+// app.get('/', function(req, res, next) { res.send('Hello world!'); });
+
+var server = require('http').createServer(app);
+
+var options = {
+    debug: true
+}
+
+app.use('/node_modules/peerjs', ExpressPeerServer(server, options));
+
+server.listen(9000);
+
+/* GET home page. */
+router.get('/', function(req, res, next) {
+  res.render('index');
+});
+
+router.get('/send', function(req, res, next) {
+  res.render('send');
+});
+
+router.get('/receive', function(req, res, next) {
+  res.render('receive');
+});
+
+module.exports = router;

+ 9 - 0
routes/users.js

@@ -0,0 +1,9 @@
+var express = require('express');
+var router = express.Router();
+
+/* GET users listing. */
+router.get('/', function(req, res, next) {
+  res.send('respond with a resource');
+});
+
+module.exports = router;

+ 3 - 0
views/error.ejs

@@ -0,0 +1,3 @@
+<h1><%= message %></h1>
+<h2><%= error.status %></h2>
+<pre><%= error.stack %></pre>

+ 19 - 0
views/index.ejs

@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <title>Sendd</title>
+    <link rel='stylesheet' href='/stylesheets/style.css' />
+  </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>
+  </body>
+</html>

+ 33 - 0
views/receive.ejs

@@ -0,0 +1,33 @@
+<!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>
+

+ 46 - 0
views/send.ejs

@@ -0,0 +1,46 @@
+<!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>