File size: 27,648 Bytes
3b47d98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#coding: utf-8
#-------------------------------------------------------------------
# 宝塔Linux面板
#-------------------------------------------------------------------
# Copyright (c) 2015-2099 宝塔软件(http://bt.cn) All rights reserved.
#-------------------------------------------------------------------
# Author: cjxin <cjxin@bt.cn>
#-------------------------------------------------------------------

#
#------------------------------

import os
import re
import time
import json
import sys
import threading
import traceback
import stat
import simple_websocket
from multiprocessing import SimpleQueue, Process, connection
from typing import Tuple

from filesModel.base import filesBase
import public


class main(filesBase):

    __s_class = []

    def __init__(self):
        for i in range(1, 100):
            self.__s_class.append('f-s-%s' % i)

    def get_search_status(self, ws_get):
        """
        @name 验证是否可用
        """
        return public.returnMsg(True, '1')

    # 检查文件名
    @classmethod
    def __check_filename(cls,
                         file_name: str,
                         search_name: str,
                         is_regular: bool = False,  # 0 为普通模式  1 为正则模式  2 为替换
                         is_word: bool = False,  # 1 全词匹配  0  默认
                         is_case: bool = False,  # 1 区分大小写 0 默认
                         ) -> Tuple[bool, str]:
        search_name_tag = '<span style="color:#000;background:yellow">{}</span>'
        search_len = len(search_name)
        if is_word is True and is_regular is True:  # 全词模式-正则
            search_name = search_name.replace("\b", r"\b")
            if not search_name.startswith(r"\b"):
                search_name = r"\b" + search_name
            if not search_name.endswith(r"\b"):
                search_name = search_name + r"\b"

        search_flags = re.IGNORECASE
        if is_case is True:  # 区分大小写
            search_flags = 0

        new_file_name = ""
        if is_regular is True:  # 正则模式
            num = len(re.findall(search_name, file_name, flags=search_flags))
            new_file_name = re.sub(search_name, search_name_tag.format("\g<0>"), file_name, flags=search_flags)
        else:  # 普通模式
            num = 0

            start_idx = 0
            file_name_len = len(file_name)
            while start_idx < file_name_len:
                end_idx = start_idx + search_len
                if end_idx > file_name_len:
                    new_file_name += file_name[start_idx:]
                    break

                txt = file_name[start_idx:end_idx]
                #  区分大小写                 不区分大小写
                if is_case is True:
                    if txt.lower() != search_name.lower():
                        new_file_name += file_name[start_idx]
                        start_idx += 1
                        continue
                elif txt != search_name:
                    new_file_name += file_name[start_idx]
                    start_idx += 1
                    continue

                if is_word is True:  # 全词模式
                    if start_idx > 0:
                        if re.search(r"\w", file_name[start_idx - 1]):
                            new_file_name += file_name[start_idx]
                            start_idx += 1
                            continue
                    elif end_idx + 1 < file_name_len:
                        if re.search(r"\w", file_name[end_idx + 1]):  # 全词模式
                            new_file_name += file_name[start_idx]
                            start_idx += 1
                            continue

                new_file_name += search_name_tag.format(txt)
                start_idx += search_len
                num += 1
        if num > 0:
            return True, new_file_name
        return False, ""

    # 检查文件后缀
    @classmethod
    def __check_ext(cls, file_name: str, ext_list: list) -> bool:
        if "*" in ext_list:
            return True
        _, ext = os.path.splitext(file_name)
        if "no_ext" in ext_list:
            if len(ext) == 0:
                return True
        if len(ext) > 0:
            if str(ext[1:]).lower() in ext_list:
                return True
        return False

    # 检查文件修改时间
    @classmethod
    def __check_time(cls, path: str, start_time: int, end_time: int) -> bool:
        if not os.path.exists(path):
            return False
        file_mtime = os.path.getmtime(path)
        if file_mtime >= start_time and file_mtime <= end_time:
            return True
        return False

    # 检查文件大小
    @classmethod
    def __check_size(cls, path: str, min_size: int, max_size: int) -> bool:
        if not os.path.exists(path):
            return False
        file_size = os.path.getsize(path)
        if file_size >= min_size and file_size <= max_size:
            return True
        return False

    # 搜索文件内关键词
    @classmethod
    def __search_file_content(cls,
                              file_path: str,  # 文件路径
                              search_content: str,  # 搜索内容
                              is_regular: bool = False,  # 0 为普通模式  1 为正则模式  2 为替换
                              is_word: bool = False,  # 1 全词匹配  0  默认
                              is_case: bool = False,  # 1 区分大小写 0 默认
                              back_zip=None) -> Tuple[list, int]:
        search_content = public.html_encode(search_content)
        resutl = []
        search_num = 0
        if not os.path.exists(file_path):
            return resutl, search_num

        if os.path.getsize(file_path) > 1024 * 1024 * 20:
            return resutl, search_num

        search_content_tag = '<span style="color:#000;background:yellow">{}</span>'
        search_len = len(search_content)
        if is_word is True and is_regular is True:  # 全词模式-正则
            search_content = search_content.replace("\b", r"\b")
            if not search_content.startswith(r"\b"):
                search_content = r"\b" + search_content
            if not search_content.endswith(r"\b"):
                search_content = search_content + r"\b"

        try:
            fp = open(file_path, "r")
        except Exception as ex:
            if sys.version_info[0] != 2:
                try:
                    fp = open(file_path, "r",encoding="utf-8",errors='ignore')
                except:
                    fp = open(file_path, "r",encoding="GBK",errors='ignore')
            else:
                return resutl, search_num

        search_flags = re.IGNORECASE
        if is_case is True:  # 区分大小写
            search_flags = 0
        try:
            for row_num, line in enumerate(fp, 1):
                line = public.html_encode(line)
                new_line = ""
                if is_regular is True:  # 正则模式
                    num = len(re.findall(search_content, line, flags=search_flags))
                    new_line = re.sub(search_content, search_content_tag.format("\g<0>"), line, flags=search_flags)
                else:  # 普通模式
                    num = 0
                    new_line = ""

                    start_idx = 0
                    line_len = len(line)
                    while start_idx < line_len:
                        end_idx = start_idx + search_len
                        if end_idx > line_len:
                            new_line += line[start_idx:]
                            break

                        txt = line[start_idx:end_idx]

                        if is_case is True: #  区分大小写
                            if txt != search_content:
                                new_line += line[start_idx]
                                start_idx += 1
                                continue
                        elif txt.lower() != search_content.lower(): # 不区分大小写
                            new_line += line[start_idx]
                            start_idx += 1
                            continue

                        if is_word is True: # 全词模式
                            if start_idx > 0:
                                if re.search(r"\w", line[start_idx-1]):
                                    new_line += line[start_idx]
                                    start_idx += 1
                                    continue
                            elif end_idx + 1 < line_len:
                                if re.search(r"\w", line[end_idx+1]): # 全词模式
                                    new_line += line[start_idx]
                                    start_idx += 1
                                    continue

                        new_line += search_content_tag.format(txt)
                        start_idx += search_len
                        num += 1
                if num > 0:
                    search_num += num
                    resutl.append({"row_num": row_num, "search_num": search_num, "line": new_line})
        except Exception as err:
            pass

        if fp and not fp.closed:
            fp.close()
        return resutl, search_num

    # 搜索文件
    @classmethod
    def __search_files(cls, search_data: dict, ws_get=None):
        ws_get._ws.send(public.getJson({
            "type": "files_search",
            "ws_callback": ws_get.ws_callback,
            "result": False,
        }))
            
        for root, _, files in os.walk(search_data["path"]):
            for name in files:
                file_path = os.path.join(root, name)
                html_name = None
                if search_data["search_name"] is not None:
                    flag, html_name = cls.__check_filename(
                        file_name=name,
                        search_name=search_data["search_name"],
                        is_regular=search_data["is_regular"],
                        is_word=search_data["is_word"],
                        is_case=search_data["is_case"])
                    if flag is False:
                        continue
                if cls.__check_ext(name, ext_list=search_data["ext_list"]) is False:
                    continue
                if search_data["start_time"] is not None: # 不限时间
                    if cls.__check_time(file_path, start_time=search_data["start_time"], end_time=search_data["end_time"]) is False:
                        continue
                if search_data["max_size"] is not None:
                    if cls.__check_size(file_path, min_size=search_data["min_size"], max_size=search_data["max_size"]) is False:
                        continue

                if search_data["search_name"] is not None:
                    ws_get._ws.send(public.getJson({
                        "name": name,
                        "path": file_path,
                        "html_path": os.path.join(root, html_name) if html_name is not None else None,
                        "size": os.path.getsize(file_path),
                        "mtime": os.path.getmtime(file_path),
                    }))
                elif search_data["search_content"] is not None:
                    resutl, search_num = cls.__search_file_content(
                        file_path=file_path,
                        search_content=search_data["search_content"],
                        is_regular=search_data["is_regular"],
                        is_word=search_data["is_word"],
                        is_case=search_data["is_case"],
                    )
                    if resutl and search_num != 0:
                        ws_get._ws.send(public.getJson({
                            "name": name,
                            "path": file_path,
                            "search_num": search_num,
                            "search_result": resutl
                        }))
            if search_data["is_subdir"] != 1:  # 不包含子目录
                break

        return True

    # 搜索文件
    def get_search(self, ws_get):
        if not hasattr(ws_get, "_ws"):
            return public.returnMsg(False, "仅支持 websocket 连接!")
        ws: simple_websocket.Server = ws_get._ws
        if not hasattr(ws_get, "path") or not ws_get.path:
            return {'error': '目录不能为空'}
        if not hasattr(ws_get, "search_content") and not hasattr(ws_get, "search_name"):
            return {'error': '搜索信息不能为空'}
        if not hasattr(ws_get, "exts") or not ws_get.exts:
            return {'error': '后缀不能为空;所有文件请输入*.*'}

        if not os.path.isdir(ws_get.path):
            return {'error': '目录不存在'}

        search_data = {
            "search_content": ws_get.search_content if hasattr(ws_get, "search_content") else None, # 搜索内容
            "search_name": ws_get.search_name if hasattr(ws_get, "search_name") else None, # 搜索文件名
            "ext_list": ws_get.exts.split(',') if hasattr(ws_get, "exts") else ["*"], # 搜索后缀
            "path": ws_get.path, # 搜索路径
            "is_subdir": getattr(ws_get, "is_subdir"), # 是否包含子目录
            "is_regular": getattr(ws_get, "is_regular"), # False 为普通模式  True 为正则模式
            "is_case": getattr(ws_get, "is_case"), # True 不区分大小写 False 默认
            "is_word": getattr(ws_get, "is_word"), # True 全词匹配  False  默认
            "start_time": int(ws_get.start_time) if hasattr(ws_get, "start_time") else None, # 开始时间默认为 0
            "end_time": int(ws_get.end_time) if hasattr(ws_get, "end_time") else int(time.time()), # 结束时间
            "min_size": int(ws_get.min_size) if hasattr(ws_get, "min_size") else 0, # 最小文件大小
            "max_size": int(ws_get.max_size) if hasattr(ws_get, "max_size") else None, # 最大文件大小
        }
        public.set_module_logs("search", "get_search")
        try:
            qu = SimpleQueue()
            s = traceback_wrap(SearchByProcess(qu))
            p = Process(target=s, args=(search_data,), daemon=True)
            p.start()

            def read_from_qu():
                while True:
                    data = qu.get()
                    if data is None:
                        break
                    if isinstance(data, str):
                        ws.send(data)

            def watch_conn():
                while True:
                    if not ws.connected:
                        p.kill()
                        qu.put(None)
                        break
                    time.sleep(0.1)

            t = threading.Thread(target=read_from_qu, args=(), daemon=True)
            t2 = threading.Thread(target=watch_conn, args=(), daemon=True)
            t2.start()
            t.start()
            p.join()
            return True
        except Exception as e:
            public.print_log(traceback.format_exc())
        return True


def traceback_wrap(fun):

    def wrap(*args, **kwargs):
        try:
            res = fun(*args, **kwargs)
            public.print_log(res)
            return res
        except Exception as e:
            public.print_log(traceback.format_exc())
            return None
    return wrap

class SearchByProcess(object):

    def __init__(self, pipe: SimpleQueue):
        self.pipe = pipe

    @staticmethod
    def is_regular_file(path):
        try:
            # 获取文件状态信息
            file_stat = os.stat(path)
            if file_stat.st_size == 0:
                return False
            # 判断是否为普通文件 且可以读取
            return stat.S_ISREG(file_stat.st_mode) and os.access(path, os.R_OK)
        except FileNotFoundError:
            # 文件不存在
            return False
        except Exception as e:
            # 处理其他异常,如权限不足等
            print(f"Error checking file: {e}")
            return False

    # 检查文件名
    @classmethod
    def __check_filename(cls,
                         file_name: str,
                         search_name: str,
                         is_regular: bool = False,  # 0 为普通模式  1 为正则模式  2 为替换
                         is_word: bool = False,  # 1 全词匹配  0  默认
                         is_case: bool = False,  # 1 区分大小写 0 默认
                         ) -> Tuple[bool, str]:
        search_name_tag = '<span style="color:#000;background:yellow">{}</span>'
        search_len = len(search_name)
        if is_word is True and is_regular is True:  # 全词模式-正则
            search_name = search_name.replace("\b", r"\b")
            if not search_name.startswith(r"\b"):
                search_name = r"\b" + search_name
            if not search_name.endswith(r"\b"):
                search_name = search_name + r"\b"

        search_flags = re.IGNORECASE
        if is_case is True:  # 区分大小写
            search_flags = 0

        new_file_name = ""
        if is_regular is True:  # 正则模式
            num = len(re.findall(search_name, file_name, flags=search_flags))
            new_file_name = re.sub(search_name, search_name_tag.format("\g<0>"), file_name, flags=search_flags)
        else:  # 普通模式
            num = 0

            start_idx = 0
            file_name_len = len(file_name)
            while start_idx < file_name_len:
                end_idx = start_idx + search_len
                if end_idx > file_name_len:
                    new_file_name += file_name[start_idx:]
                    break

                txt = file_name[start_idx:end_idx]
                #  区分大小写                 不区分大小写
                if is_case is True:
                    if txt.lower() != search_name.lower():
                        new_file_name += file_name[start_idx]
                        start_idx += 1
                        continue
                elif txt != search_name:
                    new_file_name += file_name[start_idx]
                    start_idx += 1
                    continue

                if is_word is True:  # 全词模式
                    if start_idx > 0:
                        if re.search(r"\w", file_name[start_idx - 1]):
                            new_file_name += file_name[start_idx]
                            start_idx += 1
                            continue
                    elif end_idx + 1 < file_name_len:
                        if re.search(r"\w", file_name[end_idx + 1]):  # 全词模式
                            new_file_name += file_name[start_idx]
                            start_idx += 1
                            continue

                new_file_name += search_name_tag.format(txt)
                start_idx += search_len
                num += 1
        if num > 0:
            return True, new_file_name
        return False, ""

    # 检查文件后缀
    @classmethod
    def __check_ext(cls, file_name: str, ext_list: list) -> bool:
        if "*" in ext_list:
            return True
        _, ext = os.path.splitext(file_name)
        if "no_ext" in ext_list:
            if len(ext) == 0:
                return True
        if len(ext) > 0:
            if str(ext[1:]).lower() in ext_list:
                return True
        return False

    # 检查文件修改时间
    @classmethod
    def __check_time(cls, path: str, start_time: int, end_time: int) -> bool:
        if not os.path.exists(path):
            return False
        file_mtime = os.path.getmtime(path)
        if file_mtime >= start_time and file_mtime <= end_time:
            return True
        return False

    # 检查文件大小
    @classmethod
    def __check_size(cls, path: str, min_size: int, max_size: int) -> bool:
        if not os.path.exists(path):
            return False
        file_size = os.path.getsize(path)
        if file_size >= min_size and file_size <= max_size:
            return True
        return False

    # 搜索文件内关键词
    @classmethod
    def __search_file_content(cls,
                              file_path: str,  # 文件路径
                              search_content: str,  # 搜索内容
                              is_regular: bool = False,  # 0 为普通模式  1 为正则模式  2 为替换
                              is_word: bool = False,  # 1 全词匹配  0  默认
                              is_case: bool = False,  # 1 区分大小写 0 默认
                              back_zip=None) -> Tuple[list, int]:

        if not cls.is_regular_file(file_path):
            return [], 0
        search_content = public.html_encode(search_content)
        resutl = []
        search_num = 0
        if not os.path.exists(file_path):
            return resutl, search_num

        if os.path.getsize(file_path) > 1024 * 1024 * 20:
            return resutl, search_num

        search_content_tag = '<span style="color:#000;background:yellow">{}</span>'
        search_len = len(search_content)
        if is_word is True and is_regular is True:  # 全词模式-正则
            search_content = search_content.replace("\b", r"\b")
            if not search_content.startswith(r"\b"):
                search_content = r"\b" + search_content
            if not search_content.endswith(r"\b"):
                search_content = search_content + r"\b"

        stat = os.stat(file_path)
        try:
            fp = open(file_path, "r")
        except Exception as ex:
            if sys.version_info[0] != 2:
                try:
                    fp = open(file_path, "r", encoding="utf-8", errors='ignore')
                except:
                    fp = open(file_path, "r", encoding="GBK", errors='ignore')
            else:
                return resutl, search_num

        search_flags = re.IGNORECASE
        if is_case is True:  # 区分大小写
            search_flags = 0
        try:
            for row_num, line in enumerate(fp, 1):
                line = public.html_encode(line)
                new_line = ""
                if is_regular is True:  # 正则模式
                    num = len(re.findall(search_content, line, flags=search_flags))
                    new_line = re.sub(search_content, search_content_tag.format("\g<0>"), line, flags=search_flags)
                else:  # 普通模式
                    num = 0
                    new_line = ""

                    start_idx = 0
                    line_len = len(line)
                    while start_idx < line_len:
                        end_idx = start_idx + search_len
                        if end_idx > line_len:
                            new_line += line[start_idx:]
                            break

                        txt = line[start_idx:end_idx]

                        if is_case is True:  # 区分大小写
                            if txt != search_content:
                                new_line += line[start_idx]
                                start_idx += 1
                                continue
                        elif txt.lower() != search_content.lower():  # 不区分大小写
                            new_line += line[start_idx]
                            start_idx += 1
                            continue

                        if is_word is True:  # 全词模式
                            if start_idx > 0:
                                if re.search(r"\w", line[start_idx - 1]):
                                    new_line += line[start_idx]
                                    start_idx += 1
                                    continue
                            elif end_idx + 1 < line_len:
                                if re.search(r"\w", line[end_idx + 1]):  # 全词模式
                                    new_line += line[start_idx]
                                    start_idx += 1
                                    continue

                        new_line += search_content_tag.format(txt)
                        start_idx += search_len
                        num += 1
                if num > 0:
                    search_num += num
                    if search_num > 2000: # 结果数量过多,则跳过剩余部分
                        break
                    resutl.append({"row_num": row_num, "search_num": search_num, "line": new_line})
        except Exception as err:
            pass

        if fp and not fp.closed:
            fp.close()
        return resutl, search_num

    # 搜索文件
    def __call__(self, search_data: dict):
        public.print_log(search_data)
        for root, _, files in os.walk(search_data["path"]):
            for name in files:
                file_path = os.path.join(root, name)
                html_name = None
                if search_data["search_name"] is not None:
                    flag, html_name = self.__check_filename(
                        file_name=name,
                        search_name=search_data["search_name"],
                        is_regular=search_data["is_regular"],
                        is_word=search_data["is_word"],
                        is_case=search_data["is_case"])
                    if flag is False:
                        continue
                if self.__check_ext(name, ext_list=search_data["ext_list"]) is False:
                    continue
                if search_data["start_time"] is not None:  # 不限时间
                    if self.__check_time(file_path, start_time=search_data["start_time"],
                                        end_time=search_data["end_time"]) is False:
                        continue
                if search_data["max_size"] is not None:
                    if self.__check_size(file_path, min_size=search_data["min_size"],
                                        max_size=search_data["max_size"]) is False:
                        continue

                if search_data["search_name"] is not None:
                    self.pipe.put(public.getJson({
                        "name": name,
                        "path": file_path,
                        "html_path": os.path.join(root, html_name) if html_name is not None else None,
                        "size": os.path.getsize(file_path),
                        "mtime": os.path.getmtime(file_path),
                    }))
                elif search_data["search_content"] is not None:
                    result, search_num = self.__search_file_content(
                        file_path=file_path,
                        search_content=search_data["search_content"],
                        is_regular=search_data["is_regular"],
                        is_word=search_data["is_word"],
                        is_case=search_data["is_case"],
                    )
                    if result and search_num != 0:
                        # !!! 注意这里非常重要, 发送速度太快会导致面板主进程服务的主线程占用率过高, 无法处理其他请求
                        time.sleep(0.01) # 防占用大量CPU资源
                        self.pipe.put(public.getJson({
                            "name": name,
                            "path": file_path,
                            "search_num": search_num,
                            "search_result": result
                        }))
            if search_data["is_subdir"] != 1:  # 不包含子目录
                break

        self.pipe.put(None)
        return True