added mozlog and file exists route
This commit is contained in:
parent
f9c2eb1ae4
commit
900fe32460
9 changed files with 127 additions and 49 deletions
15
server/log.js
Normal file
15
server/log.js
Normal 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;
|
|
@ -16,6 +16,10 @@ let notLocalHost =
|
|||
conf.s3_bucket !== 'localhost' &&
|
||||
conf.bitly_key !== 'localhost';
|
||||
|
||||
const mozlog = require('./log.js');
|
||||
|
||||
let log = mozlog('portal.server');
|
||||
|
||||
const app = express();
|
||||
|
||||
app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
|
||||
|
@ -30,6 +34,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 => {
|
||||
|
@ -70,7 +81,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);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -100,7 +111,7 @@ app.post('/delete/:id', (req, res) => {
|
|||
.delete(id, delete_token)
|
||||
.then(err => {
|
||||
if (!err) {
|
||||
console.log('Deleted.');
|
||||
log.info('Deleted:', id);
|
||||
}
|
||||
})
|
||||
.catch(err => res.sendStatus(404));
|
||||
|
@ -114,7 +125,7 @@ app.post('/upload/:id', (req, res, next) => {
|
|||
|
||||
req.pipe(req.busboy);
|
||||
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}/`;
|
||||
|
||||
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, () => {
|
||||
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 => {
|
||||
|
|
|
@ -7,26 +7,35 @@ const path = require('path');
|
|||
const fetch = require('node-fetch');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const redis = require('redis');
|
||||
const redis_client = redis.createClient();
|
||||
|
||||
redis_client.on('error', err => {
|
||||
console.log(err);
|
||||
});
|
||||
|
||||
let notLocalhost =
|
||||
let notLocalHost =
|
||||
conf.env === 'production' &&
|
||||
conf.s3_bucket !== '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 = {
|
||||
filename: filename,
|
||||
length: awsLength,
|
||||
get: awsGet,
|
||||
set: awsSet,
|
||||
delete: awsDelete,
|
||||
forceDelete: awsForceDelete
|
||||
forceDelete: awsForceDelete,
|
||||
exists: awsExists
|
||||
};
|
||||
} else {
|
||||
module.exports = {
|
||||
|
@ -35,7 +44,8 @@ if (notLocalhost) {
|
|||
get: localGet,
|
||||
set: localSet,
|
||||
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) {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
|
@ -74,7 +92,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
|
||||
|
@ -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) {
|
||||
let params = {
|
||||
Bucket: conf.s3_bucket,
|
||||
|
@ -140,7 +168,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');
|
||||
|
@ -148,7 +176,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('awsUploadFinish', 'Upload Finished of ' + filename);
|
||||
if (conf.bitly_key) {
|
||||
fetch(
|
||||
'https://api-ssl.bitly.com/v3/shorten?access_token=' +
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue