(part_id: str, request: Request, current_user: User = Depends(get_current_active_user))
| 118 | |
| 119 | @router.post("/user/1.0/upload-part/{part_id}", tags=[""]) |
| 120 | async def upload_part(part_id: str, request: Request, current_user: User = Depends(get_current_active_user)): |
| 121 | |
| 122 | # file_name = doc_db.safe_path(part_id) |
| 123 | file_name = part_id |
| 124 | |
| 125 | # see if the user really has the part-node in the database, before receiving upload of this part |
| 126 | # and get the document_id of the part |
| 127 | document = doc_db.user_has_part(part_id, current_user) |
| 128 | |
| 129 | request_body = await request.body() |
| 130 | |
| 131 | if document: |
| 132 | |
| 133 | # try to receive the uploaded part |
| 134 | try: |
| 135 | print("File contents: ", request_body) |
| 136 | |
| 137 | # use document_id instead as dir_name |
| 138 | # dir_name = doc_db.safe_path(document.document_id) |
| 139 | dir_name = document.document_id |
| 140 | |
| 141 | path = "./data/document_parts/" + dir_name + "/" |
| 142 | |
| 143 | if not os.path.exists(path): |
| 144 | os.makedirs(path) |
| 145 | |
| 146 | with open(path + file_name, "wb") as f: |
| 147 | f.write(request_body) |
| 148 | |
| 149 | except Exception: |
| 150 | print("Error uploading file") |
| 151 | print(traceback.format_exc()) |
| 152 | print("Error uploading file") |
| 153 | print(sys.exc_info()[2]) |
| 154 | |
| 155 | finally: |
| 156 | # We will write to the database, information about part successfully uploaded. |
| 157 | doc_db.mark_part_as_uploaded(part_id, current_user) |
| 158 | |
| 159 | doc_db.debug(endpoint="upload-part", request={"part_id": part_id}, response={"uploaded": True}) |
| 160 | |
| 161 | return {"message": f"Successfully uploaded part {file_name}"} |
| 162 | |
| 163 | |
| 164 | ################################################################ |
nothing calls this directly
no test coverage detected