use header for file metadata

This commit is contained in:
Danny Coates 2017-06-29 10:27:36 -07:00
parent 4cb34844aa
commit 05fe534e14
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
4 changed files with 44 additions and 41 deletions

View File

@ -34,12 +34,11 @@ class FileReceiver extends EventEmitter {
const blob = new Blob([this.response]);
const fileReader = new FileReader();
fileReader.onload = function() {
const meta = JSON.parse(xhr.getResponseHeader('X-File-Metadata'))
resolve({
data: this.result,
aad: xhr.getResponseHeader('Additional-Data'),
fname: xhr
.getResponseHeader('Content-Disposition')
.match(/=(.+)/)[1]
aad: meta.aad,
filename: meta.filename
});
};
@ -78,7 +77,7 @@ class FileReceiver extends EventEmitter {
fdata.data
),
new Promise((resolve, reject) => {
resolve(fdata.fname);
resolve(fdata.filename);
})
]);
});

View File

@ -75,9 +75,7 @@ class FileSender extends EventEmitter {
const dataView = new DataView(encrypted);
const blob = new Blob([dataView], { type: file.type });
const fd = new FormData();
fd.append('fname', file.name);
fd.append('data', blob, file.name);
fd.append('aad', arrayToHex(this.aad));
const xhr = new XMLHttpRequest();
@ -102,6 +100,7 @@ class FileSender extends EventEmitter {
};
xhr.open('post', '/upload/' + fileId, true);
xhr.setRequestHeader('X-File-Metadata', JSON.stringify({ aad: arrayToHex(this.aad), iv: fileId, filename: file.name }))
xhr.send(fd);
});
})

View File

@ -78,17 +78,15 @@ app.get('/assets/download/:id', (req, res) => {
return;
}
Promise.all([
storage.filename(id),
storage.aad(id)])
.then(([reply, aad]) => {
storage.metadata(id)
.then(meta => {
storage.length(id).then(contentLength => {
res.writeHead(200, {
'Content-Disposition': 'attachment; filename=' + reply,
'Content-Disposition': 'attachment; filename=' + meta.filename,
'Content-Type': 'application/octet-stream',
'Content-Length': contentLength,
'Additional-Data': aad
'X-File-Metadata': JSON.stringify(meta)
});
const file_stream = storage.get(id);
@ -143,20 +141,20 @@ app.post('/upload/:id', (req, res, next) => {
res.sendStatus(404);
return;
}
const meta = JSON.parse(req.header('X-File-Metadata'));
log.info('meta', meta)
req.pipe(req.busboy);
req.busboy.on('field', (fieldname, value) => {
storage.setField(req.params.id, fieldname, value);
})
req.busboy.on('file', (fieldname, file, filename) => {
log.info('Uploading:', req.params.id);
const protocol = conf.env === 'production' ? 'https' : req.protocol;
const url = `${protocol}://${req.get('host')}/download/${req.params.id}/`;
storage.set(req.params.id, file, filename, url).then(linkAndID => {
res.json(linkAndID);
storage.set(req.params.id, file, filename, meta).then(delete_token => {
const protocol = conf.env === 'production' ? 'https' : req.protocol;
const url = `${protocol}://${req.get('host')}/download/${req.params.id}/`;
res.json({
url,
delete: delete_token
});
});
});

View File

@ -31,7 +31,8 @@ if (conf.s3_bucket) {
setField: setField,
delete: awsDelete,
forceDelete: awsForceDelete,
ping: awsPing
ping: awsPing,
metadata
};
} else {
module.exports = {
@ -44,10 +45,23 @@ if (conf.s3_bucket) {
setField: setField,
delete: localDelete,
forceDelete: localForceDelete,
ping: localPing
ping: localPing,
metadata
};
}
function metadata(id) {
return new Promise((resolve, reject) => {
redis_client.hgetall(id, (err, reply) => {
if (!err) {
resolve(reply);
} else {
reject(err);
}
})
})
}
function filename(id) {
return new Promise((resolve, reject) => {
redis_client.hget(id, 'filename', (err, reply) => {
@ -102,20 +116,16 @@ function localGet(id) {
return fs.createReadStream(path.join(__dirname, '../static', id));
}
function localSet(id, file, filename, url) {
function localSet(id, file, filename, meta) {
return new Promise((resolve, reject) => {
const fstream = fs.createWriteStream(path.join(__dirname, '../static', id));
file.pipe(fstream);
fstream.on('close', () => {
const uuid = crypto.randomBytes(10).toString('hex');
redis_client.hmset([id, 'filename', filename, 'delete', uuid]);
fstream.on('close', () => {
meta.delete = crypto.randomBytes(10).toString('hex');
redis_client.hmset(id, meta);
redis_client.expire(id, 86400000);
log.info('localSet:', 'Upload Finished of ' + id);
resolve({
uuid: uuid,
url: url
});
resolve(meta.delete);
});
fstream.on('error', () => {
@ -183,7 +193,7 @@ function awsGet(id) {
}
}
function awsSet(id, file, filename, url) {
function awsSet(id, file, filename, meta) {
const params = {
Bucket: conf.s3_bucket,
Key: id,
@ -196,16 +206,13 @@ function awsSet(id, file, filename, url) {
log.info('awsUploadError:', err.stack); // an error occurred
reject();
} else {
const uuid = crypto.randomBytes(10).toString('hex');
meta.delete = crypto.randomBytes(10).toString('hex');
redis_client.hmset([id, 'filename', filename, 'delete', uuid]);
redis_client.hmset(id, meta);
redis_client.expire(id, 86400000);
log.info('awsUploadFinish', 'Upload Finished of ' + filename);
resolve({
uuid: uuid,
url: url
});
resolve(meta.delete);
}
});
});