def init_object() -> None: try: client = ObjectStorage() client.list_objects() except Exception as e: logger.error(e) raise e
def create_document( request: Request, org_id: UUID, dataroom_id: UUID, document_request: schemas.DocumentCreateRequest, db: Session = Depends(deps.get_db), client: ObjectStorage = Depends(deps.get_object_client), auth: Oso = Depends(deps.get_oso), current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Create a new document under a given dataroom (which in turn belongs to a team). Only a person with write access to a given dataroom is able to generate a new document. With this endpoint a new document is created within the db representing the metadata of the respective document. An object storage presigned url is generated which will be returned upon successfull creation of the db entry. With that a user can upload files directly to the object storage. """ splitted_filename = document_request.file_name.split(".") extension = splitted_filename[-1] file_name = "".join(splitted_filename[:-1]) dataroom = crud.dataroom.get(db, id=dataroom_id) obj_in = schemas.DocumentCreate( dataroom=dataroom, creator=current_user, name=document_request.name, file_name=file_name, extension=extension, description=document_request.description, md5_sum=document_request.md5_sum, size=document_request.size, mime_type=document_request.mime_type, ) try: document = crud.document.create(db, obj_in=obj_in) except Exception as e: raise HTTPException(status_code=400, detail=f"Cannot add user - {e}") url_objects = client.generate_post(document) doc_schema = schemas.DocumentInDB.from_orm(document) mark_as_uploaded_token = generate_document_is_uploaded_token( str(document.id)) url_for_mark_as_uploaded_url = request.url_for( "mark_document_as_uploaded", **{"document_id": document.id}, ) response_object = { "document": doc_schema, "upload_url": url_objects["url"], "upload_form_fields": url_objects["fields"], "mark_as_uploaded_url": url_for_mark_as_uploaded_url, "mark_as_uploaded_token": mark_as_uploaded_token, } return schemas.DocumentResponse(**response_object)
def mark_document_as_uploaded( token: str, document_id: UUID, current_user: models.User = Depends(deps.get_current_active_user), db: Session = Depends(deps.get_db), client: ObjectStorage = Depends(deps.get_object_client), ): """ A document can only be marked as uploaded by the person that created the document and if it exists in the object_storage hence we need to check whether the key exists in the object_storage """ document_id_from_token = verify_document_is_uploaded_token(token) if not document_id_from_token or document_id_from_token != str( document_id): raise HTTPException(status_code=400, detail="Invalid token") # check if key exists if not client.does_key_exist(str(document_id)): raise HTTPException(status_code=400, detail="Document does not exist") crud.document.mark_as_uploaded(db, document_id=document_id) return {"msg": "Marked successfully"}
def object_storage() -> Generator: # instantiate ObjectStorage class with # testing bucket (testing bucket needs to be # created by hand) object_storage = ObjectStorage(bucket="sedotra-test") object_storage._client.create_bucket(Bucket=object_storage.bucket) yield object_storage object_storage._client.delete_bucket(Bucket=object_storage.bucket)
def test_object_storage_url(object_storage: ObjectStorage, document: Document) -> None: """ GIVEN a just created document WHEN a new url for uploading is requested THEN a new url object should be created with url and fields keys """ url_object = object_storage.generate_post(document) assert "url" in url_object assert "fields" in url_object assert url_object["fields"]["Content-MD5"] == document.md5 assert url_object["fields"]["Content-Type"] == document.mime_type
def get_object_client() -> Generator: try: client = ObjectStorage() yield client finally: pass
def test_object_storage_key_exists_true( object_storage: ObjectStorage, key: str ) -> None: response = object_storage.does_key_exist(key) assert response is True
def test_object_storage_key_exists_false(object_storage: ObjectStorage) -> None: response = object_storage.does_key_exist("random_object.txt") assert response is False