|
1 | 1 | from django.conf import settings |
| 2 | +from drf_yasg.utils import swagger_auto_schema |
2 | 3 | from rest_framework import status |
3 | 4 | from rest_framework.permissions import AllowAny |
4 | 5 | from rest_framework.response import Response |
5 | 6 | from rest_framework.views import APIView |
6 | | -from drf_yasg.utils import swagger_auto_schema |
7 | 7 |
|
8 | 8 | from common.apps.upload_file.serializers import PresignedUploadSerializer |
9 | | -from common.apps.upload_file.service import get_presigned_url, put_presigned_url |
| 9 | +from common.apps.upload_file.service import ( |
| 10 | + get_presigned_url, |
| 11 | + get_public_url, |
| 12 | + put_presigned_url, |
| 13 | +) |
10 | 14 |
|
11 | 15 |
|
12 | 16 | def _resolve_org_slug(request): |
@@ -37,7 +41,10 @@ class PutPresignedURL(APIView): |
37 | 41 |
|
38 | 42 | @swagger_auto_schema( |
39 | 43 | request_body=PresignedUploadSerializer, |
40 | | - responses={200: "Presigned URL generated successfully", 500: "Internal server error"}, |
| 44 | + responses={ |
| 45 | + 200: "Presigned URL generated successfully", |
| 46 | + 500: "Internal server error", |
| 47 | + }, |
41 | 48 | ) |
42 | 49 | def post(self, request): |
43 | 50 | serializer = PresignedUploadSerializer(data=request.data) |
@@ -79,14 +86,19 @@ def get(self, request, file_path): |
79 | 86 | status=status.HTTP_403_FORBIDDEN, |
80 | 87 | ) |
81 | 88 |
|
82 | | - if file_path.startswith("private/") and not request.user.is_authenticated: |
| 89 | + aws_s3_config = getattr(settings, "AWS_S3", {}) |
| 90 | + bucket_name = aws_s3_config.get("AWS_STORAGE_BUCKET_NAME") |
| 91 | + |
| 92 | + if file_path.startswith("public/"): |
| 93 | + url = get_public_url(bucket_name, file_path) |
| 94 | + return Response({"url": url}, status=status.HTTP_200_OK) |
| 95 | + |
| 96 | + if not request.user.is_authenticated: |
83 | 97 | return Response( |
84 | 98 | {"error": "Authentication required for private files."}, |
85 | 99 | status=status.HTTP_401_UNAUTHORIZED, |
86 | 100 | ) |
87 | 101 |
|
88 | | - aws_s3_config = getattr(settings, "AWS_S3", {}) |
89 | | - bucket_name = aws_s3_config.get("AWS_STORAGE_BUCKET_NAME") |
90 | 102 | url = get_presigned_url(bucket_name, file_path) |
91 | 103 |
|
92 | 104 | if url is not None: |
|
0 commit comments