Spaces:
Running
Running
File size: 941 Bytes
9210bde 4bdf735 9210bde 4bdf735 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import type {DescribeRequestBody, DetectRequestBody} from './types.js';
export async function resolveImagePayload(
body: DetectRequestBody | DescribeRequestBody,
): Promise<{imageBase64: string; mimeType: string}> {
if (body.image_base64) {
const base64 = body.image_base64.replace(/^data:[^;]+;base64,/, '');
return {
imageBase64: base64,
mimeType: body.image_type ?? 'image/jpeg',
};
}
if (body.image_url) {
const response = await fetch(body.image_url);
if (!response.ok) {
throw new Error(`Failed to fetch image_url (${response.status})`);
}
const buffer = Buffer.from(await response.arrayBuffer());
const mimeType =
response.headers.get('content-type')?.split(';')[0]?.trim() ??
body.image_type ??
'image/jpeg';
return {
imageBase64: buffer.toString('base64'),
mimeType,
};
}
throw new Error('Provide image_url or image_base64');
}
|