You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
"use client";
|
|
|
|
export type GatewayMediaKind = "image" | "video" | "audio";
|
|
|
|
export type GatewayMediaUploadResult = {
|
|
url: string;
|
|
filename: string;
|
|
contentType: string;
|
|
};
|
|
|
|
type GatewayMediaUploadResponse = {
|
|
success?: boolean;
|
|
url?: string;
|
|
error?: string;
|
|
};
|
|
|
|
export async function uploadFileToGatewayMedia(
|
|
file: File,
|
|
kind: GatewayMediaKind
|
|
): Promise<GatewayMediaUploadResult> {
|
|
return uploadBlobToGatewayMedia(file, file.name || `canvas-${kind}`, file.type, kind);
|
|
}
|
|
|
|
export async function uploadBlobToGatewayMedia(
|
|
blob: Blob,
|
|
filename: string,
|
|
contentType: string,
|
|
kind: GatewayMediaKind
|
|
): Promise<GatewayMediaUploadResult> {
|
|
const formData = new FormData();
|
|
formData.append("file", blob, filename || `canvas-${kind}`);
|
|
|
|
const response = await fetch("/api/popi/media/upload", {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
|
|
const result = (await response.json().catch(() => null)) as GatewayMediaUploadResponse | null;
|
|
if (!response.ok || !result?.success || !result.url) {
|
|
throw new Error(result?.error || "Media upload failed");
|
|
}
|
|
|
|
return {
|
|
url: result.url,
|
|
filename: filename || `canvas-${kind}`,
|
|
contentType: contentType || blob.type || `${kind}/*`,
|
|
};
|
|
}
|
|
|