77 lines
2.3 KiB
TypeScript
77 lines
2.3 KiB
TypeScript
import { getBearerAuthHeaders } from './authHeaders';
|
|
|
|
export type ProjectAsset = {
|
|
asset_id: number;
|
|
filename: string;
|
|
sha256: string;
|
|
mime_type?: string;
|
|
size_bytes?: number;
|
|
project_id: number;
|
|
/** MinerU 解析状态:pending | running | done | failed | skipped */
|
|
parse_state?: string;
|
|
/** 解析失败原因(parse_state=failed 时有值) */
|
|
parse_error?: string;
|
|
/** 同内容已存在、物理仅存一份的去重标记 */
|
|
deduplicated?: boolean;
|
|
};
|
|
|
|
export async function uploadProjectAsset(
|
|
projectId: number,
|
|
file: File | Blob,
|
|
filename: string,
|
|
): Promise<ProjectAsset> {
|
|
const form = new FormData();
|
|
form.append('file', file, filename);
|
|
const res = await fetch(`/api/v1/projects/${projectId}/assets`, {
|
|
method: 'POST',
|
|
headers: getBearerAuthHeaders(),
|
|
body: form,
|
|
});
|
|
if (!res.ok) {
|
|
throw new Error(`upload asset failed: HTTP ${res.status}`);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
export async function listProjectAssets(projectId: number): Promise<ProjectAsset[]> {
|
|
const res = await fetch(`/api/v1/projects/${projectId}/assets`, {
|
|
headers: getBearerAuthHeaders(),
|
|
});
|
|
if (!res.ok) {
|
|
throw new Error(`list assets failed: HTTP ${res.status}`);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
/** 解析失败后重置并重触发 MinerU 解析。 */
|
|
export async function reparseProjectAsset(
|
|
projectId: number,
|
|
assetId: number,
|
|
): Promise<{ asset_id: number; parse_state: string }> {
|
|
const res = await fetch(`/api/v1/projects/${projectId}/assets/${assetId}/reparse`, {
|
|
method: 'POST',
|
|
headers: getBearerAuthHeaders(),
|
|
});
|
|
if (!res.ok) {
|
|
const body = await res.json().catch(() => null);
|
|
throw new Error(body?.error || `reparse asset failed: HTTP ${res.status}`);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
/** 构建鉴权后的资产原件 URL(供预览/下载回退)。 */
|
|
export function projectAssetRawURL(projectId: number, assetId: number): string {
|
|
return `/api/v1/projects/${projectId}/assets/${assetId}/raw`;
|
|
}
|
|
|
|
/** 拉取资产二进制为 Blob(带 Bearer)。 */
|
|
export async function fetchProjectAssetBlob(projectId: number, assetId: number): Promise<Blob> {
|
|
const res = await fetch(projectAssetRawURL(projectId, assetId), {
|
|
headers: getBearerAuthHeaders(),
|
|
});
|
|
if (!res.ok) {
|
|
throw new Error(`fetch asset raw failed: HTTP ${res.status}`);
|
|
}
|
|
return res.blob();
|
|
}
|