|
| 1 | +const HTTP_STATUS = require('http-status-codes'); |
| 2 | +const express = require('express'); |
| 3 | + |
| 4 | +const db = require('../../../models'); |
| 5 | +const logger = require('../../../log'); |
| 6 | +const {uploadLegendImage} = require('../images'); |
| 7 | + |
| 8 | +const router = express.Router({mergeParams: true}); |
| 9 | + |
| 10 | +// Middleware for legend image |
| 11 | +router.param('legend', async (req, res, next, imgIdent) => { |
| 12 | + let result; |
| 13 | + try { |
| 14 | + result = await db.models.legend.findOne({ |
| 15 | + where: {ident: imgIdent, reportId: req.report.id}, |
| 16 | + }); |
| 17 | + } catch (error) { |
| 18 | + logger.error(`Unable to lookup legend image error: ${error}`); |
| 19 | + return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({error: {message: 'Unable to lookup legend image'}}); |
| 20 | + } |
| 21 | + |
| 22 | + if (!result) { |
| 23 | + const message = `Unable to find legend image ${imgIdent} for report ${req.report.ident}`; |
| 24 | + logger.error(message); |
| 25 | + return res.status(HTTP_STATUS.NOT_FOUND).json({error: {message}}); |
| 26 | + } |
| 27 | + |
| 28 | + // Add legend data to request |
| 29 | + req.legend = result; |
| 30 | + return next(); |
| 31 | +}); |
| 32 | + |
| 33 | +// Routes for operating on specific legends |
| 34 | +// !!Should not add update routes for legend images (PUT, PATCH, etc.) |
| 35 | +// because updates will create duplicate image entries!! |
| 36 | +router.route('/:legend([A-z0-9-]{36})') |
| 37 | + .get((req, res) => { |
| 38 | + return res.json(req.legend.view('public')); |
| 39 | + }) |
| 40 | + .delete(async (req, res) => { |
| 41 | + // Whether to hard or soft delete legend |
| 42 | + const force = (typeof req.query.force === 'boolean') ? req.query.force : false; |
| 43 | + |
| 44 | + // Delete legend image |
| 45 | + try { |
| 46 | + await req.legend.destroy({force}); |
| 47 | + return res.status(HTTP_STATUS.NO_CONTENT).send(); |
| 48 | + } catch (error) { |
| 49 | + logger.error(`Error while deleting legend image ${error}`); |
| 50 | + return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({error: {message: 'Error while deleting legend image'}}); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | +// Route for adding a legend image |
| 55 | +router.route('/') |
| 56 | + .post(async (req, res) => { |
| 57 | + // Check that image files were uploaded |
| 58 | + if (!req.files || Object.keys(req.files).length === 0) { |
| 59 | + logger.error('No attached images to upload'); |
| 60 | + return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message: 'No attached images to upload'}}); |
| 61 | + } |
| 62 | + |
| 63 | + const versions = []; |
| 64 | + |
| 65 | + for (let [version, value] of Object.entries(req.files)) { |
| 66 | + version = version.trim(); |
| 67 | + |
| 68 | + // Check if version is a duplicate |
| 69 | + if (versions.includes(version) || Array.isArray(value)) { |
| 70 | + logger.error(`Duplicate versions are not allowed. Duplicate version: ${version}`); |
| 71 | + return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message: `Duplicate versions are not allowed. Duplicate version: ${version}`}}); |
| 72 | + } |
| 73 | + |
| 74 | + versions.push(version); |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + const results = await Promise.all(Object.entries(req.files).map(async ([version, image]) => { |
| 79 | + // Remove trailing space from version |
| 80 | + version = version.trim(); |
| 81 | + |
| 82 | + try { |
| 83 | + // Set options (value or undefined) |
| 84 | + const options = { |
| 85 | + filename: image.name.trim(), |
| 86 | + title: req.body[`${version}_title`], |
| 87 | + caption: req.body[`${version}_caption`], |
| 88 | + }; |
| 89 | + |
| 90 | + // Load image |
| 91 | + await uploadLegendImage(req.report.id, version, image.data, options); |
| 92 | + // Return that this image was uploaded successfully |
| 93 | + return {version, upload: 'successful'}; |
| 94 | + } catch (error) { |
| 95 | + return {version, upload: 'failed', error}; |
| 96 | + } |
| 97 | + })); |
| 98 | + return res.status(HTTP_STATUS.MULTI_STATUS).json(results); |
| 99 | + } catch (error) { |
| 100 | + logger.error(`Error while uploading images ${error}`); |
| 101 | + return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message: `Error while uploading images ${error}`}}); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | +// Route for getting a legend image |
| 106 | +router.route('/retrieve/:version') |
| 107 | + .get(async (req, res) => { |
| 108 | + const versions = (req.params.version.includes(',')) ? req.params.version.split(',') : [req.params.version]; |
| 109 | + |
| 110 | + try { |
| 111 | + const results = await db.models.imageData.scope('public').findAll({ |
| 112 | + where: { |
| 113 | + reportId: req.report.id, |
| 114 | + version: versions, |
| 115 | + }, |
| 116 | + order: [['version', 'ASC']], |
| 117 | + }); |
| 118 | + |
| 119 | + return res.json(results); |
| 120 | + } catch (error) { |
| 121 | + logger.error(`Error while getting report images with version: ${req.params.version} ${error}`); |
| 122 | + return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({ |
| 123 | + error: {message: 'Error while getting report images by version'}, |
| 124 | + }); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | +// Route for getting a list of legend versions for the report |
| 129 | +router.route('/versionlist') |
| 130 | + .get(async (req, res) => { |
| 131 | + try { |
| 132 | + const results = await db.models.legend.scope('versionlist').findAll({ |
| 133 | + where: { |
| 134 | + reportId: req.report.id, |
| 135 | + }, |
| 136 | + order: [['version', 'ASC']], |
| 137 | + }); |
| 138 | + |
| 139 | + return res.json(results); |
| 140 | + } catch (error) { |
| 141 | + logger.error(`Error while getting report legend versionlist: ${error}`); |
| 142 | + return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({ |
| 143 | + error: {message: 'Error while getting report legend versionlist'}, |
| 144 | + }); |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | +module.exports = router; |
0 commit comments