refactored upload away from multipart forms to binary data
This commit is contained in:
parent
196d4211b6
commit
af7a262ef0
9 changed files with 56 additions and 93 deletions
|
@ -26,9 +26,8 @@ class FSStorage {
|
|||
const filepath = path.join(this.dir, id);
|
||||
const fstream = fs.createWriteStream(filepath);
|
||||
file.pipe(fstream);
|
||||
file.on('limit', () => {
|
||||
file.unpipe(fstream);
|
||||
fstream.destroy(new Error('limit'));
|
||||
file.on('error', err => {
|
||||
fstream.destroy(err);
|
||||
});
|
||||
fstream.on('error', err => {
|
||||
fs.unlinkSync(filepath);
|
||||
|
|
|
@ -18,25 +18,14 @@ class S3Storage {
|
|||
return s3.getObject({ Bucket: this.bucket, Key: id }).createReadStream();
|
||||
}
|
||||
|
||||
async set(id, file) {
|
||||
let hitLimit = false;
|
||||
set(id, file) {
|
||||
const upload = s3.upload({
|
||||
Bucket: this.bucket,
|
||||
Key: id,
|
||||
Body: file
|
||||
});
|
||||
file.on('limit', () => {
|
||||
hitLimit = true;
|
||||
upload.abort();
|
||||
});
|
||||
try {
|
||||
await upload.promise();
|
||||
} catch (e) {
|
||||
if (hitLimit) {
|
||||
throw new Error('limit');
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
file.on('error', () => upload.abort());
|
||||
return upload.promise();
|
||||
}
|
||||
|
||||
del(id) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue