@hzab/utils 1.0.8 → 1.0.9

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,3 +1,7 @@
1
+ # @hzab/utils@1.0.9
2
+
3
+ fix:获取文件后缀名
4
+
1
5
  # @hzab/utils@1.0.8
2
6
 
3
7
  feat: 添加缩略图地址
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/utils",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "utils",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -1,221 +1,222 @@
1
- export const TYPE_VIDEO = "video";
2
- export const TYPE_IMG = "image";
3
- export const TYPE_AUDIO = "audio";
4
-
5
- /**
6
- * 检查 url 是否带有指定后缀
7
- * @param {string} url url 地址
8
- * @param {Array} types 后缀数组
9
- * @returns
10
- */
11
- export function checkUrlSuffix(url, types = [], caseSensitive = false) {
12
- if (!url) {
13
- return false;
14
- }
15
- let _url = url?.replace(/\?.+/, "");
16
- const reg = new RegExp(`\.(${types.join("|")})$`, caseSensitive ? undefined : "i");
17
- if (reg.test(_url)) {
18
- return true;
19
- }
20
- }
21
-
22
- /**
23
- * 获取文件类型(文件自身类型,如:video/mp4、image/jpeg)
24
- * @param file
25
- * @returns
26
- */
27
- export function getFileType(file) {
28
- if (typeof file === "object" && file?.type) {
29
- return file?.type;
30
- }
31
- if (typeof file === "string") {
32
- let type = null;
33
- if (checkVideoUrl(file)) {
34
- type = "video/mp4";
35
- } else if (checkImageUrl(file)) {
36
- type = "image/jpeg";
37
- } else if (checkAudioUrl(file)) {
38
- type = "audio/3gpp";
39
- } else if (checkUrlSuffix(file, ["pdf"])) {
40
- type = "application/pdf";
41
- } else if (checkUrlSuffix(file, ["xlsx", "xls", "csv", "xlsm", "xlsb"])) {
42
- type = "application/vnd.ms-excel";
43
- } else if (checkUrlSuffix(file, ["doc", "docx"])) {
44
- type = "application/msword";
45
- } else if (checkUrlSuffix(file, ["ppt", "pptx"])) {
46
- type = "application/vnd.ms-powerpoint";
47
- }
48
- return type;
49
- }
50
- }
51
-
52
- /**
53
- * 判断 url 是否带有指定图片后缀
54
- * @param {string} url
55
- * @returns
56
- */
57
- export function checkImageUrl(url) {
58
- // base64
59
- if (/^data:image\/\w+;base64/i.test(url)) {
60
- return true;
61
- }
62
- const imgTypes = [
63
- "apng",
64
- "avif",
65
- "bmp",
66
- "gif",
67
- "ico",
68
- "cur",
69
- "jpg",
70
- "jpeg",
71
- "jfif",
72
- "pjpeg",
73
- "pjp",
74
- "png",
75
- "svg",
76
- "tif",
77
- "tiff",
78
- "webp",
79
- ];
80
- return checkUrlSuffix(url, imgTypes);
81
- }
82
-
83
- /**
84
- * 判断是否是合法的媒体类型
85
- * @param {String} input
86
- * @returns
87
- */
88
- export function isValidMediaType(input) {
89
- const validPrefixes = ["image/", "video/", "audio/"];
90
- return validPrefixes.some((prefix) => input?.startsWith(prefix));
91
- }
92
-
93
- /**
94
- * 判断 url 是否带有指定视频后缀
95
- * @param {string} url
96
- * @returns
97
- */
98
- export function checkVideoUrl(url) {
99
- const imgTypes = ["3gp", "mpg", "mpeg", "mp4", "m4v", "m4p", "ogv", "ogg", "mov", "webm"];
100
- return checkUrlSuffix(url, imgTypes);
101
- }
102
-
103
- /**
104
- * 判断 url 是否带有指定音频后缀
105
- * @param {string} url
106
- * @returns
107
- */
108
- export function checkAudioUrl(url) {
109
- const imgTypes = ["3gp", "adts", "mpeg", "mp3", "mp4", "ogg", "mov", "webm", "rtp", "amr", "wav"];
110
- return checkUrlSuffix(url, imgTypes);
111
- }
112
-
113
- /**
114
- * 获取文件类型(项目内部枚举,如:图片-image、视频-video)
115
- * @param file
116
- * @returns
117
- */
118
- export function getFileTypeStr(file) {
119
- const { type, url = file?.ossUrl } = file || {};
120
- let fileType = "";
121
- // 判断文件类型,获取对应展示的数据
122
- if (url) {
123
- // 图片
124
- if (url.startsWith("data:image/") || checkImageUrl(url)) {
125
- fileType = TYPE_IMG;
126
- } else if (checkVideoUrl(url)) {
127
- // 视频
128
- fileType = TYPE_VIDEO;
129
- } else if (checkAudioUrl(url)) {
130
- // 音频
131
- fileType = TYPE_AUDIO;
132
- }
133
- } else if (type) {
134
- // 图片
135
- if (type?.startsWith("image/")) {
136
- fileType = TYPE_IMG;
137
- }
138
-
139
- // 视频
140
- if (type?.startsWith("video/")) {
141
- fileType = TYPE_VIDEO;
142
- }
143
-
144
- // 音频
145
- if (type?.startsWith("audio/")) {
146
- fileType = TYPE_AUDIO;
147
- }
148
- }
149
- return fileType || type;
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
- };
1
+ export const TYPE_VIDEO = "video";
2
+ export const TYPE_IMG = "image";
3
+ export const TYPE_AUDIO = "audio";
4
+
5
+ /**
6
+ * 检查 url 是否带有指定后缀
7
+ * @param {string} url url 地址
8
+ * @param {Array} types 后缀数组
9
+ * @returns
10
+ */
11
+ export function checkUrlSuffix(url, types = [], caseSensitive = false) {
12
+ if (!url) {
13
+ return false;
14
+ }
15
+ let _url = url?.replace(/\?.+/, "");
16
+ const reg = new RegExp(`\.(${types.join("|")})$`, caseSensitive ? undefined : "i");
17
+ if (reg.test(_url)) {
18
+ return true;
19
+ }
20
+ }
21
+
22
+ /**
23
+ * 获取文件类型(文件自身类型,如:video/mp4、image/jpeg)
24
+ * @param file
25
+ * @returns
26
+ */
27
+ export function getFileType(file) {
28
+ if (typeof file === "object" && file?.type) {
29
+ return file?.type;
30
+ }
31
+ if (typeof file === "string") {
32
+ let type = null;
33
+ if (checkVideoUrl(file)) {
34
+ type = "video/mp4";
35
+ } else if (checkImageUrl(file)) {
36
+ type = "image/jpeg";
37
+ } else if (checkAudioUrl(file)) {
38
+ type = "audio/3gpp";
39
+ } else if (checkUrlSuffix(file, ["pdf"])) {
40
+ type = "application/pdf";
41
+ } else if (checkUrlSuffix(file, ["xlsx", "xls", "csv", "xlsm", "xlsb"])) {
42
+ type = "application/vnd.ms-excel";
43
+ } else if (checkUrlSuffix(file, ["doc", "docx"])) {
44
+ type = "application/msword";
45
+ } else if (checkUrlSuffix(file, ["ppt", "pptx"])) {
46
+ type = "application/vnd.ms-powerpoint";
47
+ }
48
+ return type;
49
+ }
50
+ }
51
+
52
+ /**
53
+ * 判断 url 是否带有指定图片后缀
54
+ * @param {string} url
55
+ * @returns
56
+ */
57
+ export function checkImageUrl(url) {
58
+ // base64
59
+ if (/^data:image\/\w+;base64/i.test(url)) {
60
+ return true;
61
+ }
62
+ const imgTypes = [
63
+ "apng",
64
+ "avif",
65
+ "bmp",
66
+ "gif",
67
+ "ico",
68
+ "cur",
69
+ "jpg",
70
+ "jpeg",
71
+ "jfif",
72
+ "pjpeg",
73
+ "pjp",
74
+ "png",
75
+ "svg",
76
+ "tif",
77
+ "tiff",
78
+ "webp",
79
+ ];
80
+ return checkUrlSuffix(url, imgTypes);
81
+ }
82
+
83
+ /**
84
+ * 判断是否是合法的媒体类型
85
+ * @param {String} input
86
+ * @returns
87
+ */
88
+ export function isValidMediaType(input) {
89
+ const validPrefixes = ["image/", "video/", "audio/"];
90
+ return validPrefixes.some((prefix) => input?.startsWith(prefix));
91
+ }
92
+
93
+ /**
94
+ * 判断 url 是否带有指定视频后缀
95
+ * @param {string} url
96
+ * @returns
97
+ */
98
+ export function checkVideoUrl(url) {
99
+ const imgTypes = ["3gp", "mpg", "mpeg", "mp4", "m4v", "m4p", "ogv", "ogg", "mov", "webm"];
100
+ return checkUrlSuffix(url, imgTypes);
101
+ }
102
+
103
+ /**
104
+ * 判断 url 是否带有指定音频后缀
105
+ * @param {string} url
106
+ * @returns
107
+ */
108
+ export function checkAudioUrl(url) {
109
+ const imgTypes = ["3gp", "adts", "mpeg", "mp3", "mp4", "ogg", "mov", "webm", "rtp", "amr", "wav"];
110
+ return checkUrlSuffix(url, imgTypes);
111
+ }
112
+
113
+ /**
114
+ * 获取文件类型(项目内部枚举,如:图片-image、视频-video)
115
+ * @param file
116
+ * @returns
117
+ */
118
+ export function getFileTypeStr(file) {
119
+ const { type, url = file?.ossUrl } = file || {};
120
+ let fileType = "";
121
+ // 判断文件类型,获取对应展示的数据
122
+ if (url) {
123
+ // 图片
124
+ if (url.startsWith("data:image/") || checkImageUrl(url)) {
125
+ fileType = TYPE_IMG;
126
+ } else if (checkVideoUrl(url)) {
127
+ // 视频
128
+ fileType = TYPE_VIDEO;
129
+ } else if (checkAudioUrl(url)) {
130
+ // 音频
131
+ fileType = TYPE_AUDIO;
132
+ }
133
+ } else if (type) {
134
+ // 图片
135
+ if (type?.startsWith("image/")) {
136
+ fileType = TYPE_IMG;
137
+ }
138
+
139
+ // 视频
140
+ if (type?.startsWith("video/")) {
141
+ fileType = TYPE_VIDEO;
142
+ }
143
+
144
+ // 音频
145
+ if (type?.startsWith("audio/")) {
146
+ fileType = TYPE_AUDIO;
147
+ }
148
+ }
149
+ return fileType || type;
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
+ //移动端默认获取源文件类型(后缀名)
220
+ return mainType?.match(/[^\/]*\/([^\/]+)$/)?.[1] || mimeType;
221
+ }
222
+ };