Linkedin plugin import post request

Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
Denis Bykhov 2022-01-25 19:19:36 +06:00
parent e848480ef3
commit bd63895ae8

View File

@ -220,6 +220,7 @@ export function start (config: { transactorEndpoint: string, elasticUrl: string,
}
})
// todo remove it after update all customers chrome extensions
app.get('/import', (req, res) => {
const authHeader = req.headers.authorization
if (authHeader === undefined) {
@ -298,6 +299,81 @@ export function start (config: { transactorEndpoint: string, elasticUrl: string,
})
})
app.post('/import', (req, res) => {
const authHeader = req.headers.authorization
if (authHeader === undefined) {
res.status(403).send()
return
}
const token = authHeader.split(' ')[1]
const payload = decode(token ?? '', 'secret', false) as Token
const { url, cookie, attachedTo, space } = req.body
console.log('importing from', url)
console.log('cookie', cookie)
const options = cookie !== undefined
? {
headers: {
Cookie: cookie
}
}
: {}
https.get(url, options, response => {
console.log('status', response.statusCode)
if (response.statusCode !== 200) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
res.status(500).send(`server returned ${response.statusCode}`)
return
}
const id = uuid()
const contentType = response.headers['content-type']
const meta: ItemBucketMetadata = {
'Content-Type': contentType
}
const data: Buffer[] = []
response.on('data', function (chunk) {
data.push(chunk)
}).on('end', function () {
const buffer = Buffer.concat(data)
// eslint-disable-next-line @typescript-eslint/no-misused-promises
config.minio.putObject(payload.workspace, id, buffer, 0, meta, async (err, objInfo) => {
if (err !== null) {
console.log('minio putObject error', err)
res.status(500).send(err)
} else {
console.log('uploaded uuid', id)
if (attachedTo !== undefined) {
const elastic = await createElasticAdapter(config.elasticUrl, payload.workspace)
const indexedDoc: IndexedDoc = {
id: id as Ref<Doc>,
_class: attachment.class.Attachment,
space,
modifiedOn: Date.now(),
modifiedBy: 'core:account:System' as Ref<Account>,
attachedTo,
data: buffer.toString('base64')
}
await elastic.index(indexedDoc)
}
res.status(200).send({
id,
contentType,
size: buffer.length
})
}
})
}).on('error', function (err) {
res.status(500).send(err)
})
})
})
app.get('*', function (request, response) {
response.sendFile(join(dist, 'index.html'))
})