feat: add dynamic S3 presigned URL redirection to preview_document view
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 25s
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 25s
This commit is contained in:
parent
3f713d80c5
commit
e006e773d2
1 changed files with 26 additions and 0 deletions
|
|
@ -217,6 +217,32 @@ from django.views.decorators.clickjacking import xframe_options_exempt
|
|||
@xframe_options_exempt
|
||||
def preview_document(request):
|
||||
filename = request.query_params.get('file', 'document.pdf')
|
||||
|
||||
# Dynamic S3 Presigned Redirect if AWS credentials are set
|
||||
import os
|
||||
aws_access = os.environ.get('AWS_ACCESS_KEY_ID') or os.environ.get('AWS_ACCESS_KEY')
|
||||
aws_secret = os.environ.get('AWS_SECRET_ACCESS_KEY') or os.environ.get('AWS_SECRET_KEY')
|
||||
bucket_name = os.environ.get('AWS_STORAGE_BUCKET_NAME') or os.environ.get('AWS_BUCKET_NAME') or 'digihox-kyc-bucket'
|
||||
|
||||
if aws_access and aws_secret:
|
||||
try:
|
||||
import boto3
|
||||
from django.shortcuts import redirect
|
||||
s3_client = boto3.client(
|
||||
's3',
|
||||
aws_access_key_id=aws_access,
|
||||
aws_secret_access_key=aws_secret,
|
||||
region_name=os.environ.get('AWS_REGION', 'ap-south-2')
|
||||
)
|
||||
presigned_url = s3_client.generate_presigned_url(
|
||||
'get_object',
|
||||
Params={'Bucket': bucket_name, 'Key': filename},
|
||||
ExpiresIn=3600
|
||||
)
|
||||
return redirect(presigned_url)
|
||||
except Exception as e:
|
||||
print("S3 redirect helper failed, falling back to mock files:", e)
|
||||
|
||||
if filename.endswith('.pdf'):
|
||||
pdf_data = (
|
||||
b"%PDF-1.4\n"
|
||||
|
|
|
|||
Loading…
Reference in a new issue