All checks were successful
Build and Deploy Front / deploy (push) Successful in 7m24s
Co-authored-by: Cursor <cursoragent@cursor.com>
137 lines
4.4 KiB
TypeScript
137 lines
4.4 KiB
TypeScript
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<string, string>
|
|
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<string, string>)
|
|
: undefined,
|
|
expireAt: raw?.expireAt ?? raw?.ExpireAt,
|
|
}
|
|
}
|
|
|
|
/** GET /file-storage/capabilities */
|
|
export async function getFileStorageCapabilities(): Promise<FileStorageCapabilities> {
|
|
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<PresignUploadResult> {
|
|
const res = await axiosInstance.post(omsUrl('/file-storage/presign-upload'), {
|
|
folder: input.folder,
|
|
fileName: input.fileName,
|
|
contentType: input.contentType,
|
|
})
|
|
const raw = unwrapData<any>(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<AccessUrlResult> {
|
|
const res = await axiosInstance.get(omsUrl('/file-storage/access-url'), {
|
|
params: { key },
|
|
})
|
|
const raw = unwrapData<any>(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<Blob> {
|
|
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('<!DOCTYPE') || sample.startsWith('<html') || sample.startsWith('<')) {
|
|
throw new Error('文件流读取失败:返回了网页而非文件')
|
|
}
|
|
return new Blob([bytes], { type: type || 'application/octet-stream' })
|
|
}
|
|
|
|
/** POST /file-storage/normalize-key */
|
|
export async function normalizeFileStorageKey(pathOrUrl: string): Promise<string | null> {
|
|
const res = await axiosInstance.post(omsUrl('/file-storage/normalize-key'), {
|
|
pathOrUrl,
|
|
})
|
|
assertBusinessOk(res.data)
|
|
const raw = unwrapData<any>(res.data)
|
|
const key = raw?.key ?? raw?.Key
|
|
return key != null && String(key).trim() ? String(key).trim() : null
|
|
}
|