@wuxiaolins/upload 0.0.0

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.
@@ -0,0 +1,502 @@
1
+ import { ref as F } from "vue";
2
+ import j from "dayjs";
3
+ let T = null;
4
+ async function B(e, r, l = { emitUploadEvent: () => {
5
+ } }) {
6
+ if (typeof window > "u")
7
+ return { status: "error", percent: 0 };
8
+ const { default: c } = await import("vod-js-sdk-v6");
9
+ return new Promise((d) => {
10
+ T || (T = new c({
11
+ getSignature: async () => await _(r)
12
+ }));
13
+ const i = {
14
+ mediaFile: e.file
15
+ // mediaName: file.fileName || generateFileKey({ file: file.file, uid: file.uid }, options),
16
+ }, o = T.upload(i);
17
+ o.on("media_progress", (s) => {
18
+ e.percent = Number((s.percent * 100).toFixed(2)), typeof (r == null ? void 0 : r.onProgress) == "function" && r.onProgress({ ...e, percent: e.percent, speed: s.speed, loaded: s.loaded, total: s.total, uploader: o }), l.emitUploadEvent("progress", {
19
+ ...e,
20
+ speed: (s == null ? void 0 : s.speed) || 0,
21
+ loaded: (s == null ? void 0 : s.loaded) || 0,
22
+ total: (s == null ? void 0 : s.total) || 0,
23
+ uploader: o
24
+ });
25
+ }), o.done().then((s) => {
26
+ console.log("%c [ vod上传结果 ]-46", "font-size:13px; background:pink; color:#bf2c9f;", s);
27
+ const p = s.video || {}, g = p.url || "";
28
+ g ? (l.emitUploadEvent("success", {
29
+ ...s,
30
+ ...e,
31
+ status: "done",
32
+ url: g
33
+ }), d({ ...s, taskId: (s == null ? void 0 : s.fileId) || p.fileId || "", status: "done", url: g, percent: 100, uploader: o })) : (l.emitUploadEvent("error", {
34
+ ...s,
35
+ ...e,
36
+ status: "error"
37
+ }), d({ taskId: "", status: "error", percent: 0, uploader: o }));
38
+ }).catch((s) => {
39
+ console.log("vod:err", s), l.emitUploadEvent("error", e), d({ taskId: "", status: "error", percent: 0, uploader: o });
40
+ });
41
+ });
42
+ }
43
+ async function $(e, r) {
44
+ const l = Date.now() + "", c = y({ file: e, uid: l }, r);
45
+ return await B({
46
+ uid: l,
47
+ file: e,
48
+ fileName: c,
49
+ name: c,
50
+ status: "uploading",
51
+ percent: 0
52
+ }, r);
53
+ }
54
+ const A = () => j().format("YYYY/MM/DD");
55
+ function z() {
56
+ return Date.now().toString(36) + Math.random().toString(36).substring(2);
57
+ }
58
+ function y(e, r) {
59
+ var s;
60
+ const l = W(), c = (r == null ? void 0 : r.prefixPath) || "";
61
+ let d = "";
62
+ e != null && e.fileName || (d = `/${A()}/${(/* @__PURE__ */ new Date()).getTime() + l}`), console.log("%c [ file ]-18", "font-size:13px; background:pink; color:#bf2c9f;", e);
63
+ let i = c + d;
64
+ const o = Y((s = e == null ? void 0 : e.file) == null ? void 0 : s.name);
65
+ return (i == null ? void 0 : i.indexOf(o)) === -1 && (i += o), "/" + V() + i;
66
+ }
67
+ function V(e) {
68
+ return e != null && e.prod ? "release" : "test";
69
+ }
70
+ function W() {
71
+ let e = "";
72
+ for (let r = 0; r < 6; r++)
73
+ e += Math.floor(Math.random() * 10);
74
+ return e + ((/* @__PURE__ */ new Date()).getTime() + 1).toString();
75
+ }
76
+ function Y(e) {
77
+ if (!e)
78
+ return "";
79
+ let r = "";
80
+ try {
81
+ const l = e.lastIndexOf(".");
82
+ r = e.substring(l + 1);
83
+ } catch (l) {
84
+ console.error(l);
85
+ }
86
+ return r && "." + r;
87
+ }
88
+ function H(e) {
89
+ return typeof window > "u" ? new Promise((r) => r({ width: 0, height: 0 })) : new Promise((r) => {
90
+ const l = new Image(), c = new FileReader();
91
+ c.onload = () => {
92
+ l.src = c.result, l.onload = () => r({ width: l.width, height: l.height });
93
+ }, c.readAsDataURL(e);
94
+ });
95
+ }
96
+ function L(e) {
97
+ return fetch((e == null ? void 0 : e.action) || "", {
98
+ method: (e == null ? void 0 : e.method) || "POST",
99
+ headers: {
100
+ "Content-Type": "application/json",
101
+ ...e == null ? void 0 : e.headers
102
+ },
103
+ body: JSON.stringify((e == null ? void 0 : e.data) || {})
104
+ }).then(async (r) => {
105
+ var d, i, o;
106
+ const l = await r.json(), c = l.data || l;
107
+ return typeof (e == null ? void 0 : e.transform) == "function" ? e.transform(c) : {
108
+ tmpSecretId: (d = c == null ? void 0 : c.credentials) == null ? void 0 : d.tmpSecretId,
109
+ tmpSecretKey: (i = c == null ? void 0 : c.credentials) == null ? void 0 : i.tmpSecretKey,
110
+ sessionToken: (o = c == null ? void 0 : c.credentials) == null ? void 0 : o.sessionToken,
111
+ expiredTime: c.expiredTime,
112
+ requestId: c.requestId,
113
+ startTime: c.startTime
114
+ };
115
+ });
116
+ }
117
+ function _(e) {
118
+ return fetch((e == null ? void 0 : e.action) || "", {
119
+ method: (e == null ? void 0 : e.method) || "POST",
120
+ headers: {
121
+ "Content-Type": "application/json",
122
+ ...e == null ? void 0 : e.headers
123
+ },
124
+ body: JSON.stringify((e == null ? void 0 : e.data) || {})
125
+ }).then(async (r) => {
126
+ const l = await r.json(), c = l.data || l;
127
+ return typeof (e == null ? void 0 : e.transform) == "function" ? e.transform(c) : c;
128
+ });
129
+ }
130
+ async function M(e, r, l = { emitUploadEvent: () => {
131
+ } }) {
132
+ if (typeof window > "u")
133
+ return { status: "error", percent: 0 };
134
+ const { default: c } = await import("cos-js-sdk-v5"), d = await L(r);
135
+ return d ? new Promise((i) => {
136
+ const o = new c({
137
+ getAuthorization: (g, v) => v({
138
+ TmpSecretId: d.tmpSecretId,
139
+ TmpSecretKey: d.tmpSecretKey,
140
+ XCosSecurityToken: d.sessionToken,
141
+ StartTime: d.startTime,
142
+ ExpiredTime: d.expiredTime
143
+ })
144
+ });
145
+ console.log("%c [ cos ]-28", "font-size:13px; background:pink; color:#bf2c9f;", o);
146
+ const s = {
147
+ Bucket: (r == null ? void 0 : r.bucket) || d.bucket || "bsy-sdl-web-1308012692",
148
+ Region: (r == null ? void 0 : r.region) || "ap-guangzhou",
149
+ Key: e.fileName || y({ file: e.file, uid: e.uid }, r),
150
+ Body: e.file,
151
+ Headers: {
152
+ "x-cos-meta-filename": encodeURIComponent(e.file.name)
153
+ }
154
+ };
155
+ let p = "";
156
+ o.sliceUploadFile(
157
+ {
158
+ ...s,
159
+ onProgress: (g) => {
160
+ e.percent = g.percent || 0, e.lastPercent = e.percent, l.emitUploadEvent("progress", e);
161
+ },
162
+ onTaskReady(g) {
163
+ p = g, typeof (r == null ? void 0 : r.onTaskId) == "function" && r.onTaskId({
164
+ file: e,
165
+ taskId: p,
166
+ cos: o
167
+ });
168
+ }
169
+ },
170
+ (g, v) => {
171
+ if (!g && (v == null ? void 0 : v.statusCode) === 200) {
172
+ const w = `https://${v.Location}`;
173
+ l.emitUploadEvent("success", {
174
+ ...e,
175
+ url: w
176
+ }), i({ taskId: p, status: "done", url: w, percent: 100 });
177
+ } else
178
+ l.emitUploadEvent("error", e), i({ taskId: p, status: "error", percent: 0 });
179
+ }
180
+ );
181
+ }) : { status: "error", percent: 0 };
182
+ }
183
+ async function q(e, r) {
184
+ const l = Date.now() + "", c = y({ file: e, uid: l }, r);
185
+ return await M({
186
+ uid: l,
187
+ file: e,
188
+ fileName: c,
189
+ name: c,
190
+ status: "uploading",
191
+ percent: 0
192
+ }, r);
193
+ }
194
+ async function N(e, r) {
195
+ var o;
196
+ if (typeof window > "u")
197
+ return { valid: !0 };
198
+ const l = (r == null ? void 0 : r.accept) || "*", c = (r == null ? void 0 : r.maxSize) || 0, d = (o = e.name.split(".").pop()) == null ? void 0 : o.toLowerCase();
199
+ if (!(l === "*" || l.split(",").some((s) => s.endsWith(d || ""))))
200
+ return { valid: !1, message: "文件类型不支持" };
201
+ if (c && e.size / 1024 / 1024 > c)
202
+ return { valid: !1, message: `文件超过大小限制(${c > 1024 ? `${(c / 1024).toFixed(0)}GB` : `${c}MB`})` };
203
+ if (e.type.startsWith("image/") && (r != null && r.widthRange) && (r != null && r.heightRange)) {
204
+ const s = await H(e), [p, g] = r.widthRange.split(":").map(Number), [v, w] = r.heightRange.split(":").map(Number);
205
+ if (s.width < p || s.width > g || s.height < v || s.height > w)
206
+ return { valid: !1, message: "图片尺寸不符合要求" };
207
+ }
208
+ return { valid: !0 };
209
+ }
210
+ function X(e = {}) {
211
+ const r = {};
212
+ function l(n, a) {
213
+ r[n] = r[n] || [], r[n].push(a);
214
+ }
215
+ function c(n, a) {
216
+ var t;
217
+ (t = r[n]) == null || t.forEach((u) => u(a));
218
+ }
219
+ const d = F([]), i = F(!1), o = e.concurrency || 1, s = /* @__PURE__ */ new Map();
220
+ e.onTaskId = (n) => (d.value = d.value.map((a) => {
221
+ var t;
222
+ return String(a.uid) === String((t = n == null ? void 0 : n.file) == null ? void 0 : t.uid) && (a.taskId = n.taskId), s.set(String(a.uid), { cos: n.cos, taskId: n.taskId }), a;
223
+ }), !0);
224
+ async function p(n) {
225
+ const a = await N(n, e);
226
+ if (!a.valid) {
227
+ c("beforeUploadError", a.message);
228
+ return;
229
+ }
230
+ c("beforeUpload", n), !e.customUpload && x([n]);
231
+ }
232
+ function g(n) {
233
+ d.value = [], n.map((a) => {
234
+ var u;
235
+ const t = a.fileName || ((u = a.url.split("/").pop()) == null ? void 0 : u.split("?")[0]) || "";
236
+ typeof window < "u" && d.value.push({
237
+ uid: a.id || z(),
238
+ url: a.url,
239
+ fileName: t,
240
+ name: t,
241
+ status: "done",
242
+ percent: 100,
243
+ file: new File([""], t),
244
+ speed: 0,
245
+ loaded: 0,
246
+ total: 0,
247
+ uploader: null,
248
+ uploadedSize: 0,
249
+ totalSize: 0
250
+ });
251
+ });
252
+ }
253
+ function v(n) {
254
+ var a;
255
+ d.value = (a = d.value) == null ? void 0 : a.map((t) => (t.uid === n.uid && (t = {
256
+ ...t,
257
+ ...n
258
+ }), console.log("%c [ ]-103", "font-size:13px; background:pink; color:#bf2c9f;", t), t));
259
+ }
260
+ const w = (n) => {
261
+ if (n === 0)
262
+ return "0.00MB";
263
+ const a = 1024, t = ["B", "KB", "MB", "GB"], u = Math.floor(Math.log(n) / Math.log(a));
264
+ return parseFloat((n / Math.pow(a, u)).toFixed(2)) + t[u];
265
+ }, E = (n) => {
266
+ const a = Number((n == null ? void 0 : n.speed) || 0), t = Number((n == null ? void 0 : n.loaded) || 0), u = Number((n == null ? void 0 : n.percent) || 0);
267
+ let m = "";
268
+ if (a > 0 ? a > 1024 * 1024 ? m = (a / (1024 * 1024)).toFixed(2) + " MB/s" : a > 1024 ? m = (a / 1024).toFixed(2) + " KB/s" : m = a.toFixed(2) + " B/s" : m = "0 KB/s", t > 0)
269
+ m = w(t);
270
+ else if (u > 0 && Number((n == null ? void 0 : n.size) || 0)) {
271
+ const h = u / 100 * Number(n.size || 0);
272
+ m = w(h);
273
+ }
274
+ return m;
275
+ };
276
+ async function x(n) {
277
+ for (const a of n) {
278
+ const t = z(), u = y({ file: a, uid: t }, e);
279
+ if (!(await N(a, e)).valid)
280
+ continue;
281
+ const h = {
282
+ uid: t,
283
+ file: a,
284
+ fileName: u,
285
+ name: u,
286
+ status: "start",
287
+ percent: 0,
288
+ loaded: 0,
289
+ total: 0,
290
+ uploader: null,
291
+ speed: "0.00MB",
292
+ size: a.size,
293
+ totalSize: w(a.size)
294
+ };
295
+ d.value.push(h), c("add", h);
296
+ }
297
+ typeof e.immediate == "boolean" && e.immediate === !1 || S();
298
+ }
299
+ async function S() {
300
+ if (i.value && d.value.some((t) => t.status === "uploading"))
301
+ return;
302
+ i.value = !0;
303
+ const n = d.value.filter((t) => ["start", "paused", "error", "fail"].includes(t.status)), a = Array.from(
304
+ { length: Math.ceil(n.length / o) },
305
+ (t, u) => n.slice(u * o, u * o + o)
306
+ );
307
+ for (const t of a)
308
+ await Promise.all(
309
+ t.map((u) => {
310
+ d.value = d.value.map((m) => (m.uid === u.uid && (m.status = "uploading"), m)), b(u).catch((m) => {
311
+ console.error(m);
312
+ });
313
+ })
314
+ );
315
+ i.value = !1;
316
+ }
317
+ async function b(n) {
318
+ let a;
319
+ switch (e.uploadType) {
320
+ case "vod":
321
+ a = await B(
322
+ n,
323
+ {
324
+ ...e,
325
+ onProgress: (t) => {
326
+ d.value = d.value.map((u) => {
327
+ var m, h;
328
+ return u.uid === n.uid && ((u == null ? void 0 : u.status) === "paused" ? ((m = t == null ? void 0 : t.uploader) != null && m.cancel && ((h = t == null ? void 0 : t.uploader) == null || h.cancel()), u = {
329
+ ...n,
330
+ ...t,
331
+ uploader: t.uploader || null
332
+ }) : u = {
333
+ ...n,
334
+ ...t,
335
+ status: "uploading",
336
+ total: t.total || 0,
337
+ loaded: t.loaded || 0,
338
+ speed: t.speed || 0,
339
+ percent: t.percent || 0,
340
+ uploader: t.uploader || null
341
+ }), u = {
342
+ ...u,
343
+ uploadedSize: E(u)
344
+ }, u;
345
+ });
346
+ }
347
+ },
348
+ {
349
+ emitUploadEvent: c
350
+ }
351
+ );
352
+ break;
353
+ default:
354
+ a = await M(n, e, {
355
+ emitUploadEvent: c
356
+ });
357
+ break;
358
+ }
359
+ n.percent = a.percent, n.status = a.status, n.url = a.url, n.uploader = a.uploader || null, d.value = d.value.map((t) => t.uid === n.uid ? n : t), c("change", n);
360
+ }
361
+ async function P(n) {
362
+ var u;
363
+ const a = d.value.findIndex((m) => m.uid === n.uid);
364
+ if (a === -1)
365
+ return;
366
+ const t = {
367
+ ...d.value[a],
368
+ status: "error"
369
+ };
370
+ try {
371
+ t != null && t.uploader && ((u = t.uploader) == null || u.cancel());
372
+ } catch {
373
+ }
374
+ d.value.splice(a, 1), c("change", t), c("remove", t);
375
+ }
376
+ function D(n) {
377
+ var t, u;
378
+ const a = s.get(n);
379
+ if (k()) {
380
+ (u = (t = a == null ? void 0 : a.cos) == null ? void 0 : t.cancelTask) == null || u.call(t, a.taskId);
381
+ return;
382
+ }
383
+ }
384
+ function I(n) {
385
+ var a, t;
386
+ if (d.value = d.value.map((u) => (u.uid === n.uid && (u.status = "uploading"), u)), k()) {
387
+ const u = s.get(n.uid);
388
+ (t = (a = u == null ? void 0 : u.cos) == null ? void 0 : a.restartTask) == null || t.call(a, u.taskId);
389
+ return;
390
+ }
391
+ setTimeout(() => {
392
+ b(n).catch((u) => {
393
+ console.error(u);
394
+ });
395
+ }, 4e3);
396
+ }
397
+ function O(n) {
398
+ I(n);
399
+ }
400
+ function R() {
401
+ var n;
402
+ (n = d.value) == null || n.map((a) => {
403
+ var t, u;
404
+ return ["uploading", "start"].includes(a.status) && (a.status = "paused", a != null && a.uploader && ((u = (t = a == null ? void 0 : a.uploader) == null ? void 0 : t.cancel) == null || u.call(t))), a;
405
+ }), i.value = !1;
406
+ }
407
+ function C(n) {
408
+ var a;
409
+ (a = d.value) == null || a.map((t) => {
410
+ var u, m, h, U;
411
+ if (t.uid === n.uid)
412
+ if (t.status = "paused", k()) {
413
+ const f = s.get(n.uid);
414
+ (m = (u = f == null ? void 0 : f.cos) == null ? void 0 : u.pauseTask) == null || m.call(u, f.taskId);
415
+ } else
416
+ t != null && t.uploader && ((U = (h = t == null ? void 0 : t.uploader) == null ? void 0 : h.cancel) == null || U.call(h));
417
+ return t;
418
+ }), i.value = !1;
419
+ }
420
+ function K(n) {
421
+ var a;
422
+ (a = d.value) == null || a.map((t) => {
423
+ t.uid === n.uid && (t.status = "paused", t != null && t.uploader && t.uploader.cancel());
424
+ }), d.value = [], i.value = !1;
425
+ }
426
+ function k() {
427
+ return e.uploadType === "cos";
428
+ }
429
+ return {
430
+ /**
431
+ * 新增文件到文件列表
432
+ */
433
+ add: x,
434
+ /**
435
+ * 设置单文件
436
+ */
437
+ setFile: v,
438
+ /**
439
+ * 设置文件列表
440
+ */
441
+ setFiles: g,
442
+ /**
443
+ * 开始上传
444
+ */
445
+ start: O,
446
+ /**
447
+ * 删除文件
448
+ */
449
+ remove: P,
450
+ /**
451
+ * 重新上传,继续上传
452
+ */
453
+ restart: I,
454
+ /**
455
+ * 一键暂停
456
+ */
457
+ pauseAll: R,
458
+ /**
459
+ * 暂停
460
+ */
461
+ pause: C,
462
+ /**
463
+ * 一键开始
464
+ */
465
+ startAll: S,
466
+ /**
467
+ *
468
+ */
469
+ abort: D,
470
+ /**
471
+ * 重置
472
+ */
473
+ reset: K,
474
+ /**
475
+ * 直接上传到云储存
476
+ */
477
+ justUpload: q,
478
+ /**
479
+ * 直接上传到云点播
480
+ */
481
+ justUploadVod: $,
482
+ /**
483
+ * 上传前校验
484
+ */
485
+ beforeUpload: p,
486
+ /**
487
+ * 文件列表
488
+ */
489
+ urls: d,
490
+ /**
491
+ * 上传状态
492
+ */
493
+ uploading: i,
494
+ /**
495
+ * 上传事件监听
496
+ */
497
+ onUploadEvent: l
498
+ };
499
+ }
500
+ export {
501
+ X as useUpload
502
+ };
@@ -0,0 +1 @@
1
+ (function(v,w){typeof exports=="object"&&typeof module<"u"?w(exports,require("vue"),require("dayjs")):typeof define=="function"&&define.amd?define(["exports","vue","dayjs"],w):(v=typeof globalThis<"u"?globalThis:v||self,w(v.hook={},v.Vue,v.dayjs))})(this,function(v,w,P){"use strict";let S=null;async function b(e,r,o={emitUploadEvent:()=>{}}){if(typeof window>"u")return{status:"error",percent:0};const{default:d}=await import("vod-js-sdk-v6");return new Promise(c=>{S||(S=new d({getSignature:async()=>await A(r)}));const i={mediaFile:e.file},l=S.upload(i);l.on("media_progress",s=>{e.percent=Number((s.percent*100).toFixed(2)),typeof(r==null?void 0:r.onProgress)=="function"&&r.onProgress({...e,percent:e.percent,speed:s.speed,loaded:s.loaded,total:s.total,uploader:l}),o.emitUploadEvent("progress",{...e,speed:(s==null?void 0:s.speed)||0,loaded:(s==null?void 0:s.loaded)||0,total:(s==null?void 0:s.total)||0,uploader:l})}),l.done().then(s=>{console.log("%c [ vod上传结果 ]-46","font-size:13px; background:pink; color:#bf2c9f;",s);const f=s.video||{},h=f.url||"";h?(o.emitUploadEvent("success",{...s,...e,status:"done",url:h}),c({...s,taskId:(s==null?void 0:s.fileId)||f.fileId||"",status:"done",url:h,percent:100,uploader:l})):(o.emitUploadEvent("error",{...s,...e,status:"error"}),c({taskId:"",status:"error",percent:0,uploader:l}))}).catch(s=>{console.log("vod:err",s),o.emitUploadEvent("error",e),c({taskId:"",status:"error",percent:0,uploader:l})})})}async function O(e,r){const o=Date.now()+"",d=k({file:e,uid:o},r);return await b({uid:o,file:e,fileName:d,name:d,status:"uploading",percent:0},r)}const j=()=>P().format("YYYY/MM/DD");function I(){return Date.now().toString(36)+Math.random().toString(36).substring(2)}function k(e,r){var s;const o=R(),d=(r==null?void 0:r.prefixPath)||"";let c="";e!=null&&e.fileName||(c=`/${j()}/${new Date().getTime()+o}`),console.log("%c [ file ]-18","font-size:13px; background:pink; color:#bf2c9f;",e);let i=d+c;const l=C((s=e==null?void 0:e.file)==null?void 0:s.name);return(i==null?void 0:i.indexOf(l))===-1&&(i+=l),"/"+D()+i}function D(e){return e!=null&&e.prod?"release":"test"}function R(){let e="";for(let r=0;r<6;r++)e+=Math.floor(Math.random()*10);return e+(new Date().getTime()+1).toString()}function C(e){if(!e)return"";let r="";try{const o=e.lastIndexOf(".");r=e.substring(o+1)}catch(o){console.error(o)}return r&&"."+r}function K(e){return typeof window>"u"?new Promise(r=>r({width:0,height:0})):new Promise(r=>{const o=new Image,d=new FileReader;d.onload=()=>{o.src=d.result,o.onload=()=>r({width:o.width,height:o.height})},d.readAsDataURL(e)})}function $(e){return fetch((e==null?void 0:e.action)||"",{method:(e==null?void 0:e.method)||"POST",headers:{"Content-Type":"application/json",...e==null?void 0:e.headers},body:JSON.stringify((e==null?void 0:e.data)||{})}).then(async r=>{var c,i,l;const o=await r.json(),d=o.data||o;return typeof(e==null?void 0:e.transform)=="function"?e.transform(d):{tmpSecretId:(c=d==null?void 0:d.credentials)==null?void 0:c.tmpSecretId,tmpSecretKey:(i=d==null?void 0:d.credentials)==null?void 0:i.tmpSecretKey,sessionToken:(l=d==null?void 0:d.credentials)==null?void 0:l.sessionToken,expiredTime:d.expiredTime,requestId:d.requestId,startTime:d.startTime}})}function A(e){return fetch((e==null?void 0:e.action)||"",{method:(e==null?void 0:e.method)||"POST",headers:{"Content-Type":"application/json",...e==null?void 0:e.headers},body:JSON.stringify((e==null?void 0:e.data)||{})}).then(async r=>{const o=await r.json(),d=o.data||o;return typeof(e==null?void 0:e.transform)=="function"?e.transform(d):d})}async function U(e,r,o={emitUploadEvent:()=>{}}){if(typeof window>"u")return{status:"error",percent:0};const{default:d}=await import("cos-js-sdk-v5"),c=await $(r);return c?new Promise(i=>{const l=new d({getAuthorization:(h,p)=>p({TmpSecretId:c.tmpSecretId,TmpSecretKey:c.tmpSecretKey,XCosSecurityToken:c.sessionToken,StartTime:c.startTime,ExpiredTime:c.expiredTime})});console.log("%c [ cos ]-28","font-size:13px; background:pink; color:#bf2c9f;",l);const s={Bucket:(r==null?void 0:r.bucket)||c.bucket||"bsy-sdl-web-1308012692",Region:(r==null?void 0:r.region)||"ap-guangzhou",Key:e.fileName||k({file:e.file,uid:e.uid},r),Body:e.file,Headers:{"x-cos-meta-filename":encodeURIComponent(e.file.name)}};let f="";l.sliceUploadFile({...s,onProgress:h=>{e.percent=h.percent||0,e.lastPercent=e.percent,o.emitUploadEvent("progress",e)},onTaskReady(h){f=h,typeof(r==null?void 0:r.onTaskId)=="function"&&r.onTaskId({file:e,taskId:f,cos:l})}},(h,p)=>{if(!h&&(p==null?void 0:p.statusCode)===200){const y=`https://${p.Location}`;o.emitUploadEvent("success",{...e,url:y}),i({taskId:f,status:"done",url:y,percent:100})}else o.emitUploadEvent("error",e),i({taskId:f,status:"error",percent:0})})}):{status:"error",percent:0}}async function V(e,r){const o=Date.now()+"",d=k({file:e,uid:o},r);return await U({uid:o,file:e,fileName:d,name:d,status:"uploading",percent:0},r)}async function F(e,r){var l;if(typeof window>"u")return{valid:!0};const o=(r==null?void 0:r.accept)||"*",d=(r==null?void 0:r.maxSize)||0,c=(l=e.name.split(".").pop())==null?void 0:l.toLowerCase();if(!(o==="*"||o.split(",").some(s=>s.endsWith(c||""))))return{valid:!1,message:"文件类型不支持"};if(d&&e.size/1024/1024>d)return{valid:!1,message:`文件超过大小限制(${d>1024?`${(d/1024).toFixed(0)}GB`:`${d}MB`})`};if(e.type.startsWith("image/")&&(r!=null&&r.widthRange)&&(r!=null&&r.heightRange)){const s=await K(e),[f,h]=r.widthRange.split(":").map(Number),[p,y]=r.heightRange.split(":").map(Number);if(s.width<f||s.width>h||s.height<p||s.height>y)return{valid:!1,message:"图片尺寸不符合要求"}}return{valid:!0}}function q(e={}){const r={};function o(n,a){r[n]=r[n]||[],r[n].push(a)}function d(n,a){var t;(t=r[n])==null||t.forEach(u=>u(a))}const c=w.ref([]),i=w.ref(!1),l=e.concurrency||1,s=new Map;e.onTaskId=n=>(c.value=c.value.map(a=>{var t;return String(a.uid)===String((t=n==null?void 0:n.file)==null?void 0:t.uid)&&(a.taskId=n.taskId),s.set(String(a.uid),{cos:n.cos,taskId:n.taskId}),a}),!0);async function f(n){const a=await F(n,e);if(!a.valid){d("beforeUploadError",a.message);return}d("beforeUpload",n),!e.customUpload&&z([n])}function h(n){c.value=[],n.map(a=>{var u;const t=a.fileName||((u=a.url.split("/").pop())==null?void 0:u.split("?")[0])||"";typeof window<"u"&&c.value.push({uid:a.id||I(),url:a.url,fileName:t,name:t,status:"done",percent:100,file:new File([""],t),speed:0,loaded:0,total:0,uploader:null,uploadedSize:0,totalSize:0})})}function p(n){var a;c.value=(a=c.value)==null?void 0:a.map(t=>(t.uid===n.uid&&(t={...t,...n}),console.log("%c [ ]-103","font-size:13px; background:pink; color:#bf2c9f;",t),t))}const y=n=>{if(n===0)return"0.00MB";const a=1024,t=["B","KB","MB","GB"],u=Math.floor(Math.log(n)/Math.log(a));return parseFloat((n/Math.pow(a,u)).toFixed(2))+t[u]},W=n=>{const a=Number((n==null?void 0:n.speed)||0),t=Number((n==null?void 0:n.loaded)||0),u=Number((n==null?void 0:n.percent)||0);let m="";if(a>0?a>1024*1024?m=(a/(1024*1024)).toFixed(2)+" MB/s":a>1024?m=(a/1024).toFixed(2)+" KB/s":m=a.toFixed(2)+" B/s":m="0 KB/s",t>0)m=y(t);else if(u>0&&Number((n==null?void 0:n.size)||0)){const g=u/100*Number(n.size||0);m=y(g)}return m};async function z(n){for(const a of n){const t=I(),u=k({file:a,uid:t},e);if(!(await F(a,e)).valid)continue;const g={uid:t,file:a,fileName:u,name:u,status:"start",percent:0,loaded:0,total:0,uploader:null,speed:"0.00MB",size:a.size,totalSize:y(a.size)};c.value.push(g),d("add",g)}typeof e.immediate=="boolean"&&e.immediate===!1||N()}async function N(){if(i.value&&c.value.some(t=>t.status==="uploading"))return;i.value=!0;const n=c.value.filter(t=>["start","paused","error","fail"].includes(t.status)),a=Array.from({length:Math.ceil(n.length/l)},(t,u)=>n.slice(u*l,u*l+l));for(const t of a)await Promise.all(t.map(u=>{c.value=c.value.map(m=>(m.uid===u.uid&&(m.status="uploading"),m)),B(u).catch(m=>{console.error(m)})}));i.value=!1}async function B(n){let a;switch(e.uploadType){case"vod":a=await b(n,{...e,onProgress:t=>{c.value=c.value.map(u=>{var m,g;return u.uid===n.uid&&((u==null?void 0:u.status)==="paused"?((m=t==null?void 0:t.uploader)!=null&&m.cancel&&((g=t==null?void 0:t.uploader)==null||g.cancel()),u={...n,...t,uploader:t.uploader||null}):u={...n,...t,status:"uploading",total:t.total||0,loaded:t.loaded||0,speed:t.speed||0,percent:t.percent||0,uploader:t.uploader||null}),u={...u,uploadedSize:W(u)},u})}},{emitUploadEvent:d});break;default:a=await U(n,e,{emitUploadEvent:d});break}n.percent=a.percent,n.status=a.status,n.url=a.url,n.uploader=a.uploader||null,c.value=c.value.map(t=>t.uid===n.uid?n:t),d("change",n)}async function Y(n){var u;const a=c.value.findIndex(m=>m.uid===n.uid);if(a===-1)return;const t={...c.value[a],status:"error"};try{t!=null&&t.uploader&&((u=t.uploader)==null||u.cancel())}catch{}c.value.splice(a,1),d("change",t),d("remove",t)}function H(n){var t,u;const a=s.get(n);if(x()){(u=(t=a==null?void 0:a.cos)==null?void 0:t.cancelTask)==null||u.call(t,a.taskId);return}}function M(n){var a,t;if(c.value=c.value.map(u=>(u.uid===n.uid&&(u.status="uploading"),u)),x()){const u=s.get(n.uid);(t=(a=u==null?void 0:u.cos)==null?void 0:a.restartTask)==null||t.call(a,u.taskId);return}setTimeout(()=>{B(n).catch(u=>{console.error(u)})},4e3)}function L(n){M(n)}function _(){var n;(n=c.value)==null||n.map(a=>{var t,u;return["uploading","start"].includes(a.status)&&(a.status="paused",a!=null&&a.uploader&&((u=(t=a==null?void 0:a.uploader)==null?void 0:t.cancel)==null||u.call(t))),a}),i.value=!1}function G(n){var a;(a=c.value)==null||a.map(t=>{var u,m,g,E;if(t.uid===n.uid)if(t.status="paused",x()){const T=s.get(n.uid);(m=(u=T==null?void 0:T.cos)==null?void 0:u.pauseTask)==null||m.call(u,T.taskId)}else t!=null&&t.uploader&&((E=(g=t==null?void 0:t.uploader)==null?void 0:g.cancel)==null||E.call(g));return t}),i.value=!1}function J(n){var a;(a=c.value)==null||a.map(t=>{t.uid===n.uid&&(t.status="paused",t!=null&&t.uploader&&t.uploader.cancel())}),c.value=[],i.value=!1}function x(){return e.uploadType==="cos"}return{add:z,setFile:p,setFiles:h,start:L,remove:Y,restart:M,pauseAll:_,pause:G,startAll:N,abort:H,reset:J,justUpload:V,justUploadVod:O,beforeUpload:f,urls:c,uploading:i,onUploadEvent:o}}v.useUpload=q,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
@@ -0,0 +1,2 @@
1
+ export * from './useUpload';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/hook/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { WxFile, UploadOptions } from './types';
2
+ export declare function startUpload(file: WxFile, options?: UploadOptions & {
3
+ onTaskId?: (data: {
4
+ file: WxFile;
5
+ taskId: string;
6
+ cos: any;
7
+ }) => void;
8
+ }, func?: any): Promise<unknown>;
9
+ export declare function justUpload(file: File, options?: UploadOptions): Promise<unknown>;
10
+ //# sourceMappingURL=cos.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cos.d.ts","sourceRoot":"","sources":["../../../../../src/hook/useUpload/cos.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAGrD,wBAAsB,WAAW,CAC/B,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,aAAa,GAAG;IAAE,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,GAAG,CAAA;KAAE,KAAK,IAAI,CAAA;CAAE,EACnG,IAAI,GAAE,GAAmC,oBAoE1C;AAED,wBAAsB,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,aAAa,oBAYnE"}
@@ -0,0 +1,4 @@
1
+ export declare function onUploadEvent(event: string, handler: any): void;
2
+ export declare function offUploadEvent(event: string, handler: any): void;
3
+ export declare function emitUploadEvent(event: string, payload: any): void;
4
+ //# sourceMappingURL=event.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../../../../../src/hook/useUpload/event.ts"],"names":[],"mappings":"AAEA,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,QAGxD;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,QAEzD;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,QAE1D"}
@@ -0,0 +1,2 @@
1
+ export * from './uploader';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/hook/useUpload/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
@@ -0,0 +1,54 @@
1
+ export type UploadStatus = "start" | "uploading" | "done" | "error" | "paused";
2
+ export interface WxFile {
3
+ uid: string;
4
+ key?: string;
5
+ file: any;
6
+ name: string;
7
+ fileName?: string;
8
+ total?: number | string;
9
+ loaded?: number | string;
10
+ speed?: number | string;
11
+ percent?: number | string;
12
+ lastPercent?: number | string;
13
+ status: UploadStatus;
14
+ url?: string;
15
+ uploader?: any;
16
+ uploadedSize?: number | string;
17
+ totalSize?: number | string;
18
+ size?: number | string;
19
+ }
20
+ export interface UploadCredential {
21
+ tmpSecretId: string;
22
+ tmpSecretKey: string;
23
+ sessionToken: string;
24
+ bucket: string;
25
+ startTime: string;
26
+ expiredTime: string;
27
+ }
28
+ export interface UploadOptions {
29
+ prod?: boolean;
30
+ immediate?: boolean;
31
+ customUpload?: boolean;
32
+ action?: string;
33
+ headers?: Record<string, any>;
34
+ data?: Record<string, any>;
35
+ method?: string;
36
+ bucket?: string;
37
+ region?: string;
38
+ /**
39
+ * 上传类型,可选值:vod云点播,cos云储存。默认值:cos
40
+ */
41
+ uploadType?: string;
42
+ fileType?: string;
43
+ accept?: string;
44
+ maxSize?: number;
45
+ widthRange?: string;
46
+ heightRange?: string;
47
+ count?: number;
48
+ prefixPath?: string;
49
+ allowPrefixes?: string[];
50
+ concurrency?: number;
51
+ beforeUpload?: (file: any) => boolean | Promise<boolean>;
52
+ transform?: (data: Record<string, any>) => Promise<Record<string, any>>;
53
+ }
54
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../src/hook/useUpload/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,WAAW,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;AAE/E,MAAM,WAAW,MAAM;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb,IAAI,EAAE,GAAG,CAAC;IACV,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC9B,MAAM,EAAE,YAAY,CAAC;IACrB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC/B,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACzD,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;CACzE"}