From 2c577cb7aed146ea012bcb8204cf90e7edffdd97 Mon Sep 17 00:00:00 2001 From: Jonathan Cremin Date: Sun, 7 Aug 2016 20:10:04 +0100 Subject: [PATCH] Wait for the image to finish writing before resizing --- api/routes/file.js | 2 +- lib/uploader.js | 64 +++++++++++++++++++++++++--------------------- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/api/routes/file.js b/api/routes/file.js index 0248965..5ca6522 100644 --- a/api/routes/file.js +++ b/api/routes/file.js @@ -19,7 +19,7 @@ export function* post(next) { uploader.acceptedEvent(); - uploader.receive(); + yield uploader.receive(); yield uploader.promise; diff --git a/lib/uploader.js b/lib/uploader.js index 956669f..7b8fd0b 100644 --- a/lib/uploader.js +++ b/lib/uploader.js @@ -70,41 +70,47 @@ export default class Uploader { } receive() { - this.path = join(this.file.id[0], `${this.file.id}_${this.file.name}`); - this.localStream = fs.createWriteStream(join(storePath, this.path)); + return new Promise((resolve) => { + this.path = join(this.file.id[0], `${this.file.id}_${this.file.name}`); + this.localStream = fs.createWriteStream(join(storePath, this.path)); - this.upload.pause(); + this.upload.pause(); - this.upload.on('data', (data) => { - this.receivedSize += data.length; - if (this.receivedSize > this.context.user.max_filesize) { - fs.unlink(join(storePath, this.path)); - this.context.throw(413, `{"error": {"message": "The file you tried to upload is too large.", - "code": 601}}`); - } + this.localStream.on('finish', () => { + resolve(); + }); - this.localStream.write(data); + this.upload.on('data', (data) => { + this.receivedSize += data.length; + if (this.receivedSize > this.context.user.max_filesize) { + fs.unlink(join(storePath, this.path)); + this.context.throw(413, `{"error": {"message": "The file you uploaded is too large.", + "code": 601}}`); + } - this.percentComplete = Math.floor(this.receivedSize * 100 / this.expectedSize); - if (this.percentComplete > this.lastPercent && this.lastTick < Date.now() - 1000) { - const progressEvent = `{"type": "file-progress", "data": - {"id": "${this.file.id}", "complete": ${this.percentComplete}}}`; - this.context.redis.publish(`/file/${this.file.id}`, progressEvent); - this.context.redis.publish(`/user/${this.context.user.id}`, progressEvent); - this.lastTick = Date.now(); - } - this.lastPercent = this.percentComplete; + this.localStream.write(data); - this.md5sum.update(data); + this.percentComplete = Math.floor(this.receivedSize * 100 / this.expectedSize); + if (this.percentComplete > this.lastPercent && this.lastTick < Date.now() - 1000) { + const progressEvent = `{"type": "file-progress", "data": + {"id": "${this.file.id}", "complete": ${this.percentComplete}}}`; + this.context.redis.publish(`/file/${this.file.id}`, progressEvent); + this.context.redis.publish(`/user/${this.context.user.id}`, progressEvent); + this.lastTick = Date.now(); + } + this.lastPercent = this.percentComplete; + + this.md5sum.update(data); + }); + + this.upload.on('end', () => { + this.file.size = this.receivedSize; + this.file.md5 = this.md5sum.digest('hex'); + this.localStream.end(); + }); + + this.upload.resume(); }); - - this.upload.on('end', () => { - this.file.size = this.receivedSize; - this.file.md5 = this.md5sum.digest('hex'); - this.localStream.end(); - }); - - this.upload.resume(); } acceptedEvent() {