import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' export type ResourcePhotoKind = 'VEHICLE' | 'DRIVER' export interface ResourcePhoto { fileId: string; fileName: string; contentType: string; sizeBytes: number } export interface ResourcePhotoDetail { recordVersion: string; photo: ResourcePhoto | null } const path = (kind: ResourcePhotoKind, id: string) => `/platform/v1/last-leg/fleet/resource-photos/${kind}/${encodeURIComponent(id)}` type Wire = { recordVersion: string | number; photo: (Omit & { sizeBytes: number | string }) | null } function normalize(value: Wire): ResourcePhotoDetail { return { recordVersion: String(value.recordVersion), photo: value.photo ? { ...value.photo, sizeBytes: Number(value.photo.sizeBytes) } : null } } export async function getResourcePhoto(kind: ResourcePhotoKind, id: string) { return normalize(unwrapApiResponse(await http.get, ApiEnvelope>(path(kind, id)))) } export async function uploadResourcePhoto(kind: ResourcePhotoKind, id: string, version: string, file: File) { const body = new FormData(); body.append('recordVersion', version); body.append('file', file) return normalize(unwrapApiResponse(await http.post, ApiEnvelope>(path(kind, id), body))) } export async function downloadResourcePhoto(kind: ResourcePhotoKind, id: string, fileId: string) { const blob = await http.get(`${path(kind, id)}/${encodeURIComponent(fileId)}/content`, { responseType: 'blob' }) if (!(blob instanceof Blob) || !['image/jpeg', 'image/png'].includes(blob.type) || !blob.size || blob.size > 10 * 1024 * 1024) throw new Error('档案照片读取失败,请重新核对') return blob }