Code clean ups

This commit is contained in:
Bobby Wibowo 2020-06-01 08:44:48 +07:00
parent e7fc354729
commit 5f8bad907c
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
2 changed files with 17 additions and 12 deletions

View File

@ -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,

View File

@ -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