mirror of
https://github.com/suitenumerique/drive.git
synced 2026-08-17 20:15:40 +02:00
♻️(backend) remove usage of boto3 put_object method
We don't want to use the s3_client anymore but to rely on the django storages API. This first step will allow us to create a dedicated backend for mirroring file saving.
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user