feat: add fallback mechanism to serve documents locally from mounted directory when S3 redirect fails
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 21s

This commit is contained in:
vickytechkey 2026-08-16 19:42:23 +05:30
parent 00832194cb
commit 4eb66f838d

View file

@ -252,7 +252,27 @@ def preview_document(request):
) )
return redirect(presigned_url) return redirect(presigned_url)
except Exception as e: except Exception as e:
print("S3 redirect helper failed, falling back to mock files:", e) print("S3 redirect helper failed, falling back to local files / mock:", e)
# Check if file exists locally in the mounted folder
base_dir = os.environ.get('UPLOAD_MOUNT_DIR', '/home/ubuntu/mnt/s3files/supplierdocuments')
if not os.path.exists('/home/ubuntu/mnt/s3files/supplierdocuments') and not os.environ.get('UPLOAD_MOUNT_DIR'):
from django.conf import settings
base_dir = os.path.join(settings.BASE_DIR, 'mnt/s3files/supplierdocuments')
file_path = os.path.join(base_dir, filename)
if os.path.exists(file_path):
content_type = 'application/octet-stream'
if filename.endswith('.pdf'):
content_type = 'application/pdf'
elif filename.endswith('.png'):
content_type = 'image/png'
elif filename.endswith('.jpeg') or filename.endswith('.jpg'):
content_type = 'image/jpeg'
try:
return FileResponse(open(file_path, 'rb'), content_type=content_type)
except Exception as e:
print("Failed to serve local document file:", e)
if filename.endswith('.pdf'): if filename.endswith('.pdf'):
pdf_data = ( pdf_data = (