@ysgroup/ui 0.1.50 → 0.1.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/components/YsDialog.vue +6 -1
- package/src/components/YsFileDropZone.vue +138 -0
- package/src/components/YsFileNotes.vue +178 -0
- package/src/components/YsFilePreview.vue +20 -16
- package/src/components/YsImageViewer.vue +317 -0
- package/src/components/YsUploadQueue.vue +287 -0
- package/src/components/YsViewerIcon.vue +1 -0
- package/src/filePreview.ts +7 -0
- package/src/fileRules.ts +154 -0
- package/src/index.ts +33 -0
- package/src/preview/imageZoom.ts +107 -0
- package/src/uploadQueue.ts +399 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
/* 上传队列(无界面):选择、拖入、粘贴进来的文件排队上传,统一状态、进度、续传、重试、取消和「原图」选择。
|
|
2
|
+
* 传输用 @ysgroup/core 的 uploadChunked(整份 sha256 → 秒传 / 续传 / 分块),界面用 YsUploadQueue 显示。
|
|
3
|
+
*
|
|
4
|
+
* 两种模式:
|
|
5
|
+
* - 立即上传(autoStart,默认):加入即传;批次里有会被转换的图片时先进「待上传」,确认后再传(confirm: "convertible")。
|
|
6
|
+
* - 待提交(autoStart: false):文件留在本地,业务对象建好后调 start(extra) 再传(新建表单、回复框)。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { computed, reactive, ref, shallowRef, type Ref } from "vue";
|
|
10
|
+
import {
|
|
11
|
+
YsLocalFileError,
|
|
12
|
+
uploadChunked,
|
|
13
|
+
type YsFileCall,
|
|
14
|
+
type YsFileDuplicate,
|
|
15
|
+
type YsUploadStage,
|
|
16
|
+
} from "@ysgroup/core";
|
|
17
|
+
import { checkUploadFile, expectedHandling, sameLocalFile, type YsExpectedHandling } from "./fileRules";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 行状态:pending 待确认 / 待提交,waiting 排队,hashing 校验中,uploading 上传中,processing 服务端核对与转换,
|
|
21
|
+
* done 完成,paused 已中断(可继续),failed 失败(可重试)。
|
|
22
|
+
*/
|
|
23
|
+
export type YsUploadState = "pending" | "waiting" | "hashing" | "uploading" | "processing" | "done" | "paused" | "failed";
|
|
24
|
+
|
|
25
|
+
export type YsUploadItem<T = Record<string, unknown>> = {
|
|
26
|
+
key: number;
|
|
27
|
+
file: File;
|
|
28
|
+
state: YsUploadState;
|
|
29
|
+
/** 本阶段进度 0~1(校验中是算 sha256 的进度,上传中是发块进度)。 */
|
|
30
|
+
progress: number;
|
|
31
|
+
/** 续传时上传阶段的起点(0~1),0 表示从头传。 */
|
|
32
|
+
resumedFrom: number;
|
|
33
|
+
instant: boolean;
|
|
34
|
+
error: string;
|
|
35
|
+
/** 选了「原图」:原样保存,不做格式转换。 */
|
|
36
|
+
asIs: boolean;
|
|
37
|
+
handling: YsExpectedHandling;
|
|
38
|
+
duplicates: YsFileDuplicate[];
|
|
39
|
+
result: T | null;
|
|
40
|
+
/** 服务端分配的上传编号(放弃已中断的上传时用)。 */
|
|
41
|
+
uploadId: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type YsUploadEvents = {
|
|
45
|
+
begin: string;
|
|
46
|
+
chunk: string;
|
|
47
|
+
finish: string;
|
|
48
|
+
cancel: string;
|
|
49
|
+
prove: string;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const YS_UPLOAD_EVENTS: YsUploadEvents = {
|
|
53
|
+
begin: "begin_file_upload",
|
|
54
|
+
chunk: "upload_file_chunk",
|
|
55
|
+
finish: "finish_file_upload",
|
|
56
|
+
cancel: "cancel_file_upload",
|
|
57
|
+
prove: "prove_file_upload",
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type YsUploadQueueOptions<T> = {
|
|
61
|
+
call: YsFileCall;
|
|
62
|
+
events?: Partial<YsUploadEvents>;
|
|
63
|
+
/** begin 时附带的业务字段(如所属对象),start(extra) 的 extra 会合并进来。 */
|
|
64
|
+
beginData?: (item: YsUploadItem<T>) => Record<string, unknown>;
|
|
65
|
+
/** 区分「同一文件传给不同业务对象」的续传键(如所属对象编号)。 */
|
|
66
|
+
resumeKey?: (item: YsUploadItem<T>) => string;
|
|
67
|
+
/** 单个文件上限(字节),可按文件给(如视频 200 MB、其他 50 MB),默认 50 MB。 */
|
|
68
|
+
maxSize?: number | ((file: File) => number);
|
|
69
|
+
maxName?: number;
|
|
70
|
+
/** 最多几个文件同时上传,默认 2(服务端每人最多 6 个进行中的上传)。 */
|
|
71
|
+
concurrency?: number;
|
|
72
|
+
/** 最多加入几个文件(含已完成的行),不限为 0。 */
|
|
73
|
+
limit?: number;
|
|
74
|
+
autoStart?: boolean;
|
|
75
|
+
/** 立即上传模式下什么时候先进「待上传」让用户选「原图」:convertible 有会被转换的图片时(默认)、always、never。 */
|
|
76
|
+
confirm?: "convertible" | "always" | "never";
|
|
77
|
+
/** 视频转码目标的显示名,默认 AV1。 */
|
|
78
|
+
videoCodec?: string;
|
|
79
|
+
onDone?: (item: YsUploadItem<T>, result: T) => void;
|
|
80
|
+
onDuplicates?: (item: YsUploadItem<T>, list: YsFileDuplicate[]) => void;
|
|
81
|
+
/** 本地检查不通过、超过数量、重复加入时的提示(默认什么都不做,由 YsUploadQueue 显示 notices)。 */
|
|
82
|
+
onNotice?: (text: string) => void;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const TRANSIENT_CODES = new Set([408, 429, 502, 503, 504]);
|
|
86
|
+
|
|
87
|
+
/** 网络中断、超时、服务暂不可用:已收到的块还在服务端,可以续传。 */
|
|
88
|
+
export function isResumableError(error: unknown): boolean {
|
|
89
|
+
if (error instanceof YsLocalFileError) return false;
|
|
90
|
+
const code = (error as { code?: unknown } | null)?.code;
|
|
91
|
+
if (typeof code === "number") return TRANSIENT_CODES.has(code);
|
|
92
|
+
return (error as { name?: unknown } | null)?.name !== "AbortError";
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function errorText(error: unknown): string {
|
|
96
|
+
if (error instanceof YsLocalFileError) return error.message;
|
|
97
|
+
const message = (error as { message?: unknown } | null)?.message;
|
|
98
|
+
return typeof message === "string" && message ? message : "上传失败";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let seq = 0;
|
|
102
|
+
|
|
103
|
+
export function useYsUploadQueue<T = Record<string, unknown>>(options: YsUploadQueueOptions<T>) {
|
|
104
|
+
const events = { ...YS_UPLOAD_EVENTS, ...options.events };
|
|
105
|
+
const items: Ref<YsUploadItem<T>[]> = ref([]);
|
|
106
|
+
const notices = ref<string[]>([]);
|
|
107
|
+
const extra = shallowRef<Record<string, unknown>>({});
|
|
108
|
+
const controllers = new Map<number, AbortController>();
|
|
109
|
+
let running = 0;
|
|
110
|
+
let waiters: (() => void)[] = [];
|
|
111
|
+
|
|
112
|
+
const concurrency = Math.max(1, options.concurrency ?? 2);
|
|
113
|
+
const autoStart = options.autoStart ?? true;
|
|
114
|
+
const confirmMode = options.confirm ?? "convertible";
|
|
115
|
+
|
|
116
|
+
const pending = computed(() => items.value.filter((item) => item.state === "pending"));
|
|
117
|
+
const active = computed(() =>
|
|
118
|
+
items.value.filter((item) => ["waiting", "hashing", "uploading", "processing"].includes(item.state)),
|
|
119
|
+
);
|
|
120
|
+
const busy = computed(() => active.value.length > 0);
|
|
121
|
+
const paused = computed(() => items.value.filter((item) => item.state === "paused"));
|
|
122
|
+
const failed = computed(() => items.value.filter((item) => item.state === "failed"));
|
|
123
|
+
const done = computed(() => items.value.filter((item) => item.state === "done"));
|
|
124
|
+
/** 离开页面前要提醒的:还在传、已中断或待确认。 */
|
|
125
|
+
const unfinished = computed(() => busy.value || paused.value.length > 0 || pending.value.length > 0);
|
|
126
|
+
|
|
127
|
+
function notify(text: string) {
|
|
128
|
+
notices.value = [...notices.value, text].slice(-5);
|
|
129
|
+
options.onNotice?.(text);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function dismissNotice(index: number) {
|
|
133
|
+
notices.value = notices.value.filter((_, at) => at !== index);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 加入文件(选择、拖入、粘贴)。本地检查不通过的不加入,原因进 notices;返回加入的行。 */
|
|
137
|
+
function add(input: File[] | FileList): YsUploadItem<T>[] {
|
|
138
|
+
const list = Array.from(input);
|
|
139
|
+
const added: YsUploadItem<T>[] = [];
|
|
140
|
+
const skipped = new Map<string, string[]>();
|
|
141
|
+
for (const file of list) {
|
|
142
|
+
const maxSize = typeof options.maxSize === "function" ? options.maxSize(file) : options.maxSize;
|
|
143
|
+
const reason = checkUploadFile(file, { maxSize, maxName: options.maxName });
|
|
144
|
+
if (reason) {
|
|
145
|
+
skipped.set(reason, [...(skipped.get(reason) ?? []), file.name || "未命名"]);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const same = items.value.find((item) => item.state !== "done" && sameLocalFile(item.file, file));
|
|
149
|
+
if (same) {
|
|
150
|
+
if (same.state === "paused" || same.state === "failed") {
|
|
151
|
+
same.file = file;
|
|
152
|
+
same.state = "waiting";
|
|
153
|
+
added.push(same);
|
|
154
|
+
} else skipped.set("已在列表里", [...(skipped.get("已在列表里") ?? []), file.name]);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (options.limit && items.value.length >= options.limit) {
|
|
158
|
+
skipped.set(`最多 ${options.limit} 个`, [...(skipped.get(`最多 ${options.limit} 个`) ?? []), file.name]);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const handling = expectedHandling(file, options.videoCodec);
|
|
162
|
+
const item = reactive({
|
|
163
|
+
key: ++seq,
|
|
164
|
+
file,
|
|
165
|
+
state: "waiting",
|
|
166
|
+
progress: 0,
|
|
167
|
+
resumedFrom: 0,
|
|
168
|
+
instant: false,
|
|
169
|
+
error: "",
|
|
170
|
+
asIs: false,
|
|
171
|
+
handling,
|
|
172
|
+
duplicates: [],
|
|
173
|
+
result: null,
|
|
174
|
+
uploadId: "",
|
|
175
|
+
}) as YsUploadItem<T>;
|
|
176
|
+
items.value.push(item);
|
|
177
|
+
added.push(item);
|
|
178
|
+
}
|
|
179
|
+
for (const [reason, names] of skipped) notify(`${reason},没有加入:${names.join("、")}`);
|
|
180
|
+
const fresh = added.filter((item) => item.state === "waiting" && item.progress === 0 && !item.uploadId);
|
|
181
|
+
const needConfirm =
|
|
182
|
+
!autoStart ||
|
|
183
|
+
confirmMode === "always" ||
|
|
184
|
+
(confirmMode === "convertible" && (pending.value.length > 0 || fresh.some((item) => item.handling.canKeepOriginal)));
|
|
185
|
+
if (needConfirm) for (const item of fresh) item.state = "pending";
|
|
186
|
+
pump();
|
|
187
|
+
return added;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function setAsIs(item: YsUploadItem<T>, value: boolean) {
|
|
191
|
+
if (item.state === "pending" || item.state === "waiting" || item.state === "failed") item.asIs = value;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** 待上传里能勾「原图」的行全部勾上 / 全部取消。 */
|
|
195
|
+
function setAllAsIs(value: boolean) {
|
|
196
|
+
for (const item of pending.value) if (item.handling.canKeepOriginal) item.asIs = value;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** 确认「待上传」(立即上传模式),或开始上传待提交的文件(待提交模式,extra 合并进 begin)。全部结束时 resolve。 */
|
|
200
|
+
function start(more: Record<string, unknown> = {}): Promise<void> {
|
|
201
|
+
extra.value = { ...extra.value, ...more };
|
|
202
|
+
for (const item of pending.value) item.state = "waiting";
|
|
203
|
+
pump();
|
|
204
|
+
return settled();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** 等到没有在传的文件。 */
|
|
208
|
+
function settled(): Promise<void> {
|
|
209
|
+
if (!busy.value) return Promise.resolve();
|
|
210
|
+
return new Promise((resolve) => waiters.push(resolve));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function pump() {
|
|
214
|
+
while (running < concurrency) {
|
|
215
|
+
const next = items.value.find((item) => item.state === "waiting");
|
|
216
|
+
if (!next) break;
|
|
217
|
+
running += 1;
|
|
218
|
+
void upload(next).finally(() => {
|
|
219
|
+
running -= 1;
|
|
220
|
+
pump();
|
|
221
|
+
if (!busy.value) {
|
|
222
|
+
const list = waiters;
|
|
223
|
+
waiters = [];
|
|
224
|
+
list.forEach((resolve) => resolve());
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async function upload(item: YsUploadItem<T>) {
|
|
231
|
+
const controller = new AbortController();
|
|
232
|
+
controllers.set(item.key, controller);
|
|
233
|
+
item.state = "hashing";
|
|
234
|
+
item.progress = 0;
|
|
235
|
+
item.resumedFrom = 0;
|
|
236
|
+
item.instant = false;
|
|
237
|
+
item.error = "";
|
|
238
|
+
let firstUpload = true;
|
|
239
|
+
const call: YsFileCall = async (event, data, callOptions) => {
|
|
240
|
+
const result = await options.call(event, data, callOptions);
|
|
241
|
+
if (event === events.begin && result && typeof result === "object" && "upload_id" in result)
|
|
242
|
+
item.uploadId = String((result as { upload_id: unknown }).upload_id ?? "");
|
|
243
|
+
return result;
|
|
244
|
+
};
|
|
245
|
+
try {
|
|
246
|
+
const result = await uploadChunked<T & object>(call, item.file, {
|
|
247
|
+
begin: {
|
|
248
|
+
event: events.begin,
|
|
249
|
+
data: {
|
|
250
|
+
content_type: item.file.type,
|
|
251
|
+
as_is: item.asIs,
|
|
252
|
+
...extra.value,
|
|
253
|
+
...(options.beginData?.(item) ?? {}),
|
|
254
|
+
},
|
|
255
|
+
},
|
|
256
|
+
chunkEvent: events.chunk,
|
|
257
|
+
finishEvent: events.finish,
|
|
258
|
+
cancelEvent: events.cancel,
|
|
259
|
+
proveEvent: events.prove,
|
|
260
|
+
idKey: "upload_id",
|
|
261
|
+
resume: { key: options.resumeKey?.(item) ?? "" },
|
|
262
|
+
signal: controller.signal,
|
|
263
|
+
onPhase: (stage: YsUploadStage) => {
|
|
264
|
+
if (stage === "hash") item.state = "hashing";
|
|
265
|
+
else if (stage === "upload") item.state = "uploading";
|
|
266
|
+
else item.state = "processing";
|
|
267
|
+
},
|
|
268
|
+
onProgress: (_ratio, phase = "upload", phaseRatio = 0) => {
|
|
269
|
+
if (phase === "upload" && firstUpload) {
|
|
270
|
+
firstUpload = false;
|
|
271
|
+
if (phaseRatio > 0 && phaseRatio < 1) item.resumedFrom = phaseRatio;
|
|
272
|
+
}
|
|
273
|
+
item.progress = phaseRatio;
|
|
274
|
+
},
|
|
275
|
+
onDuplicates: (list) => {
|
|
276
|
+
item.duplicates = list;
|
|
277
|
+
options.onDuplicates?.(item, list);
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
item.state = "done";
|
|
281
|
+
item.progress = 1;
|
|
282
|
+
item.instant = Boolean((result as { instant?: unknown }).instant);
|
|
283
|
+
item.uploadId = "";
|
|
284
|
+
item.result = result as T;
|
|
285
|
+
options.onDone?.(item, result as T);
|
|
286
|
+
} catch (error) {
|
|
287
|
+
if (controller.signal.aborted) return;
|
|
288
|
+
if (isResumableError(error)) {
|
|
289
|
+
item.state = "paused";
|
|
290
|
+
item.error = "网络中断,已收到的部分保留在服务器上,点「继续」接着传";
|
|
291
|
+
} else {
|
|
292
|
+
item.state = "failed";
|
|
293
|
+
item.progress = 0;
|
|
294
|
+
item.uploadId = "";
|
|
295
|
+
item.error = errorText(error);
|
|
296
|
+
}
|
|
297
|
+
} finally {
|
|
298
|
+
controllers.delete(item.key);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** 「继续」与「重试」同一个动作:先按内容续传,续不上再从头传。 */
|
|
303
|
+
function retry(item: YsUploadItem<T>) {
|
|
304
|
+
if (item.state !== "paused" && item.state !== "failed") return;
|
|
305
|
+
item.state = "waiting";
|
|
306
|
+
pump();
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function retryAll() {
|
|
310
|
+
for (const item of items.value) if (item.state === "paused" || item.state === "failed") item.state = "waiting";
|
|
311
|
+
pump();
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* 移除一行:在传的中止(core 发 cancel 清掉半截),已中断的用上传编号发 cancel,服务端立即删掉已传的块、
|
|
316
|
+
* 不占同时上传的名额(不必等 24 小时过期)。
|
|
317
|
+
*/
|
|
318
|
+
function remove(item: YsUploadItem<T>) {
|
|
319
|
+
controllers.get(item.key)?.abort();
|
|
320
|
+
if (item.state === "paused" && item.uploadId)
|
|
321
|
+
void Promise.resolve(options.call(events.cancel, { upload_id: item.uploadId })).catch(() => undefined);
|
|
322
|
+
items.value = items.value.filter((row) => row !== item);
|
|
323
|
+
pump();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** 取消「待上传」:只丢掉还没开始的行。 */
|
|
327
|
+
function cancelPending() {
|
|
328
|
+
items.value = items.value.filter((item) => item.state !== "pending");
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function cancelAll() {
|
|
332
|
+
for (const item of [...items.value]) if (item.state !== "done") remove(item);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function clearDone() {
|
|
336
|
+
items.value = items.value.filter((item) => item.state !== "done");
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** 清空(切换业务对象、卸载时):中止在传的,不发 cancel 给已中断的(留给续传或过期清理)。 */
|
|
340
|
+
function reset() {
|
|
341
|
+
for (const controller of controllers.values()) controller.abort();
|
|
342
|
+
controllers.clear();
|
|
343
|
+
items.value = [];
|
|
344
|
+
notices.value = [];
|
|
345
|
+
extra.value = {};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return {
|
|
349
|
+
items,
|
|
350
|
+
notices,
|
|
351
|
+
pending,
|
|
352
|
+
active,
|
|
353
|
+
paused,
|
|
354
|
+
failed,
|
|
355
|
+
done,
|
|
356
|
+
busy,
|
|
357
|
+
unfinished,
|
|
358
|
+
add,
|
|
359
|
+
start,
|
|
360
|
+
settled,
|
|
361
|
+
setAsIs,
|
|
362
|
+
setAllAsIs,
|
|
363
|
+
retry,
|
|
364
|
+
retryAll,
|
|
365
|
+
remove,
|
|
366
|
+
cancelPending,
|
|
367
|
+
cancelAll,
|
|
368
|
+
clearDone,
|
|
369
|
+
reset,
|
|
370
|
+
dismissNotice,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export type YsUploadQueue<T = Record<string, unknown>> = ReturnType<typeof useYsUploadQueue<T>>;
|
|
375
|
+
|
|
376
|
+
/** 行状态文字(与规范的进度文案一致)。 */
|
|
377
|
+
export function uploadStateText(item: Pick<YsUploadItem, "state" | "progress" | "resumedFrom" | "instant" | "error">): string {
|
|
378
|
+
const percent = (ratio: number) => `${Math.round(ratio * 100)}%`;
|
|
379
|
+
switch (item.state) {
|
|
380
|
+
case "pending":
|
|
381
|
+
return "待上传";
|
|
382
|
+
case "waiting":
|
|
383
|
+
return item.progress > 0 ? "等待继续" : "排队中";
|
|
384
|
+
case "hashing":
|
|
385
|
+
return `校验中 ${percent(item.progress)}`;
|
|
386
|
+
case "uploading":
|
|
387
|
+
return item.resumedFrom
|
|
388
|
+
? `上传中 ${percent(item.progress)}(从 ${percent(item.resumedFrom)} 续传)`
|
|
389
|
+
: `上传中 ${percent(item.progress)}`;
|
|
390
|
+
case "processing":
|
|
391
|
+
return "处理中(核对与格式检查)";
|
|
392
|
+
case "done":
|
|
393
|
+
return item.instant ? "秒传完成" : item.resumedFrom ? "已上传(续传完成)" : "已上传";
|
|
394
|
+
case "paused":
|
|
395
|
+
return item.progress > 0 ? `已中断,停在 ${percent(item.progress)}` : "已中断";
|
|
396
|
+
default:
|
|
397
|
+
return item.error || "上传失败";
|
|
398
|
+
}
|
|
399
|
+
}
|