-
Notifications
You must be signed in to change notification settings - Fork 0
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
Nithriel
wants to merge
8
commits into
develop
Choose a base branch
from
feature/DEVSU-2543-auto-image-sizing
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4c45be1
Add auto image resizing
Nithriel 48f5df9
Merge branch 'develop' into feature/DEVSU-2543-auto-image-sizing
Nithriel 8470f06
Add a max allowed limit of images
Nithriel aa37d72
Add size control
Nithriel 9f16fa5
Remove maxitems
Nithriel 120305e
Remove 80 images limit
Nithriel 9cae984
Rollback
Nithriel e64c3df
Fix image dependant sections
Nithriel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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([ | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
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 |
---|---|---|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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'); | ||
}; | ||
|
||
/** | ||
|
@@ -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({ | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?