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

@ -1,7 +1,7 @@
const FileReceiver = require('./fileReceiver');
$(document).ready(function(){
$('.send-new').click(()=>{
$(document).ready(function() {
$('.send-new').click(() => {
window.location.replace(`${window.location.origin}`);
});
let download = () => {
@ -23,7 +23,7 @@ $(document).ready(function(){
if (percentComplete === 100) {
fileReceiver.removeAllListeners('progress');
btn.text('Download complete!');
btn.attr("disabled", "true");
btn.attr('disabled', 'true');
// let finished = document.createElement('p');
// finished.innerText = 'Your download has finished.';
// li.appendChild(finished);
@ -37,27 +37,30 @@ $(document).ready(function(){
}
});
fileReceiver.download()
.catch((err) => {
$('.title').text('This link has expired or never existed in the first place.');
$('#download-btn').hide();
$('#expired-img').show();
console.log('The file has expired, or has already been deleted.');
// document.getElementById('downloaded_files').removeChild(li);
return;
})
.then(([decrypted, fname]) => {
name.innerText = fname;
let dataView = new DataView(decrypted);
let blob = new Blob([dataView]);
let downloadUrl = URL.createObjectURL(blob);
fileReceiver
.download()
.catch(err => {
$('.title').text(
'This link has expired or never existed in the first place.'
);
$('#download-btn').hide();
$('#expired-img').show();
console.log('The file has expired, or has already been deleted.');
// document.getElementById('downloaded_files').removeChild(li);
return;
})
.then(([decrypted, fname]) => {
name.innerText = fname;
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 = fname;
document.body.appendChild(a);
a.click();
});
let a = document.createElement('a');
a.href = downloadUrl;
a.download = fname;
document.body.appendChild(a);
a.click();
});
};
window.download = download;

View File

@ -20,9 +20,10 @@ class FileReceiver extends EventEmitter {
};
xhr.onload = function(e) {
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;
}
@ -58,8 +59,7 @@ class FileReceiver extends EventEmitter {
true,
['encrypt', 'decrypt']
)
])
.then(([fdata, key]) => {
]).then(([fdata, key]) => {
let salt = this.salt;
return Promise.all([
window.crypto.subtle.decrypt(

View File

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

View File

@ -1,115 +1,112 @@
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 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();
const app = express();
const redis = require('redis'),
client = redis.createClient();
client.on("error", (err) => {
client.on('error', err => {
console.log(err);
})
});
app.use(busboy());
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) => {
res.sendFile(path.join(__dirname + "/../public/download.html"));
app.get('/download/:id', (req, res) => {
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;
if (!validateID(id)){
if (!validateID(id)) {
res.send(404);
return;
}
client.hget(id, "filename", (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, (err) => {
res.setHeader('Content-Disposition', 'attachment; filename=' + reply);
res.setHeader('Content-Type', 'application/octet-stream');
res.download(__dirname + '/../static/' + id, reply, err => {
if (!err) {
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;
if (!validateID(id)){
if (!validateID(id)) {
res.send(404);
return;
}
let delete_token = req.body.delete_token;
if (!delete_token){
if (!delete_token) {
res.sendStatus(404);
}
client.hget(id, "delete", (err, reply) => {
client.hget(id, 'delete', (err, reply) => {
if (!reply) {
res.sendStatus(404);
} else {
client.del(id);
fs.unlinkSync(__dirname + "/../static/" + id);
fs.unlinkSync(__dirname + '/../static/' + id);
res.sendStatus(200);
}
})
});
});
app.post("/upload/:id", (req, res, next) => {
if (!validateID(req.params.id)){
res.send(404);
return;
}
app.post('/upload/:id', (req, res, next) => {
if (!validateID(req.params.id)) {
res.send(404);
return;
}
let fstream;
req.pipe(req.busboy);
req.busboy.on("file", (fieldname, file, filename) => {
console.log("Uploading: " + filename);
let fstream;
req.pipe(req.busboy);
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", () => {
let id = req.params.id;
let uuid = crypto.randomBytes(10).toString('hex');
//Path where image will be uploaded
fstream = fs.createWriteStream(__dirname + '/../static/' + req.params.id);
file.pipe(fstream);
fstream.on('close', () => {
let id = req.params.id;
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
// setTimeout(() => {
// fs.unlinkSync(__dirname + "/static/" + id);
// }, 86400000);
// delete the file off the server in 24 hours
// setTimeout(() => {
// fs.unlinkSync(__dirname + "/static/" + id);
// }, 86400000);
client.expire(id, 86400000);
console.log("Upload Finished of " + filename);
res.send(uuid);
});
client.expire(id, 86400000);
console.log('Upload Finished of ' + filename);
res.send(uuid);
});
});
});
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;
}
};