-
Notifications
You must be signed in to change notification settings - Fork 614
Modernize file storage and transport using Django Storage API #3733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cesarbenjamindotnet
wants to merge
28
commits into
ohcnetwork:develop
Choose a base branch
from
xiidigital:django-storages
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
e280e0f
docs: add scope and goals document for GCP deployment
cesarbenjamindotnet 6a2976d
docs(gcp): complete runtime call-site inventory
cesarbenjamindotnet 2fe40cd
docs(xii): complete runtime call-site inventory
cesarbenjamindotnet 755e8cb
docs(gcp): record local runtime baseline
cesarbenjamindotnet d98a189
Merge branch 'feature/gcp-phase-0-inventory' into gcp
cesarbenjamindotnet 30da93e
docs(gcp): complete runtime call-site inventory
cesarbenjamindotnet 26b4f2e
chore(storage): add django-storages with s3 and google backends
cesarbenjamindotnet a9e3fce
feat(storage): configure portable storage aliases
cesarbenjamindotnet c587c5f
refactor(storage): route file persistence through Django Storage
cesarbenjamindotnet e52ffbf
test(storage): cover aliases, object names and MinIO integration
cesarbenjamindotnet 86d0038
fix(storage): name the GCS project setting per the configuration refe…
cesarbenjamindotnet bdf212c
docs(storage): record the IS-01 storage modernization
cesarbenjamindotnet 49a9e58
refactor(storage): remove signed URL transport, serve objects through…
cesarbenjamindotnet f4875d0
refactor(storage): keep S3FilesManager as a deprecated plugin shim
cesarbenjamindotnet 12b4e02
test(storage): verify provider-neutral transport
cesarbenjamindotnet 0714146
docs(storage): finalize ADR-0001
cesarbenjamindotnet 50783d2
docs(adr): add ADR-0007 for Terraform infrastructure on GCP
cesarbenjamindotnet 4898f12
refactor(files): replace base64 upload with multipart transport
cesarbenjamindotnet 98cfbfa
test(files): cover multipart upload and provider-neutral round trips
cesarbenjamindotnet bfa85df
docs(files): complete file transport modernization
cesarbenjamindotnet e6d928f
Merge branch 'ohcnetwork:develop' into gcp
cesarbenjamindotnet 672c532
fix(storage): address review findings on transport and inventories
cesarbenjamindotnet 97058e9
fix(storage): address review findings on transport and inventories
cesarbenjamindotnet 0bde688
fix(files): update file transport documentation and improve upload ha…
cesarbenjamindotnet b1e32bc
test(files): share the streaming-response helper
cesarbenjamindotnet 3582f7e
Merge branch 'fix/coderabbit-file-transport' into feature/django-stor…
cesarbenjamindotnet 402ca6e
docs(inventory): correct report_utils line references and retry analysis
cesarbenjamindotnet 699b936
docs: reconcile the IS-01 documents with the delivered ES-02 state
cesarbenjamindotnet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,62 @@ | ||
| """ | ||
| Public asset delivery for facility cover images and user avatars. | ||
|
|
||
| ADR-0001: a client never receives a storage-provider URL. These two objects were | ||
| already world-readable directly from the bucket, so the routes stay | ||
| unauthenticated — who can see the image is unchanged. What changes is that CARE | ||
| serves the bytes, which lets the bucket become private and keeps the provider | ||
| interchangeable. | ||
|
|
||
| They are separate views rather than actions on FacilityViewSet / UserViewSet | ||
| because those viewsets filter their querysets by ``request.user`` and cannot | ||
| serve an anonymous request. | ||
| """ | ||
|
|
||
| from django.core.files.storage import storages | ||
| from rest_framework.exceptions import NotFound | ||
| from rest_framework.views import APIView | ||
|
|
||
| from care.emr.utils.file_download import storage_file_response | ||
| from care.facility.models.facility import Facility | ||
| from care.users.models import User | ||
| from care.utils.file_uploads.cover_image import STORAGE_ALIAS | ||
| from care.utils.shortcuts import get_object_or_404 | ||
|
|
||
| #: ``upload_cover_image`` mints a fresh key containing a random token for every | ||
| #: upload, so the bytes behind a given key never change -- replacing an image | ||
| #: produces a different key. The response is therefore immutable per key and | ||
| #: safe for anonymous browser, CDN and reverse-proxy caches, which is what CARE | ||
| #: serving the bytes would otherwise cost us over reading the bucket directly. | ||
| PUBLIC_ASSET_CACHE_CONTROL = "public, max-age=31536000, immutable" | ||
|
|
||
|
|
||
| class PublicAssetView(APIView): | ||
| """Unauthenticated read-only delivery of a public image.""" | ||
|
|
||
| authentication_classes = () | ||
| permission_classes = () | ||
|
|
||
| @staticmethod | ||
| def serve(object_key: str | None): | ||
| if not object_key: | ||
| msg = "No image set" | ||
| raise NotFound(msg) | ||
| response = storage_file_response( | ||
| storages[STORAGE_ALIAS], | ||
| object_key, | ||
| filename=object_key.rsplit("/", 1)[-1], | ||
| ) | ||
| response.headers["Cache-Control"] = PUBLIC_ASSET_CACHE_CONTROL | ||
| return response | ||
|
|
||
|
|
||
| class FacilityCoverImageView(PublicAssetView): | ||
| def get(self, request, external_id): | ||
| facility = get_object_or_404(Facility, external_id=external_id) | ||
| return self.serve(facility.cover_image_url) | ||
|
|
||
|
|
||
| class UserProfilePictureView(PublicAssetView): | ||
| def get(self, request, username): | ||
| user = get_object_or_404(User, username=username, deleted=False) | ||
| return self.serve(user.profile_picture_url) | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.