93 lines
2.8 KiB
TypeScript
93 lines
2.8 KiB
TypeScript
import {
|
|
httpDeleteInterceptor,
|
|
httpGetInterceptor,
|
|
httpPostFormDataInterceptor,
|
|
httpPostInterceptor,
|
|
} from "./http-config/http-interceptor-services";
|
|
export type MediaLibraryItem = {
|
|
id: number;
|
|
public_url: string;
|
|
object_key?: string | null;
|
|
original_filename?: string | null;
|
|
file_category: string;
|
|
size_bytes?: number | null;
|
|
source_type: string;
|
|
source_label?: string | null;
|
|
article_file_id?: number | null;
|
|
created_by_id: number;
|
|
created_at: string;
|
|
updated_at: string;
|
|
};
|
|
|
|
export type MediaLibraryListMeta = {
|
|
limit?: number;
|
|
page?: number;
|
|
count?: number;
|
|
totalPage?: number;
|
|
nextPage?: number;
|
|
previousPage?: number;
|
|
};
|
|
|
|
export async function getMediaLibrary(params: {
|
|
page?: number;
|
|
limit?: number;
|
|
q?: string;
|
|
source_type?: string;
|
|
}) {
|
|
const sp = new URLSearchParams();
|
|
if (params.page != null) sp.set("page", String(params.page));
|
|
if (params.limit != null) sp.set("limit", String(params.limit));
|
|
if (params.q?.trim()) sp.set("q", params.q.trim());
|
|
if (params.source_type?.trim()) sp.set("source_type", params.source_type.trim());
|
|
const q = sp.toString();
|
|
return await httpGetInterceptor(`/media-library${q ? `?${q}` : ""}`);
|
|
}
|
|
|
|
export function parseMediaLibraryList(res: unknown): {
|
|
items: MediaLibraryItem[];
|
|
meta: MediaLibraryListMeta | null;
|
|
} {
|
|
if (!res || typeof res !== "object") return { items: [], meta: null };
|
|
const r = res as { error?: boolean; data?: { data?: unknown; meta?: MediaLibraryListMeta } };
|
|
if (r.error || !r.data) return { items: [], meta: null };
|
|
const raw = r.data.data;
|
|
const items = Array.isArray(raw) ? (raw as MediaLibraryItem[]) : [];
|
|
return { items, meta: r.data.meta ?? null };
|
|
}
|
|
|
|
/** Multipart: field `file` */
|
|
export async function uploadMediaLibraryFile(formData: FormData) {
|
|
return await httpPostFormDataInterceptor("/media-library/upload", formData);
|
|
}
|
|
|
|
/** Reads `public_url` from upload API (`data.data.public_url` after interceptor wrap). */
|
|
export function parseMediaLibraryUploadPublicUrl(
|
|
interceptorResult: unknown,
|
|
): string | null {
|
|
if (!interceptorResult || typeof interceptorResult !== "object") return null;
|
|
const r = interceptorResult as {
|
|
error?: boolean;
|
|
data?: { data?: { public_url?: string } };
|
|
};
|
|
if (r.error) return null;
|
|
const url = r.data?.data?.public_url;
|
|
return typeof url === "string" && url.trim() ? url.trim() : null;
|
|
}
|
|
|
|
export async function registerMediaLibrary(body: {
|
|
public_url: string;
|
|
object_key?: string;
|
|
original_filename?: string;
|
|
file_category?: string;
|
|
size_bytes?: number;
|
|
source_type: string;
|
|
source_label?: string;
|
|
article_file_id?: number;
|
|
}) {
|
|
return await httpPostInterceptor("/media-library/register", body);
|
|
}
|
|
|
|
export async function deleteMediaLibraryItem(id: number) {
|
|
return await httpDeleteInterceptor(`/media-library/${id}`);
|
|
}
|