Skip to content

Commit 1f0c4c5

Browse files
committed
Create separate endpoint for getting file upload fields.
1 parent 8878383 commit 1f0c4c5

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

missioncontrol/home/models.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,14 @@ def get_download_url(self):
525525

526526
return url
527527

528-
def get_upload_url(self):
528+
@classmethod
529+
def get_post_data_fields(cls, **kwargs):
530+
# Create the object but don't save it
531+
obj = cls(**kwargs)
529532
s3 = boto3.client('s3')
530533
post = s3.generate_presigned_post(
531-
Bucket=self.bucket,
532-
Key=self.key,
534+
Bucket=obj.bucket,
535+
Key=obj.key,
533536
)
534537

535538
return post

missioncontrol/openapi/openapi.yaml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,32 @@ paths:
11721172
application/json:
11731173
schema:
11741174
$ref: '#/components/schemas/Error'
1175+
/files/get_post_data_fields/:
1176+
get:
1177+
tags: ['files']
1178+
description: Get the fields required for uploading a new file
1179+
operationId: v0.files.get_post_data_fields
1180+
parameters:
1181+
- in: query
1182+
name: cid
1183+
required: true
1184+
description:
1185+
The content ID of a file (blake2b hash)
1186+
schema:
1187+
type: string
1188+
responses:
1189+
200:
1190+
description: The fields required for a file upload
1191+
content:
1192+
application/json:
1193+
schema:
1194+
$ref: '#/components/schemas/PostDataFields'
1195+
default:
1196+
description: unexpected error
1197+
content:
1198+
application/json:
1199+
schema:
1200+
$ref: '#/components/schemas/Error'
11751201
/files/{cid}/:
11761202
get:
11771203
tags: ['files']
@@ -1558,7 +1584,7 @@ components:
15581584
type: integer
15591585
readOnly: true
15601586

1561-
PostUrlFields:
1587+
PostDataFields:
15621588
properties:
15631589
url:
15641590
description: The url to post to

missioncontrol/tests/test_files.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,26 @@ def test_version_increment(test_client, simple_file, some_hash, another_hash):
182182
assert result1.pop('cid') == some_hash
183183
assert result2.pop('cid') == another_hash
184184
assert result1 == result2
185+
186+
# Still needs a database for the user setup.
187+
@patch('home.models.boto3')
188+
@pytest.mark.django_db
189+
def test_get_post_data_fields(boto3_mock, test_client, some_hash):
190+
post_values = {
191+
'url': 'https://test.example',
192+
'url_fields': {},
193+
}
194+
boto3_mock.client.return_value.generate_presigned_post.return_value = post_values
195+
196+
response = test_client.get(
197+
f'/api/v0/files/get_post_data_fields/',
198+
query_string={'cid': some_hash}
199+
)
200+
assert response.status_code == 200, response.get_data()
201+
202+
assert response.json == post_values
203+
204+
boto3_mock.client.return_value.generate_presigned_post.assert_called_with(
205+
Bucket=settings.FILE_STORAGE_PATH.split('/')[2],
206+
Key=f'django-file-storage/{some_hash}',
207+
)

missioncontrol/v0/files.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,13 @@ def put(cid, file_body):
4444
)
4545
obj, created = S3File.objects.update_or_create(cid=cid, defaults=file_body)
4646
retval = obj.to_dict()
47-
return retval, 201 if created else 200
47+
return retval, 201 if created else 200
48+
49+
def get_post_data_fields(cid):
50+
if S3File.objects.filter(cid=cid).exists():
51+
raise ProblemException(
52+
status=409,
53+
title='Conflict',
54+
detail='This cid already exists in metadata',
55+
)
56+
return S3File.get_post_data_fields(cid=cid)

0 commit comments

Comments
 (0)