From fd40c17528a04a8df7c2d57fc6f2ffc84db4bcdc Mon Sep 17 00:00:00 2001 From: Cristian Lupu Date: Thu, 14 Jan 2021 13:19:18 +0200 Subject: [PATCH 1/4] Close write stream on connection error --- storage/disk.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/storage/disk.js b/storage/disk.js index 2f77c9f1..617cbea1 100644 --- a/storage/disk.js +++ b/storage/disk.js @@ -36,8 +36,14 @@ DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) { var finalPath = path.join(destination, filename) var outStream = fs.createWriteStream(finalPath) + var connection = req.socket || req.connection file.stream.pipe(outStream) + + connection.on('error', (error) => { + outStream.end(); + }) + outStream.on('error', cb) outStream.on('finish', function () { cb(null, { From adcd69198e7872669087c0974e9a9f0393d0aaee Mon Sep 17 00:00:00 2001 From: Cristian Lupu Date: Thu, 14 Jan 2021 15:04:05 +0200 Subject: [PATCH 2/4] #971 Supporting older node versions --- storage/disk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/disk.js b/storage/disk.js index 617cbea1..9d76658e 100644 --- a/storage/disk.js +++ b/storage/disk.js @@ -40,7 +40,7 @@ DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) { file.stream.pipe(outStream) - connection.on('error', (error) => { + connection.on('error', function(error) { outStream.end(); }) From deeb12599c2b58647c0697bf029c0eedbf92218a Mon Sep 17 00:00:00 2001 From: Cristian Lupu Date: Thu, 14 Jan 2021 15:08:11 +0200 Subject: [PATCH 3/4] #971 Tests fake the request component --- storage/disk.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/storage/disk.js b/storage/disk.js index 9d76658e..c3e81ddd 100644 --- a/storage/disk.js +++ b/storage/disk.js @@ -40,9 +40,11 @@ DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) { file.stream.pipe(outStream) - connection.on('error', function(error) { - outStream.end(); - }) + if (connection) { + connection.on('error', function(error) { + outStream.end(); + }) + } outStream.on('error', cb) outStream.on('finish', function () { From 831a4e06b2a5bf016f2d55fff778a64562a4ee36 Mon Sep 17 00:00:00 2001 From: Cristian Lupu Date: Thu, 14 Jan 2021 15:17:38 +0200 Subject: [PATCH 4/4] #971 :art: StandardJs coding standards --- storage/disk.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/storage/disk.js b/storage/disk.js index c3e81ddd..b395e3ba 100644 --- a/storage/disk.js +++ b/storage/disk.js @@ -41,8 +41,9 @@ DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) { file.stream.pipe(outStream) if (connection) { - connection.on('error', function(error) { - outStream.end(); + connection.on('error', function (error) { + outStream.end() + cb(error) }) }