(part_id: str, request: Request, current_user: User = Depends(get_current_active_user))
| 217 | |
| 218 | @router.post("/documents/1.0/upload-part/{part_id}", tags=[""]) |
| 219 | async def upload_part(part_id: str, request: Request, current_user: User = Depends(get_current_active_user)): |
| 220 | |
| 221 | # file_name = doc_db.safe_path(part_id) |
| 222 | file_name = part_id |
| 223 | |
| 224 | # see if the user really has the part-node in the database, before receiving upload of this part |
| 225 | # and get the document_id of the part |
| 226 | document = doc_db.user_has_part(part_id, current_user) |
| 227 | |
| 228 | request_body = await request.body() |
| 229 | |
| 230 | if document: |
| 231 | |
| 232 | # try to receive the uploaded part |
| 233 | try: |
| 234 | print("File contents: ", request_body) |
| 235 | |
| 236 | # use document_id instead as dir_name |
| 237 | # dir_name = doc_db.safe_path(document.document_id) |
| 238 | dir_name = document.document_id |
| 239 | |
| 240 | path = "./data/document_parts/" + dir_name + "/" |
| 241 | |
| 242 | if not os.path.exists(path): |
| 243 | os.makedirs(path) |
| 244 | |
| 245 | with open(path + file_name, "wb") as f: |
| 246 | f.write(request_body) |
| 247 | |
| 248 | except Exception: |
| 249 | print("Error uploading file") |
| 250 | print(traceback.format_exc()) |
| 251 | print("Error uploading file") |
| 252 | print(sys.exc_info()[2]) |
| 253 | |
| 254 | finally: |
| 255 | # We will write to the database, information about part successfully uploaded. |
| 256 | doc_db.mark_part_as_uploaded(part_id, current_user) |
| 257 | |
| 258 | doc_db.debug(endpoint="upload-part", request={"part_id": part_id}, response={"uploaded": True}) |
| 259 | |
| 260 | return {"message": f"Successfully uploaded part {file_name}"} |
| 261 | |
| 262 | |
| 263 | # ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- |
nothing calls this directly
no test coverage detected