Merge branch 'feature/zip'
Showing
4 changed files
with
103 additions
and
8 deletions
This diff is collapsed.
Click to expand it.
... | @@ -570,12 +570,11 @@ class UploadDocView(GenericView, DocHandler): | ... | @@ -570,12 +570,11 @@ class UploadDocView(GenericView, DocHandler): |
570 | data_source = self.fix_data_source(data_source) | 570 | data_source = self.fix_data_source(data_source) |
571 | document_scheme = self.fix_scheme(document_scheme) | 571 | document_scheme = self.fix_scheme(document_scheme) |
572 | 572 | ||
573 | if document_name.endswith('.zip'): | 573 | # if document_name.endswith('.zip'): |
574 | self.running_log.info('[doc upload success] [zip file skip] [args={0}]'.format(args)) | 574 | # self.running_log.info('[doc upload success] [zip file skip] [args={0}]'.format(args)) |
575 | return response.ok() | 575 | # return response.ok() |
576 | 576 | ||
577 | if data_source == consts.DATA_SOURCE_LIST[1]: | 577 | if data_source == consts.DATA_SOURCE_LIST[1]: |
578 | if isinstance(document_name, str): | ||
579 | if document_name.endswith('-证书.pdf') or document_name.endswith('-证书'): | 578 | if document_name.endswith('-证书.pdf') or document_name.endswith('-证书'): |
580 | self.running_log.info('[doc upload success] [eapp license skip] [args={0}]'.format(args)) | 579 | self.running_log.info('[doc upload success] [eapp license skip] [args={0}]'.format(args)) |
581 | return response.ok() | 580 | return response.ok() |
... | @@ -594,17 +593,24 @@ class UploadDocView(GenericView, DocHandler): | ... | @@ -594,17 +593,24 @@ class UploadDocView(GenericView, DocHandler): |
594 | data_source=data_source, | 593 | data_source=data_source, |
595 | upload_finish_time=document.get('uploadFinishTime'), | 594 | upload_finish_time=document.get('uploadFinishTime'), |
596 | ) | 595 | ) |
596 | |||
597 | # 3. 选择队列进入 | 597 | # 3. 选择队列进入 |
598 | is_priority = PriorityApplication.objects.filter(application_id=application_id, on_off=True).exists() | 598 | is_priority = PriorityApplication.objects.filter(application_id=application_id, on_off=True).exists() |
599 | is_zip = False | ||
599 | 600 | ||
600 | classify_1 = 0 | 601 | classify_1 = 0 |
602 | # 电子合同 | ||
601 | if data_source == consts.DATA_SOURCE_LIST[-1] and document_scheme == consts.DOC_SCHEME_LIST[1]: | 603 | if data_source == consts.DATA_SOURCE_LIST[-1] and document_scheme == consts.DOC_SCHEME_LIST[1]: |
602 | for keyword, classify_1_tmp in consts.ECONTRACT_KEYWORDS_MAP.get(prefix): | 604 | for keyword, classify_1_tmp in consts.ECONTRACT_KEYWORDS_MAP.get(prefix): |
603 | if keyword in document_name: | 605 | if keyword in document_name: |
604 | classify_1 = classify_1_tmp | 606 | classify_1 = classify_1_tmp |
605 | break | 607 | break |
608 | elif document_name.endswith('.zip') or document_name.endswith('.rar') or document_name.endswith('.ZIP') \ | ||
609 | or document_name.endswith('.RAR'): | ||
610 | is_zip = True | ||
611 | |||
606 | task = consts.SPLIT_STR.join([prefix, str(doc.id), str(classify_1)]) | 612 | task = consts.SPLIT_STR.join([prefix, str(doc.id), str(classify_1)]) |
607 | enqueue_res = rh.enqueue([task], is_priority) | 613 | enqueue_res = rh.enqueue([task], is_priority, is_zip) |
608 | self.running_log.info('[doc upload success] [args={0}] [business_type={1}] [doc_id={2}] ' | 614 | self.running_log.info('[doc upload success] [args={0}] [business_type={1}] [doc_id={2}] ' |
609 | '[is_priority={3}] [enqueue_res={4}]'.format(args, prefix, doc.id, | 615 | '[is_priority={3}] [enqueue_res={4}]'.format(args, prefix, doc.id, |
610 | is_priority, enqueue_res)) | 616 | is_priority, enqueue_res)) |
... | @@ -669,7 +675,7 @@ class PriorityDocView(GenericView, DocHandler): | ... | @@ -669,7 +675,7 @@ class PriorityDocView(GenericView, DocHandler): |
669 | self.running_log.info( | 675 | self.running_log.info( |
670 | '[priority doc success] [args={0}]'.format(args)) | 676 | '[priority doc success] [args={0}]'.format(args)) |
671 | else: | 677 | else: |
672 | enqueue_res = rh.enqueue(tasks_list, is_priority=True) | 678 | enqueue_res = rh.enqueue(tasks_list, is_priority=True) # TODO 可能把压缩文件放入优先队列 |
673 | self.running_log.info('[priority doc success] [args={0}] [tasks_list={1}] [enqueue_res={2}]'.format( | 679 | self.running_log.info('[priority doc success] [args={0}] [tasks_list={1}] [enqueue_res={2}]'.format( |
674 | args, tasks_list, enqueue_res)) | 680 | args, tasks_list, enqueue_res)) |
675 | return response.ok() | 681 | return response.ok() | ... | ... |
... | @@ -35,16 +35,27 @@ class RedisHandler: | ... | @@ -35,16 +35,27 @@ class RedisHandler: |
35 | self.prefix = 'bwm_ocr' | 35 | self.prefix = 'bwm_ocr' |
36 | self.common_queue_key = '{0}:common_queue'.format(self.prefix) | 36 | self.common_queue_key = '{0}:common_queue'.format(self.prefix) |
37 | self.priority_queue_key = '{0}:priority_queue'.format(self.prefix) | 37 | self.priority_queue_key = '{0}:priority_queue'.format(self.prefix) |
38 | self.zip_queue_key = '{0}:zip_queue'.format(self.prefix) | ||
38 | self.session_id_key = '{0}:session_id'.format(self.prefix) | 39 | self.session_id_key = '{0}:session_id'.format(self.prefix) |
39 | self.cms_token_key = '{0}:cms_token'.format(self.prefix) | 40 | self.cms_token_key = '{0}:cms_token'.format(self.prefix) |
40 | self.ecm_token_key = '{0}:ecm_token'.format(self.prefix) | 41 | self.ecm_token_key = '{0}:ecm_token'.format(self.prefix) |
41 | self.login_limit_key = '{0}:login_limit'.format(self.prefix) | 42 | self.login_limit_key = '{0}:login_limit'.format(self.prefix) |
42 | 43 | ||
43 | def enqueue(self, tasks, is_priority=False): | 44 | def enqueue(self, tasks, is_priority=False, is_zip=False): |
44 | # 1 | 45 | # 1 |
45 | key = self.priority_queue_key if is_priority else self.common_queue_key | 46 | if is_zip: |
47 | key = self.zip_queue_key | ||
48 | elif is_priority: | ||
49 | key = self.priority_queue_key | ||
50 | else: | ||
51 | key = self.common_queue_key | ||
46 | return self.redis.lpush(key, tasks) | 52 | return self.redis.lpush(key, tasks) |
47 | 53 | ||
54 | def dequeue_zip(self): | ||
55 | # task or None | ||
56 | task = self.redis.rpop(self.zip_queue_key) | ||
57 | return task | ||
58 | |||
48 | def dequeue(self): | 59 | def dequeue(self): |
49 | # task or None | 60 | # task or None |
50 | task = self.redis.rpop(self.priority_queue_key) | 61 | task = self.redis.rpop(self.priority_queue_key) | ... | ... |
1 | import os | 1 | import os |
2 | import re | ||
3 | import zipfile | ||
4 | |||
5 | import rarfile | ||
2 | from zipfile import ZipFile | 6 | from zipfile import ZipFile |
3 | 7 | ||
4 | 8 | ||
... | @@ -18,3 +22,77 @@ def write_zip_file(dir_name, zipfile_path): | ... | @@ -18,3 +22,77 @@ def write_zip_file(dir_name, zipfile_path): |
18 | src_file_path = os.path.join(root, single_file) | 22 | src_file_path = os.path.join(root, single_file) |
19 | file_target_path = os.path.join(root_target_path, single_file) | 23 | file_target_path = os.path.join(root_target_path, single_file) |
20 | z.write(src_file_path, file_target_path) | 24 | z.write(src_file_path, file_target_path) |
25 | |||
26 | |||
27 | def get_pwd_list_from_str(doc_name): | ||
28 | try: | ||
29 | pwd_list = re.findall(r'\d{6}', doc_name) | ||
30 | return pwd_list | ||
31 | except Exception as e: | ||
32 | return [] | ||
33 | |||
34 | |||
35 | def extract_zip_or_rar(file_path, extract_path, pwd_list=[]): | ||
36 | if file_path.endswith('.zip') or file_path.endswith('.ZIP'): | ||
37 | if len(pwd_list) > 0: | ||
38 | for password in pwd_list: | ||
39 | try: | ||
40 | with zipfile.ZipFile(file_path) as zf: | ||
41 | zf.extractall(extract_path, pwd=bytes(password, 'utf-8')) | ||
42 | except Exception as e: | ||
43 | continue | ||
44 | else: | ||
45 | return True | ||
46 | else: | ||
47 | return False | ||
48 | else: | ||
49 | try: | ||
50 | with zipfile.ZipFile(file_path) as zf: | ||
51 | zf.extractall(extract_path) | ||
52 | except Exception as e: | ||
53 | return False | ||
54 | else: | ||
55 | return True | ||
56 | elif file_path.endswith('.rar') or file_path.endswith('.RAR'): | ||
57 | if len(pwd_list) > 0: | ||
58 | for password in pwd_list: | ||
59 | try: | ||
60 | with rarfile.RarFile(file_path) as rf: | ||
61 | rf.extractall(extract_path, pwd=password) | ||
62 | except Exception as e: | ||
63 | continue | ||
64 | else: | ||
65 | return True | ||
66 | else: | ||
67 | return False | ||
68 | else: | ||
69 | try: | ||
70 | with rarfile.RarFile(file_path) as rf: | ||
71 | rf.extractall(extract_path) | ||
72 | except Exception as e: | ||
73 | return False | ||
74 | else: | ||
75 | return True | ||
76 | else: | ||
77 | return False | ||
78 | |||
79 | |||
80 | def get_file_paths(input_path, suffix_list): | ||
81 | """ | ||
82 | |||
83 | Args: | ||
84 | input_path: str 目标目录 | ||
85 | suffix_list: list 搜索的文件的后缀列表 | ||
86 | |||
87 | Returns: list 搜索到的相关文件绝对路径列表 | ||
88 | |||
89 | """ | ||
90 | for parent, _, filenames in os.walk(input_path): | ||
91 | for filename in filenames: | ||
92 | for suffix in suffix_list: | ||
93 | if filename.endswith(suffix): | ||
94 | file_path = os.path.join(parent, filename) | ||
95 | break | ||
96 | else: | ||
97 | continue | ||
98 | yield file_path | ... | ... |
-
Please register or sign in to post a comment