From 4eb66f838d5487b242763e26a3389825b7338df3 Mon Sep 17 00:00:00 2001 From: vickytechkey Date: Sun, 16 Aug 2026 19:42:23 +0530 Subject: [PATCH] feat: add fallback mechanism to serve documents locally from mounted directory when S3 redirect fails --- crm/views/seller_views.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/crm/views/seller_views.py b/crm/views/seller_views.py index 8f5ff0a..50a2bbe 100644 --- a/crm/views/seller_views.py +++ b/crm/views/seller_views.py @@ -252,7 +252,27 @@ def preview_document(request): ) return redirect(presigned_url) 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'): pdf_data = (