@hzab/utils 1.0.4 → 1.0.6
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/README.md +20 -3
- package/changelog.md +8 -0
- package/package.json +1 -1
- package/src/file/fileName.ts +21 -0
- package/src/file/fileType.ts +71 -2
- package/src/formily/global-components.ts +48 -0
- package/src/upload/OssUploadUtil.ts +204 -195
- package/src/upload/uploadUtils.ts +11 -38
package/README.md
CHANGED
|
@@ -98,17 +98,34 @@ export async function handleOssUpload(files, opt) {
|
|
|
98
98
|
|
|
99
99
|
- 生成文档:npm run docs
|
|
100
100
|
- 本地运行:npm run dev
|
|
101
|
-
- 打包编译:npm run build
|
|
102
101
|
|
|
103
102
|
## 发布
|
|
104
103
|
|
|
105
|
-
-
|
|
104
|
+
- npm 源和云效源都需要发布
|
|
106
105
|
|
|
107
|
-
- 编译组件:npm run build
|
|
108
106
|
- 命令:npm publish --access public
|
|
109
107
|
- 发布目录:
|
|
110
108
|
- src
|
|
111
109
|
|
|
110
|
+
### 迭代发布命令-版本自增
|
|
111
|
+
|
|
112
|
+
- beta: 需要手动修改 package.json 中的 version,添加 -betaX 版本号。使用 npm publish --beta 发布
|
|
113
|
+
- 0.0.x: npm run publish-patch
|
|
114
|
+
- 0.x.0: npm run publish-minor
|
|
115
|
+
- x.0.0: npm run publish-major
|
|
116
|
+
|
|
117
|
+
### nrm
|
|
118
|
+
|
|
119
|
+
- 安装
|
|
120
|
+
npm install -g nrm
|
|
121
|
+
- 增加源
|
|
122
|
+
nrm add aliyun https://packages.aliyun.com/62046985b3ead41b374a17f7/npm/npm-registry/
|
|
123
|
+
- 切换源
|
|
124
|
+
nrm use aliyun
|
|
125
|
+
nrm use npm
|
|
126
|
+
- 登录(账号密码在 https://packages.aliyun.com/npm/npm-registry/guide 查看)
|
|
127
|
+
npm login --registry=https://packages.aliyun.com/62046985b3ead41b374a17f7/npm/npm-registry/
|
|
128
|
+
|
|
112
129
|
## 配置
|
|
113
130
|
|
|
114
131
|
### 配置文件
|
package/changelog.md
CHANGED
package/package.json
CHANGED
package/src/file/fileName.ts
CHANGED
|
@@ -75,3 +75,24 @@ export const mergeFileName = function (fileName, str = "") {
|
|
|
75
75
|
export const getUFileName = function (fileName) {
|
|
76
76
|
return mergeFileName(getFileName(fileName) || "up", "-" + `~kid-${nanoidNumALetters()}~`);
|
|
77
77
|
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 获取文件名的后缀
|
|
81
|
+
* @param fileName
|
|
82
|
+
* @returns
|
|
83
|
+
*/
|
|
84
|
+
export const getFileNameExt = function (fileName) {
|
|
85
|
+
const regex = /^(.*)\.([^.]+)$/;
|
|
86
|
+
const result = fileName.match(regex);
|
|
87
|
+
if (result) {
|
|
88
|
+
const [, mainPart, ext] = result;
|
|
89
|
+
return {
|
|
90
|
+
mainPart,
|
|
91
|
+
ext,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
mainPart: fileName,
|
|
96
|
+
ext: "",
|
|
97
|
+
};
|
|
98
|
+
};
|
package/src/file/fileType.ts
CHANGED
|
@@ -38,8 +38,6 @@ export function getFileType(file) {
|
|
|
38
38
|
type = "audio/3gpp";
|
|
39
39
|
} else if (checkUrlSuffix(file, ["pdf"])) {
|
|
40
40
|
type = "application/pdf";
|
|
41
|
-
} else if (checkUrlSuffix(file, ["pdf"])) {
|
|
42
|
-
type = "application/pdf";
|
|
43
41
|
} else if (checkUrlSuffix(file, ["xlsx", "xls", "csv", "xlsm", "xlsb"])) {
|
|
44
42
|
type = "application/vnd.ms-excel";
|
|
45
43
|
} else if (checkUrlSuffix(file, ["doc", "docx"])) {
|
|
@@ -150,3 +148,74 @@ export function getFileTypeStr(file) {
|
|
|
150
148
|
}
|
|
151
149
|
return fileType || type;
|
|
152
150
|
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 根据文件的 MIME 类型 (file.type) 获取对应的文件后缀名
|
|
154
|
+
* @param {string} mimeType - 文件的 MIME 类型(如 'image/jpeg'、'application/pdf')
|
|
155
|
+
* @returns {string} 返回文件后缀名(带点,如 '.jpg'),未知类型返回 '.unknown'
|
|
156
|
+
*/
|
|
157
|
+
export const getFileTypeExt = function (mimeType) {
|
|
158
|
+
// 常见 MIME 类型到文件后缀的映射表(可根据需求扩展)
|
|
159
|
+
const mimeToExtMap = {
|
|
160
|
+
// 图片类型
|
|
161
|
+
"image/jpeg": "jpg",
|
|
162
|
+
"image/jpg": "jpg",
|
|
163
|
+
"image/png": "png",
|
|
164
|
+
"image/gif": "gif",
|
|
165
|
+
"image/webp": "webp",
|
|
166
|
+
"image/svg+xml": "svg",
|
|
167
|
+
"image/bmp": "bmp",
|
|
168
|
+
"image/tiff": "tiff",
|
|
169
|
+
|
|
170
|
+
// 文档类型
|
|
171
|
+
"application/pdf": "pdf",
|
|
172
|
+
"application/msword": "doc",
|
|
173
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
174
|
+
"application/vnd.ms-excel": "xls",
|
|
175
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
176
|
+
"application/vnd.ms-powerpoint": "ppt",
|
|
177
|
+
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx",
|
|
178
|
+
"text/plain": "txt",
|
|
179
|
+
"text/html": "html",
|
|
180
|
+
"application/json": "json",
|
|
181
|
+
|
|
182
|
+
// 音频/视频类型
|
|
183
|
+
"audio/mpeg": "mp3",
|
|
184
|
+
"audio/wav": "wav",
|
|
185
|
+
"video/mp4": "mp4",
|
|
186
|
+
"video/avi": "avi",
|
|
187
|
+
"video/mpeg": "mpeg",
|
|
188
|
+
|
|
189
|
+
// 压缩包类型
|
|
190
|
+
"application/zip": "zip",
|
|
191
|
+
"application/x-rar-compressed": "rar",
|
|
192
|
+
"application/x-7z-compressed": "7z",
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// 处理空值、非字符串的情况
|
|
196
|
+
if (!mimeType || typeof mimeType !== "string") {
|
|
197
|
+
// return "unknown";
|
|
198
|
+
return "";
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 去除首尾空格,统一转小写(避免大小写不一致问题)
|
|
202
|
+
const normalizedMimeType = mimeType.trim().toLowerCase();
|
|
203
|
+
|
|
204
|
+
// 优先匹配完整 MIME 类型
|
|
205
|
+
if (mimeToExtMap[normalizedMimeType]) {
|
|
206
|
+
return mimeToExtMap[normalizedMimeType];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// 兼容部分不标准的 MIME 类型(如只取主类型)
|
|
210
|
+
const mainType = normalizedMimeType.split("/")[0];
|
|
211
|
+
switch (mainType) {
|
|
212
|
+
case "image":
|
|
213
|
+
return "img";
|
|
214
|
+
case "audio":
|
|
215
|
+
return "audio";
|
|
216
|
+
case "video":
|
|
217
|
+
return "video";
|
|
218
|
+
default:
|
|
219
|
+
return mainType?.match(/[^\/]*\/([^\/]+)$/)?.[1];
|
|
220
|
+
}
|
|
221
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* formily 全局组件
|
|
3
|
+
* 项目初始化时进行 formily 组件的挂载,解决 form render 组件日益庞大的问题。
|
|
4
|
+
*/
|
|
5
|
+
window.formilyGlobalComponents = {};
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 获取全局 formily 组件对象
|
|
9
|
+
* @returns {Object} formilyGlobalComponents
|
|
10
|
+
*/
|
|
11
|
+
export const getFormilyGlobalComponents = function () {
|
|
12
|
+
return window.formilyGlobalComponents || {};
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 批量设置全局 formily 组件
|
|
17
|
+
* @param {Object} components
|
|
18
|
+
* @returns {Object} formilyGlobalComponents
|
|
19
|
+
*/
|
|
20
|
+
export const setFormilyGlobalComponents = function (components) {
|
|
21
|
+
Object.assign(window.formilyGlobalComponents, components);
|
|
22
|
+
return window.formilyGlobalComponents;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 单个设置全局 formily 组件
|
|
27
|
+
* @returns {Object} formilyGlobalComponents
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* 单个设置全局 formily 组件
|
|
31
|
+
* @param {string} key
|
|
32
|
+
* @param {ReactComponent} component
|
|
33
|
+
* @returns {Object} formilyGlobalComponents
|
|
34
|
+
*/
|
|
35
|
+
export const setFormilyGlobalComponent = function (key, component) {
|
|
36
|
+
window.formilyGlobalComponents[key] = component;
|
|
37
|
+
return window.formilyGlobalComponents;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 单个删除全局 formily 组件
|
|
42
|
+
* @param {string} key
|
|
43
|
+
* @returns {Object} formilyGlobalComponents
|
|
44
|
+
*/
|
|
45
|
+
export const rmFormilyGlobalComponents = function (key) {
|
|
46
|
+
delete window.formilyGlobalComponents[key];
|
|
47
|
+
return window.formilyGlobalComponents;
|
|
48
|
+
};
|
|
@@ -1,195 +1,204 @@
|
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
formData
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
formData.set("
|
|
163
|
-
formData.set("
|
|
164
|
-
|
|
165
|
-
if (
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
})
|
|
184
|
-
.
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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
|
+
* 获取配置接口请求入参
|
|
61
|
+
*/
|
|
62
|
+
signatureParams?: {
|
|
63
|
+
/** 公开还是私有 */
|
|
64
|
+
isPublic?: number;
|
|
65
|
+
};
|
|
66
|
+
/** 文件上传的接口自定义入参 */
|
|
67
|
+
ossParams?: {};
|
|
68
|
+
/** 是否使用 hash 文件名 */
|
|
69
|
+
useHashName?: boolean;
|
|
70
|
+
/**缩略图参数 */
|
|
71
|
+
thumbnailParams?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function getSignature(opt: IGetSignatureOpt = {}) {
|
|
75
|
+
const { serverUrl = "/api/v1/user/oss/getWebOssConfig" } = opt;
|
|
76
|
+
// 减 10 秒,避免发起请求时 刚好过期的情况
|
|
77
|
+
if (
|
|
78
|
+
window._$ossSignatureRes &&
|
|
79
|
+
serverUrl === window._$ossSignatureRes.serverUrl &&
|
|
80
|
+
dayjs().valueOf() - window._$ossSignatureRes.__saveTime < window._$ossSignatureRes.expireTimeMilles - 10000
|
|
81
|
+
) {
|
|
82
|
+
return Promise.resolve(window._$ossSignatureRes);
|
|
83
|
+
}
|
|
84
|
+
const { axios: _ax = axios, params = {}, axiosConf } = opt;
|
|
85
|
+
// 处理 dir 格式,必须为非 / 开头, / 结尾。如: test/
|
|
86
|
+
params.dir = formatDirStr(params.dir);
|
|
87
|
+
|
|
88
|
+
return _ax
|
|
89
|
+
.get(serverUrl, {
|
|
90
|
+
...axiosConf,
|
|
91
|
+
params: {
|
|
92
|
+
isPublic: 1,
|
|
93
|
+
...params,
|
|
94
|
+
},
|
|
95
|
+
})
|
|
96
|
+
.then((res) => {
|
|
97
|
+
window._$ossSignatureRes = res?.data?.data ?? res?.data ?? res;
|
|
98
|
+
if (window._$ossSignatureRes) {
|
|
99
|
+
window._$ossSignatureRes.__saveTime = dayjs().valueOf();
|
|
100
|
+
window._$ossSignatureRes.serverUrl = serverUrl;
|
|
101
|
+
}
|
|
102
|
+
return window._$ossSignatureRes;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* oss 上传工具类
|
|
108
|
+
*/
|
|
109
|
+
class OssUploadUtil {
|
|
110
|
+
/** axios 实例 */
|
|
111
|
+
axios: typeof axios;
|
|
112
|
+
/** axios 配置参数 */
|
|
113
|
+
axiosConf: Object;
|
|
114
|
+
/** 获取配置的接口地址 */
|
|
115
|
+
serverUrl: string;
|
|
116
|
+
/** 获取配置的接口自定义入参 */
|
|
117
|
+
signatureParams?: {
|
|
118
|
+
/** 文件路径 */
|
|
119
|
+
dir?: string;
|
|
120
|
+
/** 是否是公开库 */
|
|
121
|
+
isPublic?: number;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
constructor(props: IOssUploadProps = {}) {
|
|
125
|
+
this.axios = props.axios || axios;
|
|
126
|
+
this.axiosConf = props.axiosConf || {};
|
|
127
|
+
this.serverUrl = props.serverUrl || "/api/v1/user/oss/getWebOssConfig";
|
|
128
|
+
this.signatureParams = props.signatureParams || {};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
getSignature(serverUrl = this.serverUrl, opt) {
|
|
132
|
+
return this._getSignature(serverUrl, opt);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
upload(file, opt: IUploadOpt = {}): Promise<{ fileUrl: string }> {
|
|
136
|
+
return this._upload(file, { ...opt });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
_getSignature(serverUrl = this.serverUrl, opt) {
|
|
140
|
+
return getSignature({
|
|
141
|
+
...opt,
|
|
142
|
+
serverUrl,
|
|
143
|
+
axios: opt?.axios || this.axios,
|
|
144
|
+
axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
_upload(file, opt: IUploadOpt = {}): Promise<{ fileUrl: string }> {
|
|
149
|
+
return new Promise(async (resolve, reject) => {
|
|
150
|
+
const ossParams = await this._getSignature(opt.serverUrl || this.serverUrl, {
|
|
151
|
+
...opt,
|
|
152
|
+
params: { ...this.signatureParams, ...opt.params },
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const { ossParams: propOssParams, fileName } = opt || {};
|
|
156
|
+
const formData = new FormData();
|
|
157
|
+
// key 表示上传到 Bucket 内的 Object 的完整路径,例如 exampledir/exampleobject.txtObject,完整路径中不能包含 Bucket 名称。
|
|
158
|
+
// filename 表示待上传的本地文件名称。
|
|
159
|
+
const filename = fileName || file.name;
|
|
160
|
+
const key = `${ossParams?.dir}${filename}`;
|
|
161
|
+
formData.set("key", key);
|
|
162
|
+
formData.set("OSSAccessKeyId", ossParams.accessid);
|
|
163
|
+
formData.set("policy", ossParams.policy);
|
|
164
|
+
formData.set("Signature", ossParams.signature);
|
|
165
|
+
if (ossParams.callback) {
|
|
166
|
+
formData.set("callback", ossParams.callback);
|
|
167
|
+
}
|
|
168
|
+
// @ts-ignore
|
|
169
|
+
formData.set("success_action_status", 200);
|
|
170
|
+
formData.set("file", file);
|
|
171
|
+
|
|
172
|
+
if (propOssParams) {
|
|
173
|
+
for (const key in propOssParams) {
|
|
174
|
+
if (Object.hasOwnProperty.call(propOssParams, key)) {
|
|
175
|
+
formData.set(key, propOssParams[key]);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const _axios = opt?.axios || this.axios;
|
|
181
|
+
|
|
182
|
+
return _axios
|
|
183
|
+
.post(ossParams.host, formData, { ...this.axiosConf, ...opt?.axiosConf })
|
|
184
|
+
.then((res) => {
|
|
185
|
+
if (opt?.signatureParams?.isPublic === 1) {
|
|
186
|
+
res.data.data.thumbnailUrl = `${res?.data?.data?.fileUrl}${
|
|
187
|
+
opt?.thumbnailParams ? "?" + opt?.thumbnailParams : ""
|
|
188
|
+
}`;
|
|
189
|
+
}
|
|
190
|
+
resolve(res);
|
|
191
|
+
return res;
|
|
192
|
+
})
|
|
193
|
+
.catch((err) => {
|
|
194
|
+
console.error("oss upload err", err);
|
|
195
|
+
reject(err);
|
|
196
|
+
return Promise.reject(err);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export { axios };
|
|
203
|
+
|
|
204
|
+
export default OssUploadUtil;
|
|
@@ -3,7 +3,8 @@ import dayjs from "dayjs";
|
|
|
3
3
|
import { getArr } from "../array";
|
|
4
4
|
import { isBase64Str } from "../base64Str";
|
|
5
5
|
|
|
6
|
-
import { mergeFileName, getFileName } from "../file/fileName";
|
|
6
|
+
import { mergeFileName, getFileName, getFileNameExt } from "../file/fileName";
|
|
7
|
+
import { getFileTypeExt } from "../file/fileType";
|
|
7
8
|
import { nanoidNumALetters } from "../nanoid";
|
|
8
9
|
|
|
9
10
|
/**
|
|
@@ -54,30 +55,6 @@ export function getPreviewUrl(uri, previewConfig) {
|
|
|
54
55
|
return uri;
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
|
|
58
|
-
export const getFileNameExt = function (fileName,) {
|
|
59
|
-
const regex = /^(.*)\.([^.]+)$/;
|
|
60
|
-
const result = fileName.match(regex);
|
|
61
|
-
if (result) {
|
|
62
|
-
const [, mainPart, ext] = result;
|
|
63
|
-
return {
|
|
64
|
-
mainPart,
|
|
65
|
-
ext
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return {
|
|
69
|
-
mainPart: fileName,
|
|
70
|
-
ext: ""
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export const getFileTypeExt = function (type,) {
|
|
75
|
-
if (type.indexOf("/") >= 0) {
|
|
76
|
-
return type?.match(/[^\/]*\/([^\/]+)$/)?.[1]
|
|
77
|
-
};
|
|
78
|
-
return type;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
58
|
/**
|
|
82
59
|
* 根据 file 对象获取文件名称,文件名称包含:初始名称、类型
|
|
83
60
|
* 数据存储格式 ~k[key]-[data]~ 如: ~ktime-1743649562530~
|
|
@@ -85,31 +62,27 @@ export const getFileTypeExt = function (type,) {
|
|
|
85
62
|
* @param file
|
|
86
63
|
*/
|
|
87
64
|
export const getFileNameByFileObj = (file, opt: any) => {
|
|
88
|
-
|
|
89
65
|
const {
|
|
90
66
|
useHashName = true,
|
|
91
|
-
params: { fileName = "" } = {}
|
|
67
|
+
params: { fileName = "" } = {}, // 给 params 加默认空对象
|
|
92
68
|
} = opt || {};
|
|
93
69
|
|
|
94
|
-
const
|
|
95
|
-
|
|
70
|
+
const _fullFileName = file.name || fileName || "";
|
|
71
|
+
const { mainPart, ext } = getFileNameExt(_fullFileName);
|
|
96
72
|
|
|
97
|
-
|
|
73
|
+
console.log("getFileTypeExt(file.type)", file.type, getFileTypeExt(file.type));
|
|
98
74
|
|
|
99
|
-
const _fileName =
|
|
75
|
+
const _fileName = mainPart || _fullFileName || getFileName(file.url) || "file";
|
|
76
|
+
const _ext = ext || getFileTypeExt(file.type);
|
|
100
77
|
|
|
101
78
|
if (_fileName.indexOf("~kid-") >= 0) {
|
|
102
79
|
return _fileName;
|
|
103
80
|
}
|
|
104
81
|
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
ext: file.ext || (file.type || file.contentType)?.replace("/", "_"),
|
|
108
|
-
});
|
|
109
|
-
const hashStr = useHashName ? `${nanoidNumALetters()}-` : "";
|
|
110
|
-
const nm = mergeFileName(_fileName + "." + type, `~kid-${hashStr}${objStr}`);
|
|
82
|
+
const hashStr = useHashName ? nanoidNumALetters() : "";
|
|
83
|
+
const nm = mergeFileName(_fileName + "." + _ext, `~kid-${hashStr}-${file.createTime || dayjs().valueOf()}-${ext}`);
|
|
111
84
|
|
|
112
|
-
return nm
|
|
85
|
+
return nm;
|
|
113
86
|
};
|
|
114
87
|
|
|
115
88
|
/**
|