@v-c/upload 0.0.1
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/LICENSE +21 -0
- package/dist/AjaxUploader.cjs +415 -0
- package/dist/AjaxUploader.d.ts +3 -0
- package/dist/AjaxUploader.js +408 -0
- package/dist/Upload.cjs +196 -0
- package/dist/Upload.d.ts +3 -0
- package/dist/Upload.js +190 -0
- package/dist/_virtual/rolldown_runtime.cjs +21 -0
- package/dist/attrAccept.cjs +34 -0
- package/dist/attrAccept.d.ts +2 -0
- package/dist/attrAccept.js +29 -0
- package/dist/index.cjs +7 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/interface.cjs +1 -0
- package/dist/interface.d.ts +82 -0
- package/dist/interface.js +0 -0
- package/dist/request.cjs +60 -0
- package/dist/request.d.ts +4 -0
- package/dist/request.js +56 -0
- package/dist/traverseFileTree.cjs +55 -0
- package/dist/traverseFileTree.d.ts +17 -0
- package/dist/traverseFileTree.js +51 -0
- package/dist/uid.cjs +10 -0
- package/dist/uid.d.ts +1 -0
- package/dist/uid.js +6 -0
- package/package.json +39 -0
package/dist/Upload.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import AjaxUploader_default from "./AjaxUploader.js";
|
|
2
|
+
import { computed, createVNode, defineComponent, mergeDefaults, mergeProps, ref } from "vue";
|
|
3
|
+
function empty() {}
|
|
4
|
+
var defaults = {
|
|
5
|
+
component: "span",
|
|
6
|
+
prefixCls: "vc-upload",
|
|
7
|
+
data: {},
|
|
8
|
+
headers: {},
|
|
9
|
+
name: "file",
|
|
10
|
+
onStart: empty,
|
|
11
|
+
onError: empty,
|
|
12
|
+
onSuccess: empty,
|
|
13
|
+
multiple: false,
|
|
14
|
+
beforeUpload: void 0,
|
|
15
|
+
customRequest: void 0,
|
|
16
|
+
withCredentials: false,
|
|
17
|
+
openFileDialogOnClick: true,
|
|
18
|
+
hasControlInside: false
|
|
19
|
+
};
|
|
20
|
+
var Upload_default = /* @__PURE__ */ defineComponent((props, { attrs, expose, slots }) => {
|
|
21
|
+
const uploaderRef = ref();
|
|
22
|
+
const abort = (file) => {
|
|
23
|
+
uploaderRef.value?.abort(file);
|
|
24
|
+
};
|
|
25
|
+
expose({ abort });
|
|
26
|
+
const mergedProps = computed(() => ({
|
|
27
|
+
...defaults,
|
|
28
|
+
...props
|
|
29
|
+
}));
|
|
30
|
+
return () => createVNode(AjaxUploader_default, mergeProps({ "ref": uploaderRef }, mergedProps.value, attrs), { default: () => [slots.default?.()] });
|
|
31
|
+
}, {
|
|
32
|
+
props: /* @__PURE__ */ mergeDefaults({
|
|
33
|
+
name: {
|
|
34
|
+
type: String,
|
|
35
|
+
required: false,
|
|
36
|
+
default: void 0
|
|
37
|
+
},
|
|
38
|
+
style: {
|
|
39
|
+
type: Object,
|
|
40
|
+
required: false,
|
|
41
|
+
default: void 0
|
|
42
|
+
},
|
|
43
|
+
className: {
|
|
44
|
+
type: String,
|
|
45
|
+
required: false,
|
|
46
|
+
default: void 0
|
|
47
|
+
},
|
|
48
|
+
disabled: {
|
|
49
|
+
type: Boolean,
|
|
50
|
+
required: false,
|
|
51
|
+
default: void 0
|
|
52
|
+
},
|
|
53
|
+
component: {
|
|
54
|
+
type: [
|
|
55
|
+
Object,
|
|
56
|
+
Function,
|
|
57
|
+
String
|
|
58
|
+
],
|
|
59
|
+
required: false,
|
|
60
|
+
default: void 0
|
|
61
|
+
},
|
|
62
|
+
action: {
|
|
63
|
+
type: [String, Function],
|
|
64
|
+
required: false,
|
|
65
|
+
default: void 0
|
|
66
|
+
},
|
|
67
|
+
method: {
|
|
68
|
+
type: String,
|
|
69
|
+
required: false,
|
|
70
|
+
default: void 0
|
|
71
|
+
},
|
|
72
|
+
directory: {
|
|
73
|
+
type: Boolean,
|
|
74
|
+
required: false,
|
|
75
|
+
default: void 0
|
|
76
|
+
},
|
|
77
|
+
data: {
|
|
78
|
+
type: [Object, Function],
|
|
79
|
+
required: false,
|
|
80
|
+
default: void 0
|
|
81
|
+
},
|
|
82
|
+
headers: {
|
|
83
|
+
type: Object,
|
|
84
|
+
required: false,
|
|
85
|
+
default: void 0
|
|
86
|
+
},
|
|
87
|
+
accept: {
|
|
88
|
+
type: [String, Object],
|
|
89
|
+
required: false,
|
|
90
|
+
default: void 0
|
|
91
|
+
},
|
|
92
|
+
multiple: {
|
|
93
|
+
type: Boolean,
|
|
94
|
+
required: false,
|
|
95
|
+
default: void 0
|
|
96
|
+
},
|
|
97
|
+
onBatchStart: {
|
|
98
|
+
type: Function,
|
|
99
|
+
required: false,
|
|
100
|
+
default: void 0
|
|
101
|
+
},
|
|
102
|
+
onStart: {
|
|
103
|
+
type: Function,
|
|
104
|
+
required: false,
|
|
105
|
+
default: void 0
|
|
106
|
+
},
|
|
107
|
+
onError: {
|
|
108
|
+
type: Function,
|
|
109
|
+
required: false,
|
|
110
|
+
default: void 0
|
|
111
|
+
},
|
|
112
|
+
onSuccess: {
|
|
113
|
+
type: Function,
|
|
114
|
+
required: false,
|
|
115
|
+
default: void 0
|
|
116
|
+
},
|
|
117
|
+
onProgress: {
|
|
118
|
+
type: Function,
|
|
119
|
+
required: false,
|
|
120
|
+
default: void 0
|
|
121
|
+
},
|
|
122
|
+
beforeUpload: {
|
|
123
|
+
type: Function,
|
|
124
|
+
required: false,
|
|
125
|
+
default: void 0
|
|
126
|
+
},
|
|
127
|
+
customRequest: {
|
|
128
|
+
type: Function,
|
|
129
|
+
required: false,
|
|
130
|
+
default: void 0
|
|
131
|
+
},
|
|
132
|
+
withCredentials: {
|
|
133
|
+
type: Boolean,
|
|
134
|
+
required: false,
|
|
135
|
+
default: void 0
|
|
136
|
+
},
|
|
137
|
+
openFileDialogOnClick: {
|
|
138
|
+
type: Boolean,
|
|
139
|
+
required: false,
|
|
140
|
+
default: void 0
|
|
141
|
+
},
|
|
142
|
+
prefixCls: {
|
|
143
|
+
type: String,
|
|
144
|
+
required: false,
|
|
145
|
+
default: void 0
|
|
146
|
+
},
|
|
147
|
+
id: {
|
|
148
|
+
type: String,
|
|
149
|
+
required: false,
|
|
150
|
+
default: void 0
|
|
151
|
+
},
|
|
152
|
+
onMouseEnter: {
|
|
153
|
+
type: Function,
|
|
154
|
+
required: false,
|
|
155
|
+
default: void 0
|
|
156
|
+
},
|
|
157
|
+
onMouseLeave: {
|
|
158
|
+
type: Function,
|
|
159
|
+
required: false,
|
|
160
|
+
default: void 0
|
|
161
|
+
},
|
|
162
|
+
onClick: {
|
|
163
|
+
type: Function,
|
|
164
|
+
required: false,
|
|
165
|
+
default: void 0
|
|
166
|
+
},
|
|
167
|
+
classNames: {
|
|
168
|
+
type: Object,
|
|
169
|
+
required: false,
|
|
170
|
+
default: void 0
|
|
171
|
+
},
|
|
172
|
+
styles: {
|
|
173
|
+
type: Object,
|
|
174
|
+
required: false,
|
|
175
|
+
default: void 0
|
|
176
|
+
},
|
|
177
|
+
hasControlInside: {
|
|
178
|
+
type: Boolean,
|
|
179
|
+
required: false,
|
|
180
|
+
default: void 0
|
|
181
|
+
},
|
|
182
|
+
pastable: {
|
|
183
|
+
type: Boolean,
|
|
184
|
+
required: false,
|
|
185
|
+
default: void 0
|
|
186
|
+
}
|
|
187
|
+
}, defaults),
|
|
188
|
+
name: "Upload"
|
|
189
|
+
});
|
|
190
|
+
export { Upload_default as default };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __copyProps = (to, from, except, desc) => {
|
|
8
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
9
|
+
key = keys[i];
|
|
10
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
11
|
+
get: ((k) => from[k]).bind(null, key),
|
|
12
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
18
|
+
value: mod,
|
|
19
|
+
enumerable: true
|
|
20
|
+
}) : target, mod));
|
|
21
|
+
exports.__toESM = __toESM;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
const require_rolldown_runtime = require("./_virtual/rolldown_runtime.cjs");
|
|
6
|
+
let _v_c_util = require("@v-c/util");
|
|
7
|
+
var attrAccept_default = (file, acceptedFiles) => {
|
|
8
|
+
if (file && acceptedFiles) {
|
|
9
|
+
const acceptedFilesArray = Array.isArray(acceptedFiles) ? acceptedFiles : acceptedFiles.split(",");
|
|
10
|
+
const fileName = file.name || "";
|
|
11
|
+
const mimeType = file.type || "";
|
|
12
|
+
const baseMimeType = mimeType.replace(/\/.*$/, "");
|
|
13
|
+
return acceptedFilesArray.some((type) => {
|
|
14
|
+
const validType = type.trim();
|
|
15
|
+
if (/^\*(\/\*)?$/.test(type)) return true;
|
|
16
|
+
if (validType.charAt(0) === ".") {
|
|
17
|
+
const lowerFileName = fileName.toLowerCase();
|
|
18
|
+
const lowerType = validType.toLowerCase();
|
|
19
|
+
let affixList = [lowerType];
|
|
20
|
+
if (lowerType === ".jpg" || lowerType === ".jpeg") affixList = [".jpg", ".jpeg"];
|
|
21
|
+
return affixList.some((affix) => lowerFileName.endsWith(affix));
|
|
22
|
+
}
|
|
23
|
+
if (/\/\*$/.test(validType)) return baseMimeType === validType.replace(/\/.*$/, "");
|
|
24
|
+
if (mimeType === validType) return true;
|
|
25
|
+
if (/^\w+$/.test(validType)) {
|
|
26
|
+
(0, _v_c_util.warning)(false, `Upload takes an invalidate 'accept' type '${validType}'.Skip for check.`);
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return true;
|
|
33
|
+
};
|
|
34
|
+
exports.default = attrAccept_default;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { warning } from "@v-c/util";
|
|
2
|
+
var attrAccept_default = (file, acceptedFiles) => {
|
|
3
|
+
if (file && acceptedFiles) {
|
|
4
|
+
const acceptedFilesArray = Array.isArray(acceptedFiles) ? acceptedFiles : acceptedFiles.split(",");
|
|
5
|
+
const fileName = file.name || "";
|
|
6
|
+
const mimeType = file.type || "";
|
|
7
|
+
const baseMimeType = mimeType.replace(/\/.*$/, "");
|
|
8
|
+
return acceptedFilesArray.some((type) => {
|
|
9
|
+
const validType = type.trim();
|
|
10
|
+
if (/^\*(\/\*)?$/.test(type)) return true;
|
|
11
|
+
if (validType.charAt(0) === ".") {
|
|
12
|
+
const lowerFileName = fileName.toLowerCase();
|
|
13
|
+
const lowerType = validType.toLowerCase();
|
|
14
|
+
let affixList = [lowerType];
|
|
15
|
+
if (lowerType === ".jpg" || lowerType === ".jpeg") affixList = [".jpg", ".jpeg"];
|
|
16
|
+
return affixList.some((affix) => lowerFileName.endsWith(affix));
|
|
17
|
+
}
|
|
18
|
+
if (/\/\*$/.test(validType)) return baseMimeType === validType.replace(/\/.*$/, "");
|
|
19
|
+
if (mimeType === validType) return true;
|
|
20
|
+
if (/^\w+$/.test(validType)) {
|
|
21
|
+
warning(false, `Upload takes an invalidate 'accept' type '${validType}'.Skip for check.`);
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
};
|
|
29
|
+
export { attrAccept_default as default };
|
package/dist/index.cjs
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Component, CSSProperties } from 'vue';
|
|
2
|
+
export interface VcFile extends File {
|
|
3
|
+
uid: string;
|
|
4
|
+
}
|
|
5
|
+
export type BeforeUploadFileType = File | Blob | boolean | string;
|
|
6
|
+
export type Action = string | ((file: VcFile) => string | PromiseLike<string>);
|
|
7
|
+
export interface AcceptConfig {
|
|
8
|
+
format: string;
|
|
9
|
+
filter?: 'native' | ((file: VcFile) => boolean);
|
|
10
|
+
}
|
|
11
|
+
export type UploadRequestMethod = 'POST' | 'PUT' | 'PATCH' | 'post' | 'put' | 'patch';
|
|
12
|
+
export type UploadRequestHeader = Record<string, string>;
|
|
13
|
+
export type UploadRequestFile = Exclude<BeforeUploadFileType, File | boolean> | VcFile;
|
|
14
|
+
export interface UploadRequestError extends Error {
|
|
15
|
+
status?: number;
|
|
16
|
+
method?: UploadRequestMethod;
|
|
17
|
+
url?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface UploadProgressEvent extends Partial<ProgressEvent> {
|
|
20
|
+
percent?: number;
|
|
21
|
+
}
|
|
22
|
+
export interface AjaxUploaderExpose {
|
|
23
|
+
abort: (file: any) => void;
|
|
24
|
+
}
|
|
25
|
+
export interface UploadRequestOption<T = any> {
|
|
26
|
+
onProgress?: (event: UploadProgressEvent, file?: UploadRequestFile) => void;
|
|
27
|
+
onError?: (event: UploadRequestError | ProgressEvent, body?: T) => void;
|
|
28
|
+
onSuccess?: (body: T, fileOrXhr?: UploadRequestFile | XMLHttpRequest) => void;
|
|
29
|
+
data?: Record<string, unknown>;
|
|
30
|
+
filename?: string;
|
|
31
|
+
file: UploadRequestFile;
|
|
32
|
+
withCredentials?: boolean;
|
|
33
|
+
action: string;
|
|
34
|
+
headers?: UploadRequestHeader;
|
|
35
|
+
method: UploadRequestMethod;
|
|
36
|
+
}
|
|
37
|
+
export type CustomUploadRequestOption = (option: UploadRequestOption, info: {
|
|
38
|
+
defaultRequest: (option: UploadRequestOption) => {
|
|
39
|
+
abort: () => void;
|
|
40
|
+
} | void;
|
|
41
|
+
}) => void | {
|
|
42
|
+
abort: () => void;
|
|
43
|
+
};
|
|
44
|
+
export interface UploadProps {
|
|
45
|
+
name?: string;
|
|
46
|
+
style?: CSSProperties;
|
|
47
|
+
className?: string;
|
|
48
|
+
disabled?: boolean;
|
|
49
|
+
component?: Component | string;
|
|
50
|
+
action?: Action;
|
|
51
|
+
method?: UploadRequestMethod;
|
|
52
|
+
directory?: boolean;
|
|
53
|
+
data?: Record<string, unknown> | ((file: VcFile | string | Blob) => Record<string, unknown>);
|
|
54
|
+
headers?: UploadRequestHeader;
|
|
55
|
+
accept?: string | AcceptConfig;
|
|
56
|
+
multiple?: boolean;
|
|
57
|
+
onBatchStart?: (fileList: {
|
|
58
|
+
file: VcFile;
|
|
59
|
+
parsedFile: Exclude<BeforeUploadFileType, boolean> | null;
|
|
60
|
+
}[]) => void;
|
|
61
|
+
onStart?: (file: VcFile) => void;
|
|
62
|
+
onError?: (error: Error, ret: Record<string, unknown>, file: VcFile | null) => void;
|
|
63
|
+
onSuccess?: (response: Record<string, unknown>, file: VcFile | null, xhr: XMLHttpRequest) => void;
|
|
64
|
+
onProgress?: (event: UploadProgressEvent, file: VcFile | null) => void;
|
|
65
|
+
beforeUpload?: (file: VcFile, FileList: VcFile[]) => BeforeUploadFileType | Promise<void | BeforeUploadFileType> | void;
|
|
66
|
+
customRequest?: CustomUploadRequestOption;
|
|
67
|
+
withCredentials?: boolean;
|
|
68
|
+
openFileDialogOnClick?: boolean;
|
|
69
|
+
prefixCls?: string;
|
|
70
|
+
id?: string;
|
|
71
|
+
onMouseEnter?: (e: MouseEvent) => void;
|
|
72
|
+
onMouseLeave?: (e: MouseEvent) => void;
|
|
73
|
+
onClick?: (e: MouseEvent | KeyboardEvent) => void;
|
|
74
|
+
classNames?: {
|
|
75
|
+
input?: string;
|
|
76
|
+
};
|
|
77
|
+
styles?: {
|
|
78
|
+
input?: CSSProperties;
|
|
79
|
+
};
|
|
80
|
+
hasControlInside?: boolean;
|
|
81
|
+
pastable?: boolean;
|
|
82
|
+
}
|
|
File without changes
|
package/dist/request.cjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
function getError(option, xhr) {
|
|
6
|
+
const msg = `cannot ${option.method} ${option.action} ${xhr.status}'`;
|
|
7
|
+
const err = new Error(msg);
|
|
8
|
+
err.status = xhr.status;
|
|
9
|
+
err.method = option.method;
|
|
10
|
+
err.url = option.action;
|
|
11
|
+
return err;
|
|
12
|
+
}
|
|
13
|
+
function getBody(xhr) {
|
|
14
|
+
const text = xhr.responseText || xhr.response;
|
|
15
|
+
if (!text) return text;
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(text);
|
|
18
|
+
} catch (e) {
|
|
19
|
+
return text;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function upload(option) {
|
|
23
|
+
const xhr = new XMLHttpRequest();
|
|
24
|
+
if (option.onProgress && xhr.upload) xhr.upload.onprogress = function progress(e) {
|
|
25
|
+
if (e.total > 0) e.percent = e.loaded / e.total * 100;
|
|
26
|
+
option.onProgress?.(e);
|
|
27
|
+
};
|
|
28
|
+
const formData = new FormData();
|
|
29
|
+
if (option.data) Object.keys(option.data).forEach((key) => {
|
|
30
|
+
const value = option.data[key];
|
|
31
|
+
if (Array.isArray(value)) {
|
|
32
|
+
value.forEach((item) => {
|
|
33
|
+
formData.append(`${key}[]`, item);
|
|
34
|
+
});
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
formData.append(key, value);
|
|
38
|
+
});
|
|
39
|
+
if (option.file instanceof Blob) formData.append(option.filename, option.file, option.file.name);
|
|
40
|
+
else formData.append(option.filename, option.file);
|
|
41
|
+
xhr.onerror = function error(e) {
|
|
42
|
+
option.onError?.(e);
|
|
43
|
+
};
|
|
44
|
+
xhr.onload = function onload() {
|
|
45
|
+
if (xhr.status < 200 || xhr.status >= 300) return option.onError?.(getError(option, xhr), getBody(xhr));
|
|
46
|
+
return option.onSuccess?.(getBody(xhr), xhr);
|
|
47
|
+
};
|
|
48
|
+
xhr.open(option.method, option.action, true);
|
|
49
|
+
if (option.withCredentials && "withCredentials" in xhr) xhr.withCredentials = true;
|
|
50
|
+
const headers = option.headers || {};
|
|
51
|
+
if (headers["X-Requested-With"] !== null) xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
|
52
|
+
Object.keys(headers).forEach((h) => {
|
|
53
|
+
if (headers[h] !== null) xhr.setRequestHeader(h, headers[h]);
|
|
54
|
+
});
|
|
55
|
+
xhr.send(formData);
|
|
56
|
+
return { abort() {
|
|
57
|
+
xhr.abort();
|
|
58
|
+
} };
|
|
59
|
+
}
|
|
60
|
+
exports.default = upload;
|
package/dist/request.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
function getError(option, xhr) {
|
|
2
|
+
const msg = `cannot ${option.method} ${option.action} ${xhr.status}'`;
|
|
3
|
+
const err = new Error(msg);
|
|
4
|
+
err.status = xhr.status;
|
|
5
|
+
err.method = option.method;
|
|
6
|
+
err.url = option.action;
|
|
7
|
+
return err;
|
|
8
|
+
}
|
|
9
|
+
function getBody(xhr) {
|
|
10
|
+
const text = xhr.responseText || xhr.response;
|
|
11
|
+
if (!text) return text;
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(text);
|
|
14
|
+
} catch (e) {
|
|
15
|
+
return text;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function upload(option) {
|
|
19
|
+
const xhr = new XMLHttpRequest();
|
|
20
|
+
if (option.onProgress && xhr.upload) xhr.upload.onprogress = function progress(e) {
|
|
21
|
+
if (e.total > 0) e.percent = e.loaded / e.total * 100;
|
|
22
|
+
option.onProgress?.(e);
|
|
23
|
+
};
|
|
24
|
+
const formData = new FormData();
|
|
25
|
+
if (option.data) Object.keys(option.data).forEach((key) => {
|
|
26
|
+
const value = option.data[key];
|
|
27
|
+
if (Array.isArray(value)) {
|
|
28
|
+
value.forEach((item) => {
|
|
29
|
+
formData.append(`${key}[]`, item);
|
|
30
|
+
});
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
formData.append(key, value);
|
|
34
|
+
});
|
|
35
|
+
if (option.file instanceof Blob) formData.append(option.filename, option.file, option.file.name);
|
|
36
|
+
else formData.append(option.filename, option.file);
|
|
37
|
+
xhr.onerror = function error(e) {
|
|
38
|
+
option.onError?.(e);
|
|
39
|
+
};
|
|
40
|
+
xhr.onload = function onload() {
|
|
41
|
+
if (xhr.status < 200 || xhr.status >= 300) return option.onError?.(getError(option, xhr), getBody(xhr));
|
|
42
|
+
return option.onSuccess?.(getBody(xhr), xhr);
|
|
43
|
+
};
|
|
44
|
+
xhr.open(option.method, option.action, true);
|
|
45
|
+
if (option.withCredentials && "withCredentials" in xhr) xhr.withCredentials = true;
|
|
46
|
+
const headers = option.headers || {};
|
|
47
|
+
if (headers["X-Requested-With"] !== null) xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
|
48
|
+
Object.keys(headers).forEach((h) => {
|
|
49
|
+
if (headers[h] !== null) xhr.setRequestHeader(h, headers[h]);
|
|
50
|
+
});
|
|
51
|
+
xhr.send(formData);
|
|
52
|
+
return { abort() {
|
|
53
|
+
xhr.abort();
|
|
54
|
+
} };
|
|
55
|
+
}
|
|
56
|
+
export { upload as default };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
async function traverseFileTree(files, isAccepted) {
|
|
6
|
+
const flattenFileList = [];
|
|
7
|
+
const progressFileList = [];
|
|
8
|
+
files.forEach((file) => progressFileList.push(file.webkitGetAsEntry()));
|
|
9
|
+
async function readDirectory(directory) {
|
|
10
|
+
const dirReader = directory.createReader();
|
|
11
|
+
const entries = [];
|
|
12
|
+
while (true) {
|
|
13
|
+
const results = await new Promise((resolve) => {
|
|
14
|
+
dirReader.readEntries(resolve, () => resolve([]));
|
|
15
|
+
});
|
|
16
|
+
const n = results.length;
|
|
17
|
+
if (!n) break;
|
|
18
|
+
for (let i = 0; i < n; i++) entries.push(results[i]);
|
|
19
|
+
}
|
|
20
|
+
return entries;
|
|
21
|
+
}
|
|
22
|
+
async function readFile(item) {
|
|
23
|
+
return new Promise((resolve) => {
|
|
24
|
+
item.file((file) => {
|
|
25
|
+
if (isAccepted(file)) {
|
|
26
|
+
if (item.fullPath && !file.webkitRelativePath) {
|
|
27
|
+
Object.defineProperties(file, { webkitRelativePath: { writable: true } });
|
|
28
|
+
file.webkitRelativePath = item.fullPath.replace(/^\//, "");
|
|
29
|
+
Object.defineProperties(file, { webkitRelativePath: { writable: false } });
|
|
30
|
+
}
|
|
31
|
+
resolve(file);
|
|
32
|
+
} else resolve(null);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const _traverseFileTree = async (item, path) => {
|
|
37
|
+
if (!item) return;
|
|
38
|
+
item.path = path || "";
|
|
39
|
+
if (item.isFile) {
|
|
40
|
+
const file = await readFile(item);
|
|
41
|
+
if (file) flattenFileList.push(file);
|
|
42
|
+
} else if (item.isDirectory) {
|
|
43
|
+
const entries = await readDirectory(item);
|
|
44
|
+
progressFileList.push(...entries);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
let wipIndex = 0;
|
|
48
|
+
while (wipIndex < progressFileList.length) {
|
|
49
|
+
await _traverseFileTree(progressFileList[wipIndex]);
|
|
50
|
+
wipIndex++;
|
|
51
|
+
}
|
|
52
|
+
return flattenFileList;
|
|
53
|
+
}
|
|
54
|
+
var traverseFileTree_default = traverseFileTree;
|
|
55
|
+
exports.default = traverseFileTree_default;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { VcFile } from './interface';
|
|
2
|
+
interface InternalDataTransferItem extends DataTransferItem {
|
|
3
|
+
isFile: boolean;
|
|
4
|
+
file: (cd: (file: VcFile & {
|
|
5
|
+
webkitRelativePath?: string;
|
|
6
|
+
}) => void) => void;
|
|
7
|
+
createReader: () => any;
|
|
8
|
+
fullPath: string;
|
|
9
|
+
isDirectory: boolean;
|
|
10
|
+
name: string;
|
|
11
|
+
path: string;
|
|
12
|
+
}
|
|
13
|
+
type InternalFile = VcFile & {
|
|
14
|
+
webkitRelativePath?: string;
|
|
15
|
+
};
|
|
16
|
+
declare function traverseFileTree(files: InternalDataTransferItem[], isAccepted: (file: InternalFile) => boolean): Promise<InternalFile[]>;
|
|
17
|
+
export default traverseFileTree;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
async function traverseFileTree(files, isAccepted) {
|
|
2
|
+
const flattenFileList = [];
|
|
3
|
+
const progressFileList = [];
|
|
4
|
+
files.forEach((file) => progressFileList.push(file.webkitGetAsEntry()));
|
|
5
|
+
async function readDirectory(directory) {
|
|
6
|
+
const dirReader = directory.createReader();
|
|
7
|
+
const entries = [];
|
|
8
|
+
while (true) {
|
|
9
|
+
const results = await new Promise((resolve) => {
|
|
10
|
+
dirReader.readEntries(resolve, () => resolve([]));
|
|
11
|
+
});
|
|
12
|
+
const n = results.length;
|
|
13
|
+
if (!n) break;
|
|
14
|
+
for (let i = 0; i < n; i++) entries.push(results[i]);
|
|
15
|
+
}
|
|
16
|
+
return entries;
|
|
17
|
+
}
|
|
18
|
+
async function readFile(item) {
|
|
19
|
+
return new Promise((resolve) => {
|
|
20
|
+
item.file((file) => {
|
|
21
|
+
if (isAccepted(file)) {
|
|
22
|
+
if (item.fullPath && !file.webkitRelativePath) {
|
|
23
|
+
Object.defineProperties(file, { webkitRelativePath: { writable: true } });
|
|
24
|
+
file.webkitRelativePath = item.fullPath.replace(/^\//, "");
|
|
25
|
+
Object.defineProperties(file, { webkitRelativePath: { writable: false } });
|
|
26
|
+
}
|
|
27
|
+
resolve(file);
|
|
28
|
+
} else resolve(null);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
const _traverseFileTree = async (item, path) => {
|
|
33
|
+
if (!item) return;
|
|
34
|
+
item.path = path || "";
|
|
35
|
+
if (item.isFile) {
|
|
36
|
+
const file = await readFile(item);
|
|
37
|
+
if (file) flattenFileList.push(file);
|
|
38
|
+
} else if (item.isDirectory) {
|
|
39
|
+
const entries = await readDirectory(item);
|
|
40
|
+
progressFileList.push(...entries);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
let wipIndex = 0;
|
|
44
|
+
while (wipIndex < progressFileList.length) {
|
|
45
|
+
await _traverseFileTree(progressFileList[wipIndex]);
|
|
46
|
+
wipIndex++;
|
|
47
|
+
}
|
|
48
|
+
return flattenFileList;
|
|
49
|
+
}
|
|
50
|
+
var traverseFileTree_default = traverseFileTree;
|
|
51
|
+
export { traverseFileTree_default as default };
|
package/dist/uid.cjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
var now = +/* @__PURE__ */ new Date();
|
|
6
|
+
var index = 0;
|
|
7
|
+
function uid() {
|
|
8
|
+
return `vc-upload-${now}-${++index}`;
|
|
9
|
+
}
|
|
10
|
+
exports.default = uid;
|
package/dist/uid.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function uid(): string;
|