fix: artwork cache never throws on body/disk failures
This commit is contained in:
@@ -22,20 +22,24 @@ export async function cacheArtwork(
|
||||
): Promise<string | null> {
|
||||
if (!url) return null
|
||||
const key = createHash('sha256').update(url).digest('hex')
|
||||
mkdirSync(artworkDir, { recursive: true })
|
||||
for (const ext of Object.values(EXT_BY_TYPE)) {
|
||||
if (existsSync(path.join(artworkDir, key + ext))) return key + ext
|
||||
}
|
||||
let res: Response
|
||||
try {
|
||||
res = await fetchImpl(url)
|
||||
mkdirSync(artworkDir, { recursive: true })
|
||||
for (const ext of Object.values(EXT_BY_TYPE)) {
|
||||
if (existsSync(path.join(artworkDir, key + ext))) return key + ext
|
||||
}
|
||||
let res: Response
|
||||
try {
|
||||
res = await fetchImpl(url)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
if (!res.ok) return null
|
||||
const type = (res.headers.get('content-type') ?? '').split(';')[0]?.trim() ?? ''
|
||||
const ext = EXT_BY_TYPE[type] ?? '.jpg'
|
||||
const buf = Buffer.from(await res.arrayBuffer())
|
||||
await writeFile(path.join(artworkDir, key + ext), buf)
|
||||
return key + ext
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
if (!res.ok) return null
|
||||
const type = (res.headers.get('content-type') ?? '').split(';')[0]?.trim() ?? ''
|
||||
const ext = EXT_BY_TYPE[type] ?? '.jpg'
|
||||
const buf = Buffer.from(await res.arrayBuffer())
|
||||
await writeFile(path.join(artworkDir, key + ext), buf)
|
||||
return key + ext
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user