-
Notifications
You must be signed in to change notification settings - Fork 18
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
Fix/dpp 125 #159
Conversation
Codecov ReportAttention:
❗ 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. |
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.
See remarks. Espacially the toString is too of a naive implementation when working with images
type?: string | ||
} | ||
|
||
const uint8ArrayToString = (uint8Array: Uint8Array): string => { |
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.
Please use the uint8arrays package for that as we are using that in everywhere. It has a toString method with encoding support
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.
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) |
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.
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
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.
yeah, changed the code to use only first bytes
|
||
const getSvgDimensions = (uint8Array: Uint8Array) => { | ||
const svgContent = uint8ArrayToString(uint8Array) | ||
const widthMatch = svgContent.match(/width="([^"]+)"/) |
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.
Does this work at all? For a simple SVG probably it might, but what about an SVG with all kinds of objects in it?
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.
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 |
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.
Why are these not typed?
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.
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() |
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.
Why a tostring on something that is mapped to a number?
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.
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) |
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.
Just create a getImageDimensions function, so other places can also reuse it
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.
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?
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.
Okay then it does make sense to also add an uint8array next to the string for this function
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.
now it's accepting both base64 string and uint8array. also added explanation at the top of the method
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.
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') |
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.
We avoid Buffer by default because of RN. Use U8Int Array which is compatible, simply use the u8a.fromString method
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.
changed this and one other place (in this file) that we're using buffer and used u8a.fromString(value, 'base64')
instead.
… Uint8Array. plus some code style changes in the core package
# Conflicts: # packages/ssi-sdk-core/package.json # pnpm-lock.yaml
fixed the problem with image-size. the version of image-size previously used was not react friendly