improve stability of encodeImageToBlurhash

This commit is contained in:
tmshv committed 2022-09-06 13:51:41 +03:00
1 parent 13c84f7520
commit f692f74553
1 file changed
+15 -5
+15 -5
View File
@@ -1,13 +1,23 @@
import sharp from "sharp" import sharp from "sharp"
export async function encodeImageToBlurhash(url: string) { async function fetchBuffer(url: string) {
const res = await fetch(url) try {
if (!res.ok) { const res = await fetch(url)
if (!res.ok) {
return undefined
}
const data = await res.arrayBuffer()
return Buffer.from(data)
} catch (error) {
return undefined return undefined
} }
}
const data = await res.arrayBuffer() export async function encodeImageToBlurhash(url: string) {
const buffer = Buffer.from(data) const buffer = await fetchBuffer(url)
if (!buffer) {
return undefined
}
const t = sharp(buffer) const t = sharp(buffer)
.raw() .raw()