|
|
@ -4,6 +4,7 @@ export type GatewayMediaKind = "image" | "video" | "audio"; |
|
|
|
|
|
|
|
|
export type GatewayMediaUploadResult = { |
|
|
export type GatewayMediaUploadResult = { |
|
|
url: string; |
|
|
url: string; |
|
|
|
|
|
previewUrl?: string; |
|
|
filename: string; |
|
|
filename: string; |
|
|
contentType: string; |
|
|
contentType: string; |
|
|
}; |
|
|
}; |
|
|
@ -12,8 +13,38 @@ type GatewayMediaUploadResponse = { |
|
|
success?: boolean; |
|
|
success?: boolean; |
|
|
url?: string; |
|
|
url?: string; |
|
|
error?: string; |
|
|
error?: string; |
|
|
|
|
|
media?: GatewayMedia | GatewayMedia[] | GatewayMediaCollection; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
type GatewayMedia = { |
|
|
|
|
|
url?: string; |
|
|
|
|
|
thumbUrl?: string; |
|
|
|
|
|
coverThumb?: string; |
|
|
|
|
|
cover?: string; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
type GatewayMediaCollection = { |
|
|
|
|
|
list?: GatewayMedia[]; |
|
|
|
|
|
files?: GatewayMedia[]; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
function isGatewayMedia(value: GatewayMedia | GatewayMediaCollection): value is GatewayMedia { |
|
|
|
|
|
return "url" in value || "thumbUrl" in value || "coverThumb" in value || "cover" in value; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function firstMedia(media: GatewayMediaUploadResponse["media"]): GatewayMedia | undefined { |
|
|
|
|
|
if (!media) return undefined; |
|
|
|
|
|
if (Array.isArray(media)) return media.find(Boolean); |
|
|
|
|
|
if ("list" in media && Array.isArray(media.list)) return media.list.find(Boolean); |
|
|
|
|
|
if ("files" in media && Array.isArray(media.files)) return media.files.find(Boolean); |
|
|
|
|
|
return isGatewayMedia(media) ? media : undefined; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getPreviewUrl(result: GatewayMediaUploadResponse): string | undefined { |
|
|
|
|
|
const media = firstMedia(result.media); |
|
|
|
|
|
return media?.thumbUrl || media?.coverThumb || media?.cover || result.url || undefined; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
export async function uploadFileToGatewayMedia( |
|
|
export async function uploadFileToGatewayMedia( |
|
|
file: File, |
|
|
file: File, |
|
|
kind: GatewayMediaKind |
|
|
kind: GatewayMediaKind |
|
|
@ -42,6 +73,7 @@ export async function uploadBlobToGatewayMedia( |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
url: result.url, |
|
|
url: result.url, |
|
|
|
|
|
previewUrl: getPreviewUrl(result), |
|
|
filename: filename || `canvas-${kind}`, |
|
|
filename: filename || `canvas-${kind}`, |
|
|
contentType: contentType || blob.type || `${kind}/*`, |
|
|
contentType: contentType || blob.type || `${kind}/*`, |
|
|
}; |
|
|
}; |
|
|
|