added mozlog and file exists route

This commit is contained in:
Abhinav Adduri 2017-06-08 13:45:28 -07:00
parent f9c2eb1ae4
commit 900fe32460
9 changed files with 127 additions and 49 deletions

View File

@ -1,4 +1,5 @@
const FileReceiver = require('./fileReceiver'); const FileReceiver = require('./fileReceiver');
const $ = require('jquery');
$(document).ready(function() { $(document).ready(function() {
$('#send-file').click(() => { $('#send-file').click(() => {
@ -12,11 +13,6 @@ $(document).ready(function() {
let progress = document.createElement('p'); let progress = document.createElement('p');
let btn = $('#download-btn'); let btn = $('#download-btn');
// li.appendChild(name);
// li.appendChild(progress);
//document.getElementById('downloaded_files').appendChild(li);
fileReceiver.on('progress', percentComplete => { fileReceiver.on('progress', percentComplete => {
progress.innerText = `Progress: ${percentComplete}%`; progress.innerText = `Progress: ${percentComplete}%`;
@ -24,16 +20,6 @@ $(document).ready(function() {
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');
// finished.innerText = 'Your download has finished.';
// li.appendChild(finished);
// let close = document.createElement('button');
// close.innerText = 'Ok';
// close.addEventListener('click', () => {
// document.getElementById('downloaded_files').removeChild(li);
// });
// li.appendChild(close);
} }
}); });
@ -46,7 +32,6 @@ $(document).ready(function() {
$('#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.');
// document.getElementById('downloaded_files').removeChild(li);
return; return;
}) })
.then(([decrypted, fname]) => { .then(([decrypted, fname]) => {

View File

@ -1,4 +1,5 @@
const FileSender = require('./fileSender'); const FileSender = require('./fileSender');
const $ = require('jquery');
$(document).ready(function() { $(document).ready(function() {
let copyBtn = $('#copy-btn'); let copyBtn = $('#copy-btn');

66
package-lock.json generated
View File

@ -27,14 +27,12 @@
"ansi-regex": { "ansi-regex": {
"version": "2.1.1", "version": "2.1.1",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz",
"integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8="
"dev": true
}, },
"ansi-styles": { "ansi-styles": {
"version": "2.2.1", "version": "2.2.1",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz",
"integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=", "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4="
"dev": true
}, },
"anymatch": { "anymatch": {
"version": "1.3.0", "version": "1.3.0",
@ -343,8 +341,7 @@
"chalk": { "chalk": {
"version": "1.1.3", "version": "1.1.3",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz",
"integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg="
"dev": true
}, },
"chokidar": { "chokidar": {
"version": "1.7.0", "version": "1.7.0",
@ -558,6 +555,11 @@
"integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs=", "integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs=",
"dev": true "dev": true
}, },
"dbug": {
"version": "0.4.2",
"resolved": "https://registry.npmjs.org/dbug/-/dbug-0.4.2.tgz",
"integrity": "sha1-MrSzEF6IYQQ6b5rHVdgOVC02WzE="
},
"debug": { "debug": {
"version": "2.6.7", "version": "2.6.7",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.7.tgz", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.7.tgz",
@ -685,8 +687,7 @@
"escape-string-regexp": { "escape-string-regexp": {
"version": "1.0.5", "version": "1.0.5",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
"integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ="
"dev": true
}, },
"esutils": { "esutils": {
"version": "2.0.2", "version": "2.0.2",
@ -1557,8 +1558,7 @@
"has-ansi": { "has-ansi": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz",
"integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE="
"dev": true
}, },
"hash-base": { "hash-base": {
"version": "2.0.2", "version": "2.0.2",
@ -1633,6 +1633,11 @@
"integrity": "sha1-wDv04BywhtW15azorQr+eInWOMM=", "integrity": "sha1-wDv04BywhtW15azorQr+eInWOMM=",
"dev": true "dev": true
}, },
"intel": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/intel/-/intel-1.2.0.tgz",
"integrity": "sha1-EdEUfraz9Fgr31M3s31UFYTp5B4="
},
"ipaddr.js": { "ipaddr.js": {
"version": "1.3.0", "version": "1.3.0",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.3.0.tgz", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.3.0.tgz",
@ -1750,6 +1755,11 @@
"resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.15.0.tgz", "resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.15.0.tgz",
"integrity": "sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc=" "integrity": "sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc="
}, },
"jquery": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/jquery/-/jquery-3.2.1.tgz",
"integrity": "sha1-XE2d5lKvbNCncBVKYxu6ErAVx4c="
},
"js-tokens": { "js-tokens": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.1.tgz", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.1.tgz",
@ -1841,6 +1851,11 @@
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
"integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g="
}, },
"merge": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/merge/-/merge-1.2.0.tgz",
"integrity": "sha1-dTHjnUlJwoGma4xabgJl6LBYlNo="
},
"merge-descriptors": { "merge-descriptors": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz",
@ -1927,6 +1942,11 @@
} }
} }
}, },
"mozlog": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/mozlog/-/mozlog-2.1.1.tgz",
"integrity": "sha1-JxCOChxQvOMuo3Q4kRiDrjdO77s="
},
"ms": { "ms": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
@ -2356,6 +2376,11 @@
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.6.tgz", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.6.tgz",
"integrity": "sha1-dc449SvwczxafwwRjYEzSiu19BI=" "integrity": "sha1-dc449SvwczxafwwRjYEzSiu19BI="
}, },
"stack-trace": {
"version": "0.0.10",
"resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz",
"integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA="
},
"statuses": { "statuses": {
"version": "1.3.1", "version": "1.3.1",
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.3.1.tgz", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.3.1.tgz",
@ -2470,6 +2495,11 @@
"resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz", "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz",
"integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo=" "integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo="
}, },
"strftime": {
"version": "0.10.0",
"resolved": "https://registry.npmjs.org/strftime/-/strftime-0.10.0.tgz",
"integrity": "sha1-s/D6QZKVICpaKJ9ta+n0kJphcZM="
},
"string_decoder": { "string_decoder": {
"version": "0.10.31", "version": "0.10.31",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz",
@ -2478,8 +2508,7 @@
"strip-ansi": { "strip-ansi": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz",
"integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8="
"dev": true
}, },
"subarg": { "subarg": {
"version": "1.0.0", "version": "1.0.0",
@ -2490,8 +2519,12 @@
"supports-color": { "supports-color": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz",
"integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=", "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc="
"dev": true },
"symbol": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/symbol/-/symbol-0.3.1.tgz",
"integrity": "sha1-tvmpANSWpX8CQI8iGYwQndoGMEE="
}, },
"syntax-error": { "syntax-error": {
"version": "1.3.0", "version": "1.3.0",
@ -2602,6 +2635,11 @@
} }
} }
}, },
"utcstring": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/utcstring/-/utcstring-0.1.0.tgz",
"integrity": "sha1-Qw/VEKt/yVtdWRDJAteYgMIIQ2s="
},
"util": { "util": {
"version": "0.10.3", "version": "0.10.3",
"resolved": "https://registry.npmjs.org/util/-/util-0.10.3.tgz", "resolved": "https://registry.npmjs.org/util/-/util-0.10.3.tgz",

View File

@ -13,6 +13,8 @@
"express": "^4.15.3", "express": "^4.15.3",
"express-handlebars": "^3.0.0", "express-handlebars": "^3.0.0",
"fs-extra": "^3.0.1", "fs-extra": "^3.0.1",
"jquery": "^3.2.1",
"mozlog": "^2.1.1",
"node-fetch": "^1.7.1", "node-fetch": "^1.7.1",
"path": "^0.12.7", "path": "^0.12.7",
"redis": "^2.7.1" "redis": "^2.7.1"

15
server/log.js Normal file
View File

@ -0,0 +1,15 @@
const conf = require('./config.js');
let notLocalHost =
conf.env === 'production' &&
conf.s3_bucket !== 'localhost' &&
conf.bitly_key !== 'localhost';
const mozlog = require('mozlog') ({
app: 'FirefoxFileshare',
level: notLocalHost ? 'INFO' : 'verbose',
fmt: notLocalHost ? 'heka' : 'pretty',
debug: !notLocalHost
})
module.exports = mozlog;

View File

@ -16,6 +16,10 @@ let notLocalHost =
conf.s3_bucket !== 'localhost' && conf.s3_bucket !== 'localhost' &&
conf.bitly_key !== 'localhost'; conf.bitly_key !== 'localhost';
const mozlog = require('./log.js');
let log = mozlog('portal.server');
const app = express(); const app = express();
app.engine('handlebars', exphbs({ defaultLayout: 'main' })); app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
@ -30,6 +34,13 @@ app.get('/', (req, res) => {
res.render('index'); res.render('index');
}); });
app.get('/exists/:id', (req, res) => {
let id = req.params.id;
storage.exists(id).then(doesExist => {
res.sendStatus(doesExist ? 200 : 404);
})
});
app.get('/download/:id', (req, res) => { app.get('/download/:id', (req, res) => {
let id = req.params.id; let id = req.params.id;
storage.filename(id).then(filename => { storage.filename(id).then(filename => {
@ -70,7 +81,7 @@ app.get('/assets/download/:id', (req, res) => {
file_stream.on(notLocalHost ? 'finish' : 'close', () => { file_stream.on(notLocalHost ? 'finish' : 'close', () => {
storage.forceDelete(id).then(err => { storage.forceDelete(id).then(err => {
if (!err) { if (!err) {
console.log('Deleted.'); log.info('Deleted:', id);
} }
}); });
}); });
@ -100,7 +111,7 @@ app.post('/delete/:id', (req, res) => {
.delete(id, delete_token) .delete(id, delete_token)
.then(err => { .then(err => {
if (!err) { if (!err) {
console.log('Deleted.'); log.info('Deleted:', id);
} }
}) })
.catch(err => res.sendStatus(404)); .catch(err => res.sendStatus(404));
@ -114,7 +125,7 @@ app.post('/upload/:id', (req, res, next) => {
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); log.info('Uploading:', req.params.id);
let url = `${req.protocol}://${req.get('host')}/download/${req.params.id}/`; let url = `${req.protocol}://${req.get('host')}/download/${req.params.id}/`;
storage.set(req.params.id, file, filename, url).then(linkAndID => { storage.set(req.params.id, file, filename, url).then(linkAndID => {
@ -124,7 +135,7 @@ app.post('/upload/:id', (req, res, next) => {
}); });
let server = app.listen(conf.listen_port, () => { let server = app.listen(conf.listen_port, () => {
console.log(`Portal app listening on port ${conf.listen_port}!`); log.info('startServer:', `Portal app listening on port ${conf.listen_port}!`);
}); });
let validateID = route_id => { let validateID = route_id => {

View File

@ -7,26 +7,35 @@ const path = require('path');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const crypto = require('crypto'); const crypto = require('crypto');
const redis = require('redis');
const redis_client = redis.createClient();
redis_client.on('error', err => { let notLocalHost =
console.log(err);
});
let notLocalhost =
conf.env === 'production' && conf.env === 'production' &&
conf.s3_bucket !== 'localhost' && conf.s3_bucket !== 'localhost' &&
conf.bitly_key !== 'localhost'; conf.bitly_key !== 'localhost';
if (notLocalhost) { const mozlog = require('./log.js');
let log = mozlog('portal.storage');
const redis = require('redis');
const redis_client = redis.createClient();
redis_client.on('error', err => {
log.info('Redis: ', err);
});
if (notLocalHost) {
module.exports = { module.exports = {
filename: filename, filename: filename,
length: awsLength, length: awsLength,
get: awsGet, get: awsGet,
set: awsSet, set: awsSet,
delete: awsDelete, delete: awsDelete,
forceDelete: awsForceDelete forceDelete: awsForceDelete,
exists: awsExists
}; };
} else { } else {
module.exports = { module.exports = {
@ -35,7 +44,8 @@ if (notLocalhost) {
get: localGet, get: localGet,
set: localSet, set: localSet,
delete: localDelete, delete: localDelete,
forceDelete: localForceDelete forceDelete: localForceDelete,
exists: localExists
}; };
} }
@ -51,6 +61,14 @@ function filename(id) {
}); });
} }
function localExists(id) {
return new Promise((resolve, reject) => {
redis_client.hget(id, 'filename', (rediserr, reply) => {
resolve(fs.existsSync(__dirname + '/../static/' + id) && !rediserr)
});
});
}
function localLength(id) { function localLength(id) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
@ -74,7 +92,7 @@ function localSet(id, file, filename, url) {
redis_client.hmset([id, 'filename', filename, 'delete', uuid]); redis_client.hmset([id, 'filename', filename, 'delete', uuid]);
redis_client.expire(id, 86400000); redis_client.expire(id, 86400000);
console.log('Upload Finished of ' + filename); log.info('localSet:', 'Upload Finished of ' + id);
resolve({ resolve({
uuid: uuid, uuid: uuid,
url: url url: url
@ -105,6 +123,16 @@ function localForceDelete(id) {
}); });
} }
function awsExists(id) {
return new Promise((resolve, reject) => {
s3.getObject(params, function(awserr, data) {
redis_client.hget(id, 'filename', (rediserr, reply) => {
resolve(!awserr && !rediserr);
})
})
})
}
function awsLength(id) { function awsLength(id) {
let params = { let params = {
Bucket: conf.s3_bucket, Bucket: conf.s3_bucket,
@ -140,7 +168,7 @@ function awsSet(id, file, filename, url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
s3.upload(params, function(err, data) { s3.upload(params, function(err, data) {
if (err) { if (err) {
console.log(err, err.stack); // an error occurred log.info('awsUploadError:', err.stack); // an error occurred
reject(); reject();
} else { } else {
let uuid = crypto.randomBytes(10).toString('hex'); let uuid = crypto.randomBytes(10).toString('hex');
@ -148,7 +176,7 @@ function awsSet(id, file, filename, url) {
redis_client.hmset([id, 'filename', filename, 'delete', uuid]); redis_client.hmset([id, 'filename', filename, 'delete', uuid]);
redis_client.expire(id, 86400000); redis_client.expire(id, 86400000);
console.log('Upload Finished of ' + filename); log('awsUploadFinish', 'Upload Finished of ' + filename);
if (conf.bitly_key) { if (conf.bitly_key) {
fetch( fetch(
'https://api-ssl.bitly.com/v3/shorten?access_token=' + 'https://api-ssl.bitly.com/v3/shorten?access_token=' +

View File

@ -2,7 +2,6 @@
<html> <html>
<head> <head>
<title>Download your file</title> <title>Download your file</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="/bundle.js"></script> <script src="/bundle.js"></script>
<link rel="stylesheet" href="https://code.cdn.mozilla.net/fonts/fira.css"> <link rel="stylesheet" href="https://code.cdn.mozilla.net/fonts/fira.css">
<link rel="stylesheet" type="text/css" href="/main.css" /> <link rel="stylesheet" type="text/css" href="/main.css" />

View File

@ -2,7 +2,6 @@
<html> <html>
<head> <head>
<title>Firefox Fileshare</title> <title>Firefox Fileshare</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="./bundle.js"></script> <script src="./bundle.js"></script>
<link rel="stylesheet" href="https://code.cdn.mozilla.net/fonts/fira.css"> <link rel="stylesheet" href="https://code.cdn.mozilla.net/fonts/fira.css">
<link rel="stylesheet" type="text/css" href="./main.css" /> <link rel="stylesheet" type="text/css" href="./main.css" />