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