eDMS upload doc
Showing
6 changed files
with
50 additions
and
8 deletions
... | @@ -11,7 +11,9 @@ class EDMS: | ... | @@ -11,7 +11,9 @@ class EDMS: |
11 | self.sm_client = Client(wsdl=conf.SM_WSDL) | 11 | self.sm_client = Client(wsdl=conf.SM_WSDL) |
12 | self.dm_client = Client(wsdl=conf.DM_WSDL) | 12 | self.dm_client = Client(wsdl=conf.DM_WSDL) |
13 | self.df_client = Client(wsdl=conf.DF_WSDL) | 13 | self.df_client = Client(wsdl=conf.DF_WSDL) |
14 | self.rc_client = Client(wsdl=conf.RC_WSDL) | ||
14 | self.download_url = conf.EDMS_DOWNLOAD_URL | 15 | self.download_url = conf.EDMS_DOWNLOAD_URL |
16 | self.upload_url = conf.EDMS_UPLOAD_URL | ||
15 | self.user_name = user_name | 17 | self.user_name = user_name |
16 | self.pwd = pwd | 18 | self.pwd = pwd |
17 | self.session_id = None | 19 | self.session_id = None |
... | @@ -35,6 +37,11 @@ class EDMS: | ... | @@ -35,6 +37,11 @@ class EDMS: |
35 | return self.session_id | 37 | return self.session_id |
36 | return self.set_session_id() | 38 | return self.set_session_id() |
37 | 39 | ||
40 | def get_headers(self): | ||
41 | session_id = self.get_session_id() | ||
42 | headers = {'Cookie': '{0}={1}'.format(consts.SESSION_PREFIX, session_id)} | ||
43 | return headers | ||
44 | |||
38 | def get_download_token(self, headers, metadata_version_id): | 45 | def get_download_token(self, headers, metadata_version_id): |
39 | with self.dm_client.settings(extra_http_headers=headers): | 46 | with self.dm_client.settings(extra_http_headers=headers): |
40 | res = self.dm_client.service.PrepareSingleDocumentToDownload(metadataVersionId=metadata_version_id, | 47 | res = self.dm_client.service.PrepareSingleDocumentToDownload(metadataVersionId=metadata_version_id, |
... | @@ -43,12 +50,7 @@ class EDMS: | ... | @@ -43,12 +50,7 @@ class EDMS: |
43 | actionType=consts.DOWNLOAD_ACTION_TYPE) | 50 | actionType=consts.DOWNLOAD_ACTION_TYPE) |
44 | return res.token | 51 | return res.token |
45 | 52 | ||
46 | def download(self, save_path, metadata_version_id): | 53 | def download_handler(self, params, headers, save_path): |
47 | session_id = self.get_session_id() | ||
48 | headers = {'Cookie': '{0}={1}'.format(consts.SESSION_PREFIX, session_id)} | ||
49 | token = self.get_download_token(headers, metadata_version_id) | ||
50 | params = {'token': token} | ||
51 | |||
52 | r = requests.get(self.download_url, params=params, headers=headers, stream=True) | 54 | r = requests.get(self.download_url, params=params, headers=headers, stream=True) |
53 | with open(save_path, "wb") as f: | 55 | with open(save_path, "wb") as f: |
54 | # chunk是指定每次写入的大小,每次只写了512byte | 56 | # chunk是指定每次写入的大小,每次只写了512byte |
... | @@ -57,3 +59,39 @@ class EDMS: | ... | @@ -57,3 +59,39 @@ class EDMS: |
57 | f.write(chunk) | 59 | f.write(chunk) |
58 | f.flush() | 60 | f.flush() |
59 | 61 | ||
62 | def download(self, save_path, metadata_version_id): | ||
63 | headers = self.get_headers() | ||
64 | token = self.get_download_token(headers, metadata_version_id) | ||
65 | params = {'token': token} | ||
66 | self.download_handler(params, headers, save_path) | ||
67 | |||
68 | def create_upload_token(self, headers, file_size): | ||
69 | with self.rc_client.settings(extra_http_headers=headers): | ||
70 | token = self.rc_client.service.CreateUploadToken(fileSize=file_size) | ||
71 | return token | ||
72 | |||
73 | def upload_handler(self, file_path, params, headers): | ||
74 | with open(file_path, 'rb') as f: | ||
75 | data = f.read() | ||
76 | for retry in range(4): | ||
77 | res = requests.post(self.upload_url, params=params, headers=headers, data=data) | ||
78 | if res.status_code == requests.codes.ok: | ||
79 | break | ||
80 | else: | ||
81 | raise Exception | ||
82 | |||
83 | def add_doc_info(self, headers, token, app_info): | ||
84 | with self.dm_client.settings(extra_http_headers=headers): | ||
85 | res = self.dm_client.service.AddDocumentInfo() | ||
86 | return res | ||
87 | |||
88 | def upload(self, file_path, file_size, app_info): | ||
89 | headers = self.get_headers() | ||
90 | token = self.create_upload_token(headers, file_size) | ||
91 | headers.update({'Content-Type': 'application/octet-stream'}) | ||
92 | params = {'token': token} | ||
93 | self.upload_handler(file_path, params, headers) | ||
94 | headers.pop('Content-Type') | ||
95 | self.add_doc_info(headers, token, app_info) | ||
96 | |||
97 | ... | ... |
... | @@ -11,6 +11,7 @@ LOGGING_CONFIG_FILE = os.path.join(COMMON_CONF_DIR, 'logging.conf') | ... | @@ -11,6 +11,7 @@ LOGGING_CONFIG_FILE = os.path.join(COMMON_CONF_DIR, 'logging.conf') |
11 | SM_WSDL = os.path.join(WSDL_DIR, 'SessionManager.wsdl') | 11 | SM_WSDL = os.path.join(WSDL_DIR, 'SessionManager.wsdl') |
12 | DM_WSDL = os.path.join(WSDL_DIR, 'DocumentManager.wsdl') | 12 | DM_WSDL = os.path.join(WSDL_DIR, 'DocumentManager.wsdl') |
13 | DF_WSDL = os.path.join(WSDL_DIR, 'DocumentFinder.wsdl') | 13 | DF_WSDL = os.path.join(WSDL_DIR, 'DocumentFinder.wsdl') |
14 | RC_WSDL = os.path.join(WSDL_DIR, 'RepositoryController.wsdl') | ||
14 | 15 | ||
15 | # 文件存放根目录 | 16 | # 文件存放根目录 |
16 | LOG_DIR = os.path.join(os.path.dirname(BASE_DIR), 'logs') | 17 | LOG_DIR = os.path.join(os.path.dirname(BASE_DIR), 'logs') | ... | ... |
... | @@ -3,4 +3,5 @@ DEBUG = False | ... | @@ -3,4 +3,5 @@ DEBUG = False |
3 | SLEEP_SECOND = 5 | 3 | SLEEP_SECOND = 5 |
4 | MAX_SLEEP_SECOND = 60 | 4 | MAX_SLEEP_SECOND = 60 |
5 | 5 | ||
6 | EDMS_DOWNLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/DownloadHandler.ashx | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
6 | EDMS_DOWNLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/DownloadHandler.ashx | ||
7 | EDMS_UPLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/UploadHandler.ashx | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -3,4 +3,5 @@ DEBUG = True | ... | @@ -3,4 +3,5 @@ DEBUG = True |
3 | SLEEP_SECOND = 5 | 3 | SLEEP_SECOND = 5 |
4 | MAX_SLEEP_SECOND = 60 | 4 | MAX_SLEEP_SECOND = 60 |
5 | 5 | ||
6 | EDMS_DOWNLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/DownloadHandler.ashx | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
6 | EDMS_DOWNLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/DownloadHandler.ashx | ||
7 | EDMS_UPLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/UploadHandler.ashx | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -4,3 +4,4 @@ SLEEP_SECOND = 5 | ... | @@ -4,3 +4,4 @@ SLEEP_SECOND = 5 |
4 | MAX_SLEEP_SECOND = 60 | 4 | MAX_SLEEP_SECOND = 60 |
5 | 5 | ||
6 | EDMS_DOWNLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/DownloadHandler.ashx | 6 | EDMS_DOWNLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/DownloadHandler.ashx |
7 | EDMS_UPLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/UploadHandler.ashx | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
wsdl/RepositoryController.wsdl
0 → 100644
This diff is collapsed.
Click to expand it.
-
Please register or sign in to post a comment