@duxweb/dvha-core 0.1.13 → 0.1.14
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/dist/cjs/hooks/upload.cjs +1 -1
- package/dist/esm/hooks/upload.js +40 -38
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Q=require("@vueuse/core"),R=require("lodash-es"),u=require("vue"),V=require("./upload/local.cjs");function W(l){const v=u.ref(!1),i=u.ref([]),d=u.ref(-1),f=u.ref(new Map),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Q=require("@vueuse/core"),R=require("lodash-es"),u=require("vue"),V=require("./upload/local.cjs");function W(l){const v=u.ref(!1),i=u.ref([]),d=u.ref(-1),f=u.ref(new Map),A=l.driver||V.createLocalUploadDriver(),$=u.computed(()=>{const{onProgress:e,onDataCallback:n,onCancel:a,onComplete:t,maxFileSize:c,maxFileCount:o,accept:s,multiple:r,autoUpload:m,options:U,onSuccess:P,onError:F,method:X,driver:Y,...J}=l;return J}),I=u.computed(()=>l.method||"POST"),b=u.computed(()=>{const e=i.value.length,n=i.value.reduce((o,s)=>o+(s.filesize||0),0);let a=0,t=0;return i.value.forEach((o,s)=>{var r;s<d.value?a+=o.filesize||0:s===d.value&&(a+=((r=o.progress)==null?void 0:r.loaded)||0)}),n>0&&(t=Math.round(a/n*100)),{index:d.value>=0?d.value+1:0,totalFiles:e,currentFile:i.value[d.value],totalPercent:t,totalLoaded:a,totalSize:n}}),w=e=>{if(e===0)return"0 Bytes";const n=1024,a=["Bytes","KB","MB","GB"],t=Math.floor(Math.log(e)/Math.log(n));return`${Number.parseFloat((e/n**t).toFixed(2))} ${a[t]}`},h=e=>{if(e===0)return"0 B/s";const n=1024,a=["B/s","KB/s","MB/s","GB/s"],t=Math.floor(Math.log(e)/Math.log(n));return`${Number.parseFloat((e/n**t).toFixed(2))} ${a[t]}`},x=()=>R.uniqueId("upload-"),k=async(e,n,a)=>{switch(n){case"file":{if(!(e instanceof File))throw new TypeError('Payload must be a File when type is "file"');return e}case"blob":{if(!(e instanceof Blob))throw new TypeError('Payload must be a Blob when type is "blob"');return new File([e],"blob-file",{type:e.type})}case"base64":{if(typeof e!="string")throw new TypeError('Payload must be a string when type is "base64"');const t=e.includes(",")?e.split(",")[1]:e,c=e.includes(",")?e.split(",")[0].split(":")[1].split(";")[0]:"application/octet-stream",o=atob(t),s=new Uint8Array(o.length);for(let m=0;m<o.length;m++)s[m]=o.charCodeAt(m);const r=new Blob([s],{type:c});return new File([r],"base64-file",{type:c})}case"arrayBuffer":{if(!(e instanceof ArrayBuffer))throw new TypeError('Payload must be ArrayBuffer when type is "arrayBuffer"');const t=new Blob([e]);return new File([t],"array-buffer-file",{type:"application/octet-stream"})}default:throw new Error(`Unsupported upload type: ${n}`)}},T=e=>{if(l.maxFileSize&&e.size>l.maxFileSize)throw new Error(`File size cannot exceed ${w(l.maxFileSize)}`);if(l.maxFileCount&&i.value.length>=l.maxFileCount)throw new Error(`File count cannot exceed ${l.maxFileCount}`)},q=async(e,n,a)=>{const t=await k(e,n);T(t);const c={id:x(),file:t,filename:t.name,filesize:t.size,filetype:t.type,filesizeText:w(t.size),status:"pending",progress:{loaded:0,total:t.size,percent:0,speed:0,speedText:h(0),remainingTime:0,uploadTime:0}};return i.value.push(c),c},g=(e,n)=>{const a=i.value.findIndex(t=>t.id===e);a!==-1&&(i.value[a]={...i.value[a],...n})},z=()=>{var n;i.value.every(a=>a.status!=="uploading")&&((n=l.onComplete)==null||n.call(l,i.value))},y=e=>{var a;const n=i.value.find(t=>t.id===e);if(!n||n.status!=="pending"&&n.status!=="uploading")return!1;if(n.status==="uploading"){const t=f.value.get(e);t&&(t.abort(),f.value.delete(e))}return g(e,{status:"cancelled"}),(a=l.onCancel)==null||a.call(l,e),z(),!0},D=e=>{e?e.forEach(n=>y(n)):i.value.forEach(n=>y(n.id))},G=async e=>{if(!e.file)throw new Error("File not found");T(e.file);const n=new AbortController;f.value.set(e.id,n);const a=Date.now();g(e.id,{status:"uploading"}),await A.upload(e.file,{...$.value,method:I.value,signal:n.signal,onUploadProgress:t=>{var F;const s=(Date.now()-a)/1e3,r=s>0?t.loaded/s:0,m=(t.total||0)-t.loaded,U=r>0?m/r:0,P={loaded:t.loaded,total:t.total,percent:t.percent||0,speed:Math.round(r),speedText:h(r),uploadTime:Math.round(s),remainingTime:Math.round(U)};g(e.id,{status:"uploading",progress:P}),(F=l.onProgress)==null||F.call(l,b.value)}}).then(t=>{var s,r;if(e.status==="cancelled")return;const c=((s=l.onDataCallback)==null?void 0:s.call(l,t,e))||{},o=Math.round((Date.now()-a)/1e3);g(e.id,{status:"success",data:t,...c,progress:{...e.progress,loaded:e.filesize||0,percent:100,speed:0,speedText:h(0),uploadTime:o,remainingTime:0}}),f.value.delete(e.id),(r=l.onSuccess)==null||r.call(l,t)}).catch(t=>{if(!(t.message==="canceled"||e.status==="cancelled"))throw g(e.id,{status:"error",error:t.message}),f.value.delete(e.id),t}).finally(()=>{z()})},C=async()=>{var n;const e=i.value.filter(a=>a.status==="pending");if(e.length!==0){v.value=!0;try{for(let a=0;a<e.length;a++){const t=e[a],c=i.value.findIndex(o=>o.id===t.id);d.value=c,await G(t)}}catch(a){throw(n=l.onError)==null||n.call(l,{status:(a==null?void 0:a.status)||500,message:(a==null?void 0:a.message)||"upload failed"}),a}finally{v.value=!1,d.value=-1}}},B=async(e,n="file")=>{var a;try{if(l.multiple||E(),!l.multiple&&e.length>1)throw new Error("Single file mode: only one file can be selected");if(l.maxFileCount&&i.value.length+e.length>l.maxFileCount)throw new Error(`Adding ${e.length} files would exceed the maximum limit of ${l.maxFileCount}`);for(const t of e)await q(t,n)}catch(t){throw(a=l.onError)==null||a.call(l,{status:(t==null?void 0:t.status)||400,message:(t==null?void 0:t.message)||"Failed to add files"}),t}l.autoUpload&&C()},K=e=>{const n=e.map(a=>j(a));i.value.push(...n)},E=()=>{i.value.forEach(e=>{y(e.id)}),f.value.clear(),i.value=[],d.value=-1},M=e=>{const n=i.value.find(t=>t.id===e);if(n&&n.status==="uploading"){g(e,{status:"cancelled"});const t=f.value.get(e);if(!t)return;t.abort(),f.value.delete(e)}const a=i.value.findIndex(t=>t.id===e);a!==-1&&i.value.splice(a,1)},L=e=>{e?e.forEach(n=>M(n)):i.value.forEach(n=>M(n.id))},{files:N,open:O,reset:S}=Q.useFileDialog({accept:l.accept,multiple:l.multiple||!1}),j=e=>({id:x(),...e,filesizeText:e.filesize?w(e.filesize):void 0,status:e.url?"success":"pending",progress:{loaded:0,total:e.filesize||0,percent:e.url?100:0,speed:0,speedText:h(0),remainingTime:0,uploadTime:0}});u.watch(N,async e=>{if(e&&e.length>0){const n=Array.from(e);S(),await B(n,"file").catch(a=>{console.warn("Failed to add selected files:",a)})}});const H=u.computed(()=>i.value.filter(e=>e.status==="success").map(e=>({url:e.url,filename:e.filename,filesize:e.filesize,filetype:e.filetype})));return{isUploading:v,uploadFiles:i,dataFiles:H,progress:b,open:O,trigger:C,resetFiles:S,clearFiles:E,removeFiles:L,addFiles:B,addDataFiles:K,cancelFiles:D}}exports.useUpload=W;
|
package/dist/esm/hooks/upload.js
CHANGED
|
@@ -3,15 +3,15 @@ import { uniqueId as V } from "lodash-es";
|
|
|
3
3
|
import { ref as g, computed as v, watch as W } from "vue";
|
|
4
4
|
import { createLocalUploadDriver as X } from "./upload/local.js";
|
|
5
5
|
function ne(l) {
|
|
6
|
-
const w = g(!1), i = g([]), u = g(-1), d = g(/* @__PURE__ */ new Map()),
|
|
7
|
-
const { onProgress: e, onDataCallback: n, onCancel: a, onComplete: t, maxFileSize: c, maxFileCount:
|
|
6
|
+
const w = g(!1), i = g([]), u = g(-1), d = g(/* @__PURE__ */ new Map()), $ = l.driver || X(), I = v(() => {
|
|
7
|
+
const { onProgress: e, onDataCallback: n, onCancel: a, onComplete: t, maxFileSize: c, maxFileCount: s, accept: o, multiple: r, autoUpload: f, options: P, onSuccess: A, onError: x, method: Y, driver: Z, ...Q } = l;
|
|
8
8
|
return Q;
|
|
9
|
-
}),
|
|
10
|
-
const e = i.value.length, n = i.value.reduce((
|
|
9
|
+
}), k = v(() => l.method || "POST"), b = v(() => {
|
|
10
|
+
const e = i.value.length, n = i.value.reduce((s, o) => s + (o.filesize || 0), 0);
|
|
11
11
|
let a = 0, t = 0;
|
|
12
|
-
return i.value.forEach((
|
|
12
|
+
return i.value.forEach((s, o) => {
|
|
13
13
|
var r;
|
|
14
|
-
|
|
14
|
+
o < u.value ? a += s.filesize || 0 : o === u.value && (a += ((r = s.progress) == null ? void 0 : r.loaded) || 0);
|
|
15
15
|
}), n > 0 && (t = Math.round(a / n * 100)), {
|
|
16
16
|
index: u.value >= 0 ? u.value + 1 : 0,
|
|
17
17
|
totalFiles: e,
|
|
@@ -30,7 +30,7 @@ function ne(l) {
|
|
|
30
30
|
return "0 B/s";
|
|
31
31
|
const n = 1024, a = ["B/s", "KB/s", "MB/s", "GB/s"], t = Math.floor(Math.log(e) / Math.log(n));
|
|
32
32
|
return `${Number.parseFloat((e / n ** t).toFixed(2))} ${a[t]}`;
|
|
33
|
-
}, T = () => V("upload-"),
|
|
33
|
+
}, T = () => V("upload-"), D = async (e, n, a) => {
|
|
34
34
|
switch (n) {
|
|
35
35
|
case "file": {
|
|
36
36
|
if (!(e instanceof File))
|
|
@@ -45,10 +45,10 @@ function ne(l) {
|
|
|
45
45
|
case "base64": {
|
|
46
46
|
if (typeof e != "string")
|
|
47
47
|
throw new TypeError('Payload must be a string when type is "base64"');
|
|
48
|
-
const t = e.includes(",") ? e.split(",")[1] : e, c = e.includes(",") ? e.split(",")[0].split(":")[1].split(";")[0] : "application/octet-stream",
|
|
49
|
-
for (let f = 0; f <
|
|
50
|
-
|
|
51
|
-
const r = new Blob([
|
|
48
|
+
const t = e.includes(",") ? e.split(",")[1] : e, c = e.includes(",") ? e.split(",")[0].split(":")[1].split(";")[0] : "application/octet-stream", s = atob(t), o = new Uint8Array(s.length);
|
|
49
|
+
for (let f = 0; f < s.length; f++)
|
|
50
|
+
o[f] = s.charCodeAt(f);
|
|
51
|
+
const r = new Blob([o], { type: c });
|
|
52
52
|
return new File([r], "base64-file", { type: c });
|
|
53
53
|
}
|
|
54
54
|
case "arrayBuffer": {
|
|
@@ -65,8 +65,8 @@ function ne(l) {
|
|
|
65
65
|
throw new Error(`File size cannot exceed ${y(l.maxFileSize)}`);
|
|
66
66
|
if (l.maxFileCount && i.value.length >= l.maxFileCount)
|
|
67
67
|
throw new Error(`File count cannot exceed ${l.maxFileCount}`);
|
|
68
|
-
},
|
|
69
|
-
const t = await
|
|
68
|
+
}, G = async (e, n, a) => {
|
|
69
|
+
const t = await D(e, n);
|
|
70
70
|
z(t);
|
|
71
71
|
const c = {
|
|
72
72
|
id: T(),
|
|
@@ -105,40 +105,40 @@ function ne(l) {
|
|
|
105
105
|
return m(e, {
|
|
106
106
|
status: "cancelled"
|
|
107
107
|
}), (a = l.onCancel) == null || a.call(l, e), C(), !0;
|
|
108
|
-
},
|
|
108
|
+
}, K = (e) => {
|
|
109
109
|
e ? e.forEach((n) => F(n)) : i.value.forEach((n) => F(n.id));
|
|
110
|
-
},
|
|
110
|
+
}, L = async (e) => {
|
|
111
111
|
if (!e.file)
|
|
112
112
|
throw new Error("File not found");
|
|
113
113
|
z(e.file);
|
|
114
114
|
const n = new AbortController();
|
|
115
115
|
d.value.set(e.id, n);
|
|
116
116
|
const a = Date.now();
|
|
117
|
-
m(e.id, { status: "uploading" }), await
|
|
118
|
-
|
|
119
|
-
method:
|
|
117
|
+
m(e.id, { status: "uploading" }), await $.upload(e.file, {
|
|
118
|
+
...I.value,
|
|
119
|
+
method: k.value,
|
|
120
120
|
signal: n.signal,
|
|
121
121
|
onUploadProgress: (t) => {
|
|
122
122
|
var x;
|
|
123
|
-
const
|
|
123
|
+
const o = (Date.now() - a) / 1e3, r = o > 0 ? t.loaded / o : 0, f = (t.total || 0) - t.loaded, P = r > 0 ? f / r : 0, A = {
|
|
124
124
|
loaded: t.loaded,
|
|
125
125
|
total: t.total,
|
|
126
126
|
percent: t.percent || 0,
|
|
127
127
|
speed: Math.round(r),
|
|
128
128
|
speedText: h(r),
|
|
129
|
-
uploadTime: Math.round(
|
|
130
|
-
remainingTime: Math.round(
|
|
129
|
+
uploadTime: Math.round(o),
|
|
130
|
+
remainingTime: Math.round(P)
|
|
131
131
|
};
|
|
132
132
|
m(e.id, {
|
|
133
133
|
status: "uploading",
|
|
134
|
-
progress:
|
|
134
|
+
progress: A
|
|
135
135
|
}), (x = l.onProgress) == null || x.call(l, b.value);
|
|
136
136
|
}
|
|
137
137
|
}).then((t) => {
|
|
138
|
-
var
|
|
138
|
+
var o, r;
|
|
139
139
|
if (e.status === "cancelled")
|
|
140
140
|
return;
|
|
141
|
-
const c = ((
|
|
141
|
+
const c = ((o = l.onDataCallback) == null ? void 0 : o.call(l, t, e)) || {}, s = Math.round((Date.now() - a) / 1e3);
|
|
142
142
|
m(e.id, {
|
|
143
143
|
status: "success",
|
|
144
144
|
data: t,
|
|
@@ -149,7 +149,7 @@ function ne(l) {
|
|
|
149
149
|
percent: 100,
|
|
150
150
|
speed: 0,
|
|
151
151
|
speedText: h(0),
|
|
152
|
-
uploadTime:
|
|
152
|
+
uploadTime: s,
|
|
153
153
|
remainingTime: 0
|
|
154
154
|
}
|
|
155
155
|
}), d.value.delete(e.id), (r = l.onSuccess) == null || r.call(l, t);
|
|
@@ -169,8 +169,8 @@ function ne(l) {
|
|
|
169
169
|
w.value = !0;
|
|
170
170
|
try {
|
|
171
171
|
for (let a = 0; a < e.length; a++) {
|
|
172
|
-
const t = e[a], c = i.value.findIndex((
|
|
173
|
-
u.value = c, await
|
|
172
|
+
const t = e[a], c = i.value.findIndex((s) => s.id === t.id);
|
|
173
|
+
u.value = c, await L(t);
|
|
174
174
|
}
|
|
175
175
|
} catch (a) {
|
|
176
176
|
throw (n = l.onError) == null || n.call(l, {
|
|
@@ -184,10 +184,12 @@ function ne(l) {
|
|
|
184
184
|
}, E = async (e, n = "file") => {
|
|
185
185
|
var a;
|
|
186
186
|
try {
|
|
187
|
+
if (l.multiple || M(), !l.multiple && e.length > 1)
|
|
188
|
+
throw new Error("Single file mode: only one file can be selected");
|
|
187
189
|
if (l.maxFileCount && i.value.length + e.length > l.maxFileCount)
|
|
188
190
|
throw new Error(`Adding ${e.length} files would exceed the maximum limit of ${l.maxFileCount}`);
|
|
189
191
|
for (const t of e)
|
|
190
|
-
await
|
|
192
|
+
await G(t, n);
|
|
191
193
|
} catch (t) {
|
|
192
194
|
throw (a = l.onError) == null || a.call(l, {
|
|
193
195
|
status: (t == null ? void 0 : t.status) || 400,
|
|
@@ -195,14 +197,14 @@ function ne(l) {
|
|
|
195
197
|
}), t;
|
|
196
198
|
}
|
|
197
199
|
l.autoUpload && B();
|
|
198
|
-
},
|
|
200
|
+
}, N = (e) => {
|
|
199
201
|
const n = e.map((a) => H(a));
|
|
200
202
|
i.value.push(...n);
|
|
201
|
-
},
|
|
203
|
+
}, M = () => {
|
|
202
204
|
i.value.forEach((e) => {
|
|
203
205
|
F(e.id);
|
|
204
206
|
}), d.value.clear(), i.value = [], u.value = -1;
|
|
205
|
-
},
|
|
207
|
+
}, S = (e) => {
|
|
206
208
|
const n = i.value.find((t) => t.id === e);
|
|
207
209
|
if (n && n.status === "uploading") {
|
|
208
210
|
m(e, {
|
|
@@ -216,8 +218,8 @@ function ne(l) {
|
|
|
216
218
|
const a = i.value.findIndex((t) => t.id === e);
|
|
217
219
|
a !== -1 && i.value.splice(a, 1);
|
|
218
220
|
}, q = (e) => {
|
|
219
|
-
e ? e.forEach((n) =>
|
|
220
|
-
}, { files: O, open: j, reset:
|
|
221
|
+
e ? e.forEach((n) => S(n)) : i.value.forEach((n) => S(n.id));
|
|
222
|
+
}, { files: O, open: j, reset: U } = R({
|
|
221
223
|
accept: l.accept,
|
|
222
224
|
multiple: l.multiple || !1
|
|
223
225
|
}), H = (e) => ({
|
|
@@ -238,7 +240,7 @@ function ne(l) {
|
|
|
238
240
|
W(O, async (e) => {
|
|
239
241
|
if (e && e.length > 0) {
|
|
240
242
|
const n = Array.from(e);
|
|
241
|
-
|
|
243
|
+
U(), await E(n, "file").catch((a) => {
|
|
242
244
|
console.warn("Failed to add selected files:", a);
|
|
243
245
|
});
|
|
244
246
|
}
|
|
@@ -256,12 +258,12 @@ function ne(l) {
|
|
|
256
258
|
progress: b,
|
|
257
259
|
open: j,
|
|
258
260
|
trigger: B,
|
|
259
|
-
resetFiles:
|
|
260
|
-
clearFiles:
|
|
261
|
+
resetFiles: U,
|
|
262
|
+
clearFiles: M,
|
|
261
263
|
removeFiles: q,
|
|
262
264
|
addFiles: E,
|
|
263
|
-
addDataFiles:
|
|
264
|
-
cancelFiles:
|
|
265
|
+
addDataFiles: N,
|
|
266
|
+
cancelFiles: K
|
|
265
267
|
};
|
|
266
268
|
}
|
|
267
269
|
export {
|