@minto-ai/tools 1.0.666 → 1.0.668
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.
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 单文件下载
|
|
3
|
+
* @param source 下载源,支持 Base64 字符串或 URL
|
|
4
|
+
* @param fileName 下载文件名
|
|
5
|
+
* - 当 source 为 Base64 字符串时,必须提供文件名
|
|
6
|
+
* - 当 source 为 URL 且为文件路径时,若未提供文件名,则默认使用 URL 路径中的文件名
|
|
7
|
+
* - 当 source 为 URL 且不为文件路径时,传入的文件名参数无效,将使用服务器响应头中的文件名
|
|
3
8
|
*
|
|
4
|
-
* @
|
|
5
|
-
* @param fileName 下载时保存的文件名(可选)。如果未提供,将自动从 第一个参数`filePath` 提取文件名。
|
|
6
|
-
* @returns 返回一个 Promise,在文件下载完成后。
|
|
9
|
+
* @returns Promise<void> 下载完成后解析的 Promise
|
|
7
10
|
*/
|
|
8
|
-
declare function singleDownloadFile(
|
|
11
|
+
declare function singleDownloadFile(source: string, fileName?: string): Promise<void>;
|
|
9
12
|
export default singleDownloadFile;
|
package/dist/index.js
CHANGED
|
@@ -14125,33 +14125,117 @@ function isVideoFilePath(filePath) {
|
|
|
14125
14125
|
var fileSuffix = getFileSuffix(filePath);
|
|
14126
14126
|
return Object.values(VideoFileSuffixEnum).includes(fileSuffix);
|
|
14127
14127
|
}
|
|
14128
|
+
var DATA_URI_PREFIX = "data:";
|
|
14129
|
+
var BASE64_MARKER = ";base64,";
|
|
14130
|
+
var BASE64_REGEX = /^[A-Z0-9+/]+={0,2}$/i;
|
|
14131
|
+
function isValidBase64(value) {
|
|
14132
|
+
if (!isString$1(value)) {
|
|
14133
|
+
return false;
|
|
14134
|
+
}
|
|
14135
|
+
var str = value.trim();
|
|
14136
|
+
if (!str) {
|
|
14137
|
+
return false;
|
|
14138
|
+
}
|
|
14139
|
+
if (str.startsWith(DATA_URI_PREFIX)) {
|
|
14140
|
+
var markerIndex = str.indexOf(BASE64_MARKER);
|
|
14141
|
+
if (markerIndex === -1) {
|
|
14142
|
+
return false;
|
|
14143
|
+
}
|
|
14144
|
+
str = str.slice(markerIndex + BASE64_MARKER.length);
|
|
14145
|
+
}
|
|
14146
|
+
var content = str.replace(/\s+/g, "");
|
|
14147
|
+
if (content.length % 4 !== 0) {
|
|
14148
|
+
return false;
|
|
14149
|
+
}
|
|
14150
|
+
return BASE64_REGEX.test(content);
|
|
14151
|
+
}
|
|
14152
|
+
var URL_REGEX = /^https?:\/\/(?:[\w-]+:[\w-]+@)?(?:localhost|(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}|(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d))(?::\d{1,5})?(?:\/[^\s?#]*)?(?:\?[^\s#]*)?(?:#\S*)?$/i;
|
|
14153
|
+
function isValidUrl(value) {
|
|
14154
|
+
if (!isString$1(value)) {
|
|
14155
|
+
return false;
|
|
14156
|
+
}
|
|
14157
|
+
return URL_REGEX.test(value.trim());
|
|
14158
|
+
}
|
|
14128
14159
|
function singleDownloadFile(_x, _x2) {
|
|
14129
14160
|
return _singleDownloadFile.apply(this, arguments);
|
|
14130
14161
|
}
|
|
14131
14162
|
function _singleDownloadFile() {
|
|
14132
|
-
_singleDownloadFile = _asyncToGenerator(/* @__PURE__ */ _regenerator().m(function _callee(
|
|
14133
|
-
var
|
|
14163
|
+
_singleDownloadFile = _asyncToGenerator(/* @__PURE__ */ _regenerator().m(function _callee(source, fileName) {
|
|
14164
|
+
var base64Url, response, blob, downloadName, requestUrl, _response, link2, _t, _t2;
|
|
14134
14165
|
return _regenerator().w(function(_context) {
|
|
14135
|
-
while (1) switch (_context.n) {
|
|
14166
|
+
while (1) switch (_context.p = _context.n) {
|
|
14136
14167
|
case 0:
|
|
14137
|
-
if (!
|
|
14138
|
-
|
|
14168
|
+
if (!isValidBase64(source)) {
|
|
14169
|
+
_context.n = 6;
|
|
14170
|
+
break;
|
|
14171
|
+
}
|
|
14172
|
+
if (fileName) {
|
|
14173
|
+
_context.n = 1;
|
|
14174
|
+
break;
|
|
14175
|
+
}
|
|
14176
|
+
throw new Error("下载失败:Base64 数据必须提供文件名");
|
|
14177
|
+
case 1:
|
|
14178
|
+
base64Url = source;
|
|
14179
|
+
if (!source.startsWith("data:")) {
|
|
14180
|
+
base64Url = "data:application/octet-stream;base64,".concat(source);
|
|
14139
14181
|
}
|
|
14140
|
-
|
|
14182
|
+
_context.p = 2;
|
|
14183
|
+
_context.n = 3;
|
|
14184
|
+
return fetch(base64Url);
|
|
14185
|
+
case 3:
|
|
14186
|
+
response = _context.v;
|
|
14187
|
+
_context.n = 4;
|
|
14188
|
+
return response.blob();
|
|
14189
|
+
case 4:
|
|
14190
|
+
blob = _context.v;
|
|
14191
|
+
FileSaver.saveAs(blob, fileName);
|
|
14192
|
+
return _context.a(2);
|
|
14193
|
+
case 5:
|
|
14194
|
+
_context.p = 5;
|
|
14195
|
+
_t = _context.v;
|
|
14196
|
+
console.error("Base64 下载失败:", _t);
|
|
14197
|
+
throw _t;
|
|
14198
|
+
case 6:
|
|
14199
|
+
if (!isValidUrl(source)) {
|
|
14200
|
+
_context.n = 11;
|
|
14201
|
+
break;
|
|
14202
|
+
}
|
|
14203
|
+
if (!isFilePath(source)) {
|
|
14204
|
+
_context.n = 10;
|
|
14205
|
+
break;
|
|
14206
|
+
}
|
|
14207
|
+
downloadName = fileName || getFileName(source) || "download";
|
|
14208
|
+
requestUrl = updateFilePathQuery(source, {
|
|
14141
14209
|
"response-content-type": "application/octet-stream"
|
|
14142
14210
|
});
|
|
14143
|
-
_context.
|
|
14144
|
-
|
|
14211
|
+
_context.p = 7;
|
|
14212
|
+
_context.n = 8;
|
|
14213
|
+
return axios.get(requestUrl, {
|
|
14145
14214
|
responseType: "blob"
|
|
14146
14215
|
});
|
|
14147
|
-
case
|
|
14148
|
-
|
|
14149
|
-
|
|
14150
|
-
|
|
14151
|
-
case
|
|
14216
|
+
case 8:
|
|
14217
|
+
_response = _context.v;
|
|
14218
|
+
FileSaver.saveAs(_response.data, downloadName);
|
|
14219
|
+
return _context.a(2);
|
|
14220
|
+
case 9:
|
|
14221
|
+
_context.p = 9;
|
|
14222
|
+
_t2 = _context.v;
|
|
14223
|
+
console.error("文件资源下载失败:", _t2);
|
|
14224
|
+
throw _t2;
|
|
14225
|
+
case 10:
|
|
14226
|
+
link2 = document.createElement("a");
|
|
14227
|
+
link2.href = source;
|
|
14228
|
+
link2.style.display = "none";
|
|
14229
|
+
document.body.appendChild(link2);
|
|
14230
|
+
link2.click();
|
|
14231
|
+
document.body.removeChild(link2);
|
|
14232
|
+
return _context.a(2);
|
|
14233
|
+
case 11:
|
|
14234
|
+
throw new Error("下载失败:无效的下载源 (非有效 Base64 或 URL)");
|
|
14235
|
+
case 12:
|
|
14152
14236
|
return _context.a(2);
|
|
14153
14237
|
}
|
|
14154
|
-
}, _callee);
|
|
14238
|
+
}, _callee, null, [[7, 9], [2, 5]]);
|
|
14155
14239
|
}));
|
|
14156
14240
|
return _singleDownloadFile.apply(this, arguments);
|
|
14157
14241
|
}
|
|
@@ -16648,13 +16732,6 @@ function useTheme() {
|
|
|
16648
16732
|
toggleTheme
|
|
16649
16733
|
};
|
|
16650
16734
|
}
|
|
16651
|
-
function isValidUrl(url) {
|
|
16652
|
-
if (!isString$1(url)) {
|
|
16653
|
-
return false;
|
|
16654
|
-
}
|
|
16655
|
-
var urlPattern = /^https?:\/\/(?:[\w-]+:[\w-]+@)?(?:localhost|(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}|(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d))(?::\d{1,5})?(?:\/[^\s?#]*)?(?:\?[^\s#]*)?(?:#\S*)?$/i;
|
|
16656
|
-
return urlPattern.test(url.trim());
|
|
16657
|
-
}
|
|
16658
16735
|
function createWebSocket(url) {
|
|
16659
16736
|
return new WebSocket(url);
|
|
16660
16737
|
}
|
|
@@ -16747,6 +16824,7 @@ export {
|
|
|
16747
16824
|
isPptFilePath,
|
|
16748
16825
|
isString$1 as isString,
|
|
16749
16826
|
isUndefined,
|
|
16827
|
+
isValidBase64,
|
|
16750
16828
|
isValidUrl,
|
|
16751
16829
|
isVideoFilePath,
|
|
16752
16830
|
isWebSocketSupported,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @param
|
|
4
|
-
* @returns
|
|
2
|
+
* 检查给定的字符串是否符合标准网页地址格式。
|
|
3
|
+
* @param value - 需要验证的字符串。
|
|
4
|
+
* @returns 如果字符串格式合格,返回true;否则返回false。
|
|
5
5
|
*/
|
|
6
|
-
export declare function isValidUrl(
|
|
6
|
+
export declare function isValidUrl(value: string): boolean;
|
|
7
7
|
export default isValidUrl;
|