From 51910b5fc9a6d951f47577dc51cd152c96d47e32 Mon Sep 17 00:00:00 2001 From: Abhinav Adduri Date: Thu, 1 Jun 2017 15:10:00 -0700 Subject: [PATCH] added browserify, refactored code to separate UI and network logic --- app.js | 111 ------- frontend/src/download.js | 132 +++++++++ frontend/src/file.js | 27 ++ frontend/src/main.js | 3 + frontend/src/upload.js | 142 +++++++++ package.json | 6 +- public/bundle.js | 614 +++++++++++++++++++++++++++++++++++++++ public/download.html | 2 +- public/download.js | 135 --------- public/file.js | 10 - public/index.html | 6 +- public/upload.js | 164 ----------- server/portal_server.js | 31 +- 13 files changed, 942 insertions(+), 441 deletions(-) delete mode 100644 app.js create mode 100644 frontend/src/download.js create mode 100644 frontend/src/file.js create mode 100644 frontend/src/main.js create mode 100644 frontend/src/upload.js create mode 100644 public/bundle.js delete mode 100644 public/download.js delete mode 100644 public/file.js delete mode 100644 public/upload.js diff --git a/app.js b/app.js deleted file mode 100644 index dcd8a9df..00000000 --- a/app.js +++ /dev/null @@ -1,111 +0,0 @@ -const express = require('express'); -const busboy = require('connect-busboy'); -const path = require('path'); -const fs = require('fs-extra'); -const bodyParser = require('body-parser'); -const crypto = require('crypto'); - -const app = express(); -const redis = require('redis'), client = redis.createClient(); - -client.on('error', function(err) { - console.log(err); -}); - -app.use(busboy()); -app.use(bodyParser.json()); -app.use(express.static(path.join(__dirname, 'public'))); - -app.get('/download/:id', function(req, res) { - res.sendFile(path.join(__dirname + '/public/download.html')); -}); - -app.get('/assets/download/:id', function(req, res) { - let id = req.params.id; - if (!validateID(id)) { - res.send(404); - return; - } - - client.hget(id, 'filename', function(err, reply) { - // maybe some expiration logic too - if (!reply) { - res.sendStatus(404); - } else { - res.setHeader('Content-Disposition', 'attachment; filename=' + reply); - res.setHeader('Content-Type', 'application/octet-stream'); - - res.download(__dirname + '/static/' + id, reply, function(err) { - if (!err) { - client.del(id); - fs.unlinkSync(__dirname + '/static/' + id); - } - }); - } - }); -}); - -app.post('/delete/:id', function(req, res) { - let id = req.params.id; - - if (!validateID(id)) { - res.send(404); - return; - } - - let delete_token = req.body.delete_token; - - if (!delete_token) { - res.sendStatus(404); - } - - client.hget(id, 'delete', function(err, reply) { - if (!reply) { - res.sendStatus(404); - } else { - client.del(id); - fs.unlinkSync(__dirname + '/static/' + id); - res.sendStatus(200); - } - }); -}); - -app.post('/upload/:id', function(req, res, next) { - if (!validateID(req.params.id)) { - res.send(404); - return; - } - - var fstream; - req.pipe(req.busboy); - req.busboy.on('file', function(fieldname, file, filename) { - console.log('Uploading: ' + filename); - - //Path where image will be uploaded - fstream = fs.createWriteStream(__dirname + '/static/' + req.params.id); - file.pipe(fstream); - fstream.on('close', function() { - let id = req.params.id; - let uuid = crypto.randomBytes(10).toString('hex'); - - client.hmset([id, 'filename', filename, 'delete', uuid]); - - // delete the file off the server in 24 hours - // setTimeout(function() { - // fs.unlinkSync(__dirname + "/static/" + id); - // }, 86400000); - - client.expire(id, 86400000); - console.log('Upload Finished of ' + filename); - res.send(uuid); - }); - }); -}); - -app.listen(3000, function() { - console.log('Portal app listening on port 3000!'); -}); - -function validateID(route_id) { - return route_id.match(/^[0-9a-fA-F]{32}$/) !== null; -} diff --git a/frontend/src/download.js b/frontend/src/download.js new file mode 100644 index 00000000..43852d75 --- /dev/null +++ b/frontend/src/download.js @@ -0,0 +1,132 @@ +const UIWrapper = require('./file').UIWrapper; + +let download = () => { + + let xhr = new XMLHttpRequest(); + xhr.open("get", "/assets" + location.pathname.slice(0, -1), true); + xhr.responseType = "blob"; + + let listelem = setupUI(); + xhr.addEventListener("progress", updateProgress.bind(null, listelem)); + + xhr.onload = function(e) { + + // maybe send a separate request before this one to get the filename? + + // maybe render the html itself with the filename, since it's generated server side + // after a get request with the unique id + listelem.emit('name', xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1]); + + if (this.status == 200) { + let self = this; + let blob = new Blob([this.response]); + let arrayBuffer; + let fileReader = new FileReader(); + fileReader.onload = function() { + arrayBuffer = this.result; + let array = new Uint8Array(arrayBuffer); + salt = strToIv(location.pathname.slice(10, -1)); + + window.crypto.subtle.importKey( + "jwk", + { + kty: "oct", + k: location.hash.slice(1), + alg: "A128CBC", + ext: true, + }, + { + name: "AES-CBC", + }, + true, + ["encrypt", "decrypt"]) + .then((key) => { + return window.crypto.subtle.decrypt( + { + name: "AES-CBC", + iv: salt, + }, + key, + array) + }) + .then((decrypted) => { + let dataView = new DataView(decrypted); + let blob = new Blob([dataView]); + let downloadUrl = URL.createObjectURL(blob); + let a = document.createElement("a"); + a.href = downloadUrl; + a.download = xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1]; + document.body.appendChild(a); + a.click(); + }) + .catch((err) => { + alert("This link is either invalid or has expired, or the uploader has deleted the file."); + console.error(err); + }); + }; + + fileReader.readAsArrayBuffer(blob); + } else { + alert("This link is either invalid or has expired, or the uploader has deleted the file.") + } + }; + xhr.send(); +} + +window.download = download; + +let setupUI = () => { + let li = document.createElement("li"); + let name = document.createElement("p"); + li.appendChild(name); + + let progress = document.createElement("p"); + li.appendChild(progress); + + document.getElementById("downloaded_files").appendChild(li); + + return new UIWrapper(li, name, null, progress); +} + +let ivToStr = (iv) => { + let hexStr = ""; + for (let i in iv) { + if (iv[i] < 16) { + hexStr += "0" + iv[i].toString(16); + } else { + hexStr += iv[i].toString(16); + } + } + window.hexStr = hexStr; + return hexStr; +} + +let strToIv = (str) => { + let iv = new Uint8Array(16); + for (let i = 0; i < str.length; i += 2) { + iv[i/2] = parseInt((str.charAt(i) + str.charAt(i + 1)), 16); + } + + return iv; +} + +let updateProgress = (UIelem, e) => { + if (e.lengthComputable) { + let percentComplete = Math.floor((e.loaded / e.total) * 100); + UIelem.emit('progress', "Progress: " + percentComplete + "%"); + + if (percentComplete === 100) { + let finished = document.createElement("p"); + finished.innerText = "Your download has finished."; + UIelem.li.appendChild(finished); + + let close = document.createElement("button"); + close.innerText = "Ok"; + close.addEventListener("click", () => { + document.getElementById("downloaded_files").removeChild(UIelem.li); + }); + + UIelem.li.appendChild(close); + } + } +} \ No newline at end of file diff --git a/frontend/src/file.js b/frontend/src/file.js new file mode 100644 index 00000000..c581b1ca --- /dev/null +++ b/frontend/src/file.js @@ -0,0 +1,27 @@ +const EventEmitter = require('events'); + +class UIWrapper extends EventEmitter { + constructor(li, name, link, progress) { + super(); + this.li = li; + this.name = name; + this.link = link; + this.progress = progress; + + this.on("name", (filename) => { + this.name.innerText = filename; + }); + + this.on("link", (link) => { + this.link.innerText = link; + this.link.setAttribute('href', link); + }); + + this.on("progress", (progress) => { + this.progress.innerText = progress; + + }); + } +} + +exports.UIWrapper = UIWrapper; \ No newline at end of file diff --git a/frontend/src/main.js b/frontend/src/main.js new file mode 100644 index 00000000..23b00a19 --- /dev/null +++ b/frontend/src/main.js @@ -0,0 +1,3 @@ +require('./upload'); +require('./download'); +require('./file'); \ No newline at end of file diff --git a/frontend/src/upload.js b/frontend/src/upload.js new file mode 100644 index 00000000..df4d2eed --- /dev/null +++ b/frontend/src/upload.js @@ -0,0 +1,142 @@ +const EventEmitter = require('events'); +const UIWrapper = require('./file').UIWrapper; + +let onChange = (event) => { + let file = event.target.files[0]; + let reader = new FileReader(); + reader.readAsArrayBuffer(file); + + let random_iv = window.crypto.getRandomValues(new Uint8Array(16)); + let hex = ivToStr(random_iv); + + reader.onload = function(event) { + let self = this; + window.crypto.subtle.generateKey({ + name: "AES-CBC", + length: 128 + }, + true, + ["encrypt", "decrypt"]) + .then((key) => { + let arrayBuffer = self.result; + let array = new Uint8Array(arrayBuffer); + + window.crypto.subtle.encrypt({ + name: "AES-CBC", + iv: random_iv }, + key, + array) + .then(uploadFile.bind(null, file, hex, key)) + .catch((err) => console.error(err)); + + }).catch((err) => console.error(err)); + }; +} + +window.onChange = onChange; + +let uploadFile = (file, hex, key, encrypted) => { + let dataView = new DataView(encrypted); + let blob = new Blob([dataView], { type: file.type }); + + let fd = new FormData(); + fd.append("fname", file.name); + fd.append("data", blob, file.name); + + let xhr = new XMLHttpRequest(); + xhr.open("post", "/upload/" + hex, true); + + let listelem = setupUI(); + listelem.emit('name', file.name); + xhr.upload.addEventListener("progress", updateProgress.bind(null, listelem)); + + xhr.onreadystatechange = () => { + if (xhr.readyState == XMLHttpRequest.DONE) { + window.crypto.subtle.exportKey("jwk", key).then((keydata) => { + localStorage.setItem(hex, xhr.responseText); + + listelem.emit('link', "http://localhost:3000/download/" + hex + "/#" + keydata.k) + + console.log("Share this link with a friend: http://localhost:3000/download/" + hex + "/#" + keydata.k); + }) + } + }; + + xhr.send(fd); +} + +let updateProgress = (UIelem, e) => { + if (e.lengthComputable) { + let percentComplete = Math.floor((e.loaded / e.total) * 100); + UIelem.emit('progress', "Progress: " + percentComplete + "%") + + if (percentComplete === 100) { + let btn = document.createElement("button"); + btn.innerText = "Delete from server"; + btn.addEventListener("click", () => { + let segments = UIelem.link.innerText.split("/"); + let key = segments[segments.length - 2]; + + let xhr = new XMLHttpRequest(); + xhr.open("post", "/delete/" + key, true); + xhr.setRequestHeader("Content-Type", "application/json"); + if (!localStorage.getItem(key)) return; + + xhr.send(JSON.stringify({delete_token: localStorage.getItem(key)})); + + xhr.onreadystatechange = () => { + if (xhr.readyState === XMLHttpRequest.DONE) { + document.getElementById("uploaded_files").removeChild(UIelem.li); + localStorage.removeItem(key); + } + + if (xhr.status === 200) { + console.log("The file was successfully deleted."); + } else { + console.log("The file has expired, or has already been deleted."); + } + } + + }); + UIelem.li.appendChild(btn); + } + } +} + +let setupUI = () => { + let li = document.createElement("li"); + let name = document.createElement("p"); + li.appendChild(name); + + let link = document.createElement("a"); + li.appendChild(link); + + let progress = document.createElement("p"); + li.appendChild(progress); + + document.getElementById("uploaded_files").appendChild(li); + + return new UIWrapper(li, name, link, progress); +} + +let ivToStr = (iv) => { + let hexStr = ""; + for (let i in iv) { + if (iv[i] < 16) { + hexStr += "0" + iv[i].toString(16); + } else { + hexStr += iv[i].toString(16); + } + } + window.hexStr = hexStr; + return hexStr; +} + +let strToIv = (str) => { + let iv = new Uint8Array(16); + for (let i = 0; i < str.length; i += 2) { + iv[i/2] = parseInt((str.charAt(i) + str.charAt(i + 1)), 16); + } + + return iv; +} \ No newline at end of file diff --git a/package.json b/package.json index 1a2c0418..0f96ffd1 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "scripts": { "format": "prettier --single-quote --write 'public/*.js' 'app.js'", "test": "echo \"Error: no test specified\" && exit 1", - "start": "node server/portal_server.js" + "start": "watchify frontend/src/main.js -o public/bundle.js -d | node server/portal_server.js" }, "author": "", "license": "ISC", @@ -17,5 +17,9 @@ "path": "^0.12.7", "prettier": "^1.3.1", "redis": "^2.7.1" + }, + "devDependencies": { + "browserify": "^14.4.0", + "watchify": "^3.9.0" } } diff --git a/public/bundle.js b/public/bundle.js new file mode 100644 index 00000000..9a01e7fd --- /dev/null +++ b/public/bundle.js @@ -0,0 +1,614 @@ +(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o { + + let xhr = new XMLHttpRequest(); + xhr.open("get", "/assets" + location.pathname.slice(0, -1), true); + xhr.responseType = "blob"; + + let listelem = setupUI(); + xhr.addEventListener("progress", updateProgress.bind(null, listelem)); + + xhr.onload = function(e) { + + // maybe send a separate request before this one to get the filename? + + // maybe render the html itself with the filename, since it's generated server side + // after a get request with the unique id + listelem.emit('name', xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1]); + + if (this.status == 200) { + let self = this; + let blob = new Blob([this.response]); + let arrayBuffer; + let fileReader = new FileReader(); + fileReader.onload = function() { + arrayBuffer = this.result; + let array = new Uint8Array(arrayBuffer); + salt = strToIv(location.pathname.slice(10, -1)); + + window.crypto.subtle.importKey( + "jwk", + { + kty: "oct", + k: location.hash.slice(1), + alg: "A128CBC", + ext: true, + }, + { + name: "AES-CBC", + }, + true, + ["encrypt", "decrypt"]) + .then((key) => { + return window.crypto.subtle.decrypt( + { + name: "AES-CBC", + iv: salt, + }, + key, + array) + }) + .then((decrypted) => { + let dataView = new DataView(decrypted); + let blob = new Blob([dataView]); + let downloadUrl = URL.createObjectURL(blob); + let a = document.createElement("a"); + a.href = downloadUrl; + a.download = xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1]; + document.body.appendChild(a); + a.click(); + }) + .catch((err) => { + alert("This link is either invalid or has expired, or the uploader has deleted the file."); + console.error(err); + }); + }; + + fileReader.readAsArrayBuffer(blob); + } else { + alert("This link is either invalid or has expired, or the uploader has deleted the file.") + } + }; + xhr.send(); +} + +window.download = download; + +let setupUI = () => { + let li = document.createElement("li"); + let name = document.createElement("p"); + li.appendChild(name); + + let progress = document.createElement("p"); + li.appendChild(progress); + + document.getElementById("downloaded_files").appendChild(li); + + return new UIWrapper(li, name, null, progress); +} + +let ivToStr = (iv) => { + let hexStr = ""; + for (let i in iv) { + if (iv[i] < 16) { + hexStr += "0" + iv[i].toString(16); + } else { + hexStr += iv[i].toString(16); + } + } + window.hexStr = hexStr; + return hexStr; +} + +let strToIv = (str) => { + let iv = new Uint8Array(16); + for (let i = 0; i < str.length; i += 2) { + iv[i/2] = parseInt((str.charAt(i) + str.charAt(i + 1)), 16); + } + + return iv; +} + +let updateProgress = (UIelem, e) => { + if (e.lengthComputable) { + let percentComplete = Math.floor((e.loaded / e.total) * 100); + UIelem.emit('progress', "Progress: " + percentComplete + "%"); + + if (percentComplete === 100) { + let finished = document.createElement("p"); + finished.innerText = "Your download has finished."; + UIelem.li.appendChild(finished); + + let close = document.createElement("button"); + close.innerText = "Ok"; + close.addEventListener("click", () => { + document.getElementById("downloaded_files").removeChild(UIelem.li); + }); + + UIelem.li.appendChild(close); + } + } +} +},{"./file":2}],2:[function(require,module,exports){ +const EventEmitter = require('events'); + +class UIWrapper extends EventEmitter { + constructor(li, name, link, progress) { + super(); + this.li = li; + this.name = name; + this.link = link; + this.progress = progress; + + this.on("name", (filename) => { + this.name.innerText = filename; + }); + + this.on("link", (link) => { + this.link.innerText = link; + this.link.setAttribute('href', link); + }); + + this.on("progress", (progress) => { + this.progress.innerText = progress; + + }); + } +} + +exports.UIWrapper = UIWrapper; +},{"events":5}],3:[function(require,module,exports){ +require('./upload'); +require('./download'); +require('./file'); +},{"./download":1,"./file":2,"./upload":4}],4:[function(require,module,exports){ +const EventEmitter = require('events'); +const UIWrapper = require('./file').UIWrapper; + +let onChange = (event) => { + let file = event.target.files[0]; + let reader = new FileReader(); + reader.readAsArrayBuffer(file); + + let random_iv = window.crypto.getRandomValues(new Uint8Array(16)); + let hex = ivToStr(random_iv); + + reader.onload = function(event) { + let self = this; + window.crypto.subtle.generateKey({ + name: "AES-CBC", + length: 128 + }, + true, + ["encrypt", "decrypt"]) + .then((key) => { + let arrayBuffer = self.result; + let array = new Uint8Array(arrayBuffer); + + window.crypto.subtle.encrypt({ + name: "AES-CBC", + iv: random_iv }, + key, + array) + .then(uploadFile.bind(null, file, hex, key)) + .catch((err) => console.error(err)); + + }).catch((err) => console.error(err)); + }; +} + +window.onChange = onChange; + +let uploadFile = (file, hex, key, encrypted) => { + let dataView = new DataView(encrypted); + let blob = new Blob([dataView], { type: file.type }); + + let fd = new FormData(); + fd.append("fname", file.name); + fd.append("data", blob, file.name); + + let xhr = new XMLHttpRequest(); + xhr.open("post", "/upload/" + hex, true); + + let listelem = setupUI(); + listelem.emit('name', file.name); + xhr.upload.addEventListener("progress", updateProgress.bind(null, listelem)); + + xhr.onreadystatechange = () => { + if (xhr.readyState == XMLHttpRequest.DONE) { + window.crypto.subtle.exportKey("jwk", key).then((keydata) => { + localStorage.setItem(hex, xhr.responseText); + + listelem.emit('link', "http://localhost:3000/download/" + hex + "/#" + keydata.k) + + console.log("Share this link with a friend: http://localhost:3000/download/" + hex + "/#" + keydata.k); + }) + } + }; + + xhr.send(fd); +} + +let updateProgress = (UIelem, e) => { + if (e.lengthComputable) { + let percentComplete = Math.floor((e.loaded / e.total) * 100); + UIelem.emit('progress', "Progress: " + percentComplete + "%") + + if (percentComplete === 100) { + let btn = document.createElement("button"); + btn.innerText = "Delete from server"; + btn.addEventListener("click", () => { + let segments = UIelem.link.innerText.split("/"); + let key = segments[segments.length - 2]; + + let xhr = new XMLHttpRequest(); + xhr.open("post", "/delete/" + key, true); + xhr.setRequestHeader("Content-Type", "application/json"); + if (!localStorage.getItem(key)) return; + + xhr.send(JSON.stringify({delete_token: localStorage.getItem(key)})); + + xhr.onreadystatechange = () => { + if (xhr.readyState === XMLHttpRequest.DONE) { + document.getElementById("uploaded_files").removeChild(UIelem.li); + localStorage.removeItem(key); + } + + if (xhr.status === 200) { + console.log("The file was successfully deleted."); + } else { + console.log("The file has expired, or has already been deleted."); + } + } + + }); + UIelem.li.appendChild(btn); + } + } +} + +let setupUI = () => { + let li = document.createElement("li"); + let name = document.createElement("p"); + li.appendChild(name); + + let link = document.createElement("a"); + li.appendChild(link); + + let progress = document.createElement("p"); + li.appendChild(progress); + + document.getElementById("uploaded_files").appendChild(li); + + return new UIWrapper(li, name, link, progress); +} + +let ivToStr = (iv) => { + let hexStr = ""; + for (let i in iv) { + if (iv[i] < 16) { + hexStr += "0" + iv[i].toString(16); + } else { + hexStr += iv[i].toString(16); + } + } + window.hexStr = hexStr; + return hexStr; +} + +let strToIv = (str) => { + let iv = new Uint8Array(16); + for (let i = 0; i < str.length; i += 2) { + iv[i/2] = parseInt((str.charAt(i) + str.charAt(i + 1)), 16); + } + + return iv; +} +},{"./file":2,"events":5}],5:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +function EventEmitter() { + this._events = this._events || {}; + this._maxListeners = this._maxListeners || undefined; +} +module.exports = EventEmitter; + +// Backwards-compat with node 0.10.x +EventEmitter.EventEmitter = EventEmitter; + +EventEmitter.prototype._events = undefined; +EventEmitter.prototype._maxListeners = undefined; + +// By default EventEmitters will print a warning if more than 10 listeners are +// added to it. This is a useful default which helps finding memory leaks. +EventEmitter.defaultMaxListeners = 10; + +// Obviously not all Emitters should be limited to 10. This function allows +// that to be increased. Set to zero for unlimited. +EventEmitter.prototype.setMaxListeners = function(n) { + if (!isNumber(n) || n < 0 || isNaN(n)) + throw TypeError('n must be a positive number'); + this._maxListeners = n; + return this; +}; + +EventEmitter.prototype.emit = function(type) { + var er, handler, len, args, i, listeners; + + if (!this._events) + this._events = {}; + + // If there is no 'error' event listener then throw. + if (type === 'error') { + if (!this._events.error || + (isObject(this._events.error) && !this._events.error.length)) { + er = arguments[1]; + if (er instanceof Error) { + throw er; // Unhandled 'error' event + } else { + // At least give some kind of context to the user + var err = new Error('Uncaught, unspecified "error" event. (' + er + ')'); + err.context = er; + throw err; + } + } + } + + handler = this._events[type]; + + if (isUndefined(handler)) + return false; + + if (isFunction(handler)) { + switch (arguments.length) { + // fast cases + case 1: + handler.call(this); + break; + case 2: + handler.call(this, arguments[1]); + break; + case 3: + handler.call(this, arguments[1], arguments[2]); + break; + // slower + default: + args = Array.prototype.slice.call(arguments, 1); + handler.apply(this, args); + } + } else if (isObject(handler)) { + args = Array.prototype.slice.call(arguments, 1); + listeners = handler.slice(); + len = listeners.length; + for (i = 0; i < len; i++) + listeners[i].apply(this, args); + } + + return true; +}; + +EventEmitter.prototype.addListener = function(type, listener) { + var m; + + if (!isFunction(listener)) + throw TypeError('listener must be a function'); + + if (!this._events) + this._events = {}; + + // To avoid recursion in the case that type === "newListener"! Before + // adding it to the listeners, first emit "newListener". + if (this._events.newListener) + this.emit('newListener', type, + isFunction(listener.listener) ? + listener.listener : listener); + + if (!this._events[type]) + // Optimize the case of one listener. Don't need the extra array object. + this._events[type] = listener; + else if (isObject(this._events[type])) + // If we've already got an array, just append. + this._events[type].push(listener); + else + // Adding the second element, need to change to array. + this._events[type] = [this._events[type], listener]; + + // Check for listener leak + if (isObject(this._events[type]) && !this._events[type].warned) { + if (!isUndefined(this._maxListeners)) { + m = this._maxListeners; + } else { + m = EventEmitter.defaultMaxListeners; + } + + if (m && m > 0 && this._events[type].length > m) { + this._events[type].warned = true; + console.error('(node) warning: possible EventEmitter memory ' + + 'leak detected. %d listeners added. ' + + 'Use emitter.setMaxListeners() to increase limit.', + this._events[type].length); + if (typeof console.trace === 'function') { + // not supported in IE 10 + console.trace(); + } + } + } + + return this; +}; + +EventEmitter.prototype.on = EventEmitter.prototype.addListener; + +EventEmitter.prototype.once = function(type, listener) { + if (!isFunction(listener)) + throw TypeError('listener must be a function'); + + var fired = false; + + function g() { + this.removeListener(type, g); + + if (!fired) { + fired = true; + listener.apply(this, arguments); + } + } + + g.listener = listener; + this.on(type, g); + + return this; +}; + +// emits a 'removeListener' event iff the listener was removed +EventEmitter.prototype.removeListener = function(type, listener) { + var list, position, length, i; + + if (!isFunction(listener)) + throw TypeError('listener must be a function'); + + if (!this._events || !this._events[type]) + return this; + + list = this._events[type]; + length = list.length; + position = -1; + + if (list === listener || + (isFunction(list.listener) && list.listener === listener)) { + delete this._events[type]; + if (this._events.removeListener) + this.emit('removeListener', type, listener); + + } else if (isObject(list)) { + for (i = length; i-- > 0;) { + if (list[i] === listener || + (list[i].listener && list[i].listener === listener)) { + position = i; + break; + } + } + + if (position < 0) + return this; + + if (list.length === 1) { + list.length = 0; + delete this._events[type]; + } else { + list.splice(position, 1); + } + + if (this._events.removeListener) + this.emit('removeListener', type, listener); + } + + return this; +}; + +EventEmitter.prototype.removeAllListeners = function(type) { + var key, listeners; + + if (!this._events) + return this; + + // not listening for removeListener, no need to emit + if (!this._events.removeListener) { + if (arguments.length === 0) + this._events = {}; + else if (this._events[type]) + delete this._events[type]; + return this; + } + + // emit removeListener for all listeners on all events + if (arguments.length === 0) { + for (key in this._events) { + if (key === 'removeListener') continue; + this.removeAllListeners(key); + } + this.removeAllListeners('removeListener'); + this._events = {}; + return this; + } + + listeners = this._events[type]; + + if (isFunction(listeners)) { + this.removeListener(type, listeners); + } else if (listeners) { + // LIFO order + while (listeners.length) + this.removeListener(type, listeners[listeners.length - 1]); + } + delete this._events[type]; + + return this; +}; + +EventEmitter.prototype.listeners = function(type) { + var ret; + if (!this._events || !this._events[type]) + ret = []; + else if (isFunction(this._events[type])) + ret = [this._events[type]]; + else + ret = this._events[type].slice(); + return ret; +}; + +EventEmitter.prototype.listenerCount = function(type) { + if (this._events) { + var evlistener = this._events[type]; + + if (isFunction(evlistener)) + return 1; + else if (evlistener) + return evlistener.length; + } + return 0; +}; + +EventEmitter.listenerCount = function(emitter, type) { + return emitter.listenerCount(type); +}; + +function isFunction(arg) { + return typeof arg === 'function'; +} + +function isNumber(arg) { + return typeof arg === 'number'; +} + +function isObject(arg) { + return typeof arg === 'object' && arg !== null; +} + +function isUndefined(arg) { + return arg === void 0; +} + +},{}]},{},[3]) +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlc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JheUJ1ZmZlcjtcbiAgICAgIGxldCBmaWxlUmVhZGVyID0gbmV3IEZpbGVSZWFkZXIoKTtcbiAgICAgIGZpbGVSZWFkZXIub25sb2FkID0gZnVuY3Rpb24oKSB7XG4gICAgICAgIGFycmF5QnVmZmVyID0gdGhpcy5yZXN1bHQ7XG4gICAgICAgIGxldCBhcnJheSA9IG5ldyBVaW50OEFycmF5KGFycmF5QnVmZmVyKTtcbiAgICAgICAgc2FsdCA9IHN0clRvSXYobG9jYXRpb24ucGF0aG5hbWUuc2xpY2UoMTAsIC0xKSk7XG5cbiAgICAgICAgd2luZG93LmNyeXB0by5zdWJ0bGUuaW1wb3J0S2V5KFxuICAgICAgICAgIFwiandrXCIsXG4gICAgICAgICAge1xuICAgICAgICAgICAgICBrdHk6IFwib2N0XCIsXG4gICAgICAgICAgICAgIGs6IGxvY2F0aW9uLmhhc2guc2xpY2UoMSksXG4gICAgICAgICAgICAgIGFsZzogXCJBMTI4Q0JDXCIsXG4gICAgICAgICAgICAgIGV4dDogdHJ1ZSxcbiAgICAgICAgICB9LFxuICAgICAgICAgIHtcbiAgICAgICAgICAgICAgbmFtZTogXCJBRVMtQ0JDXCIsXG4gICAgICAgICAgfSxcbiAgICAgICAgICB0cnVlLFxuICAgICAgICAgIFtcImVuY3J5cHRcIiwgXCJkZWNyeXB0XCJdKVxuICAgICAgICAudGhlbigoa2V5KSA9PiB7ICBcbiAgICAgICAgICByZXR1cm4gd2luZG93LmNyeXB0by5zdWJ0bGUuZGVjcnlwdChcbiAgICAgICAgICAgICAge1xuICAgICAgICAgICAgICAgIG5hbWU6IFwiQUVTLUNCQ1wiLFxuICAgICAgICAgICAgICAgIGl2OiBzYWx0LFxuICAgICAgICAgICAgICB9LFxuICAgICAgICAgICAgICBrZXksXG4gICAgICAgICAgICAgIGFycmF5KVxuICAgICAgICB9KVxuICAgICAgICAudGhlbigoZGVjcnlwdGVkKSA9PiB7XG4gICAgICAgICAgbGV0IGRhdGFWaWV3ID0gbmV3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Y3Rpb24nO1xufVxuXG5mdW5jdGlvbiBpc051bWJlcihhcmcpIHtcbiAgcmV0dXJuIHR5cGVvZiBhcmcgPT09ICdudW1iZXInO1xufVxuXG5mdW5jdGlvbiBpc09iamVjdChhcmcpIHtcbiAgcmV0dXJuIHR5cGVvZiBhcmcgPT09ICdvYmplY3QnICYmIGFyZyAhPT0gbnVsbDtcbn1cblxuZnVuY3Rpb24gaXNVbmRlZmluZWQoYXJnKSB7XG4gIHJldHVybiBhcmcgPT09IHZvaWQgMDtcbn1cbiJdfQ== diff --git a/public/download.html b/public/download.html index bf5686b0..3a0e6ac5 100644 --- a/public/download.html +++ b/public/download.html @@ -2,7 +2,7 @@ Download your file - + diff --git a/public/download.js b/public/download.js deleted file mode 100644 index 7ecc9a49..00000000 --- a/public/download.js +++ /dev/null @@ -1,135 +0,0 @@ -function download() { - var xhr = new XMLHttpRequest(); - xhr.open('get', '/assets' + location.pathname.slice(0, -1), true); - xhr.responseType = 'blob'; - - var li = document.createElement('li'); - var progress = document.createElement('p'); - li.appendChild(progress); - document.getElementById('downloaded_files').appendChild(li); - - xhr.addEventListener('progress', returnBindedLI(li, progress)); - - xhr.onload = function(e) { - // maybe send a separate request before this one to get the filename? - - // maybe render the html itself with the filename, since it's generated server side - // after a get request with the unique id - var name = document.createElement('p'); - name.innerHTML = xhr - .getResponseHeader('Content-Disposition') - .match(/filename="(.+)"/)[1]; - li.insertBefore(name, li.firstChild); - - if (this.status == 200) { - let self = this; - var blob = new Blob([this.response]); - var arrayBuffer; - var fileReader = new FileReader(); - fileReader.onload = function() { - arrayBuffer = this.result; - var array = new Uint8Array(arrayBuffer); - salt = strToIv(location.pathname.slice(10, -1)); - - window.crypto.subtle - .importKey( - 'jwk', - { - kty: 'oct', - k: location.hash.slice(1), - alg: 'A128CBC', - ext: true - }, - { - name: 'AES-CBC' - }, - true, - ['encrypt', 'decrypt'] - ) - .then(function(key) { - window.crypto.subtle - .decrypt( - { - name: 'AES-CBC', - iv: salt - }, - key, - array - ) - .then(function(decrypted) { - var dataView = new DataView(decrypted); - var blob = new Blob([dataView]); - var downloadUrl = URL.createObjectURL(blob); - var a = document.createElement('a'); - a.href = downloadUrl; - a.download = xhr - .getResponseHeader('Content-Disposition') - .match(/filename="(.+)"/)[1]; - document.body.appendChild(a); - a.click(); - }) - .catch(function(err) { - alert( - 'This link is either invalid or has expired, or the uploader has deleted the file.' - ); - console.error(err); - }); - }) - .catch(function(err) { - console.error(err); - }); - }; - fileReader.readAsArrayBuffer(blob); - } else { - alert( - 'This link is either invalid or has expired, or the uploader has deleted the file.' - ); - } - }; - xhr.send(); -} - -function ivToStr(iv) { - let hexStr = ''; - for (var i in iv) { - if (iv[i] < 16) { - hexStr += '0' + iv[i].toString(16); - } else { - hexStr += iv[i].toString(16); - } - } - window.hexStr = hexStr; - return hexStr; -} - -function strToIv(str) { - var iv = new Uint8Array(16); - for (var i = 0; i < str.length; i += 2) { - iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16); - } - - return iv; -} - -function returnBindedLI(li, progress) { - return function updateProgress(e) { - if (e.lengthComputable) { - var percentComplete = Math.floor(e.loaded / e.total * 100); - progress.innerHTML = 'Progress: ' + percentComplete + '%'; - } - - if (percentComplete === 100) { - var finished = document.createElement('p'); - finished.innerHTML = 'Your download has finished.'; - li.appendChild(finished); - - var close = document.createElement('button'); - close.innerHTML = 'Ok'; - close.addEventListener('click', function() { - document.getElementById('downloaded_files').removeChild(li); - }); - - li.appendChild(close); - } - }; -} diff --git a/public/file.js b/public/file.js deleted file mode 100644 index ce680be4..00000000 --- a/public/file.js +++ /dev/null @@ -1,10 +0,0 @@ -function ProgressEmitter(name, uuid, type) { - this.name = name; - this.uuid = uuid; - this.type = type; - this.link = null; - - this.emit = () => { - - }; -} \ No newline at end of file diff --git a/public/index.html b/public/index.html index aa77f317..e258c9f1 100644 --- a/public/index.html +++ b/public/index.html @@ -2,8 +2,8 @@ Firefox Fileshare - - + + @@ -13,6 +13,6 @@ - + diff --git a/public/upload.js b/public/upload.js deleted file mode 100644 index f213490f..00000000 --- a/public/upload.js +++ /dev/null @@ -1,164 +0,0 @@ -function onChange(event) { - - var file = event.target.files[0]; - var reader = new FileReader(); - reader.readAsArrayBuffer(file); - - let random_iv = window.crypto.getRandomValues(new Uint8Array(16)); - var hex = ivToStr(random_iv); - - reader.onload = function(event) { - let self = this; - window.crypto.subtle.generateKey({ - name: "AES-CBC", - length: 128 - }, - true, - ["encrypt", "decrypt"]) - .then((key) => { - let arrayBuffer = self.result; - let array = new Uint8Array(arrayBuffer); - - window.crypto.subtle.encrypt({ - name: "AES-CBC", - iv: random_iv }, - key, - array) - .then(uploadFile.bind(null, file, hex, key)) - .catch((err) => console.error(err)); - - }).catch((err) => console.error(err)); - }; -} - -let uploadFile = (file, hex, key, encrypted) => { - var dataView = new DataView(encrypted); - var blob = new Blob([dataView], { type: file.type }); - - var fd = new FormData(); - fd.append("fname", file.name); - fd.append("data", blob, file.name); - - var xhr = new XMLHttpRequest(); - - xhr.open("post", "/upload/" + hex, true); - - let prog = new window.ProgressEmitter(file.name, hex, file.type); - - var li = document.createElement("li"); - var name = document.createElement("p"); - name.innerHTML = file.name; - li.appendChild(name); - - var link = document.createElement("a"); - li.appendChild(link); - - var progress = document.createElement("p"); - li.appendChild(progress); - - - document.getElementById("uploaded_files").appendChild(li); - - - xhr.upload.addEventListener("progress", returnBindedLI(progress, name, link, li)); - - xhr.onreadystatechange = function() { - if (xhr.readyState == XMLHttpRequest.DONE) { - window.crypto.subtle.exportKey("jwk", key).then(function(keydata) { - var curr_name = localStorage.getItem(file.name); - - localStorage.setItem(hex, xhr.responseText); - - prog.link = "http://localhost:3000/download/" + hex + "/#" + keydata.k; - prog.emit(); - - link.innerHTML = "http://localhost:3000/download/" + hex + "/#" + keydata.k; - link.setAttribute("href", "http://localhost:3000/download/" + hex + "/#" + keydata.k); - - console.log("Share this link with a friend: http://localhost:3000/download/" + hex + "/#" + keydata.k); - }) - } - }; - - xhr.send(fd); - // setupLI(file.name); - -} - -function setupLI(emitter) { - var li = document.createElement("li"); - var name = document.createElement("p"); - name.innerHTML = emitter; - li.appendChild(name); - - var link = document.createElement("a"); - link.addEventListener('NotifyProgress', (progress_event) => { - console.log(progress_event); - }); - li.appendChild(link); - - var progress = document.createElement("p"); - li.appendChild(progress); - document.getElementById("uploaded_files").appendChild(li); -} - -function ivToStr(iv) { - let hexStr = ''; - for (var i in iv) { - if (iv[i] < 16) { - hexStr += '0' + iv[i].toString(16); - } else { - hexStr += iv[i].toString(16); - } - } - window.hexStr = hexStr; - return hexStr; -} - -function strToIv(str) { - var iv = new Uint8Array(16); - for (var i = 0; i < str.length; i += 2) { - iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16); - } - - return iv; -} - -function returnBindedLI(a_element, name, link, li) { - return function updateProgress(e) { - if (e.lengthComputable) { - var percentComplete = Math.floor(e.loaded / e.total * 100); - a_element.innerHTML = 'Progress: ' + percentComplete + '%'; - - if (percentComplete === 100) { - var btn = document.createElement('button'); - btn.innerHTML = 'Delete from server'; - btn.addEventListener('click', function() { - var segments = link.innerHTML.split('/'); - var key = segments[segments.length - 2]; - - var xhr = new XMLHttpRequest(); - xhr.open('post', '/delete/' + key, true); - xhr.setRequestHeader('Content-Type', 'application/json'); - if (!localStorage.getItem(key)) return; - - xhr.send(JSON.stringify({ delete_token: localStorage.getItem(key) })); - - xhr.onreadystatechange = function() { - if (xhr.readyState === XMLHttpRequest.DONE) { - document.getElementById('uploaded_files').removeChild(li); - localStorage.removeItem(key); - } - - if (xhr.status === 200) { - console.log('The file was successfully deleted.'); - } else { - console.log('The file has expired, or has already been deleted.'); - } - }; - }); - li.appendChild(btn); - } - } - }; -} diff --git a/server/portal_server.js b/server/portal_server.js index 4cc91688..df726ae4 100644 --- a/server/portal_server.js +++ b/server/portal_server.js @@ -1,4 +1,3 @@ - const express = require("express") const busboy = require("connect-busboy"); const path = require("path"); @@ -10,7 +9,7 @@ const app = express() const redis = require("redis"), client = redis.createClient(); -client.on("error", function(err) { +client.on("error", (err) => { console.log(err); }) @@ -18,12 +17,12 @@ app.use(busboy()); app.use(bodyParser.json()); app.use(express.static(path.join(__dirname, "../public"))); -app.get("/download/:id", function(req, res) { +app.get("/download/:id", (req, res) => { res.sendFile(path.join(__dirname + "/../public/download.html")); }); -app.get("/assets/download/:id", function(req, res) { - +app.get("/assets/download/:id", (req, res) => { + let id = req.params.id; if (!validateID(id)){ res.send(404); @@ -31,14 +30,14 @@ app.get("/assets/download/:id", function(req, res) { } - client.hget(id, "filename", function(err, reply) { // maybe some expiration logic too + client.hget(id, "filename", (err, reply) => { // maybe some expiration logic too if (!reply) { res.sendStatus(404); } else { res.setHeader("Content-Disposition", "attachment; filename=" + reply); res.setHeader("Content-Type", "application/octet-stream"); - res.download(__dirname + "/../static/" + id, reply, function(err) { + res.download(__dirname + "/../static/" + id, reply, (err) => { if (!err) { client.del(id); fs.unlinkSync(__dirname + "/../static/" + id); @@ -49,7 +48,7 @@ app.get("/assets/download/:id", function(req, res) { }); -app.post("/delete/:id", function(req, res) { +app.post("/delete/:id", (req, res) => { let id = req.params.id; if (!validateID(id)){ @@ -63,7 +62,7 @@ app.post("/delete/:id", function(req, res) { res.sendStatus(404); } - client.hget(id, "delete", function(err, reply) { + client.hget(id, "delete", (err, reply) => { if (!reply) { res.sendStatus(404); } else { @@ -74,29 +73,29 @@ app.post("/delete/:id", function(req, res) { }) }); -app.post("/upload/:id", function (req, res, next) { +app.post("/upload/:id", (req, res, next) => { if (!validateID(req.params.id)){ res.send(404); return; } - var fstream; + let fstream; req.pipe(req.busboy); - req.busboy.on("file", function (fieldname, file, filename) { + req.busboy.on("file", (fieldname, file, filename) => { console.log("Uploading: " + filename); //Path where image will be uploaded fstream = fs.createWriteStream(__dirname + "/../static/" + req.params.id); file.pipe(fstream); - fstream.on("close", function () { + fstream.on("close", () => { let id = req.params.id; let uuid = crypto.randomBytes(10).toString('hex'); client.hmset([id, "filename", filename, "delete", uuid]); // delete the file off the server in 24 hours - // setTimeout(function() { + // setTimeout(() => { // fs.unlinkSync(__dirname + "/static/" + id); // }, 86400000); @@ -107,10 +106,10 @@ app.post("/upload/:id", function (req, res, next) { }); }); -app.listen(3000, function () { +app.listen(3000, () => { console.log("Portal app listening on port 3000!") }) -function validateID(route_id) { +let validateID = (route_id) => { return route_id.match(/^[0-9a-fA-F]{32}$/) !== null; } \ No newline at end of file