40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
"use client";
|
|
|
|
import type { ChatMediaKind } from "@/data/schemas/chat";
|
|
import { loadChatRepository } from "@/data/repositories/chat_repository_loader";
|
|
import { Result, type Result as ResultT } from "@/utils/result";
|
|
|
|
import { localChatMediaRowToBlob } from "./chat_media_blob";
|
|
|
|
export interface ResolveCachedChatMediaBlobInput {
|
|
characterId: string;
|
|
messageId: string;
|
|
kind: ChatMediaKind;
|
|
}
|
|
|
|
export interface CacheRemoteChatMediaBlobInput
|
|
extends ResolveCachedChatMediaBlobInput {
|
|
remoteUrl: string;
|
|
}
|
|
|
|
export async function resolveCachedChatMediaBlob(
|
|
input: ResolveCachedChatMediaBlobInput,
|
|
): Promise<ResultT<Blob | null>> {
|
|
const repository = await loadChatRepository();
|
|
const result = await repository.getCachedMedia(input);
|
|
if (Result.isErr(result)) return result;
|
|
if (!result.data) return Result.ok(null);
|
|
return Result.ok(localChatMediaRowToBlob(result.data));
|
|
}
|
|
|
|
export async function cacheRemoteChatMediaBlob(
|
|
input: CacheRemoteChatMediaBlobInput,
|
|
): Promise<ResultT<Blob>> {
|
|
const repository = await loadChatRepository();
|
|
const result = await repository.cacheRemoteMedia(input);
|
|
if (Result.isErr(result)) return result;
|
|
const blob = localChatMediaRowToBlob(result.data);
|
|
if (!blob) return Result.err(new Error("Cached media has no readable bytes."));
|
|
return Result.ok(blob);
|
|
}
|