From bd63895ae8f9750cc7e2f1198622005ebc66395c Mon Sep 17 00:00:00 2001 From: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> Date: Tue, 25 Jan 2022 19:19:36 +0600 Subject: [PATCH] Linkedin plugin import post request Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> --- server/front/src/app.ts | 76 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/server/front/src/app.ts b/server/front/src/app.ts index 882eaa2947..eb1322f63d 100644 --- a/server/front/src/app.ts +++ b/server/front/src/app.ts @@ -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, + _class: attachment.class.Attachment, + space, + modifiedOn: Date.now(), + modifiedBy: 'core:account:System' as Ref, + 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')) })