Merge branch 'master' into ui

This commit is contained in:
Daniela Arcese 2017-06-09 09:47:43 -04:00 committed by GitHub
commit b442b65f72
11 changed files with 720 additions and 54 deletions

View file

@ -32,4 +32,10 @@ let conf = convict({
// Perform validation
conf.validate({ allowed: 'strict' });
module.exports = conf.getProperties();
let props = conf.getProperties();
module.exports = props;
module.exports.notLocalHost =
props.env === 'production' &&
props.s3_bucket !== 'localhost' &&
props.bitly_key !== 'localhost';

12
server/log.js Normal file
View file

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

View file

@ -11,10 +11,11 @@ const bytes = require('bytes');
const conf = require('./config.js');
const storage = require('./storage.js');
let notLocalHost =
conf.env === 'production' &&
conf.s3_bucket !== 'localhost' &&
conf.bitly_key !== 'localhost';
let notLocalHost = conf.notLocalHost;
const mozlog = require('./log.js');
let log = mozlog('portal.server');
const app = express();
@ -30,6 +31,13 @@ app.get('/', (req, res) => {
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) => {
let id = req.params.id;
storage.filename(id).then(filename => {
@ -68,7 +76,7 @@ app.get('/assets/download/:id', (req, res) => {
file_stream.on(notLocalHost ? 'finish' : 'close', () => {
storage.forceDelete(id).then(err => {
if (!err) {
console.log('Deleted.');
log.info('Deleted:', id);
}
});
});
@ -99,7 +107,7 @@ app.post('/delete/:id', (req, res) => {
.delete(id, delete_token)
.then(err => {
if (!err) {
console.log('Deleted.');
log.info('Deleted:', id);
res.sendStatus(200);
}
})
@ -114,8 +122,9 @@ app.post('/upload/:id', (req, res, next) => {
req.pipe(req.busboy);
req.busboy.on('file', (fieldname, file, filename) => {
console.log('Uploading: ' + filename);
const protocol = conf.env === 'development' ? req.protocol : 'https';
log.info('Uploading:', req.params.id);
const protocol = notLocalHost ? 'https' : req.protocol;
let url = `${protocol}://${req.get('host')}/download/${req.params.id}/`;
storage.set(req.params.id, file, filename, url).then(linkAndID => {
@ -125,7 +134,7 @@ app.post('/upload/:id', (req, res, next) => {
});
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 => {

View file

@ -7,23 +7,25 @@ const path = require('path');
const fetch = require('node-fetch');
const crypto = require('crypto');
let notLocalHost = conf.notLocalHost;
const mozlog = require('./log.js');
let log = mozlog('portal.storage');
const redis = require('redis');
const redis_client = redis.createClient({
host: conf.redis_host
});
redis_client.on('error', err => {
console.log(err);
log.info('Redis: ', err);
});
let notLocalhost =
conf.env === 'production' &&
conf.s3_bucket !== 'localhost' &&
conf.bitly_key !== 'localhost';
if (notLocalhost) {
if (notLocalHost) {
module.exports = {
filename: filename,
exists: exists,
length: awsLength,
get: awsGet,
set: awsSet,
@ -33,6 +35,7 @@ if (notLocalhost) {
} else {
module.exports = {
filename: filename,
exists: exists,
length: localLength,
get: localGet,
set: localSet,
@ -53,6 +56,14 @@ function filename(id) {
});
}
function exists(id) {
return new Promise((resolve, reject) => {
redis_client.exists(id, (rediserr, reply) => {
resolve(reply === 1);
});
});
}
function localLength(id) {
return new Promise((resolve, reject) => {
try {
@ -76,7 +87,7 @@ function localSet(id, file, filename, url) {
redis_client.hmset([id, 'filename', filename, 'delete', uuid]);
redis_client.expire(id, 86400000);
console.log('Upload Finished of ' + filename);
log.info('localSet:', 'Upload Finished of ' + id);
resolve({
uuid: uuid,
url: url
@ -142,7 +153,7 @@ function awsSet(id, file, filename, url) {
return new Promise((resolve, reject) => {
s3.upload(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
log.info('awsUploadError:', err.stack); // an error occurred
reject();
} else {
let uuid = crypto.randomBytes(10).toString('hex');
@ -150,7 +161,7 @@ function awsSet(id, file, filename, url) {
redis_client.hmset([id, 'filename', filename, 'delete', uuid]);
redis_client.expire(id, 86400000);
console.log('Upload Finished of ' + filename);
log.info('awsUploadFinish', 'Upload Finished of ' + filename);
if (conf.bitly_key) {
fetch(
'https://api-ssl.bitly.com/v3/shorten?access_token=' +