import { axiosInstance } from '@/api/rbac' import { assertBusinessOk, omsUrl, unwrapData } from '@/api/oms/common' export type FileStorageFolder = | 'avatars' | 'sku' | 'parcel' | 'recharge' | 'order-files' | 'order-templates' | 'xhs' | 'prints' | 'exports' | 'misc' export type FileStorageCapabilities = { supportsClientDirectUpload: boolean provider?: string } export type PresignUploadResult = { key: string uploadUrl: string method: string contentType?: string | null headers?: Record expireAt?: string } export type AccessUrlResult = { key: string url: string } function pickCaps(raw: any): FileStorageCapabilities { return { supportsClientDirectUpload: Boolean( raw?.supportsClientDirectUpload ?? raw?.SupportsClientDirectUpload, ), provider: raw?.provider ?? raw?.Provider, } } function pickPresign(raw: any): PresignUploadResult { const headers = raw?.headers ?? raw?.Headers return { key: String(raw?.key ?? raw?.Key ?? ''), uploadUrl: String(raw?.uploadUrl ?? raw?.UploadUrl ?? ''), method: String(raw?.method ?? raw?.Method ?? 'PUT'), contentType: raw?.contentType ?? raw?.ContentType ?? null, headers: headers && typeof headers === 'object' && !Array.isArray(headers) ? (headers as Record) : undefined, expireAt: raw?.expireAt ?? raw?.ExpireAt, } } /** GET /file-storage/capabilities */ export async function getFileStorageCapabilities(): Promise { const res = await axiosInstance.get(omsUrl('/file-storage/capabilities')) const body = res.data if (body && typeof body === 'object' && 'supportsClientDirectUpload' in body) { return pickCaps(body) } return pickCaps(unwrapData(body)) } /** POST /file-storage/presign-upload */ export async function presignFileUpload(input: { folder: FileStorageFolder | string fileName: string contentType?: string }): Promise { const res = await axiosInstance.post(omsUrl('/file-storage/presign-upload'), { folder: input.folder, fileName: input.fileName, contentType: input.contentType, }) const raw = unwrapData(res.data) const ticket = pickPresign(raw) if (!ticket.key || !ticket.uploadUrl) { throw new Error('预签名上传凭证不完整') } return ticket } /** GET /file-storage/access-url?key= */ export async function getFileAccessUrl(key: string): Promise { const res = await axiosInstance.get(omsUrl('/file-storage/access-url'), { params: { key }, }) const raw = unwrapData(res.data) const url = String(raw?.url ?? raw?.Url ?? '') const storageKey = String(raw?.key ?? raw?.Key ?? key) if (!url) throw new Error('未返回访问地址') return { key: storageKey, url } } /** GET /file-storage/stream?key= — 鉴权流式读取(面单预览等) */ export async function streamFileBlob(keyOrUrl: string): Promise { const res = await axiosInstance.get(omsUrl('/file-storage/stream'), { params: { key: keyOrUrl }, responseType: 'arraybuffer', timeout: 120000, headers: { Accept: '*/*' }, }) const type = String(res.headers['content-type'] || '') const bytes = new Uint8Array(res.data as ArrayBuffer) const sample = new TextDecoder('utf-8', { fatal: false }).decode(bytes.slice(0, 256)).trim() if (type.toLowerCase().includes('json') || sample.startsWith('{') || sample.startsWith('[')) { try { const body = JSON.parse(new TextDecoder().decode(bytes)) const msg = body?.msg ?? body?.message ?? body?.Message throw new Error(msg ? String(msg) : '文件流读取失败') } catch (e) { if (e instanceof Error && e.message !== '文件流读取失败') throw e throw new Error(sample || '文件流读取失败') } } if (sample.startsWith(' { const res = await axiosInstance.post(omsUrl('/file-storage/normalize-key'), { pathOrUrl, }) assertBusinessOk(res.data) const raw = unwrapData(res.data) const key = raw?.key ?? raw?.Key return key != null && String(key).trim() ? String(key).trim() : null }