Handle non-string image content types
This commit is contained in:
1 parent
51438aa5db
commit
271f2d1fb5
2 files changed
+19
-3
No files matched your search
+8
-2
@@ -16,14 +16,20 @@ export async function downloadImage(url: string, userAgent: string): Promise<Dow
|
||||
maxRedirects: 5,
|
||||
});
|
||||
|
||||
const contentType = response.headers['content-type'] || 'image/jpeg';
|
||||
const contentType = normalizeContentType(response.headers['content-type']);
|
||||
|
||||
return {
|
||||
buffer: Buffer.from(response.data),
|
||||
mimeType: contentType.split(';')[0].trim(),
|
||||
mimeType: contentType,
|
||||
};
|
||||
}
|
||||
|
||||
/** Axios permits non-string header values, but media processing needs a MIME string. */
|
||||
export function normalizeContentType(contentType: unknown): string {
|
||||
if (typeof contentType !== 'string') return 'image/jpeg';
|
||||
return contentType.split(';', 1)[0].trim() || 'image/jpeg';
|
||||
}
|
||||
|
||||
export function bufferToBase64(buffer: Buffer): string {
|
||||
return buffer.toString('base64');
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user