webgpu-cluster / server /imageLoader.ts
apssouza22's picture
Deploy: refresh Space build (landing + docs)
9210bde verified
Raw
History Blame Contribute Delete
941 Bytes
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');
}