forked from xiangsx/gpt4free-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
1257 lines (1138 loc) · 33.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import es from 'event-stream';
import { PassThrough, pipeline, Stream } from 'stream';
import * as crypto from 'crypto';
import TurndownService from 'turndown';
import stringSimilarity from 'string-similarity';
//@ts-ignore
import UserAgent from 'user-agents';
import { get_encoding } from 'tiktoken';
import chalk from 'chalk';
import * as OpenCC from 'opencc-js';
import { Message, ModelType } from '../model/base';
import moment, { max, min } from 'moment';
import { Config } from './config';
import { v4 } from 'uuid';
import fs, { createWriteStream } from 'fs';
import sizeOf from 'image-size';
import { CreateNewAxios, getDownloadClient, getProxy } from './proxyAgent';
import { promisify } from 'util';
import FormData from 'form-data';
import pdfParse from 'pdf-parse';
import * as XLSX from 'xlsx';
import path from 'path';
import Mint from 'mint-filter';
import { sha3_512 as sha3 } from 'js-sha3';
import axios, { AxiosRequestConfig } from 'axios';
import { string } from 'joi';
const tunnel = require('tunnel');
const turndownService = new TurndownService({ codeBlockStyle: 'fenced' });
type eventFunc = (eventName: string, data: string) => void;
export const TimeFormat = 'YYYY-MM-DD HH:mm:ss';
export function toEventCB(arr: Uint8Array, emit: eventFunc) {
const pt = new PassThrough();
pt.write(arr);
pt.pipe(es.split(/\r?\n\r?\n/)) //split stream to break on newlines
.pipe(
es.map(async function (chunk: any, cb: Function) {
//turn this async function into a stream
const [eventStr, dataStr] = (chunk as any).split(/\r?\n/);
const event = eventStr.replace(/event: /, '');
const data = dataStr.replace(/data: /, '');
emit(event, data);
cb(null, { data, event });
}),
);
}
export function toEventStream(arr: Uint8Array): Stream {
const pt = new PassThrough();
pt.write(arr);
return pt;
}
export function md5(str: string): string {
return crypto.createHash('md5').update(str).digest('hex');
}
const charactersForRandom =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
export function randomStr(length: number = 6): string {
let result = '';
const charactersLength = charactersForRandom.length;
for (let i = 0; i < length; i++) {
result += charactersForRandom.charAt(
Math.floor(Math.random() * charactersLength),
);
}
return result;
}
export function randomNonce(length: number = 6): string {
let result: string = '';
for (let i = 0; i < length; i++) {
result += Math.floor(Math.random() * 9);
}
return result;
}
export function parseJSON<T>(str: string, defaultObj: T): T {
try {
return JSON.parse(str);
} catch (e: any) {
return defaultObj;
}
}
export function encryptWithAes256Cbc(data: string, key: string): string {
const hash = crypto.createHash('sha256').update(key).digest();
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-cbc', hash, iv);
let encryptedData = cipher.update(data, 'utf-8', 'hex');
encryptedData += cipher.final('hex');
return iv.toString('hex') + encryptedData;
}
export async function sleep(duration: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(() => resolve(), duration);
});
}
export function shuffleArray<T>(array: T[]): T[] {
const shuffledArray = [...array];
for (let i = shuffledArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
}
return shuffledArray;
}
export async function shuffleEachArray<T>(
array: T[],
cb: (v: T) => Promise<void>,
) {
let idx = Math.floor(Math.random() * array.length);
// 从idx开始遍历完整个数组
for (let i = idx; i < array.length; i++) {
const v = array[(i + idx) % array.length];
await cb(v);
}
}
export type ErrorData = { error: string; message?: string; status?: number };
export type MessageData = {
content: string;
function_call?: { name: string; arguments: string };
role?: string;
};
export type SearchData = { search: any };
export type DoneData = MessageData;
export enum Event {
error = 'error',
message = 'message',
search = 'search',
done = 'done',
}
export type Data<T extends Event> = T extends Event.error
? ErrorData
: T extends Event.message
? MessageData
: T extends Event.done
? DoneData
: T extends Event.search
? SearchData
: any;
export type DataCB<T extends Event> = (event: T, data: Data<T>) => void;
export class EventStream {
protected readonly pt: PassThrough = new PassThrough();
protected model: ModelType = ModelType.GPT3p5Turbo;
setModel(model: ModelType) {
this.model = model;
}
constructor() {
this.pt.setEncoding('utf-8');
}
public write<T extends Event>(event: T, data: Data<T>) {
if (this.pt.writableEnded) {
return;
}
this.pt.write(`event: ${event}\n`, 'utf-8');
this.pt.write(`data: ${JSON.stringify(data)}\n\n`, 'utf-8');
}
stream() {
return this.pt;
}
end(cb?: () => void) {
this.pt.end(cb);
}
public read(dataCB: DataCB<Event>, closeCB: () => void) {
this.pt.setEncoding('utf-8');
this.pt.pipe(es.split('\n\n')).pipe(
es.map(async (chunk: any, cb: any) => {
const res = chunk.toString();
if (!res) {
return;
}
const [eventStr, dataStr] = res.split('\n');
const event: Event = eventStr.replace('event: ', '');
if (!(event in Event)) {
dataCB(Event.error, {
error: `EventStream data read failed, not support event ${eventStr}, ${dataStr}`,
});
return;
}
const data = parseJSON(
dataStr.replace('data: ', ''),
{} as Data<Event>,
);
dataCB(event, data);
}),
);
this.pt.on('close', closeCB);
}
}
export class ThroughEventStream extends EventStream {
private onData?: <T extends Event>(event: T, data: Data<T>) => void;
private onEnd?: () => void;
constructor(
onData: <T extends Event>(event: T, data: Data<T>) => void,
onEnd: () => void,
) {
super();
this.onData = onData;
this.onEnd = onEnd;
}
destroy() {
this.onData = undefined;
this.onEnd = undefined;
}
public write<T extends Event>(event: T, data: Data<T>) {
this.onData?.(event, data);
}
public end() {
this.onEnd?.();
}
}
export class OpenaiEventStream extends EventStream {
private id: string = 'chatcmpl-' + '89C' + randomStr(26);
private start: boolean = false;
private created: number = moment().unix();
write<T extends Event>(event: T, data: Data<T>) {
if (this.pt.writableEnded) {
return;
}
if (!this.start) {
this.pt.write(
`data: ${JSON.stringify({
id: this.id,
object: 'chat.completion.chunk',
model: this.model,
created: this.created,
choices: [
{
index: 0,
delta: { role: 'assistant', content: '' },
finish_reason: null,
},
],
})}\n\n`,
'utf-8',
);
this.start = true;
}
switch (event) {
case Event.done:
this.pt.write(
`data: ${JSON.stringify({
id: this.id,
object: 'chat.completion.chunk',
model: this.model,
created: this.created,
choices: [{ index: 0, delta: {}, finish_reason: 'stop' }],
})}\n\n`,
'utf-8',
);
this.pt.write(`data: [DONE]\n\n`, 'utf-8');
break;
case Event.search:
this.pt.write(
`data: ${JSON.stringify({
id: this.id,
object: 'chat.completion.chunk',
model: this.model,
created: this.created,
choices: [
{
index: 0,
delta: { content: '', ...data },
finish_reason: null,
},
],
})}\n\n`,
'utf-8',
);
break;
default:
this.pt.write(
`data: ${JSON.stringify({
id: this.id,
object: 'chat.completion.chunk',
model: this.model,
created: this.created,
choices: [{ index: 0, delta: data, finish_reason: null }],
})}\n\n`,
'utf-8',
);
break;
}
}
read(dataCB: DataCB<Event>, closeCB: () => void) {
this.pt.setEncoding('utf-8');
this.pt.pipe(es.split(/\r?\n\r?\n/)).pipe(
es.map(async (chunk: any, cb: any) => {
const dataStr = chunk.replace('data: ', '');
if (!dataStr) {
return;
}
if (dataStr === '[DONE]') {
dataCB(Event.done, { content: '' });
return;
}
const data = parseJSON(dataStr, {} as any);
if (!data?.choices) {
dataCB(Event.error, { error: `EventStream data read failed` });
return;
}
const [
{
delta: { content = '' },
finish_reason,
},
] = data.choices;
dataCB(Event.message, { content });
}),
);
this.pt.on('close', closeCB);
}
}
export class ClaudeEventStream extends EventStream {
private log_id: string = randomStr(64).toLowerCase();
write<T extends Event>(event: T, data: Data<T>) {
if (this.pt.writableEnded) {
return;
}
switch (event) {
case Event.done:
this.pt.write(
`event: completion\ndata: ${JSON.stringify({
completion: '',
stop_reason: 'stop_sequence',
model: this.model,
stop: '\n\nHuman:',
log_id: this.log_id,
})}\n\n`,
'utf-8',
);
break;
case Event.message:
this.pt.write(
`event: completion\ndata: ${JSON.stringify({
completion: (data as MessageData).content,
stop_reason: null,
model: this.model,
stop: null,
log_id: this.log_id,
})}\n\n`,
'utf-8',
);
break;
default:
break;
}
}
read(dataCB: DataCB<Event>, closeCB: () => void) {
this.pt.setEncoding('utf-8');
this.pt.pipe(es.split(/\r?\n\r?\n/)).pipe(
es.map(async (chunk: any, cb: any) => {
if (chunk.indexOf('event: ping') > -1) {
return;
}
const dataStr = chunk.replace('event: completion\ndata: ', '');
if (!dataStr) {
return;
}
const data = parseJSON<{ completion: string; stop: string }>(
dataStr,
{} as any,
);
if (!data.completion) {
return;
}
dataCB(Event.message, { content: data.completion });
}),
);
this.pt.on('close', () => {
dataCB(Event.done, { content: '' });
closeCB();
});
}
}
export const htmlToMarkdown = (html: string): string => {
return turndownService.turndown(html);
};
export const isSimilarity = (s1: string, s2: string): boolean => {
const similarity = stringSimilarity.compareTwoStrings(s1, s2);
return similarity > 0.3;
};
export const randomUserAgent = (): string => {
return new UserAgent().toString();
};
export function extractStrNumber(input: string): number {
// 使用正则表达式匹配所有的数字
let matches = input.match(/\d+/g);
if (matches) {
// 将所有匹配的数字组合成一个新的字符串
let numberString = matches.join('');
// 将新的字符串转换为整数
return parseInt(numberString);
}
// 如果输入的字符串中没有数字,返回0
return 0;
}
export function maskLinks(input: string): string {
// 定义一个正则表达式,用于匹配http或https的链接
const linkRegex =
/(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/g;
// 使用replace方法将所有的链接的http或https部分替换为"htxxp://"或"htxxps://"的字样
const output = input.replace(linkRegex, function (match: string) {
return match.replace(/http/g, 'htxxp');
});
return output;
}
export class Lock {
private locked = false;
private resolver?: () => void; // 更明确的类型
private timeoutId?: NodeJS.Timeout;
async lock(timeout = 5 * 60 * 1000) {
const timeoutPromise = new Promise<never>((_, reject) => {
this.timeoutId = setTimeout(() => {
this.locked = false;
reject(new Error('Lock timeout'));
}, timeout);
});
while (this.locked) {
try {
await Promise.race([
new Promise<void>((resolve) => (this.resolver = resolve)),
timeoutPromise,
]);
} catch (error) {
throw error;
}
}
this.locked = true; // 现在在这里设置
if (this.timeoutId) {
clearTimeout(this.timeoutId); // 清除超时
this.timeoutId = undefined;
}
}
unlock() {
if (!this.locked) {
throw new Error('Cannot unlock a lock that is not locked');
}
this.locked = false;
if (this.resolver) {
const resolve = this.resolver;
this.resolver = undefined;
resolve();
}
}
}
export function encodeBase64(
buffer: Buffer,
padded = false,
urlSafe = true,
): string {
let base64 = buffer.toString('base64');
if (!padded) {
base64 = base64.replace(/=+$/, '');
}
if (urlSafe) {
base64 = base64.replace(/\+/g, '-').replace(/\//g, '_');
}
return base64;
}
function hashString(str: string): number {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = (hash << 5) - hash + str.charCodeAt(i);
hash |= 0; // Convert to 32bit integer
}
return Math.abs(hash);
}
export function colorLabel(label: string) {
const hash = hashString(label);
const colors = [
chalk.redBright,
chalk.greenBright,
chalk.yellowBright,
chalk.blueBright,
chalk.magentaBright,
chalk.cyanBright,
chalk.whiteBright,
chalk.red,
chalk.green,
chalk.yellow,
chalk.blue,
chalk.magenta,
chalk.cyan,
chalk.white,
];
const color = colors[hash % colors.length];
if (typeof color !== 'function') {
console.log(color);
}
return color(label);
}
export function getRandomOne<T>(arr: T[]) {
return arr[Math.floor(Math.random() * arr.length)];
}
const tokenizer = get_encoding('cl100k_base');
export function tokenEncode(input: string) {
return tokenizer.encode(input);
}
export function getTokenCount(input: string) {
return tokenEncode(input).length;
}
export class ComError extends Error {
public status: number;
public data?: any;
static Status = {
BadRequest: 400,
ParamsError: 422,
Unauthorized: 401,
Forbidden: 403,
NotFound: 404,
InternalServerError: 500,
RequestTooLarge: 413,
RequestTooMany: 429,
Overload: 503,
Timeout: 504,
};
constructor(
message?: string,
code: number = ComError.Status.InternalServerError,
data?: any,
) {
super(message); // 调用父类构造函数
Object.setPrototypeOf(this, ComError.prototype);
this.name = this.constructor.name; // 设置错误的名称为当前类名
this.status = code; // 设置错误代码
this.data = data; // 其他数据
}
}
export function removeRandomChars(str: string, percentage: number): string {
const charsToRemove = Math.floor(str.length * percentage);
return str.slice(0, str.length - charsToRemove - 2);
}
const converter = OpenCC.Converter({ from: 'tw', to: 'cn' });
export function TWToCN(str: string) {
return converter(str);
}
export function matchPattern(pattern: string, str: string): boolean {
if (pattern.indexOf('|') > 0) {
const patterns = pattern.split('|');
return patterns.some((p) => matchPattern(p, str));
}
// First, escape special characters except for '*' and '?'
const escapedPattern = pattern.replace(/[-\/\\^$+.()|[\]{}]/g, '\\$&');
// Now, replace '*' with '.*' and '?' with '.'
const regexPattern = escapedPattern.replace(/\*/g, '.*').replace(/\?/g, '.');
try {
const regex = new RegExp(`^${regexPattern}$`);
return regex.test(str);
} catch (e) {
console.error(`Invalid pattern: ${pattern}`);
return false;
}
}
export function extractHttpURLs(text: string): string[] {
// 正则表达式匹配以 "https" 开头,并在空格、"]"、或 ")" 之前结束的 URL
const urlRegex = /https?:\/\/[^\s\]\)]*(?=\s|\]|\)|\n|\t|$)/g;
return text.match(urlRegex) || [];
}
export function extractHttpFileURLs(text: string): string[] {
// 正则表达式匹配以 "https" 开头,并在空格、"]"、或 ")" 之前结束的 带有文件后缀的 URL
const urlRegex =
/https?:\/\/[^\s\]\)]*\.(aac|abw|arc|avif|avi|azw|bin|bmp|bz|bz2|cda|csh|css|csv|doc|docx|eot|epub|gz|gif|heic|heif|htm|html|nc|ico|ics|jar|jpeg|jpg|js|json|jsonld|mid|midi|mjs|mp3|mp4|mpeg|mpkg|odp|ods|odt|oga|ogv|ogx|opus|otf|png|pdf|php|ppt|pptx|rar|rtf|sh|svg|swf|tar|tif|tiff|ts|ttf|txt|vsd|wav|weba|webm|webp|woff|woff2|xhtml|xls|xlsx|xml|xul|zip|7z|mkv|mov|msg|java|py|rb|cpp|c|h|hpp|cs|go|kt|swift)(?=\s|\]|\)|\n|\t|$)/g;
return text.match(urlRegex) || [];
}
export function extractHttpImageFileURLs(text: string): string[] {
// 正则表达式匹配以 "https" 开头,并在空格、"]"、或 ")" 之前结束的 带有图片文件后缀的 URL
const urlRegex =
/https?:\/\/[^\s\]\)]*\.(bmp|gif|heic|heif|ico|jpeg|jpg|png|svg|tif|tiff|webp)(?=\s|\]|\)|\n|\t|$)/g;
return text.match(urlRegex) || [];
}
export function extractHttpVideoFileURLs(text: string): string[] {
// 正则表达式匹配以 "https" 开头,并在空格、"]"、或 ")" 之前结束的 带有视频文件后缀的 URL
const urlRegex =
/https?:\/\/[^\s\]\)]*\.(avi|mp4|mpeg|mpg|mov|mkv|webm|flv|f4v|ogv|3gp|3g2|wmv|ts|mts|m2ts|m4v|vob|divx|asf|rm|rmvb|dat|swf|nsv)(?=\s|\]|\)|\n|\t|$)/g;
return text.match(urlRegex) || [];
}
export function isImageURL(url: string): boolean {
const imageExtensions = [
'bmp',
'gif',
'heic',
'heif',
'ico',
'jpeg',
'jpg',
'png',
'svg',
'tif',
'tiff',
'webp',
];
const extension = url.split('.').pop()?.toLowerCase();
return extension ? imageExtensions.includes(extension) : false;
}
// 过滤出符合条件的行
export function grepStr(v: string, filter: string | RegExp): string[] {
const lines = v.split('\n');
const result: string[] = [];
for (const line of lines) {
if (typeof filter === 'string') {
if (line.indexOf(filter) > -1) {
result.push(line);
}
} else {
line.match(filter)?.forEach((v) => result.push(v));
}
}
return result;
}
export function replaceStrInBuffer(
source: string,
startIdx: number,
endIdx: number,
targetStr: string,
): string {
// 将源字符串转换为 Buffer
let buffer = Buffer.from(source);
// 边界处理
if (startIdx >= buffer.length) {
return source;
}
if (endIdx > buffer.length) {
endIdx = buffer.length;
}
// 计算目标字符串的长度
const targetLength = Buffer.from(targetStr).length;
// 计算要替换的部分的长度
const replaceLength = endIdx - startIdx;
// 创建一个新的 Buffer 用于存放结果
let resultBuffer = Buffer.alloc(buffer.length - replaceLength + targetLength);
// 将 startIdx 之前的部分复制到新 Buffer
buffer.copy(resultBuffer, 0, 0, startIdx);
// 将目标字符串添加到新 Buffer
resultBuffer.write(targetStr, startIdx);
// 将 endIdx 之后的部分复制到新 Buffer
buffer.copy(resultBuffer, startIdx + targetLength, endIdx);
return resultBuffer.toString();
}
export async function retryFunc<T>(
func: (idx: number) => Promise<T>,
maxRetry: number,
options?: {
label?: string;
delay?: number;
defaultV?: T;
log?: boolean;
skip?: (e: any) => boolean;
},
): Promise<T> {
const { skip, log = true, label, delay = 1000, defaultV } = options || {};
let err: any = undefined;
for (let i = 0; i < maxRetry; i++) {
try {
return await func(i);
} catch (e: any) {
err = e;
if (skip?.(e)) {
console.error(`${label || 'retryFunc'} skip error: ${e.message}`);
if (defaultV === undefined) {
throw e;
}
return defaultV;
}
if (log) {
console.error(
`${label || 'retryFunc'} failed, retry ${
i + 1
}/${maxRetry} times. err:${e.message}`,
);
}
await sleep(delay);
}
}
if (defaultV === undefined) {
throw err;
}
return defaultV;
}
export function extractJSON<T>(str: string): T | null {
let start = -1;
let bracketCount = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === '{') {
bracketCount++;
if (start === -1) start = i;
} else if (str[i] === '}') {
bracketCount--;
if (bracketCount === 0 && start !== -1) {
try {
let jsonStr = str.substring(start, i + 1);
return JSON.parse(jsonStr);
} catch (e) {
console.error('Found string is not a valid JSON');
return null;
}
}
}
}
console.error('No valid JSON found in the string');
return null;
}
export function getFilenameFromContentDisposition(content: string = '') {
const match = content.match(/filename=(.+)/);
if (match) {
return match[1];
}
return '';
}
const pipelinePromisified = promisify(pipeline);
const extMimeMapList: [string, string][] = [
['aac', 'audio/aac'],
['abw', 'application/x-abiword'],
['arc', 'application/x-freearc'],
['avif', 'image/avif'],
['avi', 'video/x-msvideo'],
['azw', 'application/vnd.amazon.ebook'],
['bin', 'application/octet-stream'],
['bmp', 'image/bmp'],
['bz', 'application/x-bzip'],
['bz2', 'application/x-bzip2'],
['cda', 'application/x-cdf'],
['csh', 'application/x-csh'],
['css', 'text/css'],
['csv', 'text/csv'],
['doc', 'application/msword'],
[
'docx',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
],
['eot', 'application/vnd.ms-fontobject'],
['epub', 'application/epub+zip'],
['gz', 'application/gzip'],
['gif', 'image/gif'],
['heic', 'image/heic'],
['heif', 'image/heif'],
['htm', 'text/html'],
['html', 'text/html'],
['ico', 'image/vnd.microsoft.icon'],
['ics', 'text/calendar'],
['jar', 'application/java-archive'],
['jpeg', 'image/jpeg'],
['jpg', 'image/jpeg'],
['js', 'text/javascript'],
['json', 'application/json'],
['jsonld', 'application/ld+json'],
['mid', 'audio/midi'],
['midi', 'audio/midi'],
['mjs', 'text/javascript'],
['mp3', 'audio/mpeg'],
['mp4', 'video/mp4'],
['mpeg', 'video/mpeg'],
['mpkg', 'application/vnd.apple.installer+xml'],
['odp', 'application/vnd.oasis.opendocument.presentation'],
['ods', 'application/vnd.oasis.opendocument.spreadsheet'],
['odt', 'application/vnd.oasis.opendocument.text'],
['oga', 'audio/ogg'],
['ogv', 'video/ogg'],
['ogx', 'application/ogg'],
['opus', 'audio/opus'],
['otf', 'font/otf'],
['png', 'image/png'],
['pdf', 'application/pdf'],
['php', 'application/x-httpd-php'],
['ppt', 'application/vnd.ms-powerpoint'],
[
'pptx',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
],
['rar', 'application/vnd.rar'],
['rtf', 'application/rtf'],
['sh', 'application/x-sh'],
['svg', 'image/svg+xml'],
['swf', 'application/x-shockwave-flash'],
['tar', 'application/x-tar'],
['tif', 'image/tiff'],
['tiff', 'image/tiff'],
['ts', 'video/mp2t'],
['ttf', 'font/ttf'],
['txt', 'text/plain'],
['vsd', 'application/vnd.visio'],
['wav', 'audio/wav'],
['weba', 'audio/webm'],
['webm', 'video/webm'],
['webp', 'image/webp'],
['woff', 'font/woff'],
['woff2', 'font/woff2'],
['xhtml', 'application/xhtml+xml'],
['xls', 'application/vnd.ms-excel'],
['xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
['xml', 'application/xml'],
['xul', 'application/vnd.mozilla.xul+xml'],
['zip', 'application/zip'],
['7z', 'application/x-7z-compressed'],
['mkv', 'video/x-matroska'],
['mov', 'video/quicktime'],
['msg', 'application/vnd.ms-outlook'],
];
export const extMimeMap = new Map(extMimeMapList);
const mimeExtMapList: [string, string][] = extMimeMapList.map(([ext, mime]) => [
mime,
ext,
]);
export const mimeExtMap = new Map(mimeExtMapList);
export const replaceLocalUrl = (url: string) => {
let fileUrl = url;
if (Config.config.global.download_map) {
for (const old in Config.config.global.download_map) {
if (fileUrl.indexOf(old) > -1) {
fileUrl = fileUrl.replace(old, Config.config.global.download_map[old]);
}
}
}
return fileUrl;
};
export async function downloadFile(fileUrl: string): Promise<{
file_name: string;
file_size: number;
width: number;
height: number;
ext: string;
mime: string;
outputFilePath: string;
image: boolean;
}> {
fileUrl = await downloadAndUploadCDN(fileUrl);
let local = false;
if (Config.config.global.download_map) {
for (const old in Config.config.global.download_map) {
if (fileUrl.indexOf(old) > -1) {
fileUrl = fileUrl.replace(old, Config.config.global.download_map[old]);
local = true;
}
if (fileUrl.startsWith('http:')) {
local = true;
}
}
}
try {
let tempFilePath = path.join(Config.config.global.download.dir, v4());
let filename!: string;
let ext: string;
let mime: string = '';
if (fileUrl.startsWith('data:image/')) {
// base64 写入文件
const base64Data = fileUrl.replace(/^data:image\/\w+;base64,/, '');
const dataBuffer = Buffer.from(base64Data, 'base64');
// base64 写入文件
mime = fileUrl.split(';')[0].split(':')[1];
ext = mime.split('/')[1];
filename = `b64_${moment().format('YYYYMMDDHH')}_${randomStr(20)}.${ext}`;
fs.writeFileSync(tempFilePath, dataBuffer);
} else {
let ok = false;
await retryFunc(
async (idx) => {
const response = await getDownloadClient(local || idx === 2).get(
fileUrl,
{
responseType: 'stream',
headers: {
'User-Agent': randomUserAgent(),
},
},
);
filename = getFilenameFromContentDisposition(
response.headers['content-disposition'],
);
filename =
filename || path.basename(response.request.path.split('?')[0]);
mime = response.headers['content-type'];
let writer = createWriteStream(tempFilePath);
await pipelinePromisified(response.data, writer);
ok = true;
},
3,
{
label: 'downloadFile',
skip: (e) => {
return (
e.message.indexOf('aborted') > -1 ||
e.message.indexOf('403') > -1 ||
e.message.indexOf('404') > -1
);
},
},
);
if (!ok) {
throw new ComError(`download failed`, ComError.Status.BadRequest);
}
}
if (mime === 'application/octet-stream') {
mime = '';
}
ext =
mimeExtMap.get(mime) ||
path.extname(filename).replace(/\./g, '').toLowerCase() ||
'txt';
mime = mime || extMimeMap.get(ext) || 'text/plain';
let file_name = `${moment().format('YYYY-MM-DD-HH')}-${randomStr(
20,
)}.${ext}`;
const file_size: number = fs.statSync(tempFilePath).size;
const outputFilePath = path.join(
Config.config.global.download.dir,
file_name,