( buffer: Buffer, originalUrl?: string, contentType?: string, )
| 130 | |
| 131 | // Process image with Sharp (resize to 30x30 PNG) |
| 132 | async function processImage( |
| 133 | buffer: Buffer, |
| 134 | originalUrl?: string, |
| 135 | contentType?: string, |
| 136 | ): Promise<Buffer> { |
| 137 | // If it's an ICO file, just return it as-is (no conversion needed) |
| 138 | if (originalUrl && isIcoFile(originalUrl, contentType)) { |
| 139 | logger.debug( |
| 140 | { originalUrl, bufferSize: buffer.length }, |
| 141 | 'Serving ICO file directly', |
| 142 | ); |
| 143 | return buffer; |
| 144 | } |
| 145 | |
| 146 | if (originalUrl && isSvgFile(originalUrl, contentType)) { |
| 147 | logger.debug( |
| 148 | { originalUrl, bufferSize: buffer.length }, |
| 149 | 'Serving SVG file directly', |
| 150 | ); |
| 151 | return buffer; |
| 152 | } |
| 153 | |
| 154 | // If buffer isnt to big just return it as well |
| 155 | if (buffer.length < 5000) { |
| 156 | logger.debug( |
| 157 | { originalUrl, bufferSize: buffer.length }, |
| 158 | 'Serving image directly without processing', |
| 159 | ); |
| 160 | return buffer; |
| 161 | } |
| 162 | |
| 163 | try { |
| 164 | // For other formats, process with Sharp |
| 165 | return await sharp(buffer) |
| 166 | .resize(30, 30, { |
| 167 | fit: 'cover', |
| 168 | }) |
| 169 | .png() |
| 170 | .toBuffer(); |
| 171 | } catch (error) { |
| 172 | logger.warn( |
| 173 | { |
| 174 | err: error, |
| 175 | originalUrl, |
| 176 | bufferSize: buffer.length, |
| 177 | }, |
| 178 | 'Sharp failed to process image, trying fallback', |
| 179 | ); |
| 180 | |
| 181 | throw error; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Process OG image with Sharp (resize to 300px width) |
| 186 | async function processOgImage( |
no test coverage detected