Skip to content
This repository has been archived by the owner on Jan 29, 2022. It is now read-only.

Make file creation and validation happen prior to blob upload #140

Merged
merged 6 commits into from
Feb 5, 2021

Conversation

zachmullen
Copy link
Collaborator

This is the first of two PRs associated with #124

This provides the upload flow changes so that all uploads are pre-approved. It does not provide any of the security improvements we'd like. I may wait for kitware-resonant/django-s3-file-field#204 to land before tackling the policy enforcement side of this, as it could simplify the implementation here. #139 represents the work needed on the second PR.

@zachmullen zachmullen requested a review from brianhelba February 3, 2021 16:53
@mgrauer
Copy link
Contributor

mgrauer commented Feb 4, 2021

Fixes #124.

dkc/core/rest/file.py Outdated Show resolved Hide resolved
return HttpResponseRedirect(file.blob.url)
if file.blob: # FileFields are falsy when not populated with a file
return HttpResponseRedirect(file.blob.url)
return FileResponse(BytesIO(), filename=file.name, content_type=file.content_type)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the file hasn't been uploaded yet, I think this should return a 202, 204, or 404 response. To me, a partially uploaded file shouldn't be considered a fully valid resource by end API users.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'm not sure why I didn't think of something like 204. I wanted to specifically avoid 4xx, thinking of someone trying to sync a tree with a partially uploaded file, and I didn't want to break the sync operation if a pending file exists.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I've fully thought this through, but don't we want file upload to appear to be atomic from the perspective of read-only API endpoints? That is, the file will be non-existent / invisible until the blob (and potentially the hashsum?) is ready.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps so, but that's a lot more complicated to engineer. If you have a design in mind, please detail it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a start, we could filter the FileViewSet queryset for listing / details to always exclude anything without a blob; that would cause partial files to effectively be invisible in a folder listing. However the folder itself would already have its size incremented, so an attentive observer would notice that certain folder sizes were listed as larger as the sum of their contents.

From a user experience perspective, I'm not sure which is preferable.

I agree that to have fully robust atomicity, we'd have to maintain a separate set of quota reservations or have more complicated logic for reporting effective folder sizes.

dkc/core/models/file.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@brianhelba brianhelba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finished my review. Should be good to go after the remaining comments are addressed.

@zachmullen zachmullen requested a review from brianhelba February 5, 2021 00:31
@zachmullen
Copy link
Collaborator Author

@brianhelba this is ready for final review.

@zachmullen zachmullen merged commit 1be431f into master Feb 5, 2021
@zachmullen zachmullen deleted the file-pre-creation branch February 5, 2021 01:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants