use header for file metadata
This commit is contained in:
parent
4cb34844aa
commit
05fe534e14
@ -34,12 +34,11 @@ class FileReceiver extends EventEmitter {
|
|||||||
const blob = new Blob([this.response]);
|
const blob = new Blob([this.response]);
|
||||||
const fileReader = new FileReader();
|
const fileReader = new FileReader();
|
||||||
fileReader.onload = function() {
|
fileReader.onload = function() {
|
||||||
|
const meta = JSON.parse(xhr.getResponseHeader('X-File-Metadata'))
|
||||||
resolve({
|
resolve({
|
||||||
data: this.result,
|
data: this.result,
|
||||||
aad: xhr.getResponseHeader('Additional-Data'),
|
aad: meta.aad,
|
||||||
fname: xhr
|
filename: meta.filename
|
||||||
.getResponseHeader('Content-Disposition')
|
|
||||||
.match(/=(.+)/)[1]
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -78,7 +77,7 @@ class FileReceiver extends EventEmitter {
|
|||||||
fdata.data
|
fdata.data
|
||||||
),
|
),
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
resolve(fdata.fname);
|
resolve(fdata.filename);
|
||||||
})
|
})
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
@ -75,9 +75,7 @@ class FileSender extends EventEmitter {
|
|||||||
const dataView = new DataView(encrypted);
|
const dataView = new DataView(encrypted);
|
||||||
const blob = new Blob([dataView], { type: file.type });
|
const blob = new Blob([dataView], { type: file.type });
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
fd.append('fname', file.name);
|
|
||||||
fd.append('data', blob, file.name);
|
fd.append('data', blob, file.name);
|
||||||
fd.append('aad', arrayToHex(this.aad));
|
|
||||||
|
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
@ -102,6 +100,7 @@ class FileSender extends EventEmitter {
|
|||||||
};
|
};
|
||||||
|
|
||||||
xhr.open('post', '/upload/' + fileId, true);
|
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);
|
xhr.send(fd);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
@ -78,17 +78,15 @@ app.get('/assets/download/:id', (req, res) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Promise.all([
|
storage.metadata(id)
|
||||||
storage.filename(id),
|
.then(meta => {
|
||||||
storage.aad(id)])
|
|
||||||
.then(([reply, aad]) => {
|
|
||||||
storage.length(id).then(contentLength => {
|
storage.length(id).then(contentLength => {
|
||||||
|
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
'Content-Disposition': 'attachment; filename=' + reply,
|
'Content-Disposition': 'attachment; filename=' + meta.filename,
|
||||||
'Content-Type': 'application/octet-stream',
|
'Content-Type': 'application/octet-stream',
|
||||||
'Content-Length': contentLength,
|
'Content-Length': contentLength,
|
||||||
'Additional-Data': aad
|
'X-File-Metadata': JSON.stringify(meta)
|
||||||
});
|
});
|
||||||
const file_stream = storage.get(id);
|
const file_stream = storage.get(id);
|
||||||
|
|
||||||
@ -143,20 +141,20 @@ app.post('/upload/:id', (req, res, next) => {
|
|||||||
res.sendStatus(404);
|
res.sendStatus(404);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const meta = JSON.parse(req.header('X-File-Metadata'));
|
||||||
|
log.info('meta', meta)
|
||||||
req.pipe(req.busboy);
|
req.pipe(req.busboy);
|
||||||
|
|
||||||
req.busboy.on('field', (fieldname, value) => {
|
|
||||||
storage.setField(req.params.id, fieldname, value);
|
|
||||||
})
|
|
||||||
|
|
||||||
req.busboy.on('file', (fieldname, file, filename) => {
|
req.busboy.on('file', (fieldname, file, filename) => {
|
||||||
log.info('Uploading:', req.params.id);
|
log.info('Uploading:', req.params.id);
|
||||||
|
|
||||||
|
storage.set(req.params.id, file, filename, meta).then(delete_token => {
|
||||||
const protocol = conf.env === 'production' ? 'https' : req.protocol;
|
const protocol = conf.env === 'production' ? 'https' : req.protocol;
|
||||||
const url = `${protocol}://${req.get('host')}/download/${req.params.id}/`;
|
const url = `${protocol}://${req.get('host')}/download/${req.params.id}/`;
|
||||||
storage.set(req.params.id, file, filename, url).then(linkAndID => {
|
res.json({
|
||||||
res.json(linkAndID);
|
url,
|
||||||
|
delete: delete_token
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -31,7 +31,8 @@ if (conf.s3_bucket) {
|
|||||||
setField: setField,
|
setField: setField,
|
||||||
delete: awsDelete,
|
delete: awsDelete,
|
||||||
forceDelete: awsForceDelete,
|
forceDelete: awsForceDelete,
|
||||||
ping: awsPing
|
ping: awsPing,
|
||||||
|
metadata
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@ -44,10 +45,23 @@ if (conf.s3_bucket) {
|
|||||||
setField: setField,
|
setField: setField,
|
||||||
delete: localDelete,
|
delete: localDelete,
|
||||||
forceDelete: localForceDelete,
|
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) {
|
function filename(id) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
redis_client.hget(id, 'filename', (err, reply) => {
|
redis_client.hget(id, 'filename', (err, reply) => {
|
||||||
@ -102,20 +116,16 @@ function localGet(id) {
|
|||||||
return fs.createReadStream(path.join(__dirname, '../static', 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) => {
|
return new Promise((resolve, reject) => {
|
||||||
const fstream = fs.createWriteStream(path.join(__dirname, '../static', id));
|
const fstream = fs.createWriteStream(path.join(__dirname, '../static', id));
|
||||||
file.pipe(fstream);
|
file.pipe(fstream);
|
||||||
fstream.on('close', () => {
|
fstream.on('close', () => {
|
||||||
const uuid = crypto.randomBytes(10).toString('hex');
|
meta.delete = crypto.randomBytes(10).toString('hex');
|
||||||
|
redis_client.hmset(id, meta);
|
||||||
redis_client.hmset([id, 'filename', filename, 'delete', uuid]);
|
|
||||||
redis_client.expire(id, 86400000);
|
redis_client.expire(id, 86400000);
|
||||||
log.info('localSet:', 'Upload Finished of ' + id);
|
log.info('localSet:', 'Upload Finished of ' + id);
|
||||||
resolve({
|
resolve(meta.delete);
|
||||||
uuid: uuid,
|
|
||||||
url: url
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
fstream.on('error', () => {
|
fstream.on('error', () => {
|
||||||
@ -183,7 +193,7 @@ function awsGet(id) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function awsSet(id, file, filename, url) {
|
function awsSet(id, file, filename, meta) {
|
||||||
const params = {
|
const params = {
|
||||||
Bucket: conf.s3_bucket,
|
Bucket: conf.s3_bucket,
|
||||||
Key: id,
|
Key: id,
|
||||||
@ -196,16 +206,13 @@ function awsSet(id, file, filename, url) {
|
|||||||
log.info('awsUploadError:', err.stack); // an error occurred
|
log.info('awsUploadError:', err.stack); // an error occurred
|
||||||
reject();
|
reject();
|
||||||
} else {
|
} 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);
|
redis_client.expire(id, 86400000);
|
||||||
log.info('awsUploadFinish', 'Upload Finished of ' + filename);
|
log.info('awsUploadFinish', 'Upload Finished of ' + filename);
|
||||||
resolve({
|
resolve(meta.delete);
|
||||||
uuid: uuid,
|
|
||||||
url: url
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user