@hzab/utils 1.0.0-beta → 1.0.1-beta
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/changelog.md +9 -0
- package/package.json +1 -1
- package/src/nanoid.ts +9 -0
- package/src/upload/OssUploadUtil.ts +190 -0
- package/src/upload/ossUpload.ts +7 -168
- package/CHANGELOG.md +0 -2
package/changelog.md
ADDED
package/package.json
CHANGED
package/src/nanoid.ts
CHANGED
|
@@ -8,3 +8,12 @@ export const numALetters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP
|
|
|
8
8
|
|
|
9
9
|
/** nanoid 数字和大小写字母 */
|
|
10
10
|
export const nanoidNumALetters = customAlphabet(numALetters, 8);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 指定 nanoid 字符集合
|
|
14
|
+
* 数字和大小写字母
|
|
15
|
+
*/
|
|
16
|
+
export const numALettersAUrl = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_~";
|
|
17
|
+
|
|
18
|
+
/** nanoid url 参数规范 */
|
|
19
|
+
export const nanoidUrl = customAlphabet(numALettersAUrl, 20);
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import dayjs from "dayjs";
|
|
2
|
+
|
|
3
|
+
import { axios } from "@hzab/data-model";
|
|
4
|
+
import { formatDirStr } from "./uploadUtils";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* getSignature 配置
|
|
8
|
+
*/
|
|
9
|
+
export interface IGetSignatureOpt {
|
|
10
|
+
/** 获取配置的接口地址 */
|
|
11
|
+
serverUrl?: string;
|
|
12
|
+
/** axios 实例 */
|
|
13
|
+
axios?: typeof axios;
|
|
14
|
+
/** axios 配置参数 */
|
|
15
|
+
axiosConf?: Object;
|
|
16
|
+
/** 获取配置的接口自定义入参 */
|
|
17
|
+
params?: {
|
|
18
|
+
/** 文件路径 */
|
|
19
|
+
dir?: string;
|
|
20
|
+
/** 是否是公开库 */
|
|
21
|
+
isPublic?: number;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* OssUpload props 参数
|
|
27
|
+
*/
|
|
28
|
+
export interface IOssUploadProps {
|
|
29
|
+
/** axios 实例 */
|
|
30
|
+
axios?: typeof axios;
|
|
31
|
+
/** axios 配置参数 */
|
|
32
|
+
axiosConf?: Object;
|
|
33
|
+
/** 获取配置的接口地址 */
|
|
34
|
+
serverUrl?: string;
|
|
35
|
+
/** 获取配置的接口自定义入参 */
|
|
36
|
+
signatureParams?: {
|
|
37
|
+
/** 文件路径 */
|
|
38
|
+
dir?: string;
|
|
39
|
+
/** 是否是公开库 */
|
|
40
|
+
isPublic?: number;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface IUploadOpt {
|
|
45
|
+
fileName?: string;
|
|
46
|
+
/** axios 实例 */
|
|
47
|
+
axios?: typeof axios;
|
|
48
|
+
/** axios 配置参数 */
|
|
49
|
+
axiosConf?: Object;
|
|
50
|
+
/** 获取配置的接口地址 */
|
|
51
|
+
serverUrl?: string;
|
|
52
|
+
/** 获取配置的接口自定义入参 */
|
|
53
|
+
params?: {
|
|
54
|
+
/** 文件路径 */
|
|
55
|
+
dir?: string;
|
|
56
|
+
/** 是否是公开库 */
|
|
57
|
+
isPublic?: number;
|
|
58
|
+
};
|
|
59
|
+
/** 文件上传的接口自定义入参 */
|
|
60
|
+
ossParams?: {};
|
|
61
|
+
/** 是否使用 hash 文件名 */
|
|
62
|
+
useHashName?: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function getSignature(opt: IGetSignatureOpt = {}) {
|
|
66
|
+
const { serverUrl = "/api/v1/user/oss/getWebOssConfig" } = opt;
|
|
67
|
+
// 减 10 秒,避免发起请求时 刚好过期的情况
|
|
68
|
+
if (
|
|
69
|
+
window._$ossSignatureRes &&
|
|
70
|
+
serverUrl === window._$ossSignatureRes.serverUrl &&
|
|
71
|
+
dayjs().valueOf() - window._$ossSignatureRes.__saveTime < window._$ossSignatureRes.expireTimeMilles - 10000
|
|
72
|
+
) {
|
|
73
|
+
return Promise.resolve(window._$ossSignatureRes);
|
|
74
|
+
}
|
|
75
|
+
const { axios: _ax = axios, params = {}, axiosConf } = opt;
|
|
76
|
+
// 处理 dir 格式,必须为非 / 开头, / 结尾。如: test/
|
|
77
|
+
params.dir = formatDirStr(params.dir);
|
|
78
|
+
|
|
79
|
+
return _ax
|
|
80
|
+
.get(serverUrl, {
|
|
81
|
+
...axiosConf,
|
|
82
|
+
params: {
|
|
83
|
+
isPublic: 1,
|
|
84
|
+
...params,
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
|
+
.then((res) => {
|
|
88
|
+
window._$ossSignatureRes = res?.data?.data ?? res?.data ?? res;
|
|
89
|
+
if (window._$ossSignatureRes) {
|
|
90
|
+
window._$ossSignatureRes.__saveTime = dayjs().valueOf();
|
|
91
|
+
window._$ossSignatureRes.serverUrl = serverUrl;
|
|
92
|
+
}
|
|
93
|
+
return window._$ossSignatureRes;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* oss 上传工具类
|
|
99
|
+
*/
|
|
100
|
+
class OssUploadUtil {
|
|
101
|
+
/** axios 实例 */
|
|
102
|
+
axios: typeof axios;
|
|
103
|
+
/** axios 配置参数 */
|
|
104
|
+
axiosConf: Object;
|
|
105
|
+
/** 获取配置的接口地址 */
|
|
106
|
+
serverUrl: string;
|
|
107
|
+
/** 获取配置的接口自定义入参 */
|
|
108
|
+
signatureParams?: {
|
|
109
|
+
/** 文件路径 */
|
|
110
|
+
dir?: string;
|
|
111
|
+
/** 是否是公开库 */
|
|
112
|
+
isPublic?: number;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
constructor(props: IOssUploadProps = {}) {
|
|
116
|
+
this.axios = props.axios || axios;
|
|
117
|
+
this.axiosConf = props.axiosConf || {};
|
|
118
|
+
this.serverUrl = props.serverUrl || "/api/v1/user/oss/getWebOssConfig";
|
|
119
|
+
this.signatureParams = props.signatureParams || {};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
getSignature(serverUrl = this.serverUrl, opt) {
|
|
123
|
+
return this._getSignature(serverUrl, opt);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
upload(file, opt: IUploadOpt = {}): Promise<{ fileUrl: string }> {
|
|
127
|
+
return this._upload(file, { ...opt });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
_getSignature(serverUrl = this.serverUrl, opt) {
|
|
131
|
+
return getSignature({
|
|
132
|
+
...opt,
|
|
133
|
+
serverUrl,
|
|
134
|
+
axios: opt?.axios || this.axios,
|
|
135
|
+
axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
_upload(file, opt: IUploadOpt = {}): Promise<{ fileUrl: string }> {
|
|
140
|
+
return new Promise(async (resolve, reject) => {
|
|
141
|
+
const ossParams = await this._getSignature(opt.serverUrl || this.serverUrl, {
|
|
142
|
+
...opt,
|
|
143
|
+
params: { ...this.signatureParams, ...opt.params },
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const { ossParams: propOssParams, fileName } = opt || {};
|
|
147
|
+
const formData = new FormData();
|
|
148
|
+
// key 表示上传到 Bucket 内的 Object 的完整路径,例如 exampledir/exampleobject.txtObject,完整路径中不能包含 Bucket 名称。
|
|
149
|
+
// filename 表示待上传的本地文件名称。
|
|
150
|
+
const filename = fileName || file.name;
|
|
151
|
+
const key = `${ossParams?.dir}${filename}`;
|
|
152
|
+
formData.set("key", key);
|
|
153
|
+
formData.set("OSSAccessKeyId", ossParams.accessid);
|
|
154
|
+
formData.set("policy", ossParams.policy);
|
|
155
|
+
formData.set("Signature", ossParams.signature);
|
|
156
|
+
if (ossParams.callback) {
|
|
157
|
+
formData.set("callback", ossParams.callback);
|
|
158
|
+
}
|
|
159
|
+
// @ts-ignore
|
|
160
|
+
formData.set("success_action_status", 200);
|
|
161
|
+
formData.set("file", file);
|
|
162
|
+
|
|
163
|
+
if (propOssParams) {
|
|
164
|
+
for (const key in propOssParams) {
|
|
165
|
+
if (Object.hasOwnProperty.call(propOssParams, key)) {
|
|
166
|
+
formData.set(key, propOssParams[key]);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const _axios = opt?.axios || this.axios;
|
|
172
|
+
|
|
173
|
+
return _axios
|
|
174
|
+
.post(ossParams.host, formData, { ...this.axiosConf, ...opt?.axiosConf })
|
|
175
|
+
.then((res) => {
|
|
176
|
+
resolve(res);
|
|
177
|
+
return res;
|
|
178
|
+
})
|
|
179
|
+
.catch((err) => {
|
|
180
|
+
console.error("oss upload err", err);
|
|
181
|
+
reject(err);
|
|
182
|
+
return Promise.reject(err);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export { axios };
|
|
189
|
+
|
|
190
|
+
export default OssUploadUtil;
|
package/src/upload/ossUpload.ts
CHANGED
|
@@ -1,180 +1,19 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { axios } from "@hzab/data-model";
|
|
4
|
-
import { formatDirStr, mergeDirStr, getFileNameByFileObj } from "./uploadUtils";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* getSignature 配置
|
|
8
|
-
*/
|
|
9
|
-
export interface IGetSignatureOpt {
|
|
10
|
-
/** 获取配置的接口地址 */
|
|
11
|
-
serverUrl?: string;
|
|
12
|
-
/** axios 实例 */
|
|
13
|
-
axios?: typeof axios;
|
|
14
|
-
/** axios 配置参数 */
|
|
15
|
-
axiosConf?: Object;
|
|
16
|
-
/** 获取配置的接口自定义入参 */
|
|
17
|
-
params?: {
|
|
18
|
-
/** 文件路径 */
|
|
19
|
-
dir?: string;
|
|
20
|
-
/** 是否是公开库 */
|
|
21
|
-
isPublic?: number;
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* OssUpload props 参数
|
|
27
|
-
*/
|
|
28
|
-
export interface IOssUploadProps {
|
|
29
|
-
/** axios 实例 */
|
|
30
|
-
axios?: typeof axios;
|
|
31
|
-
/** axios 配置参数 */
|
|
32
|
-
axiosConf?: Object;
|
|
33
|
-
/** 获取配置的接口地址 */
|
|
34
|
-
serverUrl?: string;
|
|
35
|
-
/** 获取配置的接口自定义入参 */
|
|
36
|
-
signatureParams?: {
|
|
37
|
-
/** 文件路径 */
|
|
38
|
-
dir?: string;
|
|
39
|
-
/** 是否是公开库 */
|
|
40
|
-
isPublic?: number;
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export interface IUploadOpt {
|
|
45
|
-
/** axios 实例 */
|
|
46
|
-
axios?: typeof axios;
|
|
47
|
-
/** axios 配置参数 */
|
|
48
|
-
axiosConf?: Object;
|
|
49
|
-
/** 获取配置的接口地址 */
|
|
50
|
-
serverUrl?: string;
|
|
51
|
-
/** 获取配置的接口自定义入参 */
|
|
52
|
-
params?: {
|
|
53
|
-
/** 文件路径 */
|
|
54
|
-
dir?: string;
|
|
55
|
-
/** 是否是公开库 */
|
|
56
|
-
isPublic?: number;
|
|
57
|
-
};
|
|
58
|
-
/** 文件上传的接口自定义入参 */
|
|
59
|
-
ossParams?: {};
|
|
60
|
-
/** 是否使用 hash 文件名 */
|
|
61
|
-
useHashName?: boolean;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function getSignature(opt: IGetSignatureOpt = {}) {
|
|
65
|
-
const { serverUrl = "/api/v1/user/oss/getWebOssConfig" } = opt;
|
|
66
|
-
// 减 10 秒,避免发起请求时 刚好过期的情况
|
|
67
|
-
if (
|
|
68
|
-
window._$ossSignatureRes &&
|
|
69
|
-
serverUrl === window._$ossSignatureRes.serverUrl &&
|
|
70
|
-
dayjs().valueOf() - window._$ossSignatureRes.__saveTime < window._$ossSignatureRes.expireTimeMilles - 10000
|
|
71
|
-
) {
|
|
72
|
-
return Promise.resolve(window._$ossSignatureRes);
|
|
73
|
-
}
|
|
74
|
-
const { axios: _ax = axios, params = {}, axiosConf } = opt;
|
|
75
|
-
// 处理 dir 格式,必须为非 / 开头, / 结尾。如: test/
|
|
76
|
-
params.dir = formatDirStr(params.dir);
|
|
77
|
-
|
|
78
|
-
return _ax
|
|
79
|
-
.get(serverUrl, {
|
|
80
|
-
...axiosConf,
|
|
81
|
-
params: {
|
|
82
|
-
isPublic: 1,
|
|
83
|
-
...params,
|
|
84
|
-
},
|
|
85
|
-
})
|
|
86
|
-
.then((res) => {
|
|
87
|
-
window._$ossSignatureRes = res?.data?.data ?? res?.data ?? res;
|
|
88
|
-
if (window._$ossSignatureRes) {
|
|
89
|
-
window._$ossSignatureRes.__saveTime = dayjs().valueOf();
|
|
90
|
-
window._$ossSignatureRes.serverUrl = serverUrl;
|
|
91
|
-
}
|
|
92
|
-
return window._$ossSignatureRes;
|
|
93
|
-
});
|
|
94
|
-
}
|
|
1
|
+
import { mergeDirStr, getFileNameByFileObj } from "./uploadUtils";
|
|
2
|
+
import OssUploadUtil, { axios, IUploadOpt } from "./OssUploadUtil";
|
|
95
3
|
|
|
96
4
|
/**
|
|
97
5
|
* oss 上传工具类
|
|
98
6
|
*/
|
|
99
|
-
class OssUpload {
|
|
100
|
-
/** axios 实例 */
|
|
101
|
-
axios: typeof axios;
|
|
102
|
-
/** axios 配置参数 */
|
|
103
|
-
axiosConf: Object;
|
|
104
|
-
/** 获取配置的接口地址 */
|
|
105
|
-
serverUrl: string;
|
|
106
|
-
/** 获取配置的接口自定义入参 */
|
|
107
|
-
signatureParams?: {
|
|
108
|
-
/** 文件路径 */
|
|
109
|
-
dir?: string;
|
|
110
|
-
/** 是否是公开库 */
|
|
111
|
-
isPublic?: number;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
constructor(props: IOssUploadProps = {}) {
|
|
115
|
-
this.axios = props.axios || axios;
|
|
116
|
-
this.axiosConf = props.axiosConf || {};
|
|
117
|
-
this.serverUrl = props.serverUrl || "/api/v1/user/oss/getWebOssConfig";
|
|
118
|
-
this.signatureParams = props.signatureParams || {};
|
|
119
|
-
}
|
|
120
|
-
|
|
7
|
+
class OssUpload extends OssUploadUtil {
|
|
121
8
|
getSignature(serverUrl = this.serverUrl, opt) {
|
|
122
9
|
// dir 前缀 oss-upload 文件目录
|
|
123
10
|
opt.params.dir = mergeDirStr("web-upload/", opt.params.dir);
|
|
124
|
-
return
|
|
125
|
-
...opt,
|
|
126
|
-
serverUrl,
|
|
127
|
-
axios: opt?.axios || this.axios,
|
|
128
|
-
axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
|
|
129
|
-
});
|
|
11
|
+
return this._getSignature(serverUrl, opt);
|
|
130
12
|
}
|
|
131
13
|
|
|
132
|
-
upload(file, opt: IUploadOpt = {}) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
...opt,
|
|
136
|
-
params: { ...this.signatureParams, ...opt.params },
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
const { ossParams: propOssParams } = opt || {};
|
|
140
|
-
const formData = new FormData();
|
|
141
|
-
// key 表示上传到 Bucket 内的 Object 的完整路径,例如 exampledir/exampleobject.txtObject,完整路径中不能包含 Bucket 名称。
|
|
142
|
-
// filename 表示待上传的本地文件名称。
|
|
143
|
-
const filename = getFileNameByFileObj(file, opt);
|
|
144
|
-
const key = `${ossParams?.dir}${filename}`;
|
|
145
|
-
formData.set("key", key);
|
|
146
|
-
formData.set("OSSAccessKeyId", ossParams.accessid);
|
|
147
|
-
formData.set("policy", ossParams.policy);
|
|
148
|
-
formData.set("Signature", ossParams.signature);
|
|
149
|
-
if (ossParams.callback) {
|
|
150
|
-
formData.set("callback", ossParams.callback);
|
|
151
|
-
}
|
|
152
|
-
// @ts-ignore
|
|
153
|
-
formData.set("success_action_status", 200);
|
|
154
|
-
formData.set("file", file);
|
|
155
|
-
|
|
156
|
-
if (propOssParams) {
|
|
157
|
-
for (const key in propOssParams) {
|
|
158
|
-
if (Object.hasOwnProperty.call(propOssParams, key)) {
|
|
159
|
-
formData.set(key, propOssParams[key]);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const _axios = opt?.axios || this.axios;
|
|
165
|
-
|
|
166
|
-
return _axios
|
|
167
|
-
.post(ossParams.host, formData, { ...this.axiosConf, ...opt?.axiosConf })
|
|
168
|
-
.then((res) => {
|
|
169
|
-
resolve(res);
|
|
170
|
-
return res;
|
|
171
|
-
})
|
|
172
|
-
.catch((err) => {
|
|
173
|
-
console.error("oss upload err", err);
|
|
174
|
-
reject(err);
|
|
175
|
-
return Promise.reject(err);
|
|
176
|
-
});
|
|
177
|
-
});
|
|
14
|
+
upload(file, opt: IUploadOpt = {}): Promise<{ fileUrl: string }> {
|
|
15
|
+
const fileName = getFileNameByFileObj(file, opt);
|
|
16
|
+
return this._upload(file, { ...opt, fileName });
|
|
178
17
|
}
|
|
179
18
|
}
|
|
180
19
|
|
package/CHANGELOG.md
DELETED