444d2ca7dd
* Revert "Revert "feat(structured logging): implements structured logging for backend (#1217)" (#1227)"
This reverts commit 63e6581162.
* Use pino-http instead of express pino logger
* Use correct reference to knex and do not instantiate HttpLogger prematurely
* Adds missing dependency for pino to webhook-service
* Do not instantiate middleware when passed to express
* Refactor to move logging into shared
* Copy shared packages into dockerfiles
* Build shared workspace in docker build-stage for fileimport & webhook
103 lines
2.8 KiB
JavaScript
103 lines
2.8 KiB
JavaScript
'use strict'
|
|
const zlib = require('zlib')
|
|
const cors = require('cors')
|
|
|
|
const { validatePermissionsReadStream } = require('./authUtils')
|
|
|
|
const { getObject, getObjectChildrenStream } = require('../services/objects')
|
|
const { SpeckleObjectsStream } = require('./speckleObjectsStream')
|
|
const { pipeline, PassThrough } = require('stream')
|
|
const { logger } = require('@/logging/logging')
|
|
module.exports = (app) => {
|
|
app.options('/objects/:streamId/:objectId', cors())
|
|
|
|
app.get('/objects/:streamId/:objectId', cors(), async (req, res) => {
|
|
const hasStreamAccess = await validatePermissionsReadStream(
|
|
req.params.streamId,
|
|
req
|
|
)
|
|
if (!hasStreamAccess.result) {
|
|
return res.status(hasStreamAccess.status).end()
|
|
}
|
|
|
|
// Populate first object (the "commit")
|
|
const obj = await getObject({
|
|
streamId: req.params.streamId,
|
|
objectId: req.params.objectId
|
|
})
|
|
|
|
if (!obj) {
|
|
return res.status(404).send('Failed to find object.')
|
|
}
|
|
|
|
const simpleText = req.headers.accept === 'text/plain'
|
|
|
|
res.writeHead(200, {
|
|
'Content-Encoding': 'gzip',
|
|
'Content-Type': simpleText ? 'text/plain; charset=UTF-8' : 'application/json'
|
|
})
|
|
|
|
const dbStream = await getObjectChildrenStream({
|
|
streamId: req.params.streamId,
|
|
objectId: req.params.objectId
|
|
})
|
|
const speckleObjStream = new SpeckleObjectsStream(simpleText)
|
|
const gzipStream = zlib.createGzip()
|
|
|
|
speckleObjStream.write(obj)
|
|
|
|
pipeline(
|
|
dbStream,
|
|
speckleObjStream,
|
|
gzipStream,
|
|
new PassThrough({ highWaterMark: 16384 * 31 }),
|
|
res,
|
|
(err) => {
|
|
if (err) {
|
|
logger.error(
|
|
`[User ${req.context.userId || '-'}] Error downloading object ${
|
|
req.params.objectId
|
|
} from stream ${req.params.streamId}: ${err}`
|
|
)
|
|
} else {
|
|
logger.info(
|
|
`[User ${req.context.userId || '-'}] Downloaded object ${
|
|
req.params.objectId
|
|
} from stream ${req.params.streamId} (size: ${
|
|
gzipStream.bytesWritten / 1000000
|
|
} MB)`
|
|
)
|
|
}
|
|
}
|
|
)
|
|
})
|
|
|
|
app.options('/objects/:streamId/:objectId/single', cors())
|
|
app.get('/objects/:streamId/:objectId/single', cors(), async (req, res) => {
|
|
const hasStreamAccess = await validatePermissionsReadStream(
|
|
req.params.streamId,
|
|
req
|
|
)
|
|
if (!hasStreamAccess.result) {
|
|
return res.status(hasStreamAccess.status).end()
|
|
}
|
|
|
|
const obj = await getObject({
|
|
streamId: req.params.streamId,
|
|
objectId: req.params.objectId
|
|
})
|
|
|
|
if (!obj) {
|
|
return res.status(404).send('Failed to find object.')
|
|
}
|
|
|
|
logger.info(
|
|
`[User ${req.context.userId || '-'}] Downloaded single object ${
|
|
req.params.objectId
|
|
} from stream ${req.params.streamId}`
|
|
)
|
|
|
|
res.send(obj.data)
|
|
})
|
|
}
|