( imageUrlBase: string | null, imageName: string, explicitUrl?: string )
| 90 | * @returns The fetched File or null if fetch failed |
| 91 | */ |
| 92 | export async function fetchUrlImage( |
| 93 | imageUrlBase: string | null, |
| 94 | imageName: string, |
| 95 | explicitUrl?: string |
| 96 | ): Promise<File | null> { |
| 97 | const cached = urlImageState.getCached(imageName); |
| 98 | if (cached) { |
| 99 | return cached; |
| 100 | } |
| 101 | |
| 102 | const resolved = resolveImageRequestUrl(imageUrlBase, imageName, explicitUrl); |
| 103 | if (!resolved) { |
| 104 | return null; |
| 105 | } |
| 106 | const { url: imageUrl, filename } = resolved; |
| 107 | |
| 108 | if (urlImageState.isRequestPending(imageUrl)) { |
| 109 | return urlImageState.waitForRequest(imageUrl); |
| 110 | } |
| 111 | |
| 112 | urlImageState.startRequest(imageUrl); |
| 113 | let result: File | null = null; |
| 114 | |
| 115 | try { |
| 116 | const response = await fetch(imageUrl); |
| 117 | if (!response.ok) { |
| 118 | appLogger.warn(`[URL Image] Failed to fetch ${imageName}: ${response.status}`); |
| 119 | return null; |
| 120 | } |
| 121 | |
| 122 | const blob = await response.blob(); |
| 123 | const file = await compressAndResizeToJpeg(blob, filename); |
| 124 | |
| 125 | urlImageState.setCached(imageName, file); |
| 126 | result = file; |
| 127 | |
| 128 | return file; |
| 129 | } catch (err) { |
| 130 | appLogger.warn(`[URL Image] Error fetching ${imageName}:`, err); |
| 131 | return null; |
| 132 | } finally { |
| 133 | urlImageState.completeRequest(imageUrl, result); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Fetch an image from URL without display-cache resizing or JPEG recompression. |
no test coverage detected