This commit is contained in:
Daniela Arcese 2017-06-06 17:24:51 -04:00
parent a71161bf20
commit 6c29e33946
4 changed files with 125 additions and 125 deletions

View File

@ -23,7 +23,7 @@ $(document).ready(function(){
if (percentComplete === 100) { if (percentComplete === 100) {
fileReceiver.removeAllListeners('progress'); fileReceiver.removeAllListeners('progress');
btn.text('Download complete!'); btn.text('Download complete!');
btn.attr("disabled", "true"); btn.attr('disabled', 'true');
// let finished = document.createElement('p'); // let finished = document.createElement('p');
// finished.innerText = 'Your download has finished.'; // finished.innerText = 'Your download has finished.';
// li.appendChild(finished); // li.appendChild(finished);
@ -37,9 +37,12 @@ $(document).ready(function(){
} }
}); });
fileReceiver.download() fileReceiver
.catch((err) => { .download()
$('.title').text('This link has expired or never existed in the first place.'); .catch(err => {
$('.title').text(
'This link has expired or never existed in the first place.'
);
$('#download-btn').hide(); $('#download-btn').hide();
$('#expired-img').show(); $('#expired-img').show();
console.log('The file has expired, or has already been deleted.'); console.log('The file has expired, or has already been deleted.');

View File

@ -20,9 +20,10 @@ class FileReceiver extends EventEmitter {
}; };
xhr.onload = function(e) { xhr.onload = function(e) {
if (xhr.status === 404) { if (xhr.status === 404) {
reject(new Error('The file has expired, or has already been deleted.')); reject(
new Error('The file has expired, or has already been deleted.')
);
return; return;
} }
@ -58,8 +59,7 @@ class FileReceiver extends EventEmitter {
true, true,
['encrypt', 'decrypt'] ['encrypt', 'decrypt']
) )
]) ]).then(([fdata, key]) => {
.then(([fdata, key]) => {
let salt = this.salt; let salt = this.salt;
return Promise.all([ return Promise.all([
window.crypto.subtle.decrypt( window.crypto.subtle.decrypt(

View File

@ -2,31 +2,31 @@ const FileSender = require('./fileSender');
$(document).ready(function() { $(document).ready(function() {
let copyBtn = $('#copy-btn'); let copyBtn = $('#copy-btn');
copyBtn.attr("disabled", false); copyBtn.attr('disabled', false);
copyBtn.html("Copy"); copyBtn.html('Copy');
$('#page-one').show(); $('#page-one').show();
$('#file-list').hide(); $('#file-list').hide();
$('#upload-progress').hide(); $('#upload-progress').hide();
$('#share-link').hide(); $('#share-link').hide();
copyBtn.click(() => { copyBtn.click(() => {
console.log("copied"); console.log('copied');
var aux = document.createElement("input"); var aux = document.createElement('input');
aux.setAttribute("value", $('#link').attr("value")); aux.setAttribute('value', $('#link').attr('value'));
document.body.appendChild(aux); document.body.appendChild(aux);
aux.select(); aux.select();
document.execCommand("copy"); document.execCommand('copy');
document.body.removeChild(aux); document.body.removeChild(aux);
copyBtn.attr("disabled", true); copyBtn.attr('disabled', true);
copyBtn.html("Copied!"); copyBtn.html('Copied!');
}); });
$('.send-new').click(() => { $('.send-new').click(() => {
$('#page-one').show(); $('#page-one').show();
$('#file-list').show(); $('#file-list').show();
$('#upload-progress').hide(); $('#upload-progress').hide();
$('#share-link').hide(); $('#share-link').hide();
copyBtn.attr("disabled", false); copyBtn.attr('disabled', false);
copyBtn.html("Copy"); copyBtn.html('Copy');
}); });
let onChange = event => { let onChange = event => {
@ -60,14 +60,14 @@ $(document).ready(function(){
fileSender.upload().then(info => { fileSender.upload().then(info => {
const url = `${window.location const url = `${window.location
.origin}/download/${info.fileId}/#${info.secretKey}`; .origin}/download/${info.fileId}/#${info.secretKey}`;
$('#link').attr("value", url); $('#link').attr('value', url);
link.innerHTML = url; link.innerHTML = url;
localStorage.setItem(info.fileId, info.deleteToken); localStorage.setItem(info.fileId, info.deleteToken);
let del = document.createElement('td'); let del = document.createElement('td');
let btn = document.createElement('button'); let btn = document.createElement('button');
btn.innerHTML = 'x'; btn.innerHTML = 'x';
btn.classList.add('delete-btn'); btn.classList.add('delete-btn');
btn.addEventListener('click', (e) => { btn.addEventListener('click', e => {
FileSender.delete( FileSender.delete(
info.fileId, info.fileId,
localStorage.getItem(info.fileId) localStorage.getItem(info.fileId)

View File

@ -1,54 +1,52 @@
const express = require("express") const express = require('express');
const busboy = require("connect-busboy"); const busboy = require('connect-busboy');
const path = require("path"); const path = require('path');
const fs = require("fs-extra"); const fs = require('fs-extra');
const bodyParser = require("body-parser"); const bodyParser = require('body-parser');
const crypto = require("crypto"); const crypto = require('crypto');
const app = express() const app = express();
const redis = require("redis"), const redis = require('redis'),
client = redis.createClient(); client = redis.createClient();
client.on("error", (err) => { client.on('error', err => {
console.log(err); console.log(err);
}) });
app.use(busboy()); app.use(busboy());
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "../public"))); app.use(express.static(path.join(__dirname, '../public')));
app.get("/download/:id", (req, res) => { app.get('/download/:id', (req, res) => {
res.sendFile(path.join(__dirname + "/../public/download.html")); res.sendFile(path.join(__dirname + '/../public/download.html'));
}); });
app.get("/assets/download/:id", (req, res) => { app.get('/assets/download/:id', (req, res) => {
let id = req.params.id; let id = req.params.id;
if (!validateID(id)) { if (!validateID(id)) {
res.send(404); res.send(404);
return; return;
} }
client.hget(id, 'filename', (err, reply) => {
client.hget(id, "filename", (err, reply) => { // maybe some expiration logic too // maybe some expiration logic too
if (!reply) { if (!reply) {
res.sendStatus(404); res.sendStatus(404);
} else { } else {
res.setHeader("Content-Disposition", "attachment; filename=" + reply); res.setHeader('Content-Disposition', 'attachment; filename=' + reply);
res.setHeader("Content-Type", "application/octet-stream"); res.setHeader('Content-Type', 'application/octet-stream');
res.download(__dirname + "/../static/" + id, reply, (err) => { res.download(__dirname + '/../static/' + id, reply, err => {
if (!err) { if (!err) {
client.del(id); client.del(id);
fs.unlinkSync(__dirname + "/../static/" + id); fs.unlinkSync(__dirname + '/../static/' + id);
} }
}); });
} }
}) });
}); });
app.post("/delete/:id", (req, res) => { app.post('/delete/:id', (req, res) => {
let id = req.params.id; let id = req.params.id;
if (!validateID(id)) { if (!validateID(id)) {
@ -62,19 +60,18 @@ app.post("/delete/:id", (req, res) => {
res.sendStatus(404); res.sendStatus(404);
} }
client.hget(id, "delete", (err, reply) => { client.hget(id, 'delete', (err, reply) => {
if (!reply) { if (!reply) {
res.sendStatus(404); res.sendStatus(404);
} else { } else {
client.del(id); client.del(id);
fs.unlinkSync(__dirname + "/../static/" + id); fs.unlinkSync(__dirname + '/../static/' + id);
res.sendStatus(200); res.sendStatus(200);
} }
}) });
}); });
app.post("/upload/:id", (req, res, next) => { app.post('/upload/:id', (req, res, next) => {
if (!validateID(req.params.id)) { if (!validateID(req.params.id)) {
res.send(404); res.send(404);
return; return;
@ -82,17 +79,17 @@ app.post("/upload/:id", (req, res, next) => {
let fstream; let fstream;
req.pipe(req.busboy); req.pipe(req.busboy);
req.busboy.on("file", (fieldname, file, filename) => { req.busboy.on('file', (fieldname, file, filename) => {
console.log("Uploading: " + filename); console.log('Uploading: ' + filename);
//Path where image will be uploaded //Path where image will be uploaded
fstream = fs.createWriteStream(__dirname + "/../static/" + req.params.id); fstream = fs.createWriteStream(__dirname + '/../static/' + req.params.id);
file.pipe(fstream); file.pipe(fstream);
fstream.on("close", () => { fstream.on('close', () => {
let id = req.params.id; let id = req.params.id;
let uuid = crypto.randomBytes(10).toString('hex'); let uuid = crypto.randomBytes(10).toString('hex');
client.hmset([id, "filename", filename, "delete", uuid]); client.hmset([id, 'filename', filename, 'delete', uuid]);
// delete the file off the server in 24 hours // delete the file off the server in 24 hours
// setTimeout(() => { // setTimeout(() => {
@ -100,16 +97,16 @@ app.post("/upload/:id", (req, res, next) => {
// }, 86400000); // }, 86400000);
client.expire(id, 86400000); client.expire(id, 86400000);
console.log("Upload Finished of " + filename); console.log('Upload Finished of ' + filename);
res.send(uuid); res.send(uuid);
}); });
}); });
}); });
app.listen(3000, () => { app.listen(3000, () => {
console.log("Portal app listening on port 3000!") console.log('Portal app listening on port 3000!');
}) });
let validateID = (route_id) => { let validateID = route_id => {
return route_id.match(/^[0-9a-fA-F]{32}$/) !== null; return route_id.match(/^[0-9a-fA-F]{32}$/) !== null;
} };