Skip to content

Commit 122676e

Browse files
committed
Create separate endpoint for getting file upload fields.
1 parent 28a3455 commit 122676e

4 files changed

Lines changed: 66 additions & 5 deletions

File tree

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
@@ -1160,6 +1160,32 @@ paths:
11601160
application/json:
11611161
schema:
11621162
$ref: '#/components/schemas/Error'
1163+
/files/get_post_data_fields/:
1164+
get:
1165+
tags: ['files']
1166+
description: Get the fields required for uploading a new file
1167+
operationId: v0.files.get_post_data_fields
1168+
parameters:
1169+
- in: query
1170+
name: cid
1171+
required: true
1172+
description:
1173+
The content ID of a file (blake2b hash)
1174+
schema:
1175+
type: string
1176+
responses:
1177+
200:
1178+
description: The fields required for a file upload
1179+
content:
1180+
application/json:
1181+
schema:
1182+
$ref: '#/components/schemas/PostDataFields'
1183+
default:
1184+
description: unexpected error
1185+
content:
1186+
application/json:
1187+
schema:
1188+
$ref: '#/components/schemas/Error'
11631189
/files/{cid}/:
11641190
get:
11651191
tags: ['files']
@@ -1546,7 +1572,7 @@ components:
15461572
type: integer
15471573
readOnly: true
15481574

1549-
PostUrlFields:
1575+
PostDataFields:
15501576
properties:
15511577
url:
15521578
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)