isimage fetch timeout
This commit is contained in:
parent
3e4c189a1d
commit
fda7b1b140
|
@ -30,14 +30,27 @@ const createImageProxyUrl = url => {
|
||||||
return `${IMGPROXY_URL}${signature}${target}`
|
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 => {
|
const isImageURL = async url => {
|
||||||
// https://stackoverflow.com/a/68118683
|
// https://stackoverflow.com/a/68118683
|
||||||
try {
|
try {
|
||||||
const res = await fetch(url, { method: 'HEAD' })
|
const res = await fetchWithTimeout(url, { method: 'HEAD' })
|
||||||
const buf = await res.blob()
|
const buf = await res.blob()
|
||||||
return buf.type.startsWith('image/')
|
return buf.type.startsWith('image/')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err)
|
console.log(url, err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue