@wzyjs/utils 0.3.8 → 0.3.10
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/dist/common/ai/index.d.ts +3 -2
- package/dist/common/ai/text/index.d.ts +1 -0
- package/dist/common/ai/video/index.d.ts +1 -0
- package/dist/common/dayjs/index.d.ts +1 -1
- package/dist/common/index.d.ts +2 -2
- package/dist/node/file/index.d.ts +63 -0
- package/dist/node/{mail.d.ts → mail/index.d.ts} +1 -1
- package/dist/node/oss/ali.d.ts +14 -0
- package/dist/node/oss/cloudflare.d.ts +20 -0
- package/dist/node/oss/index.d.ts +2 -0
- package/dist/node.cjs.js +339 -91
- package/dist/node.d.ts +4 -5
- package/dist/node.esm.js +335 -87
- package/dist/web.cjs.js +75 -11
- package/dist/web.esm.js +75 -12
- package/package.json +5 -4
- package/dist/node/file.d.ts +0 -2
- package/dist/node/oss.d.ts +0 -9
- /package/dist/common/ai/{keling → video/keling}/axios.d.ts +0 -0
- /package/dist/common/ai/{keling → video/keling}/index.d.ts +0 -0
- /package/dist/common/ai/{keling → video/keling}/utils.d.ts +0 -0
- /package/dist/node/{cron.d.ts → cron/index.d.ts} +0 -0
package/dist/node.cjs.js
CHANGED
|
@@ -5463,7 +5463,6 @@ __export(exports_node, {
|
|
|
5463
5463
|
watch: () => watch,
|
|
5464
5464
|
void: () => voidType,
|
|
5465
5465
|
util: () => util,
|
|
5466
|
-
uploadFile: () => uploadFile,
|
|
5467
5466
|
unknown: () => unknownType,
|
|
5468
5467
|
union: () => unionType,
|
|
5469
5468
|
undefined: () => undefinedType,
|
|
@@ -5475,12 +5474,8 @@ __export(exports_node, {
|
|
|
5475
5474
|
strictObject: () => strictObjectType,
|
|
5476
5475
|
shuffleArray: () => shuffleArray,
|
|
5477
5476
|
setErrorMap: () => setErrorMap,
|
|
5478
|
-
setCookie: () => setCookie,
|
|
5479
5477
|
set: () => setType,
|
|
5480
|
-
sendMail: () => sendMail,
|
|
5481
|
-
schedule: () => schedule,
|
|
5482
5478
|
retryPromise: () => retryPromise,
|
|
5483
|
-
replaceContentInFile: () => replaceContentInFile,
|
|
5484
5479
|
replaceByVariables: () => replaceByVariables,
|
|
5485
5480
|
replaceByRules: () => replaceByRules,
|
|
5486
5481
|
replaceAll: () => replaceAll,
|
|
@@ -5493,6 +5488,7 @@ __export(exports_node, {
|
|
|
5493
5488
|
pipeline: () => pipelineType,
|
|
5494
5489
|
performDecimalOperation: () => performDecimalOperation,
|
|
5495
5490
|
ostring: () => ostring,
|
|
5491
|
+
oss: () => exports_oss,
|
|
5496
5492
|
optionsToEnum: () => optionsToEnum,
|
|
5497
5493
|
optional: () => optionalType,
|
|
5498
5494
|
onumber: () => onumber,
|
|
@@ -5508,25 +5504,24 @@ __export(exports_node, {
|
|
|
5508
5504
|
nan: () => nanType,
|
|
5509
5505
|
map: () => mapType,
|
|
5510
5506
|
makeIssue: () => makeIssue,
|
|
5507
|
+
mail: () => exports_mail,
|
|
5511
5508
|
literal: () => literalType,
|
|
5512
5509
|
limitDecimals: () => limitDecimals,
|
|
5513
5510
|
levenshteinDistance: () => levenshteinDistance,
|
|
5514
5511
|
lazy: () => lazyType,
|
|
5515
5512
|
late: () => late,
|
|
5516
5513
|
jsonParse: () => jsonParse,
|
|
5517
|
-
json5: () =>
|
|
5514
|
+
json5: () => import_json53.default,
|
|
5518
5515
|
isValidNumber: () => isValidNumber,
|
|
5519
5516
|
isValid: () => isValid,
|
|
5520
5517
|
isJson: () => isJson,
|
|
5521
5518
|
isDirty: () => isDirty,
|
|
5522
5519
|
isAsync: () => isAsync,
|
|
5523
5520
|
isAborted: () => isAborted,
|
|
5524
|
-
interval: () => interval,
|
|
5525
5521
|
intersection: () => intersectionType,
|
|
5526
5522
|
instanceof: () => instanceOfType,
|
|
5527
5523
|
initChinaDayjs: () => initChinaDayjs,
|
|
5528
5524
|
imageToBase64: () => imageToBase64,
|
|
5529
|
-
getVideoStatus: () => getVideoStatus,
|
|
5530
5525
|
getType: () => getType,
|
|
5531
5526
|
getTotal: () => getTotal,
|
|
5532
5527
|
getStrLength: () => getStrLength,
|
|
@@ -5542,16 +5537,15 @@ __export(exports_node, {
|
|
|
5542
5537
|
getCookie: () => getCookie,
|
|
5543
5538
|
getChineseByStr: () => getChineseByStr,
|
|
5544
5539
|
getChinese: () => getChinese,
|
|
5545
|
-
generateVideo: () => generateVideo,
|
|
5546
5540
|
generateUniqueFileName: () => generateUniqueFileName,
|
|
5547
5541
|
generateAlphabetArray: () => generateAlphabetArray,
|
|
5548
5542
|
function: () => functionType,
|
|
5549
5543
|
findItem: () => findItem,
|
|
5550
5544
|
filterParams: () => filterParams,
|
|
5545
|
+
file: () => exports_file,
|
|
5551
5546
|
executePromise: () => executePromise,
|
|
5552
5547
|
enum: () => enumType,
|
|
5553
5548
|
effect: () => effectsType,
|
|
5554
|
-
downloadFile: () => downloadFile,
|
|
5555
5549
|
discriminatedUnion: () => discriminatedUnionType,
|
|
5556
5550
|
delay: () => delay,
|
|
5557
5551
|
defaultErrorMap: () => en_default,
|
|
@@ -5559,18 +5553,19 @@ __export(exports_node, {
|
|
|
5559
5553
|
datetimeRegex: () => datetimeRegex,
|
|
5560
5554
|
date: () => dateType,
|
|
5561
5555
|
custom: () => custom,
|
|
5556
|
+
cron: () => exports_cron,
|
|
5562
5557
|
consola: () => import_consola.default,
|
|
5563
5558
|
coerce: () => coerce,
|
|
5564
5559
|
chinaDayjs: () => chinaDayjs,
|
|
5565
5560
|
cheerio: () => cheerio,
|
|
5566
|
-
chat: () => chat,
|
|
5567
5561
|
calcJsText: () => calcJsText,
|
|
5568
5562
|
boolean: () => booleanType,
|
|
5569
5563
|
bigint: () => bigIntType,
|
|
5570
|
-
axios: () =>
|
|
5564
|
+
axios: () => import_axios8.default,
|
|
5571
5565
|
array: () => arrayType,
|
|
5572
5566
|
any: () => anyType,
|
|
5573
5567
|
amount: () => amount,
|
|
5568
|
+
ai: () => exports_ai,
|
|
5574
5569
|
addIssueToContext: () => addIssueToContext,
|
|
5575
5570
|
_: () => import_lodash.default,
|
|
5576
5571
|
ZodVoid: () => ZodVoid,
|
|
@@ -5624,9 +5619,6 @@ __export(exports_node, {
|
|
|
5624
5619
|
INVALID: () => INVALID,
|
|
5625
5620
|
Enum: () => Enum,
|
|
5626
5621
|
EMPTY_PATH: () => EMPTY_PATH,
|
|
5627
|
-
Dayjs: () => import_dayjs2.Dayjs,
|
|
5628
|
-
DataStoreOptions: () => import_nedb.DataStoreOptions,
|
|
5629
|
-
DataStore: () => import_nedb.default,
|
|
5630
5622
|
DIRTY: () => DIRTY,
|
|
5631
5623
|
BRAND: () => BRAND
|
|
5632
5624
|
});
|
|
@@ -9608,10 +9600,23 @@ var NEVER = INVALID;
|
|
|
9608
9600
|
// src/node.ts
|
|
9609
9601
|
var cheerio = __toESM(require("cheerio"));
|
|
9610
9602
|
var import_lodash = __toESM(require_lodash());
|
|
9611
|
-
var
|
|
9612
|
-
var
|
|
9603
|
+
var import_axios8 = __toESM(require("axios"));
|
|
9604
|
+
var import_json53 = __toESM(require("json5"));
|
|
9613
9605
|
var import_consola = __toESM(require("consola"));
|
|
9614
|
-
|
|
9606
|
+
|
|
9607
|
+
// src/common/ai/index.ts
|
|
9608
|
+
var exports_ai = {};
|
|
9609
|
+
__export(exports_ai, {
|
|
9610
|
+
video: () => exports_video,
|
|
9611
|
+
text: () => exports_text,
|
|
9612
|
+
ai302: () => exports_302
|
|
9613
|
+
});
|
|
9614
|
+
|
|
9615
|
+
// src/common/ai/302/index.ts
|
|
9616
|
+
var exports_302 = {};
|
|
9617
|
+
__export(exports_302, {
|
|
9618
|
+
chat: () => chat
|
|
9619
|
+
});
|
|
9615
9620
|
|
|
9616
9621
|
// src/common/ai/302/axios.ts
|
|
9617
9622
|
var import_axios = __toESM(require("axios"));
|
|
@@ -9651,12 +9656,25 @@ var chat = async (message, model = "gpt-4.1", isParse = true) => {
|
|
|
9651
9656
|
throw new Error(error?.response?.statusText || error?.message || "未知原因");
|
|
9652
9657
|
}
|
|
9653
9658
|
};
|
|
9659
|
+
// src/common/ai/video/index.ts
|
|
9660
|
+
var exports_video = {};
|
|
9661
|
+
__export(exports_video, {
|
|
9662
|
+
keling: () => exports_keling
|
|
9663
|
+
});
|
|
9654
9664
|
|
|
9655
|
-
// src/common/ai/keling/
|
|
9665
|
+
// src/common/ai/video/keling/index.ts
|
|
9666
|
+
var exports_keling = {};
|
|
9667
|
+
__export(exports_keling, {
|
|
9668
|
+
setCookie: () => setCookie,
|
|
9669
|
+
getVideoStatus: () => getVideoStatus,
|
|
9670
|
+
generateVideo: () => generateVideo
|
|
9671
|
+
});
|
|
9672
|
+
|
|
9673
|
+
// src/common/ai/video/keling/utils.ts
|
|
9656
9674
|
var url = __toESM(require("url"));
|
|
9657
9675
|
var path = __toESM(require("path"));
|
|
9658
9676
|
|
|
9659
|
-
// src/common/ai/keling/axios.ts
|
|
9677
|
+
// src/common/ai/video/keling/axios.ts
|
|
9660
9678
|
var import_axios3 = __toESM(require("axios"));
|
|
9661
9679
|
var axios2 = import_axios3.default.create({
|
|
9662
9680
|
headers: {
|
|
@@ -9665,7 +9683,7 @@ var axios2 = import_axios3.default.create({
|
|
|
9665
9683
|
}
|
|
9666
9684
|
});
|
|
9667
9685
|
|
|
9668
|
-
// src/common/ai/keling/utils.ts
|
|
9686
|
+
// src/common/ai/video/keling/utils.ts
|
|
9669
9687
|
var getToken = async (filename) => {
|
|
9670
9688
|
try {
|
|
9671
9689
|
const response = await axios2.get("https://klingai.kuaishou.com/api/upload/issue/token", {
|
|
@@ -9838,7 +9856,7 @@ var setCookie = (newCookieValue) => {
|
|
|
9838
9856
|
axios2.defaults.headers.Cookie = newCookieValue;
|
|
9839
9857
|
};
|
|
9840
9858
|
|
|
9841
|
-
// src/common/ai/keling/index.ts
|
|
9859
|
+
// src/common/ai/video/keling/index.ts
|
|
9842
9860
|
var getVideoStatus = async (taskId) => {
|
|
9843
9861
|
if (!taskId) {
|
|
9844
9862
|
return;
|
|
@@ -9865,6 +9883,48 @@ var generateVideo = async (imageUrl, prompt) => {
|
|
|
9865
9883
|
console.log(666, "任务 id", taskId);
|
|
9866
9884
|
return taskId;
|
|
9867
9885
|
};
|
|
9886
|
+
// src/common/ai/text/index.ts
|
|
9887
|
+
var exports_text = {};
|
|
9888
|
+
__export(exports_text, {
|
|
9889
|
+
complete: () => complete
|
|
9890
|
+
});
|
|
9891
|
+
var import_json52 = __toESM(require("json5"));
|
|
9892
|
+
var complete = async (apiUrl, model, token, messages, json = true) => {
|
|
9893
|
+
try {
|
|
9894
|
+
const response = await fetch(apiUrl, {
|
|
9895
|
+
method: "POST",
|
|
9896
|
+
headers: {
|
|
9897
|
+
"Content-Type": "application/json",
|
|
9898
|
+
Authorization: `Bearer ${token}`
|
|
9899
|
+
},
|
|
9900
|
+
body: JSON.stringify({
|
|
9901
|
+
model,
|
|
9902
|
+
messages,
|
|
9903
|
+
stream: false
|
|
9904
|
+
})
|
|
9905
|
+
});
|
|
9906
|
+
if (!response.ok) {
|
|
9907
|
+
const errorText = await response.text();
|
|
9908
|
+
throw new Error(`API request failed: ${response.status} ${response.statusText} - ${errorText}`);
|
|
9909
|
+
}
|
|
9910
|
+
const data = await response.json();
|
|
9911
|
+
const content = data?.choices?.[0]?.message?.content || data?.output || "";
|
|
9912
|
+
if (!content) {
|
|
9913
|
+
throw new Error("No content in response");
|
|
9914
|
+
}
|
|
9915
|
+
if (json) {
|
|
9916
|
+
const match = /```(json)?(.*)```/s.exec(content);
|
|
9917
|
+
if (match && match[2]) {
|
|
9918
|
+
return import_json52.default.parse(match[2]);
|
|
9919
|
+
}
|
|
9920
|
+
return import_json52.default.parse(content);
|
|
9921
|
+
}
|
|
9922
|
+
return content;
|
|
9923
|
+
} catch (error) {
|
|
9924
|
+
console.error("AI completion error:", error);
|
|
9925
|
+
throw error;
|
|
9926
|
+
}
|
|
9927
|
+
};
|
|
9868
9928
|
// src/common/base/string.ts
|
|
9869
9929
|
var getChineseByStr = (str) => {
|
|
9870
9930
|
if (!str) {
|
|
@@ -12617,7 +12677,6 @@ var import_isSameOrAfter = __toESM(require("dayjs/plugin/isSameOrAfter"));
|
|
|
12617
12677
|
var import_isSameOrBefore = __toESM(require("dayjs/plugin/isSameOrBefore"));
|
|
12618
12678
|
var import_utc = __toESM(require("dayjs/plugin/utc"));
|
|
12619
12679
|
var import_zh_cn = require("dayjs/locale/zh-cn");
|
|
12620
|
-
var import_dayjs2 = require("dayjs");
|
|
12621
12680
|
import_dayjs.default.extend(import_utc.default);
|
|
12622
12681
|
import_dayjs.default.extend(import_isBetween.default);
|
|
12623
12682
|
import_dayjs.default.extend(import_weekday.default);
|
|
@@ -12645,7 +12704,148 @@ var initChinaDayjs = () => {
|
|
|
12645
12704
|
};
|
|
12646
12705
|
var chinaDayjs = initChinaDayjs();
|
|
12647
12706
|
var dayjs_default = import_dayjs.default;
|
|
12648
|
-
// src/node/
|
|
12707
|
+
// src/node/oss/index.ts
|
|
12708
|
+
var exports_oss = {};
|
|
12709
|
+
__export(exports_oss, {
|
|
12710
|
+
cf: () => exports_cloudflare,
|
|
12711
|
+
ali: () => exports_ali
|
|
12712
|
+
});
|
|
12713
|
+
|
|
12714
|
+
// src/node/oss/ali.ts
|
|
12715
|
+
var exports_ali = {};
|
|
12716
|
+
__export(exports_ali, {
|
|
12717
|
+
uploadFileToOss: () => uploadFileToOss
|
|
12718
|
+
});
|
|
12719
|
+
var import_ali_oss = __toESM(require("ali-oss"));
|
|
12720
|
+
var oss = null;
|
|
12721
|
+
var getOssClient = () => {
|
|
12722
|
+
if (!oss) {
|
|
12723
|
+
oss = new import_ali_oss.default({
|
|
12724
|
+
region: process.env.ALI_OSS_REGION,
|
|
12725
|
+
accessKeyId: process.env.ALI_OSS_ACCESS_KEY_ID,
|
|
12726
|
+
accessKeySecret: process.env.ALI_OSS_ACCESS_KEY_SECRET,
|
|
12727
|
+
bucket: process.env.ALI_OSS_BUCKET
|
|
12728
|
+
});
|
|
12729
|
+
}
|
|
12730
|
+
return oss;
|
|
12731
|
+
};
|
|
12732
|
+
var uploadFileToOss = async (params) => {
|
|
12733
|
+
const { buffer, fileName } = params;
|
|
12734
|
+
if (!buffer) {
|
|
12735
|
+
throw new Error("Missing required parameters: buffer");
|
|
12736
|
+
}
|
|
12737
|
+
if (!fileName) {
|
|
12738
|
+
throw new Error("Missing required parameters: fileName");
|
|
12739
|
+
}
|
|
12740
|
+
const client = getOssClient();
|
|
12741
|
+
try {
|
|
12742
|
+
const { url: url2 } = await client.put(decodeURIComponent(fileName), buffer, {
|
|
12743
|
+
headers: {
|
|
12744
|
+
"Cache-Control": "public, max-age=31536000, immutable"
|
|
12745
|
+
}
|
|
12746
|
+
});
|
|
12747
|
+
return {
|
|
12748
|
+
url: url2,
|
|
12749
|
+
objectKey: fileName
|
|
12750
|
+
};
|
|
12751
|
+
} catch (error) {
|
|
12752
|
+
throw new Error(`上传到 OSS 失败: ${error}`);
|
|
12753
|
+
}
|
|
12754
|
+
};
|
|
12755
|
+
// src/node/oss/cloudflare.ts
|
|
12756
|
+
var exports_cloudflare = {};
|
|
12757
|
+
__export(exports_cloudflare, {
|
|
12758
|
+
uploadFile: () => uploadFile,
|
|
12759
|
+
deleteFile: () => deleteFile
|
|
12760
|
+
});
|
|
12761
|
+
var import_client_s3 = require("@aws-sdk/client-s3");
|
|
12762
|
+
var s3Client = null;
|
|
12763
|
+
var getS3Client = () => {
|
|
12764
|
+
if (!s3Client) {
|
|
12765
|
+
s3Client = new import_client_s3.S3Client({
|
|
12766
|
+
region: "auto",
|
|
12767
|
+
endpoint: `https://${process.env.CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com`,
|
|
12768
|
+
credentials: {
|
|
12769
|
+
accessKeyId: process.env.CLOUDFLARE_ACCESS_KEY_ID,
|
|
12770
|
+
secretAccessKey: process.env.CLOUDFLARE_SECRET_ACCESS_KEY
|
|
12771
|
+
}
|
|
12772
|
+
});
|
|
12773
|
+
}
|
|
12774
|
+
return s3Client;
|
|
12775
|
+
};
|
|
12776
|
+
async function uploadFile(params) {
|
|
12777
|
+
const { buffer, mimeType, fileName } = params;
|
|
12778
|
+
if (!buffer) {
|
|
12779
|
+
throw new Error("Missing required parameters: buffer");
|
|
12780
|
+
}
|
|
12781
|
+
if (!fileName) {
|
|
12782
|
+
throw new Error("Missing required parameters: fileName");
|
|
12783
|
+
}
|
|
12784
|
+
if (!mimeType) {
|
|
12785
|
+
throw new Error("Missing required parameters: mimeType");
|
|
12786
|
+
}
|
|
12787
|
+
const client = getS3Client();
|
|
12788
|
+
try {
|
|
12789
|
+
await client.send(new import_client_s3.PutObjectCommand({
|
|
12790
|
+
Bucket: process.env.CLOUDFLARE_BUCKET,
|
|
12791
|
+
Key: fileName,
|
|
12792
|
+
Body: buffer,
|
|
12793
|
+
ContentType: mimeType,
|
|
12794
|
+
ContentDisposition: "inline",
|
|
12795
|
+
CacheControl: "public, max-age=31536000, immutable"
|
|
12796
|
+
}));
|
|
12797
|
+
const url2 = `${process.env.CLOUDFLARE_PUBLIC_URL}/${fileName}`;
|
|
12798
|
+
return {
|
|
12799
|
+
url: url2,
|
|
12800
|
+
objectKey: fileName
|
|
12801
|
+
};
|
|
12802
|
+
} catch (error) {
|
|
12803
|
+
throw new Error(`上传到 Cloudflare 失败: ${error}`);
|
|
12804
|
+
}
|
|
12805
|
+
}
|
|
12806
|
+
async function deleteFile(objectKey) {
|
|
12807
|
+
const client = getS3Client();
|
|
12808
|
+
try {
|
|
12809
|
+
await client.send(new import_client_s3.DeleteObjectCommand({
|
|
12810
|
+
Bucket: process.env.CLOUDFLARE_BUCKET,
|
|
12811
|
+
Key: objectKey
|
|
12812
|
+
}));
|
|
12813
|
+
} catch (error) {
|
|
12814
|
+
throw new Error(`从 Cloudflare 删除失败: ${error}`);
|
|
12815
|
+
}
|
|
12816
|
+
}
|
|
12817
|
+
// src/node/cron/index.ts
|
|
12818
|
+
var exports_cron = {};
|
|
12819
|
+
__export(exports_cron, {
|
|
12820
|
+
schedule: () => schedule,
|
|
12821
|
+
interval: () => interval
|
|
12822
|
+
});
|
|
12823
|
+
var import_node_cron = __toESM(require("node-cron"));
|
|
12824
|
+
var interval = (intervalMinutes, immediately, fn) => {
|
|
12825
|
+
console.log(`定时任务已启动,将每${intervalMinutes}分钟执行一次`);
|
|
12826
|
+
if (immediately) {
|
|
12827
|
+
fn();
|
|
12828
|
+
}
|
|
12829
|
+
setInterval(() => {
|
|
12830
|
+
fn();
|
|
12831
|
+
}, intervalMinutes * 60 * 1000);
|
|
12832
|
+
};
|
|
12833
|
+
var schedule = (cronExpression, immediately, fn) => {
|
|
12834
|
+
console.log(`定时任务已启动,cron表达式: ${cronExpression}`);
|
|
12835
|
+
if (immediately) {
|
|
12836
|
+
fn();
|
|
12837
|
+
}
|
|
12838
|
+
import_node_cron.default.schedule(cronExpression, () => {
|
|
12839
|
+
fn();
|
|
12840
|
+
}, {
|
|
12841
|
+
timezone: "Asia/Shanghai"
|
|
12842
|
+
});
|
|
12843
|
+
};
|
|
12844
|
+
// src/node/mail/index.ts
|
|
12845
|
+
var exports_mail = {};
|
|
12846
|
+
__export(exports_mail, {
|
|
12847
|
+
send: () => send
|
|
12848
|
+
});
|
|
12649
12849
|
var import_nodemailer = __toESM(require("nodemailer"));
|
|
12650
12850
|
var authMap = {
|
|
12651
12851
|
163: {
|
|
@@ -12663,17 +12863,73 @@ var transporter = import_nodemailer.default.createTransport({
|
|
|
12663
12863
|
secure: true,
|
|
12664
12864
|
auth: authMap.gmail
|
|
12665
12865
|
});
|
|
12666
|
-
var
|
|
12866
|
+
var send = (config2) => {
|
|
12667
12867
|
return transporter.sendMail({
|
|
12668
12868
|
from: authMap.gmail.user,
|
|
12669
12869
|
...config2
|
|
12670
12870
|
});
|
|
12671
12871
|
};
|
|
12672
|
-
// src/node/file.ts
|
|
12872
|
+
// src/node/file/index.ts
|
|
12873
|
+
var exports_file = {};
|
|
12874
|
+
__export(exports_file, {
|
|
12875
|
+
replaceContentInFile: () => replaceContentInFile,
|
|
12876
|
+
processFile: () => processFile,
|
|
12877
|
+
getMimeType: () => getMimeType,
|
|
12878
|
+
downloadFile: () => downloadFile
|
|
12879
|
+
});
|
|
12673
12880
|
var import_fs_extra = __toESM(require("fs-extra"));
|
|
12674
12881
|
var import_axios7 = __toESM(require("axios"));
|
|
12675
12882
|
var import_url = __toESM(require("url"));
|
|
12676
12883
|
var path2 = __toESM(require("path"));
|
|
12884
|
+
var import_crypto = __toESM(require("crypto"));
|
|
12885
|
+
var getMimeType = (fileName) => {
|
|
12886
|
+
const ext = fileName.split(".").pop()?.toLowerCase();
|
|
12887
|
+
switch (ext) {
|
|
12888
|
+
case "png":
|
|
12889
|
+
return "image/png";
|
|
12890
|
+
case "jpg":
|
|
12891
|
+
case "jpeg":
|
|
12892
|
+
return "image/jpeg";
|
|
12893
|
+
case "gif":
|
|
12894
|
+
return "image/gif";
|
|
12895
|
+
case "webp":
|
|
12896
|
+
return "image/webp";
|
|
12897
|
+
case "svg":
|
|
12898
|
+
return "image/svg+xml";
|
|
12899
|
+
case "mp4":
|
|
12900
|
+
return "video/mp4";
|
|
12901
|
+
case "mp3":
|
|
12902
|
+
return "audio/mpeg";
|
|
12903
|
+
case "pdf":
|
|
12904
|
+
return "application/pdf";
|
|
12905
|
+
case "doc":
|
|
12906
|
+
return "application/msword";
|
|
12907
|
+
case "docx":
|
|
12908
|
+
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
12909
|
+
case "xls":
|
|
12910
|
+
return "application/vnd.ms-excel";
|
|
12911
|
+
case "xlsx":
|
|
12912
|
+
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
12913
|
+
case "ppt":
|
|
12914
|
+
return "application/vnd.ms-powerpoint";
|
|
12915
|
+
case "pptx":
|
|
12916
|
+
return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
|
|
12917
|
+
case "txt":
|
|
12918
|
+
return "text/plain";
|
|
12919
|
+
case "html":
|
|
12920
|
+
return "text/html";
|
|
12921
|
+
case "css":
|
|
12922
|
+
return "text/css";
|
|
12923
|
+
case "js":
|
|
12924
|
+
return "application/javascript";
|
|
12925
|
+
case "json":
|
|
12926
|
+
return "application/json";
|
|
12927
|
+
case "xml":
|
|
12928
|
+
return "application/xml";
|
|
12929
|
+
default:
|
|
12930
|
+
return "application/octet-stream";
|
|
12931
|
+
}
|
|
12932
|
+
};
|
|
12677
12933
|
var replaceContentInFile = async (filePath, targetContent, replacement) => {
|
|
12678
12934
|
try {
|
|
12679
12935
|
const data = await import_fs_extra.default.readFile(filePath, "utf8");
|
|
@@ -12720,82 +12976,74 @@ var downloadFile = async (httpUrl, outputPath) => {
|
|
|
12720
12976
|
throw error;
|
|
12721
12977
|
}
|
|
12722
12978
|
};
|
|
12723
|
-
|
|
12724
|
-
|
|
12725
|
-
|
|
12726
|
-
|
|
12727
|
-
|
|
12728
|
-
|
|
12729
|
-
|
|
12730
|
-
|
|
12731
|
-
|
|
12732
|
-
|
|
12733
|
-
}
|
|
12734
|
-
async function uploadFile(file, filename) {
|
|
12979
|
+
var generateUniqueFileName2 = (originalName = "") => {
|
|
12980
|
+
const ext = path2.extname(originalName);
|
|
12981
|
+
const nameWithoutExt = path2.basename(originalName, ext);
|
|
12982
|
+
const finalFileName = `${nameWithoutExt ? `${nameWithoutExt}_` : ""}${Date.now()}_${Math.random().toString(36).slice(2)}${ext}`;
|
|
12983
|
+
return {
|
|
12984
|
+
finalFileName,
|
|
12985
|
+
ext: ext.toLowerCase()
|
|
12986
|
+
};
|
|
12987
|
+
};
|
|
12988
|
+
var processFile = async (params) => {
|
|
12989
|
+
const { file, type } = params;
|
|
12735
12990
|
let buffer;
|
|
12736
|
-
let
|
|
12737
|
-
|
|
12738
|
-
|
|
12991
|
+
let mimeType;
|
|
12992
|
+
let originalName;
|
|
12993
|
+
switch (type) {
|
|
12994
|
+
case "url":
|
|
12995
|
+
if (typeof file !== "string") {
|
|
12996
|
+
throw new Error("当类型为 url 时,file 必须是字符串");
|
|
12997
|
+
}
|
|
12739
12998
|
try {
|
|
12740
|
-
const response = await
|
|
12999
|
+
const response = await import_axios7.default.get(file, { responseType: "arraybuffer" });
|
|
12741
13000
|
buffer = Buffer.from(response.data);
|
|
12742
13001
|
} catch (error) {
|
|
12743
13002
|
throw new Error(`无法从 URL 下载文件: ${error}`);
|
|
12744
13003
|
}
|
|
12745
|
-
|
|
12746
|
-
|
|
12747
|
-
|
|
12748
|
-
|
|
12749
|
-
|
|
13004
|
+
originalName = path2.basename(new URL(file).pathname);
|
|
13005
|
+
break;
|
|
13006
|
+
case "path":
|
|
13007
|
+
if (typeof file !== "string") {
|
|
13008
|
+
throw new Error("当类型为 path 时,file 必须是字符串");
|
|
13009
|
+
}
|
|
12750
13010
|
try {
|
|
12751
|
-
buffer =
|
|
13011
|
+
buffer = import_fs_extra.default.readFileSync(file);
|
|
13012
|
+
originalName = path2.basename(file);
|
|
12752
13013
|
} catch (error) {
|
|
12753
13014
|
throw new Error(`无法读取文件路径: ${error}`);
|
|
12754
13015
|
}
|
|
12755
|
-
|
|
12756
|
-
|
|
12757
|
-
|
|
12758
|
-
|
|
12759
|
-
}
|
|
12760
|
-
} else if (Buffer.isBuffer(file)) {
|
|
12761
|
-
if (!filename) {
|
|
12762
|
-
throw new Error("当文件为 Buffer 时,必须提供文件名");
|
|
12763
|
-
}
|
|
12764
|
-
buffer = file;
|
|
12765
|
-
finalFilename = filename;
|
|
12766
|
-
} else {
|
|
12767
|
-
throw new Error("不支持的文件类型");
|
|
12768
|
-
}
|
|
12769
|
-
try {
|
|
12770
|
-
const result = await oss.put(decodeURIComponent(finalFilename), buffer, {
|
|
12771
|
-
headers: {
|
|
12772
|
-
"Cache-Control": "public, max-age=31536000, immutable"
|
|
13016
|
+
break;
|
|
13017
|
+
case "file":
|
|
13018
|
+
if (!(file instanceof File)) {
|
|
13019
|
+
throw new Error("当类型为 file 时,file 必须是 File");
|
|
12773
13020
|
}
|
|
12774
|
-
|
|
12775
|
-
|
|
12776
|
-
|
|
12777
|
-
|
|
12778
|
-
|
|
12779
|
-
|
|
12780
|
-
|
|
12781
|
-
|
|
12782
|
-
|
|
12783
|
-
|
|
12784
|
-
|
|
12785
|
-
|
|
13021
|
+
buffer = Buffer.from(await file.arrayBuffer());
|
|
13022
|
+
mimeType = file.type;
|
|
13023
|
+
originalName = file.name || "";
|
|
13024
|
+
break;
|
|
13025
|
+
case "buffer":
|
|
13026
|
+
if (!Buffer.isBuffer(file)) {
|
|
13027
|
+
throw new Error("当类型为 buffer 时,file 必须是 Buffer");
|
|
13028
|
+
}
|
|
13029
|
+
buffer = file;
|
|
13030
|
+
break;
|
|
13031
|
+
default:
|
|
13032
|
+
throw new Error("不支持的文件类型");
|
|
12786
13033
|
}
|
|
12787
|
-
|
|
12788
|
-
|
|
12789
|
-
|
|
12790
|
-
|
|
12791
|
-
|
|
12792
|
-
console.log(`定时任务已启动,cron表达式: ${cronExpression}`);
|
|
12793
|
-
if (immediately) {
|
|
12794
|
-
fn();
|
|
13034
|
+
const size = buffer.length;
|
|
13035
|
+
const hash = import_crypto.default.createHash("md5").update(buffer).digest("hex");
|
|
13036
|
+
const { finalFileName, ext } = generateUniqueFileName2(originalName);
|
|
13037
|
+
if (!mimeType) {
|
|
13038
|
+
mimeType = getMimeType(finalFileName);
|
|
12795
13039
|
}
|
|
12796
|
-
|
|
12797
|
-
|
|
12798
|
-
|
|
12799
|
-
|
|
12800
|
-
|
|
13040
|
+
return {
|
|
13041
|
+
buffer,
|
|
13042
|
+
finalFileName,
|
|
13043
|
+
originalName,
|
|
13044
|
+
hash,
|
|
13045
|
+
mimeType,
|
|
13046
|
+
size,
|
|
13047
|
+
ext
|
|
13048
|
+
};
|
|
12801
13049
|
};
|
package/dist/node.d.ts
CHANGED
|
@@ -4,9 +4,8 @@ export { default as _ } from 'lodash';
|
|
|
4
4
|
export { default as axios } from 'axios';
|
|
5
5
|
export { default as json5 } from 'json5';
|
|
6
6
|
export { default as consola } from 'consola';
|
|
7
|
-
export { default as DataStore, DataStoreOptions } from 'nedb';
|
|
8
7
|
export * from './common';
|
|
9
|
-
export * from './node/
|
|
10
|
-
export * from './node/
|
|
11
|
-
export * from './node/
|
|
12
|
-
export * from './node/
|
|
8
|
+
export * as oss from './node/oss';
|
|
9
|
+
export * as cron from './node/cron';
|
|
10
|
+
export * as mail from './node/mail';
|
|
11
|
+
export * as file from './node/file';
|