-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2130 from ResearchHub/storage-service
feat: Add endpoint for uploading assets
- Loading branch information
Showing
15 changed files
with
333 additions
and
120 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .asset_upload_serializer import AssetUploadSerializer | ||
from .serializers import DynamicModelFieldSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from rest_framework import serializers | ||
|
||
from researchhub.services.storage_service import ( | ||
SUPPORTED_CONTENT_TYPES, | ||
SUPPORTED_ENTITIES, | ||
) | ||
|
||
|
||
class AssetUploadSerializer(serializers.Serializer): | ||
""" | ||
Serializer for uploading an asset into ResearchHub storage. | ||
Used to validate request data. | ||
""" | ||
|
||
content_type = serializers.ChoiceField(choices=SUPPORTED_CONTENT_TYPES) | ||
entity = serializers.ChoiceField(choices=SUPPORTED_ENTITIES) | ||
filename = serializers.CharField(required=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import uuid | ||
from typing import NamedTuple | ||
|
||
from django.conf import settings | ||
|
||
from utils import aws as aws_utils | ||
|
||
|
||
class PresignedUrl(NamedTuple): | ||
object_key: str | ||
object_url: str | ||
url: str | ||
|
||
|
||
SUPPORTED_CONTENT_TYPES = ["application/pdf", "image/png", "image/jpeg"] | ||
SUPPORTED_ENTITIES = ["comment", "note", "paper"] | ||
|
||
|
||
class StorageService: | ||
def create_presigned_url( | ||
self, | ||
entity: str, | ||
filename: str, | ||
user_id: str, | ||
content_type: str, | ||
) -> PresignedUrl: ... | ||
|
||
|
||
class S3StorageService(StorageService): | ||
""" | ||
Service for interacting with S3 storage. | ||
""" | ||
|
||
def create_presigned_url( | ||
self, | ||
entity: str, | ||
filename: str, | ||
user_id: str, | ||
content_type: str, | ||
valid_for_min: int = 2, | ||
) -> PresignedUrl: | ||
""" | ||
Create a presigned URL for uploading a file to S3 that is time-limited. | ||
""" | ||
|
||
if entity not in SUPPORTED_ENTITIES: | ||
raise ValueError(f"Unsupported entity: {entity}") | ||
|
||
if content_type not in SUPPORTED_CONTENT_TYPES: | ||
raise ValueError(f"Unsupported content type: {content_type}") | ||
|
||
s3_filename = f"uploads/{entity}s/users/{user_id}/{uuid.uuid4()}/{filename}" | ||
|
||
s3_client = aws_utils.create_client("s3") | ||
|
||
url = s3_client.generate_presigned_url( | ||
"put_object", | ||
Params={ | ||
"Bucket": settings.AWS_STORAGE_BUCKET_NAME, | ||
"Key": s3_filename, | ||
"ContentType": content_type, | ||
"Metadata": { | ||
"created-by-id": f"{user_id}", | ||
"file-name": filename, | ||
}, | ||
}, | ||
ExpiresIn=60 * valid_for_min, | ||
) | ||
|
||
return PresignedUrl( | ||
url=url, | ||
object_key=s3_filename, | ||
object_url=f"https://{settings.AWS_S3_CUSTOM_DOMAIN}/{s3_filename}", | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
from unittest.mock import Mock | ||
|
||
from rest_framework.test import APIRequestFactory, APITestCase, force_authenticate | ||
|
||
from researchhub.views.asset_upload_view import AssetUploadView | ||
from user.related_models.user_model import User | ||
|
||
|
||
class AssetUploadViewTest(APITestCase): | ||
|
||
def setUp(self): | ||
self.factory = APIRequestFactory() | ||
self.view = AssetUploadView.as_view() | ||
self.mock_storage_service = Mock() | ||
self.user = User.objects.create(username="user1") | ||
|
||
def test_post(self): | ||
# Arrange | ||
request = self.factory.post( | ||
"/asset/upload/", | ||
{ | ||
"content_type": "application/pdf", | ||
"entity": "paper", | ||
"filename": "test.pdf", | ||
}, | ||
) | ||
|
||
force_authenticate(request, self.user) | ||
|
||
# Act | ||
response = self.view(request, storage_service=self.mock_storage_service) | ||
|
||
# Assert | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual( | ||
response.data, | ||
{ | ||
"presigned_url": self.mock_storage_service.create_presigned_url.return_value.url, | ||
"object_key": self.mock_storage_service.create_presigned_url.return_value.object_key, | ||
"object_url": self.mock_storage_service.create_presigned_url.return_value.object_url, | ||
}, | ||
) | ||
self.mock_storage_service.create_presigned_url.assert_called_once_with( | ||
"paper", | ||
"test.pdf", | ||
request.user.id, | ||
"application/pdf", | ||
) | ||
|
||
def test_post_fails_unauthenticated(self): | ||
# Arrange | ||
request = self.factory.post("/assets/upload/") | ||
|
||
# Act | ||
response = self.view(request, storage_service=self.mock_storage_service) | ||
|
||
# Assert | ||
self.assertEqual(response.status_code, 401) | ||
self.mock_storage_service.create_presigned_url.assert_not_called() | ||
|
||
def test_post_fails_with_validation_error(self): | ||
# Arrange | ||
request = self.factory.post( | ||
"/asset/upload/", | ||
{ | ||
# content type is missing! | ||
"entity": "paper", | ||
"filename": "test.pdf", | ||
}, | ||
) | ||
|
||
force_authenticate(request, self.user) | ||
|
||
# Act | ||
response = self.view(request, storage_service=self.mock_storage_service) | ||
|
||
# Assert | ||
self.assertEqual(response.status_code, 400) | ||
self.assertEqual(response.data, {"content_type": ["This field is required."]}) | ||
self.mock_storage_service.create_presigned_url.assert_not_called() | ||
|
||
def test_post_with_unsupported_entity(self): | ||
# Arrange | ||
request = self.factory.post( | ||
"/asset/upload/", | ||
{ | ||
"content_type": "application/pdf", | ||
"entity": "UNSUPPORTED", | ||
"filename": "test.pdf", | ||
}, | ||
) | ||
|
||
force_authenticate(request, self.user) | ||
|
||
# Act | ||
response = self.view(request, storage_service=self.mock_storage_service) | ||
|
||
# Assert | ||
self.assertEqual(response.status_code, 400) | ||
self.assertEqual( | ||
response.data, | ||
{"entity": ['"UNSUPPORTED" is not a valid choice.']}, | ||
) | ||
self.mock_storage_service.create_presigned_url.assert_not_called() |
Oops, something went wrong.