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

@ -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);
}
});
});