Skip to content

Commit

Permalink
feat: remove getImageDimensionsandType
Browse files Browse the repository at this point in the history
Signed-off-by: Jeroen Branje <[email protected]>
  • Loading branch information
jeroenbranje committed Dec 2, 2024
1 parent 8e34374 commit d542ac5
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 51 deletions.
6 changes: 2 additions & 4 deletions apps/envited.ascs.digital/common/asset/createTokenMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export const createTokenMetadata = ({
cid: string
fileSize: number
uri: string
dimensions: string
mimeType: string
}
domainMetadata: {
cid: string
Expand Down Expand Up @@ -85,9 +83,9 @@ export const createTokenMetadata = ({
{
uri: formatIpfsUri(display.cid),
hash: display.cid,
mimeType: display.mimeType,
mimeType: 'image/png',
dimensions: {
value: display.dimensions,
value: '1095x850',
unit: 'px',
},
fileSize: display.fileSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('common/asset/validateAndCreateMetadata', () => {
createFilename: createFilenameStub,
getFilesAsPathAndByteArrayFromManifest: getFilesAsPathAndByteArrayFromManifestStub,
getAllFilenamesFromFiles: getAllFilenamesFromFilesStub,
getImageDimensionsAndType: getImageDimensionsAndTypeStub,
// getImageDimensionsAndType: getImageDimensionsAndTypeStub,
db: dbStub,
})(byteArray as any, asset as any)

Expand Down Expand Up @@ -141,8 +141,6 @@ describe('common/asset/validateAndCreateMetadata', () => {
cid: 'DISPLAY_HASH',
uri: 'https://assets.envited-x.net/HASH/PATH',
fileSize: undefined,
dimensions: '1095x850',
mimeType: 'image/png',
},
minter: 'ISSUER_ID',
creator: 'NAME',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
getDomainMetadataPath,
getFileFromByteArray,
getFilesAsPathAndByteArrayFromManifest,
getImageDimensionsAndType,
} from './validateAndCreateMetadata.utils'

export const _getShaclSchemaAndValidate =
Expand Down Expand Up @@ -167,7 +166,6 @@ export const _validateAndCreateMetadata =
createFilename,
getFilesAsPathAndByteArrayFromManifest,
getAllFilenamesFromFiles,
getImageDimensionsAndType,
db,
}: {
getShaclSchemaAndValidate: (byteArray: Uint8Array) => Promise<
Expand Down Expand Up @@ -196,8 +194,6 @@ export const _validateAndCreateMetadata =
cid: string
fileSize: number
uri: string
dimensions: string
mimeType: string
}
domainMetadata: {
cid: string
Expand Down Expand Up @@ -229,11 +225,6 @@ export const _validateAndCreateMetadata =
getAllFilenamesFromFiles: (
extractedFiles: { path: string; type: string; arrayBuffer: ArrayBuffer }[],
) => Promise<ExtractedFileWithCID[]>
getImageDimensionsAndType: (arrayBuffer: ArrayBuffer) => Promise<{
type: string
width: number
height: number
}>
db: Database
}) =>
async (byteArray: Uint8Array, asset: Asset) => {
Expand Down Expand Up @@ -268,13 +259,11 @@ export const _validateAndCreateMetadata =
}

const displayUri = find(propEq('visualization', 'type'))(visualizationFiles) as ExtractedFileWithCID
const displayUriInfo = await getImageDimensionsAndType(displayUri.arrayBuffer)
const displayObject = {
cid: displayUri.cid,
fileSize: displayUri.arrayBuffer.byteLength,
uri: `${formatAssetUri(assetCID)}/${displayUri.path}`,
dimensions: `${displayUriInfo.width}x${displayUriInfo.height}`,
mimeType: displayUriInfo.type,
// add image dimensions
}

const manifestObject = {
Expand Down Expand Up @@ -326,6 +315,5 @@ export const validateAndCreateMetadata = _validateAndCreateMetadata({
createFilename,
getFilesAsPathAndByteArrayFromManifest,
getAllFilenamesFromFiles,
getImageDimensionsAndType,
db,
})
Original file line number Diff line number Diff line change
Expand Up @@ -198,34 +198,3 @@ export const _getFilesAsPathAndByteArrayFromManifest =
export const getFilesAsPathAndByteArrayFromManifest = _getFilesAsPathAndByteArrayFromManifest({
getPathsAndBuffersFromByteArray,
})

export const loadImageFromUrl = (
url: string,
): Promise<{
width: number
height: number
}> =>
new Promise((resolve, reject) => {
const img = new Image()
img.onload = () => {
resolve({ width: img.width, height: img.height })
URL.revokeObjectURL(url)
}
img.onerror = () => {
URL.revokeObjectURL(url)
reject(new Error('Failed to load image'))
}
img.src = url
})

export const getImageDimensionsAndType = async (arrayBuffer: ArrayBuffer) => {
const blob = new Blob([arrayBuffer])
const url = URL.createObjectURL(blob)

const dimensions = await loadImageFromUrl(url)

return {
...dimensions,
type: blob.type || 'unknown',
}
}

0 comments on commit d542ac5

Please sign in to comment.