Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/DEVSU-2543-auto-image-sizing #414

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,6 @@ sequence by running this SQL command after the insert:
Otherwise, you will get a unique constraint error when inserting via the API because Sequelize will try use an id
that is now taken.

## Adding new image types

Steps to add a new image type:
1. Open file app/routes/report/images.js
2. Add new object to IMAGES_CONFIG. Pattern will be the regex for the image key being uploaded
3. Open file app/constants.js
4. Add new entry to VALID_IMAGE_KEY_PATTERN based on the IMAGES_CONFIG pattern

## Process Manager

The production installation of IPR is run & managed by [pm2](http://pm2.keymetrics.io/)
Expand Down
22 changes: 2 additions & 20 deletions app/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,6 @@ module.exports = {
'probeResults',
'proteinVariants',
],
VALID_IMAGE_KEY_PATTERN: `^${[
'mutSignature\\.(corPcors|barplot)\\.(dbs|indels|sbs)',
'subtypePlot\\.\\S+',
'(cnv|loh)\\.[12345]',
'cnvLoh.circos',
'mutationBurden\\.(barplot|density|legend)_(sv|snv|indel|snv_indel)\\.(primary|secondary|tertiary|quaternary)',
'circosSv\\.(genome|transcriptome)',
'expDensity\\.(histogram|violin\\.(tcga|gtex|cser|hartwig|pediatric))\\.\\S+',
'expression\\.(chart|legend|spearman\\.(tcga|gtex|cser|hartwig|target|pediatric|brca\\.(molecular|receptor)))',
'microbial\\.circos\\.(genome|transcriptome)',
'cibersort\\.(cd8_positive|combined)_t-cell_scatter',
'mixcr\\.circos_trb_vj_gene_usage',
'mixcr\\.dominance_vs_alpha_beta_t-cells_scatter',
'scpPlot',
'msi.scatter',
'pathwayAnalysis.legend',
'copyNumber(.*)',
].map((patt) => {
return `(${patt})`;
}).join('|')}$`,
REPORT_CREATE_BASE_URI: '/reports/create',
REPORT_UPDATE_BASE_URI: '/reports/update',
GERMLINE_CREATE_BASE_URI: '/germline/create',
Expand All @@ -57,4 +37,6 @@ module.exports = {
MASTER_REPORT_ACCESS: ['admin', 'manager'],
ALL_PROJECTS_ACCESS: ['admin', 'all projects access'],
UPDATE_METHODS: ['POST', 'PUT', 'DELETE'],
IMAGE_UPLOAD_LIMIT: 3000000,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be possible to make these valuables configurable at runtime, with this as the default?

IMAGE_SIZE_LIMIT: 20000, // in bytes
};
22 changes: 21 additions & 1 deletion app/libs/createReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require('path');
const db = require('../models');
const {uploadReportImage} = require('../routes/report/images');
const logger = require('../log');
const {GENE_LINKED_VARIANT_MODELS, KB_PIVOT_MAPPING} = require('../constants');
const {GENE_LINKED_VARIANT_MODELS, KB_PIVOT_MAPPING, IMAGE_UPLOAD_LIMIT} = require('../constants');
const {sanitizeHtml} = require('./helperFunctions');

const EXCLUDE_SECTIONS = new Set([
Expand Down Expand Up @@ -418,6 +418,26 @@ const createReport = async (data) => {
// Create report sections
try {
await createReportSections(report, data, transaction);

const result = await db.query(
`SELECT
SUM(pg_column_size("reports_image_data")) AS avg_size_bytes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: maybe call this total_bytes

FROM
"reports_image_data"
WHERE
"report_id" = :reportId`,
{
replacements: {reportId: report.id},
type: 'select',
transaction,
},
);

const totalImageSize = result[0].avg_size_bytes;
if (totalImageSize > IMAGE_UPLOAD_LIMIT) {
throw new Error(`Total image size exceeds ${IMAGE_UPLOAD_LIMIT / 1000000} megabytes`);
}

await transaction.commit();
return report;
} catch (error) {
Expand Down
44 changes: 37 additions & 7 deletions app/libs/image.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
const sharp = require('sharp');
const db = require('../models');
const {IMAGE_SIZE_LIMIT} = require('../constants');

/**
* Delete image from images table
*
* @param {string} ident - Ident of image to delete
* @param {boolean} force - Whether it is a hard delete or not
* @param {object} transaction - Transaction to run delete under
* @returns {Promise<object>} - Returns the newly updated image data
*/
const autoDownsize = async (image, size, format = 'png') => {
// Base case: If the image size is less than or equal to size, return the image
if (image.info.size <= size) {
return image;
}

// Process the image (resize and format) and update the size
const resizedImage = await sharp(image.data)
.resize(
Math.floor(image.info.width / 1.5),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we also make the 1.5 configurable?

Math.floor(image.info.height / 1.5),
{fit: 'inside', withoutEnlargement: true},
)
.toFormat(format.toLowerCase())
.toBuffer({resolveWithObject: true});

// Recursive call with the resized image
return autoDownsize(resizedImage, size, format);
};

/**
* Resize, reformat and return base64 representation of image.
Expand All @@ -12,13 +41,14 @@ const db = require('../models');
* @returns {Promise<string>} - Returns base64 representation of image
* @throws {Promise<Error>} - File doesn't exist, incorrect permissions, etc.
*/
const processImage = async (image, width, height, format = 'png') => {
const imageData = await sharp(image)
.resize(width, height, {fit: 'inside', withoutEnlargement: true})
const processImage = async (image, size = IMAGE_SIZE_LIMIT, format = 'png') => {
let imageData = await sharp(image)
.toFormat(format.toLowerCase())
.toBuffer();
.toBuffer({resolveWithObject: true});

imageData = await autoDownsize(imageData, size, format);

return imageData.toString('base64');
return imageData.data.toString('base64');
};

/**
Expand All @@ -37,11 +67,11 @@ const processImage = async (image, width, height, format = 'png') => {
*/
const uploadImage = async (image, options = {}) => {
const {
data, width, height, filename, format, type,
data, filename, format, type,
} = image;

// Resize image
const imageData = await processImage(data, width, height, format.replace('image/', ''));
const imageData = await processImage(data, IMAGE_SIZE_LIMIT, format.replace('image/', ''));

// Upload image data
return db.models.image.create({
Expand Down
9 changes: 0 additions & 9 deletions app/routes/report/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const {Op} = require('sequelize');
const db = require('../../../models');
const logger = require('../../../log');
const {uploadReportImage} = require('../images');
const {VALID_IMAGE_KEY_PATTERN} = require('../../../constants');

const router = express.Router({mergeParams: true});

Expand Down Expand Up @@ -62,19 +61,11 @@ router.route('/')
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message: 'No attached images to upload'}});
}

// Check for valid and duplicate keys
const keys = [];
const pattern = new RegExp(VALID_IMAGE_KEY_PATTERN);

for (let [key, value] of Object.entries(req.files)) {
key = key.trim();

// Check if key is valid
if (!pattern.test(key)) {
logger.error(`Invalid key: ${key}`);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message: `Invalid key: ${key}`}});
}

// Check if key is a duplicate
if (keys.includes(key) || Array.isArray(value)) {
logger.error(`Duplicate keys are not allowed. Duplicate key: ${key}`);
Expand Down
Loading
Loading