isimage fetch timeout

This commit is contained in:
keyan 2023-08-27 13:28:10 -05:00
parent 3e4c189a1d
commit fda7b1b140
1 changed files with 15 additions and 2 deletions

View File

@ -30,14 +30,27 @@ const createImageProxyUrl = url => {
return `${IMGPROXY_URL}${signature}${target}`
}
async function fetchWithTimeout (resource, { timeout = 1000, ...options } = {}) {
const controller = new AbortController()
const id = setTimeout(() => controller.abort(), timeout)
const response = await fetch(resource, {
...options,
signal: controller.signal
})
clearTimeout(id)
return response
}
const isImageURL = async url => {
// https://stackoverflow.com/a/68118683
try {
const res = await fetch(url, { method: 'HEAD' })
const res = await fetchWithTimeout(url, { method: 'HEAD' })
const buf = await res.blob()
return buf.type.startsWith('image/')
} catch (err) {
console.log(err)
console.log(url, err)
return false
}
}