diff --git a/src/backend/core/tests/items/test_api_items_media_auth.py b/src/backend/core/tests/items/test_api_items_media_auth.py index 7d92406b..7d363dcb 100644 --- a/src/backend/core/tests/items/test_api_items_media_auth.py +++ b/src/backend/core/tests/items/test_api_items_media_auth.py @@ -245,12 +245,7 @@ def test_api_items_media_auth_related_filename_with_spaces(): filename = "image with & spaces.txt" key = f"item/{item.pk!s}/{filename:s}" - default_storage.connection.meta.client.put_object( - Bucket=default_storage.bucket_name, - Key=key, - Body=BytesIO(b"my prose"), - ContentType="text/plain", - ) + default_storage.save(key, BytesIO(b"my prose")) original_url = quote(f"http://localhost/media/{key:s}") now = timezone.now() diff --git a/src/backend/demo/management/commands/create_demo.py b/src/backend/demo/management/commands/create_demo.py index eb6fdefc..20b2fa64 100644 --- a/src/backend/demo/management/commands/create_demo.py +++ b/src/backend/demo/management/commands/create_demo.py @@ -116,12 +116,7 @@ def create_item(user): mimetype="text/plain", ) - default_storage.connection.meta.client.put_object( - Bucket=default_storage.bucket_name, - Key=item.file_key, - Body=BytesIO(fake.sentence(nb_words=50).encode()), - ContentType="text/plain", - ) + default_storage.save(item.file_key, BytesIO(fake.sentence(nb_words=50).encode())) return item diff --git a/src/backend/wopi/tests/viewset/test_check_file_info.py b/src/backend/wopi/tests/viewset/test_check_file_info.py index 51a4aea9..cd50a4d4 100644 --- a/src/backend/wopi/tests/viewset/test_check_file_info.py +++ b/src/backend/wopi/tests/viewset/test_check_file_info.py @@ -32,11 +32,9 @@ def test_check_file_info_connected_user_with_access(): item=item, user=user, role=models.RoleChoices.EDITOR ) - upload_file = default_storage.connection.meta.client.put_object( - Bucket=default_storage.bucket_name, - Key=item.file_key, - Body=BytesIO(b"my prose"), - ContentType="text/plain", + default_storage.save(item.file_key, BytesIO(b"my prose")) + head_response = default_storage.connection.meta.client.head_object( + Bucket=default_storage.bucket_name, Key=item.file_key ) service = AccessUserItemService() @@ -55,7 +53,7 @@ def test_check_file_info_connected_user_with_access(): "UserFriendlyName": user.full_name, "Size": 8, "UserId": str(user.id), - "Version": upload_file["VersionId"], + "Version": head_response["VersionId"], "UserCanWrite": True, "UserCanRename": True, "UserCanPresent": False, @@ -96,11 +94,9 @@ def test_check_file_info_connected_user_reader_access(): item=item, user=user, role=models.RoleChoices.READER ) - upload_file = default_storage.connection.meta.client.put_object( - Bucket=default_storage.bucket_name, - Key=item.file_key, - Body=BytesIO(b"my prose"), - ContentType="text/plain", + default_storage.save(item.file_key, BytesIO(b"my prose")) + head_response = default_storage.connection.meta.client.head_object( + Bucket=default_storage.bucket_name, Key=item.file_key ) service = AccessUserItemService() @@ -119,7 +115,7 @@ def test_check_file_info_connected_user_reader_access(): "UserFriendlyName": user.full_name, "Size": 8, "UserId": str(user.id), - "Version": upload_file["VersionId"], + "Version": head_response["VersionId"], "UserCanWrite": False, "UserCanRename": False, "UserCanPresent": False, @@ -190,11 +186,9 @@ def test_check_file_info_anonymous_user_with_access(): ) user = AnonymousUser() - upload_file = default_storage.connection.meta.client.put_object( - Bucket=default_storage.bucket_name, - Key=item.file_key, - Body=BytesIO(b"my prose"), - ContentType="text/plain", + default_storage.save(item.file_key, BytesIO(b"my prose")) + head_response = default_storage.connection.meta.client.head_object( + Bucket=default_storage.bucket_name, Key=item.file_key ) service = AccessUserItemService() @@ -213,7 +207,7 @@ def test_check_file_info_anonymous_user_with_access(): "UserFriendlyName": None, "Size": 8, "UserId": str(user.id), - "Version": upload_file["VersionId"], + "Version": head_response["VersionId"], "UserCanWrite": False, "UserCanRename": False, "UserCanPresent": False, diff --git a/src/backend/wopi/tests/viewset/test_get_file_content.py b/src/backend/wopi/tests/viewset/test_get_file_content.py index f41660e9..47dd5ab2 100644 --- a/src/backend/wopi/tests/viewset/test_get_file_content.py +++ b/src/backend/wopi/tests/viewset/test_get_file_content.py @@ -31,11 +31,9 @@ def test_get_file_content_connected_user_with_access(): item=item, user=user, role=models.RoleChoices.EDITOR ) - upload_file = default_storage.connection.meta.client.put_object( - Bucket=default_storage.bucket_name, - Key=item.file_key, - Body=BytesIO(b"my prose"), - ContentType="text/plain", + default_storage.save(item.file_key, BytesIO(b"my prose")) + head_response = default_storage.connection.meta.client.head_object( + Bucket=default_storage.bucket_name, Key=item.file_key ) service = AccessUserItemService() @@ -48,7 +46,7 @@ def test_get_file_content_connected_user_with_access(): ) assert response.status_code == 200 assert response.streaming_content - assert response.headers["X-WOPI-ItemVersion"] == upload_file["VersionId"] + assert response.headers["X-WOPI-ItemVersion"] == head_response["VersionId"] assert response.headers["Content-Length"] == "8" @@ -116,12 +114,7 @@ def test_get_file_content_max_expected_size(): item=item, user=user, role=models.RoleChoices.EDITOR ) - default_storage.connection.meta.client.put_object( - Bucket=default_storage.bucket_name, - Key=item.file_key, - Body=BytesIO(b"my prose"), - ContentType="text/plain", - ) + default_storage.save(item.file_key, BytesIO(b"my prose")) service = AccessUserItemService() access_token, _ = service.insert_new_access(item, user) diff --git a/src/backend/wopi/tests/viewset/test_rename.py b/src/backend/wopi/tests/viewset/test_rename.py index a7889102..dfa152a4 100644 --- a/src/backend/wopi/tests/viewset/test_rename.py +++ b/src/backend/wopi/tests/viewset/test_rename.py @@ -38,12 +38,7 @@ def test_rename_file_success(): service = AccessUserItemService() access_token, _ = service.insert_new_access(item, user) - default_storage.connection.meta.client.put_object( - Bucket=default_storage.bucket_name, - Key=item.file_key, - Body=BytesIO(b"my prose"), - ContentType="text/plain", - ) + default_storage.save(item.file_key, BytesIO(b"my prose")) client = APIClient() response = client.post( f"/api/v1.0/wopi/files/{item.id}/", @@ -83,12 +78,7 @@ def test_rename_file_success_accept_json(): service = AccessUserItemService() access_token, _ = service.insert_new_access(item, user) - default_storage.connection.meta.client.put_object( - Bucket=default_storage.bucket_name, - Key=item.file_key, - Body=BytesIO(b"my prose"), - ContentType="text/plain", - ) + default_storage.save(item.file_key, BytesIO(b"my prose")) client = APIClient() response = client.post( f"/api/v1.0/wopi/files/{item.id}/", @@ -199,12 +189,7 @@ def test_rename_file_with_lock(): factories.UserItemAccessFactory( item=item, user=user, role=models.RoleChoices.EDITOR ) - default_storage.connection.meta.client.put_object( - Bucket=default_storage.bucket_name, - Key=item.file_key, - Body=BytesIO(b"my prose"), - ContentType="text/plain", - ) + default_storage.save(item.file_key, BytesIO(b"my prose")) service = AccessUserItemService() access_token, _ = service.insert_new_access(item, user) @@ -289,12 +274,7 @@ def test_rename_file_storage_error(): service = AccessUserItemService() access_token, _ = service.insert_new_access(item, user) - default_storage.connection.meta.client.put_object( - Bucket=default_storage.bucket_name, - Key=item.file_key, - Body=BytesIO(b"my prose"), - ContentType="text/plain", - ) + default_storage.save(item.file_key, BytesIO(b"my prose")) client = APIClient() with ( diff --git a/src/backend/wopi/viewsets.py b/src/backend/wopi/viewsets.py index 86e3b356..3263f31e 100644 --- a/src/backend/wopi/viewsets.py +++ b/src/backend/wopi/viewsets.py @@ -172,15 +172,15 @@ class WopiViewSet(viewsets.ViewSet): return Response(status=413) s3_client = default_storage.connection.meta.client - updated_file = s3_client.put_object( - Bucket=default_storage.bucket_name, - Key=item.file_key, - Body=file, - ) + default_storage.save(item.file_key, file) item.size = file.size item.save(update_fields=["size", "updated_at"]) + + head_response = s3_client.head_object( + Bucket=default_storage.bucket_name, Key=item.file_key + ) return Response( - status=200, headers={X_WOPI_ITEMVERSION: updated_file["VersionId"]} + status=200, headers={X_WOPI_ITEMVERSION: head_response["VersionId"]} ) def detail_post(self, request, pk=None):