format
This commit is contained in:
parent
a71161bf20
commit
6c29e33946
|
@ -1,7 +1,7 @@
|
||||||
const FileReceiver = require('./fileReceiver');
|
const FileReceiver = require('./fileReceiver');
|
||||||
|
|
||||||
$(document).ready(function(){
|
$(document).ready(function() {
|
||||||
$('.send-new').click(()=>{
|
$('.send-new').click(() => {
|
||||||
window.location.replace(`${window.location.origin}`);
|
window.location.replace(`${window.location.origin}`);
|
||||||
});
|
});
|
||||||
let download = () => {
|
let download = () => {
|
||||||
|
@ -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,27 +37,30 @@ $(document).ready(function(){
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
fileReceiver.download()
|
fileReceiver
|
||||||
.catch((err) => {
|
.download()
|
||||||
$('.title').text('This link has expired or never existed in the first place.');
|
.catch(err => {
|
||||||
$('#download-btn').hide();
|
$('.title').text(
|
||||||
$('#expired-img').show();
|
'This link has expired or never existed in the first place.'
|
||||||
console.log('The file has expired, or has already been deleted.');
|
);
|
||||||
// document.getElementById('downloaded_files').removeChild(li);
|
$('#download-btn').hide();
|
||||||
return;
|
$('#expired-img').show();
|
||||||
})
|
console.log('The file has expired, or has already been deleted.');
|
||||||
.then(([decrypted, fname]) => {
|
// document.getElementById('downloaded_files').removeChild(li);
|
||||||
name.innerText = fname;
|
return;
|
||||||
let dataView = new DataView(decrypted);
|
})
|
||||||
let blob = new Blob([dataView]);
|
.then(([decrypted, fname]) => {
|
||||||
let downloadUrl = URL.createObjectURL(blob);
|
name.innerText = fname;
|
||||||
|
let dataView = new DataView(decrypted);
|
||||||
|
let blob = new Blob([dataView]);
|
||||||
|
let downloadUrl = URL.createObjectURL(blob);
|
||||||
|
|
||||||
let a = document.createElement('a');
|
let a = document.createElement('a');
|
||||||
a.href = downloadUrl;
|
a.href = downloadUrl;
|
||||||
a.download = fname;
|
a.download = fname;
|
||||||
document.body.appendChild(a);
|
document.body.appendChild(a);
|
||||||
a.click();
|
a.click();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
window.download = download;
|
window.download = download;
|
||||||
|
|
|
@ -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(
|
||||||
|
|
|
@ -1,32 +1,32 @@
|
||||||
const FileSender = require('./fileSender');
|
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,28 +60,28 @@ $(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)
|
||||||
).then(() => {
|
).then(() => {
|
||||||
e.target.parentNode.parentNode.remove();
|
e.target.parentNode.parentNode.remove();
|
||||||
localStorage.removeItem(info.fileId);
|
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;
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,115 +1,112 @@
|
||||||
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)) {
|
||||||
res.send(404);
|
res.send(404);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let delete_token = req.body.delete_token;
|
let delete_token = req.body.delete_token;
|
||||||
|
|
||||||
if (!delete_token){
|
if (!delete_token) {
|
||||||
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;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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(() => {
|
||||||
// fs.unlinkSync(__dirname + "/static/" + id);
|
// fs.unlinkSync(__dirname + "/static/" + id);
|
||||||
// }, 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;
|
||||||
}
|
};
|
||||||
|
|
Loading…
Reference in New Issue