Skip to content

Commit

Permalink
Create separate endpoint for getting file upload fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
Psykar committed Mar 6, 2019
1 parent 28a3455 commit 122676e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
9 changes: 6 additions & 3 deletions missioncontrol/home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,14 @@ def get_download_url(self):

return url

def get_upload_url(self):
@classmethod
def get_post_data_fields(cls, **kwargs):
# Create the object but don't save it
obj = cls(**kwargs)
s3 = boto3.client('s3')
post = s3.generate_presigned_post(
Bucket=self.bucket,
Key=self.key,
Bucket=obj.bucket,
Key=obj.key,
)

return post
Expand Down
28 changes: 27 additions & 1 deletion missioncontrol/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,32 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
/files/get_post_data_fields/:
get:
tags: ['files']
description: Get the fields required for uploading a new file
operationId: v0.files.get_post_data_fields
parameters:
- in: query
name: cid
required: true
description:
The content ID of a file (blake2b hash)
schema:
type: string
responses:
200:
description: The fields required for a file upload
content:
application/json:
schema:
$ref: '#/components/schemas/PostDataFields'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/files/{cid}/:
get:
tags: ['files']
Expand Down Expand Up @@ -1546,7 +1572,7 @@ components:
type: integer
readOnly: true

PostUrlFields:
PostDataFields:
properties:
url:
description: The url to post to
Expand Down
23 changes: 23 additions & 0 deletions missioncontrol/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,26 @@ def test_version_increment(test_client, simple_file, some_hash, another_hash):
assert result1.pop('cid') == some_hash
assert result2.pop('cid') == another_hash
assert result1 == result2

# Still needs a database for the user setup.
@patch('home.models.boto3')
@pytest.mark.django_db
def test_get_post_data_fields(boto3_mock, test_client, some_hash):
post_values = {
'url': 'https://test.example',
'url_fields': {},
}
boto3_mock.client.return_value.generate_presigned_post.return_value = post_values

response = test_client.get(
f'/api/v0/files/get_post_data_fields/',
query_string={'cid': some_hash}
)
assert response.status_code == 200, response.get_data()

assert response.json == post_values

boto3_mock.client.return_value.generate_presigned_post.assert_called_with(
Bucket=settings.FILE_STORAGE_PATH.split('/')[2],
Key=f'django-file-storage/{some_hash}',
)
11 changes: 10 additions & 1 deletion missioncontrol/v0/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ def put(cid, file_body):
)
obj, created = S3File.objects.update_or_create(cid=cid, defaults=file_body)
retval = obj.to_dict()
return retval, 201 if created else 200
return retval, 201 if created else 200

def get_post_data_fields(cid):
if S3File.objects.filter(cid=cid).exists():
raise ProblemException(
status=409,
title='Conflict',
detail='This cid already exists in metadata',
)
return S3File.get_post_data_fields(cid=cid)

0 comments on commit 122676e

Please sign in to comment.