Files
SupplierSystem/src/utils/outboundPhotoPreview.ts
T
Ubuntu 5fa2bbfd55
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

85 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 分拣出库留证照片的**本地预览 URL 释放**(2026-10-12 拍照功能的前端资源配对)。
*
* 背景:Web 分拣拍照为每张压缩图 `URL.createObjectURL(file)` 生成预览;
* 完成分拣 / 切单 / 删图时只清 React state,**没有** `revokeObjectURL` 配对 ⇒
* blob(含整张图片数据)在标签页卸载前一直驻留,一个班次数百张即持续吃内存。
*
* 只释放本地 blob:服务端已存照的预览是签名 URL(`toSameOriginStorageUrl`),
* 对它调 `revokeObjectURL` 无效,也绝不能误当成 blob 处理。
*/
export type OutboundPhotoPreview = { path: string; previewUrl: string };
export function isBlobPreviewUrl(url: unknown): boolean {
return String(url ?? '').startsWith('blob:');
}
export function releaseOutboundPhotoPreviewUrls(
photos?: Record<string, OutboundPhotoPreview[]> | OutboundPhotoPreview[] | null
): void {
if (!photos) return;
const lists: OutboundPhotoPreview[][] = Array.isArray(photos) ? [photos] : Object.values(photos);
for (const list of lists) {
for (const photo of list || []) {
const url = String(photo?.previewUrl ?? '');
if (!isBlobPreviewUrl(url)) continue;
try {
URL.revokeObjectURL(url);
} catch {
// 释放失败不影响界面(浏览器已在卸载时统一回收)
}
}
}
}
/**
* FE-4(2026-10-19 T5 红队复攻):多文件上传的预览 URL 必须在**全部上传成功之后**才创建。
*
* 旧实现每传成功一个就 `createObjectURL`,随后一个文件抛错时 `uploaded` 数组被丢弃 ⇒
* 前面已创建的 blob URL 永远没人 revoke(多文件部分失败泄漏)。这里先传完再建 URL;
* 即使创建 URL 的中间环节抛错,也会释放已创建的 blob。
*/
export async function stageOutboundPhotoPreviews<T>(
files: File[],
upload: (file: File) => Promise<T>,
makePreviewUrl: (file: File) => string
): Promise<Array<{ path: T; previewUrl: string }>> {
const paths: T[] = [];
for (const file of files) {
paths.push(await upload(file)); // 任一失败即抛出:此时尚未创建任何 blob URL
}
const created: Array<{ path: T; previewUrl: string }> = [];
try {
for (let index = 0; index < paths.length; index += 1) {
created.push({ path: paths[index], previewUrl: makePreviewUrl(files[index]) });
}
return created;
} catch (error) {
releaseOutboundPhotoPreviewUrls(
created.map((item) => ({ path: String(item.path), previewUrl: item.previewUrl }))
);
throw error;
}
}
/**
* B-9.1b(2026-10-19 T5 终局复攻):上传在途时组件可能已卸载。
* 先按正常流程上传/建预览,返回后若已卸载 ⇒ 立刻释放刚创建的 blob 并返回 null,
* 调用方不得再 setState(否则 URL 无人引用、永不 revoke)。
*/
export async function stageOutboundPhotoPreviewsIfMounted<T>(
files: File[],
upload: (file: File) => Promise<T>,
makePreviewUrl: (file: File) => string,
isMounted: () => boolean
): Promise<Array<{ path: T; previewUrl: string }> | null> {
const staged = await stageOutboundPhotoPreviews(files, upload, makePreviewUrl);
if (!isMounted()) {
releaseOutboundPhotoPreviewUrls(
staged.map((item) => ({ path: String(item.path), previewUrl: item.previewUrl }))
);
return null;
}
return staged;
}