2020-05-28 19:52:58 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const blake3 = require('blake3')
|
|
|
|
const mkdirp = require('mkdirp')
|
2022-04-15 09:41:05 +00:00
|
|
|
const logger = require('./../../logger')
|
|
|
|
|
|
|
|
const REQUIRED_WEIGHT = 2
|
2020-05-28 19:52:58 +00:00
|
|
|
|
|
|
|
function DiskStorage (opts) {
|
2020-09-26 20:20:32 +00:00
|
|
|
this.getFilename = opts.filename
|
2020-05-28 19:52:58 +00:00
|
|
|
|
|
|
|
if (typeof opts.destination === 'string') {
|
|
|
|
mkdirp.sync(opts.destination)
|
|
|
|
this.getDestination = function ($0, $1, cb) { cb(null, opts.destination) }
|
|
|
|
} else {
|
2020-09-26 20:20:32 +00:00
|
|
|
this.getDestination = opts.destination
|
2020-05-28 19:52:58 +00:00
|
|
|
}
|
2022-04-15 09:41:05 +00:00
|
|
|
|
|
|
|
this.clamscan = opts.clamscan
|
2020-05-28 19:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) {
|
|
|
|
const that = this
|
|
|
|
|
2022-04-15 09:41:05 +00:00
|
|
|
// "weighted" callback, to be able to "await" multiple callbacks
|
|
|
|
let tempError = null
|
|
|
|
let tempObject = {}
|
|
|
|
let tempWeight = 0
|
2022-04-16 06:33:11 +00:00
|
|
|
const _cb = (err = null, result = {}, weight = 2) => {
|
2022-04-15 09:41:05 +00:00
|
|
|
tempError = err
|
|
|
|
tempWeight += weight
|
|
|
|
tempObject = Object.assign(result, tempObject)
|
|
|
|
if (tempError || tempWeight >= REQUIRED_WEIGHT) {
|
|
|
|
cb(tempError, tempObject)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 19:52:58 +00:00
|
|
|
that.getDestination(req, file, function (err, destination) {
|
2022-04-15 09:41:05 +00:00
|
|
|
if (err) return _cb(err)
|
2020-05-28 19:52:58 +00:00
|
|
|
|
|
|
|
that.getFilename(req, file, function (err, filename) {
|
2022-04-15 09:41:05 +00:00
|
|
|
if (err) return _cb(err)
|
2020-05-28 19:52:58 +00:00
|
|
|
|
|
|
|
const finalPath = path.join(destination, filename)
|
2020-06-15 16:14:33 +00:00
|
|
|
const onerror = err => {
|
|
|
|
hash.dispose()
|
2022-04-15 09:41:05 +00:00
|
|
|
_cb(err)
|
2020-06-15 16:14:33 +00:00
|
|
|
}
|
2020-05-28 19:52:58 +00:00
|
|
|
|
2020-06-15 16:14:33 +00:00
|
|
|
let outStream
|
|
|
|
let hash
|
|
|
|
if (file._isChunk) {
|
|
|
|
if (!file._chunksData.stream) {
|
|
|
|
file._chunksData.stream = fs.createWriteStream(finalPath, { flags: 'a' })
|
|
|
|
file._chunksData.stream.on('error', onerror)
|
2020-06-01 01:44:48 +00:00
|
|
|
}
|
2020-10-30 18:12:09 +00:00
|
|
|
if (!file._chunksData.hasher) {
|
2020-06-15 16:14:33 +00:00
|
|
|
file._chunksData.hasher = blake3.createHash()
|
2020-10-30 18:12:09 +00:00
|
|
|
}
|
2020-06-15 16:14:33 +00:00
|
|
|
|
|
|
|
outStream = file._chunksData.stream
|
|
|
|
hash = file._chunksData.hasher
|
2020-06-01 01:44:48 +00:00
|
|
|
} else {
|
2020-06-15 16:14:33 +00:00
|
|
|
outStream = fs.createWriteStream(finalPath)
|
|
|
|
outStream.on('error', onerror)
|
|
|
|
hash = blake3.createHash()
|
2020-05-28 19:52:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 16:14:33 +00:00
|
|
|
file.stream.on('error', onerror)
|
|
|
|
file.stream.on('data', d => hash.update(d))
|
|
|
|
|
|
|
|
if (file._isChunk) {
|
|
|
|
file.stream.on('end', () => {
|
2022-04-15 09:41:05 +00:00
|
|
|
_cb(null, {
|
2020-06-15 16:14:33 +00:00
|
|
|
destination,
|
|
|
|
filename,
|
|
|
|
path: finalPath
|
2022-04-16 06:33:11 +00:00
|
|
|
})
|
2020-05-28 19:52:58 +00:00
|
|
|
})
|
2020-06-15 16:14:33 +00:00
|
|
|
file.stream.pipe(outStream, { end: false })
|
|
|
|
} else {
|
|
|
|
outStream.on('finish', () => {
|
2022-04-15 09:41:05 +00:00
|
|
|
_cb(null, {
|
2020-06-15 16:14:33 +00:00
|
|
|
destination,
|
|
|
|
filename,
|
|
|
|
path: finalPath,
|
|
|
|
size: outStream.bytesWritten,
|
|
|
|
hash: hash.digest('hex')
|
2022-04-15 09:41:05 +00:00
|
|
|
}, that.clamscan.passthrough ? 1 : 2)
|
2020-06-15 16:14:33 +00:00
|
|
|
})
|
2022-04-15 09:41:05 +00:00
|
|
|
|
|
|
|
if (that.clamscan.passthrough) {
|
|
|
|
logger.debug(`[ClamAV]: ${filename}: Passthrough scanning\u2026`)
|
|
|
|
const clamStream = that.clamscan.instance.passthrough()
|
|
|
|
clamStream.on('scan-complete', result => {
|
|
|
|
_cb(null, {
|
|
|
|
clamscan: result
|
2022-04-16 06:33:11 +00:00
|
|
|
}, 1)
|
2022-04-15 09:41:05 +00:00
|
|
|
})
|
|
|
|
file.stream.pipe(clamStream).pipe(outStream)
|
|
|
|
} else {
|
|
|
|
file.stream.pipe(outStream)
|
|
|
|
}
|
2020-06-15 16:14:33 +00:00
|
|
|
}
|
2020-05-28 19:52:58 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
DiskStorage.prototype._removeFile = function _removeFile (req, file, cb) {
|
|
|
|
const path = file.path
|
|
|
|
|
|
|
|
delete file.destination
|
|
|
|
delete file.filename
|
|
|
|
delete file.path
|
|
|
|
|
|
|
|
fs.unlink(path, cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function (opts) {
|
|
|
|
return new DiskStorage(opts)
|
|
|
|
}
|