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) { if (!file._ischunk) {
hash = blake3.createHash() hash = blake3.createHash()
file.stream.on('data', d => hash.update(d)) file.stream.on('data', d => hash.update(d))
file.stream.on('error', err => { const onerror = function (err) {
hash.dispose() 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 () { outStream.on('finish', function () {
cb(null, { cb(null, {
destination, destination,

View File

@ -508,30 +508,32 @@ self.actuallyFinishChunks = async (req, res, user) => {
self.combineChunks = async (destination, uuid) => { self.combineChunks = async (destination, uuid) => {
let errorObj let errorObj
const writeStream = fs.createWriteStream(destination, { flags: 'a' }) const outStream = fs.createWriteStream(destination, { flags: 'a' })
const hash = blake3.createHash() const hash = blake3.createHash()
outStream.on('error', error => {
hash.dispose()
errorObj = error
})
try { try {
chunksData[uuid].chunks.sort() chunksData[uuid].chunks.sort()
for (const chunk of chunksData[uuid].chunks) for (const chunk of chunksData[uuid].chunks)
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
const stream = fs.createReadStream(path.join(chunksData[uuid].root, chunk)) 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('data', d => hash.update(d))
stream.on('error', error => { stream.on('error', reject)
hash.dispose()
reject(error)
})
stream.on('end', () => resolve()) stream.on('end', () => resolve())
}) })
} catch (error) { } catch (error) {
hash.dispose()
errorObj = error errorObj = error
} }
// Close stream // Close stream
writeStream.end() outStream.end()
// Re-throw error // Re-throw error
if (errorObj) throw errorObj if (errorObj) throw errorObj