-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
144 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import debug from 'debug'; | ||
import { nanoid } from 'nanoid'; | ||
import { Router } from 'express'; | ||
|
||
import { sendToExchangeTemplate } from '../../../services/upload'; | ||
import { APIResponse } from '../../../services/express'; | ||
|
||
const logger = debug('features:templates:controller'); | ||
const route = Router(); | ||
|
||
route.post('/image/upload', async (req, res) => { | ||
const transactionApiId = nanoid(); | ||
|
||
try { | ||
const { mimetype, transactionId, metadata, name, userId } = req.body; | ||
|
||
const extension = mimetype.split('/')[1]; | ||
const path = `${name}.${extension}`; | ||
|
||
await sendToExchangeTemplate( | ||
JSON.stringify({ | ||
path, | ||
userId, | ||
transactionId, | ||
metadata, | ||
}), | ||
'image' | ||
); | ||
|
||
res.json({ | ||
code: 'vitruveo.studio.api.templates.image.upload.success', | ||
message: 'Template image upload success', | ||
transaction: transactionApiId, | ||
} as APIResponse<string>); | ||
} catch (error) { | ||
logger('Template image upload failed: %O', error); | ||
res.status(500).json({ | ||
code: 'vitruveo.studio.api.templates.image.upload.failed', | ||
message: `Template image upload failed: ${error}`, | ||
args: error, | ||
transaction: transactionApiId, | ||
} as APIResponse); | ||
} | ||
}); | ||
|
||
route.post('/json/upload', async (req, res) => { | ||
try { | ||
await sendToExchangeTemplate(JSON.stringify(req.body), 'json'); | ||
|
||
res.json({ | ||
code: 'vitruveo.studio.api.templates.json.upload.success', | ||
message: 'Template json upload success', | ||
transaction: nanoid(), | ||
} as APIResponse<string>); | ||
} catch (error) { | ||
logger('Template json upload failed: %O', error); | ||
res.status(500).json({ | ||
code: 'vitruveo.studio.api.templates.json.upload.failed', | ||
message: `Template json upload failed: ${error}`, | ||
args: error, | ||
transaction: nanoid(), | ||
} as APIResponse); | ||
} | ||
}); | ||
|
||
export { route }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Router } from 'express'; | ||
import { route as coreRouter } from './core'; | ||
|
||
const router = Router(); | ||
|
||
router.use('/templates', coreRouter); | ||
|
||
export { router }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as controller from './controller'; | ||
|
||
export { controller }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import debug from 'debug'; | ||
|
||
import { getChannel, Channel } from '../rabbitmq'; | ||
import { captureException } from '../sentry'; | ||
import { RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES } from '../../constants'; | ||
|
||
const logger = debug('features:creators:upload:queue'); | ||
|
||
const status: { | ||
channel: Channel | null; | ||
} = { | ||
channel: null, | ||
}; | ||
|
||
export const sendToExchangeTemplate = async ( | ||
message: string, | ||
routingKey: string | ||
) => { | ||
try { | ||
if (!status.channel) { | ||
status.channel = await getChannel(); | ||
logger( | ||
'Asserting exchange: %s', | ||
RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES | ||
); | ||
status.channel.assertExchange( | ||
RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES, | ||
'topic', | ||
{ | ||
durable: true, | ||
} | ||
); | ||
} | ||
logger('Sending to template exchange', { | ||
message, | ||
routingKey, | ||
exchange: RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES, | ||
}); | ||
status.channel.publish( | ||
RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES, | ||
routingKey, | ||
Buffer.from(message) | ||
); | ||
} catch (error) { | ||
logger('Error sending to exchange: %O', { | ||
error, | ||
message, | ||
routingKey, | ||
exchange: RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES, | ||
}); | ||
captureException(error, { | ||
extra: { | ||
message, | ||
routingKey, | ||
exchange: RABBITMQ_EXCHANGE_ARTCARDS_TEMPLATES, | ||
}, | ||
tags: { scope: 'sendToExchangeTemplate' }, | ||
}); | ||
} | ||
}; |