changed localstorage id's to match response from server, refactored meta.delete and newId out of storage module

This commit is contained in:
Abhinav Adduri 2017-07-10 12:19:20 -07:00
parent dc4682eaf5
commit 6f7930e34d
3 changed files with 22 additions and 26 deletions

View File

@ -102,14 +102,14 @@ class FileSender extends EventEmitter {
const responseObj = JSON.parse(xhr.responseText); const responseObj = JSON.parse(xhr.responseText);
resolve({ resolve({
url: responseObj.url, url: responseObj.url,
fileId: fileId, fileId: responseObj.id,
secretKey: keydata.k, secretKey: keydata.k,
deleteToken: responseObj.uuid deleteToken: responseObj.uuid
}); });
} }
}; };
xhr.open('post', '/upload/' + fileId, true); xhr.open('post', '/upload', true);
xhr.setRequestHeader( xhr.setRequestHeader(
'X-File-Metadata', 'X-File-Metadata',
JSON.stringify({ JSON.stringify({

View File

@ -8,6 +8,7 @@ const bytes = require('bytes');
const conf = require('./config.js'); const conf = require('./config.js');
const storage = require('./storage.js'); const storage = require('./storage.js');
const Raven = require('raven'); const Raven = require('raven');
const crypto = require('crypto');
if (conf.sentry_dsn) { if (conf.sentry_dsn) {
Raven.config(conf.sentry_dsn).install(); Raven.config(conf.sentry_dsn).install();
@ -136,24 +137,23 @@ app.post('/delete/:id', (req, res) => {
.catch(err => res.sendStatus(404)); .catch(err => res.sendStatus(404));
}); });
app.post('/upload/:id', (req, res, next) => { app.post('/upload', (req, res, next) => {
if (!validateIV(req.params.id)) { const newId = crypto.randomBytes(5).toString('hex');
res.sendStatus(404); let meta = JSON.parse(req.header('X-File-Metadata'));
return; meta.delete = crypto.randomBytes(10).toString('hex');
}
const meta = JSON.parse(req.header('X-File-Metadata'));
log.info('meta', meta); log.info('meta', meta);
req.pipe(req.busboy); req.pipe(req.busboy);
req.busboy.on('file', (fieldname, file, filename) => { req.busboy.on('file', (fieldname, file, filename) => {
log.info('Uploading:', req.params.id); log.info('Uploading:', newId);
storage.set(req.params.id, file, filename, meta).then(([delete_token, new_id]) => { storage.set(meta.iv, newId, file, filename, meta).then(() => {
const protocol = conf.env === 'production' ? 'https' : req.protocol; const protocol = conf.env === 'production' ? 'https' : req.protocol;
const url = `${protocol}://${req.get('host')}/download/${new_id}/`; const url = `${protocol}://${req.get('host')}/download/${newId}/`;
res.json({ res.json({
url, url,
delete: delete_token delete: meta.delete,
id: newId
}); });
}); });
}); });

View File

@ -116,22 +116,20 @@ function localGet(id) {
return fs.createReadStream(path.join(__dirname, '../static', id)); return fs.createReadStream(path.join(__dirname, '../static', id));
} }
function localSet(id, file, filename, meta) { function localSet(id, newId, file, filename, meta) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const new_id = crypto.randomBytes(5).toString('hex'); const fstream = fs.createWriteStream(path.join(__dirname, '../static', newId));
const fstream = fs.createWriteStream(path.join(__dirname, '../static', new_id));
file.pipe(fstream); file.pipe(fstream);
fstream.on('close', () => { fstream.on('close', () => {
meta.delete = crypto.randomBytes(10).toString('hex');
meta.id = id; meta.id = id;
redis_client.hmset(new_id, meta); redis_client.hmset(newId, meta);
redis_client.expire(id, 86400000); redis_client.expire(id, 86400000);
log.info('localSet:', 'Upload Finished of ' + new_id); log.info('localSet:', 'Upload Finished of ' + newId);
resolve([meta.delete, new_id]); resolve(meta.delete);
}); });
fstream.on('error', () => { fstream.on('error', () => {
log.error('localSet:', 'Failed upload of ' + new_id); log.error('localSet:', 'Failed upload of ' + newId);
reject(); reject();
}); });
}); });
@ -195,11 +193,10 @@ function awsGet(id) {
} }
} }
function awsSet(id, file, filename, meta) { function awsSet(id, newId, file, filename, meta) {
const new_id = crypto.randomBytes(5).toString('hex');
const params = { const params = {
Bucket: conf.s3_bucket, Bucket: conf.s3_bucket,
Key: new_id, Key: newId,
Body: file Body: file
}; };
@ -209,13 +206,12 @@ function awsSet(id, file, filename, meta) {
log.info('awsUploadError:', err.stack); // an error occurred log.info('awsUploadError:', err.stack); // an error occurred
reject(); reject();
} else { } else {
meta.delete = crypto.randomBytes(10).toString('hex');
meta.id = id; meta.id = id;
redis_client.hmset(new_id, meta); redis_client.hmset(newId, meta);
redis_client.expire(id, 86400000); redis_client.expire(id, 86400000);
log.info('awsUploadFinish', 'Upload Finished of ' + filename); log.info('awsUploadFinish', 'Upload Finished of ' + filename);
resolve([meta.delete, new_id]); resolve(meta.delete);
} }
}); });
}); });