@hzab/form-render 1.6.20 → 1.6.21-beta1

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 CHANGED
@@ -1,5 +1,10 @@
1
+ # @hzab/form-render@1.6.21
2
+
3
+ refactor: upload 接入 utils upload utils
4
+
1
5
  # @hzab/form-render@1.6.20
2
- feat: 新增 TreeCheckbox的readOnly属性
6
+
7
+ feat: 新增 TreeCheckbox 的 readOnly 属性
3
8
 
4
9
  # @hzab/form-render@1.6.19
5
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/form-render",
3
- "version": "1.6.20",
3
+ "version": "1.6.21-beta1",
4
4
  "description": "",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -19,7 +19,7 @@
19
19
  "author": "CaiYansong",
20
20
  "license": "ISC",
21
21
  "devDependencies": {
22
- "@hzab/data-model": "^1.2.0",
22
+ "@hzab/data-model": "^1.8.8",
23
23
  "@hzab/permissions": "^0.1.1",
24
24
  "@hzab/webpack-config": "0.0.12",
25
25
  "@types/react": "^17.0.62",
@@ -34,6 +34,7 @@
34
34
  "typescript": "^4.9.4"
35
35
  },
36
36
  "peerDependencies": {
37
+ "@hzab/utils": ">=1.0.5",
37
38
  "antd": "4.x",
38
39
  "axios": ">=1.6.2",
39
40
  "react": ">=16.8.0",
@@ -95,7 +95,6 @@ export const RichEditor = observer(function (props: PropsType, parentRef) {
95
95
  file,
96
96
  onSuccess: (res) => {
97
97
  setIsLoading(false);
98
- console.log(res);
99
98
  insertFn(res.url, "", "");
100
99
  },
101
100
  onError: () => {
@@ -1,339 +1,5 @@
1
- import { axios } from "@hzab/data-model";
1
+ import { OfflineUpload } from "@hzab/utils/src/upload/OfflineUpload";
2
2
 
3
- import { getFileNameByFileObj } from "./fileName";
4
- import { formatDirStr, mergeDirStr } from "./utils";
5
-
6
- /**
7
- * 获取上传相关配置入参
8
- */
9
- export interface IGetSignatureOpt {
10
- /**
11
- * 配置接口地址
12
- */
13
- serverUrl?: string;
14
- /**
15
- * axios 实例
16
- */
17
- axios?: Object;
18
- /**
19
- * axios 配置
20
- */
21
- axiosConf?: Object;
22
- /**
23
- * 请求入参
24
- */
25
- params?: {
26
- path?: string;
27
- };
28
- }
29
-
30
- export interface IOfflineUploadProps {
31
- /**
32
- * axios 实例
33
- */
34
- axios?: Object;
35
- /**
36
- * axios 配置
37
- */
38
- axiosConf?: Object;
39
- /**
40
- * 配置接口地址
41
- */
42
- serverUrl?: string;
43
- /**
44
- * 获取配置接口请求入参
45
- */
46
- signatureParams?: {
47
- path?: string;
48
- };
49
- }
50
-
51
- /**
52
- * 文件上传入参
53
- */
54
- export interface IUploadOpt {
55
- /**
56
- * 配置接口地址
57
- */
58
- serverUrl?: string;
59
- /**
60
- * 是否开启 Hash 命名
61
- */
62
- useHashName?: boolean;
63
- /**
64
- * 文件上传接口地址
65
- */
66
- uploadUrl?: string;
67
- /**
68
- * 获取配置接口请求入参
69
- */
70
- params?: {
71
- path?: string;
72
- // 兼容 老数据
73
- dir?: string;
74
- };
75
- /**
76
- * 文件上传请求入参
77
- */
78
- ossParams?: {
79
- path?: string;
80
- // 兼容 老数据
81
- dir?: string;
82
- };
83
- /**
84
- * axios 实例
85
- */
86
- axios?: Object;
87
- /**
88
- * axios 配置
89
- */
90
- axiosConf?: Object;
91
- }
92
-
93
- /**
94
- * 获取上传配置
95
- * @param opt
96
- * @returns
97
- */
98
- export function getSignature(opt: IGetSignatureOpt = {}) {
99
- const { serverUrl = "/api/v1/upload/init" } = opt;
100
- // 减 10 秒,避免发起请求时 刚好过期的情况
101
- const { axios: _ax = axios, params = {}, axiosConf } = opt;
102
- if (
103
- window._$offlineSignatureRes &&
104
- window._$offlineSignatureRes.data &&
105
- serverUrl + JSON.stringify(params) === window._$offlineSignatureRes.serverUrlParams &&
106
- Date.now() - window._$offlineSignatureRes.__saveTime < window._$offlineSignatureRes.expireTimeMillis - 10000
107
- ) {
108
- return Promise.resolve(window._$offlineSignatureRes.data);
109
- }
110
- // 处理 path 格式,必须为非 / 开头, / 结尾。如: test/
111
- params.path = formatDirStr(params.path);
112
-
113
- return _ax
114
- .post(
115
- serverUrl,
116
- {
117
- ...params,
118
- },
119
- {
120
- ...axiosConf,
121
- },
122
- )
123
- .then((res) => {
124
- if (res?.data?.code === 500) {
125
- return Promise.reject(res.data);
126
- }
127
- if (res?.data?.code === 200) {
128
- window._$offlineSignatureRes = {
129
- data: res?.data?.data,
130
- };
131
- if (window._$offlineSignatureRes) {
132
- window._$offlineSignatureRes.__saveTime = Date.now();
133
- window._$offlineSignatureRes.serverUrlParams = serverUrl + JSON.stringify(params);
134
- }
135
- return window._$offlineSignatureRes.data;
136
- }
137
- });
138
- }
139
-
140
- export interface IGetPreviewUrlsOpt {
141
- previewUrl?: string;
142
- params?: Object;
143
- axios?: Object;
144
- axiosConf?: Object;
145
- }
146
-
147
- /**
148
- * 获取预览地址-批量
149
- * @param urlList
150
- * @param opt
151
- * @returns
152
- */
153
- export const getPreviewUrls = function (urlList, opt: IGetPreviewUrlsOpt = {}) {
154
- return new Promise(async (resolve, reject) => {
155
- const { axios: _ax = axios, axiosConf, previewUrl } = opt || {};
156
-
157
- const _axios = _ax;
158
-
159
- let _list = urlList;
160
- if (_list && !Array.isArray(_list)) {
161
- _list = [_list];
162
- }
163
-
164
- return _axios
165
- .post(previewUrl || "/api/v1/upload/preview", _list, { ...axiosConf })
166
- .then((res) => {
167
- if (res?.data?.code == 200) {
168
- resolve(res?.data?.data);
169
- } else {
170
- reject(res);
171
- }
172
- return res;
173
- })
174
- .catch((err) => {
175
- console.error("offline upload err", err);
176
- reject(err);
177
- return Promise.reject(err);
178
- });
179
- });
180
- };
181
-
182
- /**
183
- * 获取预览地址
184
- * @param url
185
- * @param opt
186
- * @returns
187
- */
188
- export const getPreviewUrl = function (url, opt: IGetPreviewUrlsOpt = {}) {
189
- return new Promise(async (resolve, reject) => {
190
- const { axios: _ax = axios, axiosConf, previewUrl } = opt || {};
191
-
192
- const _axios = _ax;
193
-
194
- return _axios
195
- .post(previewUrl || "/api/v1/upload/preview", [url], { ...axiosConf })
196
- .then((res) => {
197
- if (res?.data?.code == 200) {
198
- resolve(res?.data?.data?.[0]);
199
- } else {
200
- reject(res);
201
- }
202
- return res;
203
- })
204
- .catch((err) => {
205
- console.error("offline upload err", err);
206
- reject(err);
207
- return Promise.reject(err);
208
- });
209
- });
210
- };
211
-
212
- /**
213
- * 私有部署版文件上传
214
- */
215
- export class OfflineUpload {
216
- axios;
217
- axiosConf;
218
- serverUrl;
219
- signatureParams;
220
- constructor(props: IOfflineUploadProps = {}) {
221
- this.axios = props.axios || axios;
222
- this.axiosConf = props.axiosConf || {};
223
- this.serverUrl = props.serverUrl || "/api/v1/upload/init";
224
- this.signatureParams = props.signatureParams || {};
225
- }
226
-
227
- getSignature(serverUrl = this.serverUrl, opt) {
228
- // path 前缀 oss-upload 文件目录
229
- opt.params.path = mergeDirStr("web-upload/", opt.params.path ?? opt.params.dir);
230
- return getSignature({
231
- ...opt,
232
- serverUrl,
233
- axios: opt?.axios || this.axios,
234
- axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
235
- });
236
- }
237
-
238
- upload(file, opt: IUploadOpt = {}) {
239
- return new Promise(async (resolve, reject) => {
240
- // filename 表示待上传的本地文件名称。
241
- const filename = getFileNameByFileObj(file, opt);
242
- try {
243
- const fileInfo = await this.getSignature(opt.serverUrl || this.serverUrl, {
244
- ...opt,
245
- params: {
246
- /** 最终保存的文件名称(不传默认文件名) */
247
- filename,
248
- /** 存储路径(不传默认基础路径) */
249
- path: opt.params?.dir || this.signatureParams?.dir,
250
- /** 文件存储平台(1、s3,2、本地存储,3、FTP,默认s3) */
251
- platformCode: undefined,
252
- ...this.signatureParams,
253
- ...opt.params,
254
- },
255
- });
256
- const { uploadUrl, ossParams: propOssParams } = opt || {};
257
- const formData = new FormData();
258
- formData.set("fileInfo", fileInfo);
259
- formData.set("file", file);
260
-
261
- if (propOssParams) {
262
- for (const key in propOssParams) {
263
- if (Object.hasOwnProperty.call(propOssParams, key)) {
264
- formData.set(key, propOssParams[key]);
265
- }
266
- }
267
- }
268
-
269
- const _axios = opt?.axios || this.axios;
270
-
271
- return _axios
272
- .post(uploadUrl || "/api/v1/upload/execute", formData, { ...this.axiosConf, ...opt?.axiosConf })
273
- .then((res) => {
274
- if (res?.data?.code == 200) {
275
- resolve(res?.data?.data);
276
- } else {
277
- reject(res);
278
- }
279
- return res;
280
- })
281
- .catch((err) => {
282
- console.error("offline upload err", err);
283
- reject(err);
284
- return Promise.reject(err);
285
- });
286
- } catch (error) {
287
- reject(error);
288
- return Promise.reject(error);
289
- }
290
- });
291
- }
292
-
293
- /**
294
- * 获取预览地址
295
- * @param urlList
296
- * @param opt
297
- * @returns
298
- */
299
- getPreviewUrls(urlList, opt) {
300
- return getPreviewUrls(urlList, {
301
- ...opt,
302
- axios: opt?.axios || this.axios,
303
- axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
304
- });
305
- }
306
- }
307
-
308
- /**
309
- * 批量处理预览地址
310
- * @param fileList
311
- * @param opt
312
- * @returns
313
- */
314
- export const handlePreviewUrls = async function (fileList, opt: IGetPreviewUrlsOpt) {
315
- const res = await getPreviewUrls(
316
- fileList?.map((it) => it.storeUrl || it.url),
317
- {
318
- ...opt,
319
- },
320
- );
321
-
322
- fileList.forEach((it, i) => {
323
- // TODO: storeUrl 来源于【本地 url 直接使用预览地址,解决 previewFile 无法正常使用 previewUrl 的问题】
324
- if (!it.storeUrl) {
325
- it.storeUrl = it.url;
326
- }
327
- // TODO: 本地 url 直接使用预览地址,解决 previewFile 无法正常使用 previewUrl 的问题
328
- it.url = res[i].presignedUrl;
329
-
330
- // 常规预览地址写法
331
- it.previewUrl = res[i].presignedUrl;
332
- if (it.uploadInfo) {
333
- it.uploadInfo.previewUrl = it.previewUrl;
334
- }
335
- });
336
- return fileList;
337
- };
3
+ export * from "@hzab/utils/src/upload/OfflineUpload";
338
4
 
339
5
  export default OfflineUpload;
@@ -1,39 +1,4 @@
1
- import { checkImageUrl, checkVideoUrl, checkAudioUrl } from "./utils";
1
+ export * from "@hzab/utils/src/file/fileType";
2
+ import { getFileTypeStr } from "@hzab/utils/src/file/fileType";
2
3
 
3
- export const TYPE_VIDEO = "video";
4
- export const TYPE_IMG = "image";
5
- export const TYPE_AUDIO = "audio";
6
-
7
- export function checkFileType(file) {
8
- const { type, url = file?.ossUrl } = file || {};
9
- let fileType = "";
10
- // 判断文件类型,获取对应展示的数据
11
- if (url) {
12
- // 图片
13
- if (url.startsWith("data:image/") || checkImageUrl(url)) {
14
- fileType = TYPE_IMG;
15
- } else if (checkVideoUrl(url)) {
16
- // 视频
17
- fileType = TYPE_VIDEO;
18
- } else if (checkAudioUrl(url)) {
19
- // 音频
20
- fileType = TYPE_AUDIO;
21
- }
22
- } else if (type) {
23
- // 图片
24
- if (type?.startsWith("image/")) {
25
- fileType = TYPE_IMG;
26
- }
27
-
28
- // 视频
29
- if (type?.startsWith("video/")) {
30
- fileType = TYPE_VIDEO;
31
- }
32
-
33
- // 音频
34
- if (type?.startsWith("audio/")) {
35
- fileType = TYPE_AUDIO;
36
- }
37
- }
38
- return fileType || type;
39
- }
4
+ export const checkFileType = getFileTypeStr;
@@ -1,142 +1,5 @@
1
- import { nanoid } from "./nanoid";
1
+ export * from "@hzab/utils/src/file/fileName";
2
2
 
3
- /**
4
- * 获取文件后缀
5
- * @param fileUrl
6
- * @returns
7
- */
8
- export function getFileExt(fileUrl) {
9
- if (typeof fileUrl !== "string") {
10
- return;
11
- }
12
- const res = fileUrl?.match(/([^\/]+?)\.([^\/\.]+)$/);
13
- if (res && res.length > 2) {
14
- return res[2];
15
- }
16
- return fileUrl;
17
- }
3
+ import { getFileNameByFileObj, getFileNameObj, setFileNameObj, getValByKey } from "@hzab/utils/src/upload/uploadUtils";
18
4
 
19
- /**
20
- * 从 url 获取文件名称,不包含后缀
21
- * @param {*} fileName
22
- * @param {*} str
23
- * @returns
24
- */
25
- export function getFileName(fileUrl) {
26
- if (typeof fileUrl !== "string") {
27
- return;
28
- }
29
- const res = fileUrl?.match(/([^\/]+?)\.[^\/\.]+$/);
30
- if (res && res.length > 0) {
31
- return res?.[1] || res?.[0];
32
- }
33
- return fileUrl;
34
- }
35
-
36
- /**
37
- * 从 url 获取文件名称,包含后缀
38
- * @param {*} fileName
39
- * @param {*} str
40
- * @returns
41
- */
42
- export function getFullFileName(fileUrl) {
43
- if (typeof fileUrl !== "string") {
44
- return;
45
- }
46
- const res = fileUrl?.match(/[^\/]+?\.[^\/\.]+$/);
47
- if (res && res.length > 0) {
48
- return res[0];
49
- }
50
- return fileUrl;
51
- }
52
-
53
- /**
54
- * 合并文件名称,自动处理后缀
55
- * @param {*} fileName
56
- * @param {*} str
57
- * @returns
58
- */
59
- export const mergeFileName = function (fileName, str = "") {
60
- if (typeof fileName !== "string") {
61
- return "";
62
- }
63
-
64
- const suffix = fileName?.match(/\.([^\/.]+)$/)?.[1] || "";
65
- const name = fileName?.match(/([^\/]+)\.[^.]+$/)?.[1] || "";
66
- return `${name}${str ?? ""}.${suffix}`;
67
- };
68
-
69
- /**
70
- * 获取唯一的文件名
71
- * @param {*} fileName
72
- * @returns
73
- */
74
- export const getUFileName = function (fileName) {
75
- return mergeFileName(getFileName(fileName) || "rc-upload", "-" + `~kid-${nanoid()}~` + `${Date.now()}`);
76
- };
77
-
78
- /**
79
- * 根据 file 对象获取文件名称,文件名称包含:初始名称、类型
80
- * 数据存储格式 ~k[key]-[data]~ 如: ~ktime-1743649562530~
81
- * key、data 中的 ~ 转为 _
82
- * @param file
83
- */
84
- export const getFileNameByFileObj = (file, opt: any) => {
85
- const { useHashName = true } = opt || {};
86
- const _fileName = file.name || getFullFileName(file.url);
87
- if (_fileName.indexOf("~kid-") >= 0 || !useHashName) {
88
- return _fileName;
89
- }
90
- // id、时间、文件类型
91
- const id = file.id || file.uid || nanoid();
92
- return mergeFileName(
93
- _fileName,
94
- "-" +
95
- setFileNameObj({
96
- id,
97
- time: file.createTime ?? Date.now(),
98
- type: (file.type || file.contentType)?.replace("/", "_"),
99
- }),
100
- );
101
- };
102
-
103
- /**
104
- * 解析
105
- * @param url
106
- * @returns
107
- */
108
- export const getFileNameObj = function (url): any {
109
- const list = url.match(/~k([^-~]+)-([^~]*)~/g);
110
- const res = {};
111
- list?.forEach((it) => {
112
- const arr = it?.match(/~k([^-~]+)-([^~]*)~/);
113
- if (arr?.length >= 3) {
114
- res[arr[1]] = arr[2];
115
- }
116
- });
117
- return res;
118
- };
119
-
120
- /**
121
- * 解析
122
- * @param url
123
- * @returns
124
- */
125
- export function setFileNameObj(obj) {
126
- return Object.keys(obj)
127
- .map((key) => {
128
- return `~k${key?.replace(/~/g, "_")}-${obj[key]?.toString()?.replace(/~/g, "_")}~`;
129
- })
130
- .join("");
131
- }
132
-
133
- /**
134
- * 通过 key 获取对应的值
135
- * @param url
136
- * @param key
137
- * @returns
138
- */
139
- export const getValByKey = function (url, key) {
140
- const reg = new RegExp(`~k${key?.replace(/~/g, "_")}-\([^~]*\)~`);
141
- return url.match(reg)?.[1];
142
- };
5
+ export { getFileNameByFileObj, getFileNameObj, setFileNameObj, getValByKey };
@@ -1,6 +1,9 @@
1
- import { getFileURL, getFileType, isBase64Str } from "./utils";
2
- import { getUFileName, getFileName, getFullFileName, getFileExt, getFileNameObj } from "./fileName";
3
- import { nanoid } from "./nanoid";
1
+ import { getFileURL } from "@hzab/utils/src/file/file";
2
+ import { getFileType } from "@hzab/utils/src/file/fileType";
3
+ import { isBase64Str } from "@hzab/utils/src/base64Str";
4
+ import { getUFileName, getFileName, getFullFileName, getFileExt } from "@hzab/utils/src/file/fileName";
5
+ import { getFileNameObj } from "@hzab/utils/src/upload/uploadUtils";
6
+ import { nanoidNumALetters } from "@hzab/utils/src/nanoid";
4
7
 
5
8
  /**
6
9
  * 出入参归一化
@@ -218,7 +221,8 @@ export const handleInputItem = (data, opt: IHandlerOpt) => {
218
221
  }
219
222
  if (!file.id) {
220
223
  file.id =
221
- file.uid || `${getFileName(file.url || file.name || file.filename) || "rc-upload"}-${Date.now()}-${nanoid()}`;
224
+ file.uid ||
225
+ `${getFileName(file.url || file.name || file.filename) || "rc-upload"}-${Date.now()}-${nanoidNumALetters()}`;
222
226
  }
223
227
  if (!file.name) {
224
228
  file.name = file.id;
@@ -1,7 +1,5 @@
1
- import { customAlphabet } from "nanoid";
2
- /**
3
- * 指定 nanoid 字符集合
4
- */
5
- export const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1
+ export * from "@hzab/utils/src/nanoid";
6
2
 
7
- export const nanoid = customAlphabet(alphabet, 8);
3
+ import { nanoidNumALetters } from "@hzab/utils/src/nanoid";
4
+
5
+ export const nanoid = nanoidNumALetters;
@@ -1,106 +1,4 @@
1
- import { axios } from "@hzab/data-model";
2
- import { getFileNameByFileObj } from "./fileName";
3
- import { formatDirStr, mergeDirStr } from "./utils";
4
-
5
- export function getSignature(opt = {}) {
6
- const { serverUrl = "/api/v1/user/oss/getWebOssConfig" } = opt;
7
- // 减 10 秒,避免发起请求时 刚好过期的情况
8
- if (
9
- window.__ossSignatureRes &&
10
- serverUrl === window.__ossSignatureRes.serverUrl &&
11
- Date.now() - window.__ossSignatureRes.__saveTime < window.__ossSignatureRes.expireTimeMillis - 10000
12
- ) {
13
- return Promise.resolve(window.__ossSignatureRes);
14
- }
15
- const { axios: _ax = axios, params = {}, axiosConf } = opt;
16
- // 处理 dir 格式,必须为非 / 开头, / 结尾。如: test/
17
- params.dir = formatDirStr(params.dir);
18
-
19
- return _ax
20
- .get(serverUrl, {
21
- ...axiosConf,
22
- params: {
23
- isPublic: 1,
24
- ...params,
25
- },
26
- })
27
- .then((res) => {
28
- window.__ossSignatureRes = res?.data?.data ?? res?.data ?? res;
29
- if (window.__ossSignatureRes) {
30
- window.__ossSignatureRes.__saveTime = Date.now();
31
- window.__ossSignatureRes.serverUrl = serverUrl;
32
- }
33
- return window.__ossSignatureRes;
34
- });
35
- }
36
-
37
- class OssUpload {
38
- constructor(props = {}) {
39
- this.axios = props.axios || axios;
40
- this.axiosConf = props.axiosConf || {};
41
- this.serverUrl = props.serverUrl || "/api/v1/user/oss/getWebOssConfig";
42
- this.signatureParams = props.signatureParams || {};
43
- }
44
-
45
- getSignature(serverUrl = this.serverUrl, opt) {
46
- // dir 前缀 oss-upload 文件目录
47
- opt.params.dir = mergeDirStr("web-upload/", opt.params.dir);
48
- return getSignature({
49
- ...opt,
50
- serverUrl,
51
- axios: opt?.axios || this.axios,
52
- axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
53
- });
54
- }
55
-
56
- upload(file, opt = {}) {
57
-
58
- return new Promise(async (resolve, reject) => {
59
- const ossParams = await this.getSignature(opt.serverUrl || this.serverUrl, {
60
- ...opt,
61
- params: { ...this.signatureParams, ...opt.params },
62
- });
63
-
64
- const { ossParams: propOssParams, useHashName } = opt || {};
65
- const formData = new FormData();
66
- // key 表示上传到 Bucket 内的 Object 的完整路径,例如 exampledir/exampleobject.txtObject,完整路径中不能包含 Bucket 名称。
67
- // filename 表示待上传的本地文件名称。
68
- const filename = getFileNameByFileObj(file, opt);
69
- const key = `${ossParams?.dir}${filename}`;
70
- formData.set("key", key);
71
- formData.set("OSSAccessKeyId", ossParams.accessid);
72
- formData.set("policy", ossParams.policy);
73
- formData.set("Signature", ossParams.signature);
74
- if (ossParams.callback) {
75
- formData.set("callback", ossParams.callback);
76
- }
77
- formData.set("success_action_status", 200);
78
- formData.set("file", file);
79
-
80
- if (propOssParams) {
81
- for (const key in propOssParams) {
82
- if (Object.hasOwnProperty.call(propOssParams, key)) {
83
- formData.set(key, propOssParams[key]);
84
- }
85
- }
86
- }
87
-
88
- const _axios = opt?.axios || this.axios;
89
-
90
- return _axios
91
- .post(ossParams.host, formData, { ...this.axiosConf, ...opt?.axiosConf })
92
- .then((res) => {
93
- resolve(res);
94
- return res;
95
- })
96
- .catch((err) => {
97
- console.error("oss upload err", err);
98
- reject(err);
99
- return Promise.reject(err);
100
- });
101
- });
102
- }
103
- }
1
+ import OssUploadUtil, { axios, getSignature } from "@hzab/utils/src/upload/OssUploadUtil";
104
2
 
105
3
  /**
106
4
  * 处理文件上传逻辑
@@ -111,7 +9,8 @@ class OssUpload {
111
9
  export async function handleOssUpload(files, opt) {
112
10
  const _files = files;
113
11
  const { ossUrl, signatureParams, ossParams, axiosConf, useHashName } = opt || {};
114
- const ossUpload = new OssUpload({
12
+ const ossUpload = new OssUploadUtil({
13
+ ...opt,
115
14
  axios: opt.axios,
116
15
  axiosConf: axiosConf,
117
16
  serverUrl: ossUrl || "/api/v1/user/oss/getWebOssConfig",
@@ -154,6 +53,6 @@ export async function handleOssUpload(files, opt) {
154
53
  });
155
54
  }
156
55
 
157
- export { axios };
56
+ export { axios, getSignature };
158
57
 
159
- export default OssUpload;
58
+ export default OssUploadUtil;
@@ -1,194 +1,27 @@
1
- import { nanoid } from "nanoid";
2
-
3
- /**
4
- * 格式化 dir 字符串,必须为非 / 开头, / 结尾。如: test/
5
- * @param {*} dir
6
- * @returns
7
- */
8
- export const formatDirStr = function (dir) {
9
- return dir?.replace(/^\/(.*)\/*$/, "$1").replace(/(.?=*)\/*$/, "$1/");
10
- };
11
-
12
- /**
13
- * 合并 dir 字符串
14
- * @param {*} dir
15
- */
16
- export const mergeDirStr = function (dir1, dir2) {
17
- return `${formatDirStr(dir1 || "")}${formatDirStr(dir2 || "")}`;
1
+ import { formatDirStr, mergeDirStr, handleMaxCount, mergePreviewConfig } from "@hzab/utils/upload/uploadUtils";
2
+ import { getFileURL } from "@hzab/utils/file/file";
3
+ import { isBase64Str } from "@hzab/utils/src/base64Str";
4
+ import {
5
+ checkImageUrl,
6
+ checkVideoUrl,
7
+ checkAudioUrl,
8
+ isValidMediaType,
9
+ checkUrlSuffix,
10
+ } from "@hzab/utils/src/file/fileType";
11
+ import { getArr } from "@hzab/utils/src/array";
12
+
13
+ export {
14
+ formatDirStr,
15
+ mergeDirStr,
16
+ getFileURL,
17
+ isBase64Str,
18
+ checkImageUrl,
19
+ checkVideoUrl,
20
+ checkAudioUrl,
21
+ isValidMediaType,
22
+ checkUrlSuffix,
23
+ getFileType,
24
+ getArr,
25
+ handleMaxCount,
26
+ mergePreviewConfig,
18
27
  };
19
-
20
- /**
21
- * 建立一个可以存取该 file 的 url
22
- * @param {Object} file 文件
23
- * @returns {string} url
24
- * blob:http://localhost:8000/c9950644-5118-4231-9be7-8183bde1fdc7
25
- */
26
- export function getFileURL(file) {
27
- const _file = file?.file || file;
28
- if (!(_file instanceof File)) {
29
- return;
30
- }
31
-
32
- let url = _file.url || null;
33
-
34
- try {
35
- // 下面函数执行的效果是一样的,只是需要针对不同的浏览器执行不同的 js 函数而已
36
- if (window.createObjectURL != undefined) {
37
- // basic
38
- return window.createObjectURL(_file);
39
- } else if (window.URL != undefined) {
40
- // mozilla(firefox)
41
- return window.URL.createObjectURL(_file);
42
- } else if (window.webkitURL != undefined) {
43
- // webkit or chrome
44
- return window.webkitURL.createObjectURL(_file);
45
- }
46
- } catch (error) {
47
- console.warn("getFileURL Error: ", error);
48
- }
49
-
50
- return url;
51
- }
52
-
53
- /**
54
- * 是否是 base64 字符串
55
- * @param {*} str
56
- * @returns
57
- */
58
- export const isBase64Str = function (str) {
59
- return /^data:\w+\/\w+;base64/i.test(str);
60
- };
61
-
62
- /**
63
- * 判断 url 是否带有指定图片后缀
64
- * @param {string} url
65
- * @returns
66
- */
67
- export function checkImageUrl(url) {
68
- // base64
69
- if (/^data:image\/\w+;base64/i.test(url)) {
70
- return true;
71
- }
72
- const imgTypes = [
73
- "apng",
74
- "avif",
75
- "bmp",
76
- "gif",
77
- "ico",
78
- "cur",
79
- "jpg",
80
- "jpeg",
81
- "jfif",
82
- "pjpeg",
83
- "pjp",
84
- "png",
85
- "svg",
86
- "tif",
87
- "tiff",
88
- "webp",
89
- ];
90
- return checkUrlSuffix(url, imgTypes);
91
- }
92
-
93
- /**
94
- * 判断是否是合法的媒体类型
95
- * @param {String} input
96
- * @returns
97
- */
98
- export function isValidMediaType(input) {
99
- const validPrefixes = ["image/", "video/", "audio/"];
100
- return validPrefixes.some((prefix) => input?.startsWith(prefix));
101
- }
102
-
103
- /**
104
- * 判断 url 是否带有指定视频后缀
105
- * @param {string} url
106
- * @returns
107
- */
108
- export function checkVideoUrl(url) {
109
- const imgTypes = ["3gp", "mpg", "mpeg", "mp4", "m4v", "m4p", "ogv", "ogg", "mov", "webm"];
110
- return checkUrlSuffix(url, imgTypes);
111
- }
112
-
113
- /**
114
- * 判断 url 是否带有指定音频后缀
115
- * @param {string} url
116
- * @returns
117
- */
118
- export function checkAudioUrl(url) {
119
- const imgTypes = ["3gp", "adts", "mpeg", "mp3", "mp4", "ogg", "mov", "webm", "rtp", "amr", "wav"];
120
- return checkUrlSuffix(url, imgTypes);
121
- }
122
-
123
- /**
124
- * 检查 url 是否带有指定后缀
125
- * @param {string} url url 地址
126
- * @param {Array} types 后缀数组
127
- * @returns
128
- */
129
- export function checkUrlSuffix(url, types = [], caseSensitive) {
130
- if (!url) {
131
- return false;
132
- }
133
- let _url = url?.replace(/\?.+/, "");
134
- const reg = new RegExp(`\.(${types.join("|")})$`, caseSensitive ? undefined : "i");
135
- if (reg.test(_url)) {
136
- return true;
137
- }
138
- }
139
-
140
- export function getFileType(file) {
141
- if (typeof file === "object" && file?.type) {
142
- return file?.type;
143
- }
144
- if (typeof file === "string") {
145
- let type = null;
146
- if (checkVideoUrl(file)) {
147
- type = "video/mp4";
148
- } else if (checkImageUrl(file)) {
149
- type = "image/jpeg";
150
- } else if (checkAudioUrl(file)) {
151
- type = "audio/3gpp";
152
- } else if (checkUrlSuffix(file, ["pdf"])) {
153
- type = "application/pdf";
154
- } else if (checkUrlSuffix(file, ["pdf"])) {
155
- type = "application/pdf";
156
- } else if (checkUrlSuffix(file, ["xlsx", "xls", "csv", "xlsm", "xlsb"])) {
157
- type = "application/vnd.ms-excel";
158
- } else if (checkUrlSuffix(file, ["doc", "docx"])) {
159
- type = "application/msword";
160
- } else if (checkUrlSuffix(file, ["ppt", "pptx"])) {
161
- type = "application/vnd.ms-powerpoint";
162
- }
163
- return type;
164
- }
165
- }
166
-
167
- function getArr(value) {
168
- return Array.isArray(value) ? value : (value && [value]) || [];
169
- }
170
-
171
- export function handleMaxCount(fileList, maxCount) {
172
- let list = getArr(fileList);
173
- if (maxCount > 0 && list.length > maxCount) {
174
- list = list.slice(0, maxCount);
175
- }
176
- return list;
177
- }
178
-
179
- /**
180
- * 预览 url 添加前缀
181
- * @param {*} uri
182
- * @param {*} previewConfig
183
- * @returns
184
- */
185
- export function mergePreviewConfig(uri, previewConfig) {
186
- const isUriPattern = /http[s]?:\/\/[^\s]+/.test(uri);
187
- const query = previewConfig?.query ? "?" + previewConfig?.query : "";
188
-
189
- if (isUriPattern) return `${uri}${query}`?.trim();
190
-
191
- if (previewConfig) return `${previewConfig?.url}/${uri}${query}`?.trim();
192
-
193
- return uri;
194
- }
@@ -1,6 +1,5 @@
1
- import { getFileURL } from "../../common/utils";
2
-
3
- import { TYPE_VIDEO, TYPE_IMG, TYPE_AUDIO, checkFileType } from "../../common/checkFileType";
1
+ import { getFileURL } from "@hzab/utils/src/file/file";
2
+ import { TYPE_VIDEO, TYPE_IMG, TYPE_AUDIO, getFileTypeStr } from "@hzab/utils/src/file/fileType";
4
3
 
5
4
  import Video from "../video";
6
5
  import Image from "../Image";
@@ -28,7 +27,7 @@ export const ItemList = (props) => {
28
27
  </div>
29
28
  );
30
29
 
31
- const fileType = checkFileType(it);
30
+ const fileType = getFileTypeStr(it);
32
31
  if (fileType === TYPE_IMG) {
33
32
  content = <Image src={src} alt={name} />;
34
33
  }
@@ -1,5 +1,13 @@
1
- import { checkFileType, TYPE_VIDEO, TYPE_IMG, TYPE_AUDIO } from "../../common/checkFileType";
2
- import { checkImageUrl, checkVideoUrl, checkAudioUrl, isValidMediaType } from "../../common/utils";
1
+ import {
2
+ TYPE_VIDEO,
3
+ TYPE_IMG,
4
+ TYPE_AUDIO,
5
+ getFileTypeStr,
6
+ checkImageUrl,
7
+ checkVideoUrl,
8
+ checkAudioUrl,
9
+ isValidMediaType,
10
+ } from "@hzab/utils/src/file/fileType";
3
11
 
4
12
  export const previewRenderEnum = {
5
13
  [TYPE_VIDEO]: videoRender,
@@ -8,7 +16,7 @@ export const previewRenderEnum = {
8
16
  };
9
17
 
10
18
  export function hasPreviewRender(file) {
11
- return !!previewRenderEnum[checkFileType(file)];
19
+ return !!previewRenderEnum[getFileTypeStr(file)];
12
20
  }
13
21
 
14
22
  function getFirstAcceptRule(accept: string): string {
@@ -31,11 +39,11 @@ export function hasPreviewMedium(accept: string): boolean {
31
39
  }
32
40
 
33
41
  export function getPreviewRender(file, accept) {
34
- const fileType = checkFileType(file);
42
+ const fileType = getFileTypeStr(file);
35
43
  let render = null;
36
44
 
37
45
  if (fileType) {
38
- render = previewRenderEnum[checkFileType(file)];
46
+ render = previewRenderEnum[getFileTypeStr(file)];
39
47
  }
40
48
 
41
49
  if (accept) {
@@ -50,8 +58,6 @@ export function getPreviewRender(file, accept) {
50
58
  return render;
51
59
  }
52
60
 
53
-
54
-
55
61
  export function getPreviewContent(file, accept) {
56
62
  let render = getPreviewRender(file, accept);
57
63
  if (!render) {
@@ -3,7 +3,7 @@ import { Button, Modal, message } from "antd";
3
3
 
4
4
  import ItemList from "./components/ItemList";
5
5
 
6
- import { handleMaxCount } from "./common/utils";
6
+ import { handleMaxCount } from "@hzab/utils/upload/uploadUtils";
7
7
  import { handleInputFileList, handleOutputFileList } from "./common/handleIOFileList";
8
8
  import { handleOssUpload } from "./common/ossUpload";
9
9