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

Fix/dpp 125 #159

Merged
merged 10 commits into from
Feb 22, 2024
Merged

Fix/dpp 125 #159

merged 10 commits into from
Feb 22, 2024

Conversation

sksadjad
Copy link
Contributor

@sksadjad sksadjad commented Feb 9, 2024

fixed the problem with image-size. the version of image-size previously used was not react friendly

@codecov-commenter
Copy link

codecov-commenter commented Feb 9, 2024

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (ba38097) 45.89% compared to head (a59fdcf) 33.92%.
Report is 763 commits behind head on develop.

❗ Current head a59fdcf differs from pull request most recent head 11f870c. Consider uploading reports for the commit 11f870c to get more accurate results

Files Patch % Lines
packages/ssi-sdk-core/src/utils/image.ts 84.84% 5 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff              @@
##           develop     #159       +/-   ##
============================================
- Coverage    45.89%   33.92%   -11.98%     
============================================
  Files           65      151       +86     
  Lines         2412     7216     +4804     
  Branches       539     1698     +1159     
============================================
+ Hits          1107     2448     +1341     
- Misses        1305     4767     +3462     
- Partials         0        1        +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@nklomp nklomp left a comment

Choose a reason for hiding this comment

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

See remarks. Espacially the toString is too of a naive implementation when working with images

type?: string
}

const uint8ArrayToString = (uint8Array: Uint8Array): string => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use the uint8arrays package for that as we are using that in everywhere. It has a toString method with encoding support

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed to use that library


// TODO: here we're handling svg separately, remove this section when image-size starts supporting it in version 2
const isSvg = (uint8Array: Uint8Array): boolean => {
const text = uint8ArrayToString(uint8Array)
Copy link
Contributor

@nklomp nklomp Feb 10, 2024

Choose a reason for hiding this comment

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

Can't we detect this based in the array itself, or at least only take a few indices first. This probably has a massive hit on performance. This for instance will covert 10 MB images into text first, only to determine first whether it is a SVG or not from only the first few chars

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, changed the code to use only first bytes


const getSvgDimensions = (uint8Array: Uint8Array) => {
const svgContent = uint8ArrayToString(uint8Array)
const widthMatch = svgContent.match(/width="([^"]+)"/)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this work at all? For a simple SVG probably it might, but what about an SVG with all kinds of objects in it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed this method and wrote 2 additional tests for a multi-layered and a animated svg

const heightMatch = svgContent.match(/height="([^"]+)"/)
const viewBoxMatch = svgContent.match(/viewBox="([^"]+)"/)

let width, height
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are these not typed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed these and made everything typed

if ((!width || !height) && viewBoxMatch) {
const parts = viewBoxMatch[1].split(' ').map(Number)
if (!width && parts.length === 4) {
width = parts[2].toString()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why a tostring on something that is mapped to a number?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed this, width/height is a number now

}

export const getImageDimensions = async (base64: string): Promise<IImageDimensions> => {
const buffer: Buffer = Buffer.from(base64, 'base64')
const dimensions: ISizeCalculationResult = sizeOf(buffer)
const dimensions: SizeCalculationResult = isSvg(buffer) ? getSvgDimensions(buffer) : imageSize(buffer)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just create a getImageDimensions function, so other places can also reuse it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we already have getImageDimensions if the image is svg, we're calling a special method called getSvgDimensions which is not exported. Do you want this to be also exported?

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay then it does make sense to also add an uint8array next to the string for this function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

now it's accepting both base64 string and uint8array. also added explanation at the top of the method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also applied this to the getImageMediaType function. now it's also accepting uint8array type

}
const result: SizeCalculationResult = imageSize(buffer)
return `image/${result.type}`
}

export const getImageDimensions = async (base64: string): Promise<IImageDimensions> => {
const buffer: Buffer = Buffer.from(base64, 'base64')
Copy link
Contributor

@nklomp nklomp Feb 14, 2024

Choose a reason for hiding this comment

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

We avoid Buffer by default because of RN. Use U8Int Array which is compatible, simply use the u8a.fromString method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed this and one other place (in this file) that we're using buffer and used u8a.fromString(value, 'base64') instead.

@sksadjad sksadjad merged commit 225b2a3 into develop Feb 22, 2024
2 checks passed
@sksadjad sksadjad deleted the fix/DPP-125 branch February 22, 2024 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants