fix: uploads mimetype not properly set

This commit is contained in:
Bobby Wibowo 2022-07-12 15:26:53 +07:00
parent 61904ee1a7
commit c32f18a697
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -294,15 +294,24 @@ self.upload = async (req, res) => {
// Write the file into disk, and return an object containing the required file information // Write the file into disk, and return an object containing the required file information
const file = await new Promise((resolve, reject) => { const file = await new Promise((resolve, reject) => {
const finalPath = path.join(destination, filename)
// "weighted" resolve function, to be able to "await" multiple callbacks // "weighted" resolve function, to be able to "await" multiple callbacks
const REQUIRED_WEIGHT = 2 const REQUIRED_WEIGHT = 2
let tempObject = { originalname, extname } let _result = {
let tempWeight = 0 destination,
extname,
filename,
mimetype: field.mime_type,
originalname,
path: finalPath
}
let _weight = 0
const _resolve = (result = {}, weight = 2) => { const _resolve = (result = {}, weight = 2) => {
tempWeight += weight _weight += weight
tempObject = Object.assign(result, tempObject) _result = Object.assign(result, _result)
if (tempWeight >= REQUIRED_WEIGHT) { if (_weight >= REQUIRED_WEIGHT) {
resolve(tempObject) resolve(_result)
} }
} }
@ -314,8 +323,6 @@ self.upload = async (req, res) => {
reject(error) reject(error)
} }
const finalPath = path.join(destination, filename)
if (isChunk) { if (isChunk) {
if (!chunksData.stream) { if (!chunksData.stream) {
chunksData.stream = fs.createWriteStream(finalPath, { flags: 'a' }) chunksData.stream = fs.createWriteStream(finalPath, { flags: 'a' })
@ -343,31 +350,19 @@ self.upload = async (req, res) => {
field.file.stream.on('data', d => hash.update(d)) field.file.stream.on('data', d => hash.update(d))
if (isChunk) { if (isChunk) {
field.file.stream.on('end', () => { field.file.stream.on('end', () => _resolve())
_resolve({
destination,
filename,
path: finalPath
})
})
field.file.stream.pipe(outStream, { end: false }) field.file.stream.pipe(outStream, { end: false })
} else { } else {
outStream.on('finish', () => { outStream.on('finish', () => _resolve({
_resolve({ size: outStream.bytesWritten,
destination, hash: hash.digest('hex')
filename, }, scanStream ? 1 : 2)
path: finalPath, )
size: outStream.bytesWritten,
hash: hash.digest('hex')
}, scanStream ? 1 : 2)
})
if (scanStream) { if (scanStream) {
logger.debug(`[ClamAV]: ${filename}: Passthrough scanning\u2026`) logger.debug(`[ClamAV]: ${filename}: Passthrough scanning\u2026`)
scanStream.on('error', onerror) scanStream.on('error', onerror)
scanStream.on('scan-complete', scan => { scanStream.on('scan-complete', scan => _resolve({ scan }, 1))
_resolve({ scan }, 1)
})
field.file.stream.pipe(scanStream).pipe(outStream) field.file.stream.pipe(scanStream).pipe(outStream)
} else { } else {
field.file.stream.pipe(outStream) field.file.stream.pipe(outStream)