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
+13 -3
+13 -3
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) {
try {
const res = await fetch(url) const res = await fetch(url)
if (!res.ok) { if (!res.ok) {
return undefined return undefined
} }
const data = await res.arrayBuffer() const data = await res.arrayBuffer()
const buffer = Buffer.from(data) return Buffer.from(data)
} catch (error) {
return undefined
}
}
export async function encodeImageToBlurhash(url: string) {
const buffer = await fetchBuffer(url)
if (!buffer) {
return undefined
}
const t = sharp(buffer) const t = sharp(buffer)
.raw() .raw()