Skip to content

Instantly share code, notes, and snippets.

@dohooo
Created October 28, 2023 15:51
Show Gist options
  • Save dohooo/a255370003f778128a862ed6564671c0 to your computer and use it in GitHub Desktop.
Save dohooo/a255370003f778128a862ed6564671c0 to your computer and use it in GitHub Desktop.
const images = [
'https://ipfs.xlog.app/ipfs/bafkreib2uqok2rtc6rufnt4icqexi5fi3feqty3t2ejbcy4folb5iikjqm',
'https://ipfs.xlog.app/ipfs/bafkreicviikvuogo4bfmgjwi7trtsgzd24cohee6wkaqr7c6hqgkz5pexi',
'https://ipfs.xlog.app/ipfs/bafkreia4vkm6huitzkc4dpvhm54madub4752wnv2tqdelulmawgohc7ndq',
'https://ipfs.xlog.app/ipfs/bafkreic223ab6snnkcms4wepmwfa5m4ged2gresrgajyrqykbygyqikxiy',
'https://ipfs.xlog.app/ipfs/bafkreihrcscwhxl3zaswj4pffmoq6d23iqebviihqswzwree7hk5s7qmly',
'https://ipfs.xlog.app/ipfs/bafkreihuid4goghknzt5d2yjqlzatxapmxjrxdb54hjtcksphdndanqhyq',
'https://ipfs.xlog.app/ipfs/bafkreicblcxjux32ofvi6sil3utix5g52yxfhxadds2otmy2mqzr7gqqui',
'https://ipfs.xlog.app/ipfs/bafkreihigfyb24qcbza3bigxbbmxq3izevzg5scy7dwybwtwzj4ubycuei'
]
interface ImageDimension {
height: number
width: number
}
async function testV1(uris: string[]) {
return Promise.all(
uris.map((uri) => getImageDimensionByUri(uri))
).then((res) => {
return res.reduce<Record<string, ImageDimension>>((acc, cur) => {
acc[cur.uri] = {
height: cur.height,
width: cur.width
}
return acc
}, {})
})
}
async function testV2(uris: string[]) {
return Promise
.all(uris.map((uri) => fetch(`${SITE_URL}/api/image?url=${encodeURIComponent(uri)}`)))
.then(async (res) => (
Promise.all(
res.map(async (res) => {
const { size } = await res.json() as {
size: {
width: number
height: number
}
}
return {
uri: res.url,
height: size.height,
width: size.width
}
})
)
))
.then(async (res) => {
return res.reduce<Record<string, ImageDimension>>((acc, cur) => {
acc[cur.uri] = {
height: cur.height,
width: cur.width
}
return acc
}, {})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment