abort uploads over maxfilesize

This commit is contained in:
Danny Coates 2017-07-20 12:50:20 -07:00
parent 34f26fc017
commit 55d3d1a792
No known key found for this signature in database
GPG key ID: 4C442633C62E00CB
11 changed files with 415 additions and 2438 deletions

View file

@ -129,20 +129,24 @@ function localGet(id) {
function localSet(newId, file, filename, meta) {
return new Promise((resolve, reject) => {
const fstream = fs.createWriteStream(
path.join(__dirname, '../static', newId)
);
const filepath = path.join(__dirname, '../static', newId);
const fstream = fs.createWriteStream(filepath);
file.pipe(fstream);
fstream.on('close', () => {
file.on('limit', () => {
file.unpipe(fstream);
fstream.destroy(new Error('limit'));
});
fstream.on('finish', () => {
redis_client.hmset(newId, meta);
redis_client.expire(newId, 86400000);
log.info('localSet:', 'Upload Finished of ' + newId);
resolve(meta.delete);
});
fstream.on('error', () => {
fstream.on('error', err => {
log.error('localSet:', 'Failed upload of ' + newId);
reject();
fs.unlinkSync(filepath);
reject(err);
});
});
}
@ -211,21 +215,25 @@ function awsSet(newId, file, filename, meta) {
Key: newId,
Body: file
};
return new Promise((resolve, reject) => {
s3.upload(params, function(err, _data) {
if (err) {
log.info('awsUploadError:', err.stack); // an error occurred
reject();
let hitLimit = false;
const upload = s3.upload(params);
file.on('limit', () => {
hitLimit = true;
upload.abort();
});
return upload.promise()
.then(() => {
redis_client.hmset(newId, meta);
redis_client.expire(newId, 86400000);
log.info('awsUploadFinish', 'Upload Finished of ' + filename);
},
err => {
if (hitLimit) {
throw new Error('limit');
} else {
redis_client.hmset(newId, meta);
redis_client.expire(newId, 86400000);
log.info('awsUploadFinish', 'Upload Finished of ' + filename);
resolve(meta.delete);
throw err;
}
});
});
}
function awsDelete(id, delete_token) {