mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2026-07-31 07:49:07 -04:00
- Introduced .dockerignore to exclude unnecessary files from Docker context. - Added .env.aio.example for minimal configuration of the AdventureLog All-in-One setup. - Updated .env.example to include optional SITE_URL and GUNICORN_WORKERS settings. - Enhanced deploy.sh script for improved deployment flexibility and backup options. - Updated docker-compose files to use PostGIS 16-3.5 and added health checks for services. - Created docker-compose.aio.yml for All-in-One deployment configuration. - Improved health checks and service dependencies in docker-compose.dev.yml and docker-compose.yml. - Added GitHub workflows for building and pushing Docker images, including smoke tests for AIO setup.
70 lines
2.7 KiB
Python
70 lines
2.7 KiB
Python
from django.http import JsonResponse
|
|
from django.middleware.csrf import get_token
|
|
from os import getenv
|
|
from django.conf import settings
|
|
from django.http import HttpResponse, HttpResponseForbidden, HttpResponseRedirect
|
|
from django.views.static import serve
|
|
from django.core.files.storage import default_storage
|
|
from adventures.utils.file_permissions import checkFilePermission
|
|
|
|
def get_csrf_token(request):
|
|
csrf_token = get_token(request)
|
|
return JsonResponse({'csrfToken': csrf_token})
|
|
|
|
def get_public_url(request):
|
|
return JsonResponse({'PUBLIC_URL': getenv('PUBLIC_URL')})
|
|
|
|
def health_check(request):
|
|
from django.db import connection
|
|
try:
|
|
connection.ensure_connection()
|
|
return JsonResponse({'ok': True, 'db': 'connected'})
|
|
except Exception:
|
|
return JsonResponse({'ok': False, 'db': 'disconnected'}, status=503)
|
|
|
|
protected_paths = ['images/', 'attachments/']
|
|
|
|
def _redirect_storage(path):
|
|
storage_url = default_storage.url(path)
|
|
return HttpResponseRedirect(storage_url)
|
|
|
|
def serve_protected_media(request, path):
|
|
if any([path.startswith(protected_path) for protected_path in protected_paths]):
|
|
image_id = path.split('/')[1]
|
|
user = request.user
|
|
|
|
# Session auth won't populate request.user for API key requests, so
|
|
# attempt API key authentication as a fallback.
|
|
if not user.is_authenticated:
|
|
from users.authentication import APIKeyAuthentication
|
|
from rest_framework.exceptions import AuthenticationFailed
|
|
try:
|
|
result = APIKeyAuthentication().authenticate(request)
|
|
if result is not None:
|
|
user, _ = result
|
|
except AuthenticationFailed:
|
|
return HttpResponseForbidden()
|
|
|
|
media_type = path.split('/')[0] + '/'
|
|
if checkFilePermission(image_id, user, media_type):
|
|
if settings.USE_S3_MEDIA:
|
|
return _redirect_storage(path)
|
|
if settings.DEBUG:
|
|
# In debug mode, serve the file directly
|
|
return serve(request, path, document_root=settings.MEDIA_ROOT)
|
|
# In production, use X-Accel-Redirect to serve the file using Nginx
|
|
response = HttpResponse()
|
|
response['Content-Type'] = ''
|
|
response['X-Accel-Redirect'] = '/protectedMedia/' + path
|
|
return response
|
|
else:
|
|
return HttpResponseForbidden()
|
|
else:
|
|
if settings.USE_S3_MEDIA:
|
|
return _redirect_storage(path)
|
|
if settings.DEBUG:
|
|
return serve(request, path, document_root=settings.MEDIA_ROOT)
|
|
response = HttpResponse()
|
|
response['Content-Type'] = ''
|
|
response['X-Accel-Redirect'] = '/protectedMedia/' + path
|
|
return response |