feat: implement local file storage for uploaded documents and update presigned URL endpoint to include key parameter
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 23s
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 23s
This commit is contained in:
parent
5ac86cd6dc
commit
4f5274248a
1 changed files with 23 additions and 3 deletions
26
api/views.py
26
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,
|
||||
|
|
|
|||
Loading…
Reference in a new issue