@hzab/utils 1.0.2 → 1.0.5
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/upload/OssUploadUtil.ts +14 -0
- 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
|
+
};
|
|
@@ -56,10 +56,19 @@ export interface IUploadOpt {
|
|
|
56
56
|
/** 是否是公开库 */
|
|
57
57
|
isPublic?: number;
|
|
58
58
|
};
|
|
59
|
+
/**
|
|
60
|
+
* 获取配置接口请求入参
|
|
61
|
+
*/
|
|
62
|
+
signatureParams?: {
|
|
63
|
+
/** 公开还是私有 */
|
|
64
|
+
isPublic?: number;
|
|
65
|
+
};
|
|
59
66
|
/** 文件上传的接口自定义入参 */
|
|
60
67
|
ossParams?: {};
|
|
61
68
|
/** 是否使用 hash 文件名 */
|
|
62
69
|
useHashName?: boolean;
|
|
70
|
+
/**缩略图参数 */
|
|
71
|
+
thumbnailParams?: string;
|
|
63
72
|
}
|
|
64
73
|
|
|
65
74
|
export function getSignature(opt: IGetSignatureOpt = {}) {
|
|
@@ -173,6 +182,11 @@ class OssUploadUtil {
|
|
|
173
182
|
return _axios
|
|
174
183
|
.post(ossParams.host, formData, { ...this.axiosConf, ...opt?.axiosConf })
|
|
175
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
|
+
}
|
|
176
190
|
resolve(res);
|
|
177
191
|
return res;
|
|
178
192
|
})
|
|
@@ -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
|
/**
|