ow
This commit is contained in:
1 parent
05ca116194
commit
1ff69f3cb3
4 files changed
+349
-253
No files matched your search
@@ -74,6 +74,17 @@ function extOf(fileName: string): string {
|
||||
return dot > 0 ? fileName.slice(dot) : ".ext";
|
||||
}
|
||||
|
||||
/**
|
||||
* 分片协议名唯一前缀(uid-序号-总数.ext 的 uid 段)。
|
||||
* pump() 是同步循环,同批并发任务几乎必在同一毫秒点着,只靠 Date.now() 会重名:
|
||||
* App 端重名会让 copyTo 目标冲突或互相覆盖,H5 端重名会让分片文件互相覆盖;
|
||||
* 重试任务也会重新准备,同样要换新名,故再拼模块级自增序号。
|
||||
*/
|
||||
function uniqueUploadTag(): string {
|
||||
seq += 1;
|
||||
return `uni-${Date.now()}-${seq}`;
|
||||
}
|
||||
|
||||
/** 分片大小:内网 3MB / 公网 1MB(旧系统 FileUpload 同口径;开发构建视为内网) */
|
||||
function resolveChunkSize(): number {
|
||||
const publicNet = /localhost|127\.|192\.168\.|10\./.test(baseUrl);
|
||||
@@ -88,7 +99,7 @@ function prepareAppFilePath(item: UploadItem): Promise<string> {
|
||||
// 真机验证前不引入分片循环(读不了本地字节切片)
|
||||
return copyIntoAppUploadDir(
|
||||
item.filePath,
|
||||
`uni-${Date.now()}-1-1${extOf(item.name)}`,
|
||||
`${uniqueUploadTag()}-1-1${extOf(item.name)}`,
|
||||
);
|
||||
// #endif
|
||||
// #ifndef APP-PLUS
|
||||
@@ -202,7 +213,7 @@ export const useUploadQueueStore = defineStore(
|
||||
let aborted = false;
|
||||
const chunkSize = resolveChunkSize();
|
||||
const total = Math.max(Math.ceil(blob.size / chunkSize), 1);
|
||||
const uid = `uni-${Date.now()}`;
|
||||
const uid = uniqueUploadTag();
|
||||
const startedAt = Date.now();
|
||||
let doneBytes = 0;
|
||||
|
||||
@@ -272,7 +283,7 @@ export const useUploadQueueStore = defineStore(
|
||||
};
|
||||
}
|
||||
|
||||
/** uni 通道(App):先复制成分片名再发(后端按 filename 解析分片);复制失败回退原路径,至少能拿到明确的后端报错 */
|
||||
/** uni 通道(App):先复制成分片名再发(后端按 filename 解析分片);复制失败直接判失败,不回退原路径 */
|
||||
function uniUpload(item: UploadItem): UploadHandle {
|
||||
let task: UniApp.UploadTask | null = null;
|
||||
let aborted = false;
|
||||
@@ -310,7 +321,14 @@ export const useUploadQueueStore = defineStore(
|
||||
|
||||
prepareAppFilePath(item)
|
||||
.then(start)
|
||||
.catch(() => start(item.filePath));
|
||||
.catch(() => {
|
||||
// 绝不能回退原路径重传:原文件名没有 uid-序号-总数 协议,后端
|
||||
// PackUploadDmsFile 拆不出分片序号会静默 return null(HTTP 200 空体),
|
||||
// 任务反而被当成成功、库里却是 0 条——宁可显式失败让用户重试
|
||||
item.status = "failed";
|
||||
item.error = "文件准备失败";
|
||||
finish(item);
|
||||
});
|
||||
|
||||
return {
|
||||
abort: () => {
|
||||
|
||||
Reference in new issue
Block a user