(
upload_session: str, current_user: User = Depends(get_current_active_user)
)
| 279 | |
| 280 | @router.post("/documents/1.0/upload-completion", tags=[""]) |
| 281 | def upload_completion( |
| 282 | upload_session: str, current_user: User = Depends(get_current_active_user) |
| 283 | ) -> Union[DocumentVersion, bool]: |
| 284 | |
| 285 | # check if all parts really are marked as uploaded in database |
| 286 | # retrieve document_id, file_type, file_ending, and parts_id (in order) |
| 287 | |
| 288 | if not doc_db.all_parts_uploaded(upload_session, current_user): |
| 289 | raise HTTPException(status_code=400, detail="All parts not uploaded.") |
| 290 | |
| 291 | parts = doc_db.retrieve_uploaded_parts(upload_session, current_user) |
| 292 | print("Number of parts: " + str(len(parts))) |
| 293 | document = doc_db.get_document_from_session(upload_session, current_user) |
| 294 | |
| 295 | # check if all parts really are uploaded to document_id-dir |
| 296 | document_name = doc_db.safe_path(document.document_id) |
| 297 | |
| 298 | path = "./data/document_parts/" + document_name + "/" |
| 299 | |
| 300 | for part in parts: |
| 301 | part = doc_db.safe_path(part) |
| 302 | print("Checking for part " + part + " in dir " + path) |
| 303 | |
| 304 | if not os.path.isfile(path + part): |
| 305 | print(part + " is not in dir " + path) |
| 306 | raise HTTPException(status_code=400, detail="All parts not in dir.") |
| 307 | else: |
| 308 | print(part + " is in dir " + path) |
| 309 | |
| 310 | # merge parts to a new temporary document |
| 311 | temp_doc_path = path |
| 312 | temp_doc_file_name = path + document_name |
| 313 | if not os.path.exists(temp_doc_path): |
| 314 | os.makedirs(temp_doc_path) |
| 315 | |
| 316 | new_doc_path = "./data/documents/" |
| 317 | new_doc_path_name = new_doc_path + document.file_description.name |
| 318 | |
| 319 | # Read parts and write to temp doc. |
| 320 | with open(temp_doc_file_name, "ab") as temp_doc: |
| 321 | for part in parts: |
| 322 | part = doc_db.safe_path(part) |
| 323 | with open(temp_doc_path + part, "rb") as part_doc: |
| 324 | temp_doc.write(part_doc.read()) |
| 325 | |
| 326 | # move document to new location in documents dir |
| 327 | os.rename(temp_doc_file_name, new_doc_path_name) |
| 328 | |
| 329 | # remove temp parts |
| 330 | for part in parts: |
| 331 | os.remove(temp_doc_path + part) |
| 332 | |
| 333 | # remove temp path |
| 334 | os.rmdir(temp_doc_path) |
| 335 | |
| 336 | # clean database and create new document node under correct project |
| 337 | if doc_db.document_upload_finish(upload_session, current_user, document.project): |
| 338 | # get DocumentVersion |
nothing calls this directly
no test coverage detected