diff --git a/controllers/multerStorageController.js b/controllers/multerStorageController.js index 7c55ecc..b93c060 100644 --- a/controllers/multerStorageController.js +++ b/controllers/multerStorageController.js @@ -44,13 +44,16 @@ DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) { if (!file._ischunk) { hash = blake3.createHash() file.stream.on('data', d => hash.update(d)) - file.stream.on('error', err => { + const onerror = function (err) { hash.dispose() - return cb(err) - }) + cb(err) + } + file.stream.on('error', onerror) + outStream.on('error', onerror) + } else { + outStream.on('error', cb) } - outStream.on('error', cb) outStream.on('finish', function () { cb(null, { destination, diff --git a/controllers/uploadController.js b/controllers/uploadController.js index bc2c68f..7c6f5db 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -508,30 +508,32 @@ self.actuallyFinishChunks = async (req, res, user) => { self.combineChunks = async (destination, uuid) => { let errorObj - const writeStream = fs.createWriteStream(destination, { flags: 'a' }) + const outStream = fs.createWriteStream(destination, { flags: 'a' }) const hash = blake3.createHash() + outStream.on('error', error => { + hash.dispose() + errorObj = error + }) + try { chunksData[uuid].chunks.sort() for (const chunk of chunksData[uuid].chunks) await new Promise((resolve, reject) => { const stream = fs.createReadStream(path.join(chunksData[uuid].root, chunk)) - stream.pipe(writeStream, { end: false }) + stream.pipe(outStream, { end: false }) stream.on('data', d => hash.update(d)) - stream.on('error', error => { - hash.dispose() - reject(error) - }) - + stream.on('error', reject) stream.on('end', () => resolve()) }) } catch (error) { + hash.dispose() errorObj = error } // Close stream - writeStream.end() + outStream.end() // Re-throw error if (errorObj) throw errorObj