-
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
Changes from 5 commits
308bad7
1a0e080
d7823eb
c86188e
9bc0685
608c29f
c420a2d
11f870c
67642a1
5cb8b6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,61 @@ | ||
import fetch from 'cross-fetch' | ||
import sizeOf from 'image-size' | ||
import { ISizeCalculationResult } from 'image-size/dist/types/interface' | ||
import { imageSize } from 'image-size' | ||
import { IImageDimensions, IImageResource } from '../types' | ||
import * as u8a from 'uint8arrays' | ||
|
||
type SizeCalculationResult = { | ||
width?: number | ||
height?: number | ||
orientation?: number | ||
type?: string | ||
} | ||
|
||
// 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 maxCheckLength: number = Math.min(80, uint8Array.length) | ||
const initialText: string = u8a.toString(uint8Array.subarray(0, maxCheckLength)) | ||
const normalizedText: string = initialText.trim().toLowerCase() | ||
return normalizedText.startsWith('<svg') || normalizedText.startsWith('<?xml') | ||
} | ||
|
||
function parseDimension(dimension: string): number | undefined { | ||
const match: RegExpMatchArray | null = dimension.match(/^(\d+(?:\.\d+)?)([a-z%]*)$/) | ||
return match ? parseFloat(match[1]) : 0 | ||
} | ||
|
||
const getSvgDimensions = (uint8Array: Uint8Array): SizeCalculationResult => { | ||
const svgContent: string = new TextDecoder().decode(uint8Array) | ||
const widthMatch: RegExpMatchArray | null = svgContent.match(/width="([^"]+)"/) | ||
const heightMatch: RegExpMatchArray | null = svgContent.match(/height="([^"]+)"/) | ||
const viewBoxMatch: RegExpMatchArray | null = svgContent.match(/viewBox="[^"]*"/) | ||
|
||
let width: number | undefined = widthMatch ? parseDimension(widthMatch[1]) : undefined | ||
let height: number | undefined = heightMatch ? parseDimension(heightMatch[1]) : undefined | ||
|
||
if (viewBoxMatch && (!width || !height)) { | ||
const parts = viewBoxMatch[0].match(/[\d\.]+/g)?.map(Number) | ||
if (parts && parts.length === 4) { | ||
const [x, y, viewBoxWidth, viewBoxHeight] = parts | ||
width = width ?? viewBoxWidth - x | ||
height = height ?? viewBoxHeight - y | ||
} | ||
} | ||
|
||
return { width, height, type: 'svg' } | ||
} | ||
|
||
export const getImageMediaType = async (base64: string): Promise<string | undefined> => { | ||
const buffer: Buffer = Buffer.from(base64, 'base64') | ||
const result: ISizeCalculationResult = sizeOf(buffer) | ||
|
||
switch (result.type) { | ||
case undefined: | ||
return undefined | ||
case 'svg': | ||
return `image/${result.type}+xml` | ||
default: | ||
return `image/${result.type}` | ||
if (isSvg(buffer)) { | ||
return `image/svg+xml` | ||
} | ||
const result: SizeCalculationResult = imageSize(buffer) | ||
return `image/${result.type}` | ||
} | ||
|
||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. we already have 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. 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. also applied this to the |
||
|
||
if (!dimensions.width || !dimensions.height) { | ||
return Promise.reject(Error('Unable to get image dimensions')) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.