diff --git a/api/views.py b/api/views.py index 794255e..1eea33a 100644 --- a/api/views.py +++ b/api/views.py @@ -251,7 +251,27 @@ class MockUploadView(APIView): permission_classes = [AllowAny] def put(self, request): - return Response({"status": "success", "message": "Mock upload successful"}) + s3_key = request.query_params.get('key') + if not s3_key: + return Response({"error": "key query param is required"}, status=status.HTTP_400_BAD_REQUEST) + + # Define local mount directory (falls back to local project relative path if not on EC2) + 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'): + # Local fallback for local workspace testing + base_dir = os.path.join(settings.BASE_DIR, 'mnt/s3files/supplierdocuments') + + file_path = os.path.join(base_dir, s3_key) + + try: + # Ensure target directories exist + os.makedirs(os.path.dirname(file_path), exist_ok=True) + # Save raw request body to disk + with open(file_path, 'wb') as f: + f.write(request.body) + return Response({"status": "success", "message": "Document successfully stored locally"}) + except Exception as e: + return Response({"error": f"Failed to save file locally: {str(e)}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) class PresignedUrlView(APIView): permission_classes = [IsAuthenticated] @@ -303,9 +323,9 @@ class PresignedUrlView(APIView): ) except Exception as e: print("S3 presigned URL generation failed, falling back to local mock upload endpoint:", e) - presigned_url = request.build_absolute_uri('/api/profile/mock-upload/') + presigned_url = request.build_absolute_uri(f'/api/profile/mock-upload/?key={s3_key}') else: - presigned_url = request.build_absolute_uri('/api/profile/mock-upload/') + presigned_url = request.build_absolute_uri(f'/api/profile/mock-upload/?key={s3_key}') return Response({ 'presigned_url': presigned_url,