Skip to content
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

refactor: use django-presigned-url package #598

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions alexandria/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pathlib import Path
from tempfile import NamedTemporaryFile

from rest_framework_json_api.relations import reverse
import tika.language
import tika.parser
from django.conf import settings
Expand All @@ -24,7 +25,7 @@
from manabi.token import Key, Token
from preview_generator.manager import PreviewManager

from alexandria.core.presign_urls import make_signature_components
from django_presigned_url.presign_urls import make_presigned_url
from alexandria.storages.fields import DynamicStorageFileField

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -358,13 +359,7 @@ def get_download_url(self, request):
if not request:
return None

url, expires, signature = make_signature_components(
str(self.pk),
request.get_host(),
scheme=request.META.get("wsgi.url_scheme", "http"),
)

return f"{url}?expires={expires}&signature={signature}"
return make_presigned_url(reverse("file-download", args=[self.pk]), request)

class Meta:
ordering = ["-created_at"]
Expand Down
53 changes: 0 additions & 53 deletions alexandria/core/presign_urls.py

This file was deleted.

12 changes: 3 additions & 9 deletions alexandria/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from operator import or_
from os.path import splitext
from tempfile import NamedTemporaryFile
from rest_framework_json_api.relations import reverse

import requests
from django.conf import settings
Expand Down Expand Up @@ -51,7 +52,7 @@
MarkFilterSet,
TagFilterSet,
)
from .presign_urls import verify_signed_components
from django_presigned_url.presign_urls import verify_presigned_request

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -241,14 +242,7 @@ def multi(self, request, **kwargs):
@permission_classes([AllowAny])
@action(methods=["get"], detail=True)
def download(self, request, pk=None):
if token_sig := request.query_params.get("signature"):
verify_signed_components(
pk,
request.get_host(),
expires=int(request.query_params.get("expires")),
scheme=request.META.get("wsgi.url_scheme", "http"),
token_sig=token_sig,
)
if verify_presigned_request(reverse("file-download", args=[pk]), request):
obj = models.File.objects.get(pk=pk)

return FileResponse(
Expand Down
Loading
Loading